read from the db
This commit is contained in:
54
src/main.py
54
src/main.py
@@ -1,23 +1,45 @@
|
|||||||
from flask import Flask, jsonify, request
|
from flask import Flask, g, jsonify, request
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
def convert(tupple):
|
||||||
|
return {
|
||||||
|
"id": tupple[0],
|
||||||
|
"table_name": tupple[1],
|
||||||
|
"table_link": tupple[2],
|
||||||
|
"active": tupple[3] != 0
|
||||||
|
}
|
||||||
|
|
||||||
|
def get_db():
|
||||||
|
db = getattr(g, '_database', None)
|
||||||
|
if db is None:
|
||||||
|
db = sqlite3.connect("/data/tables.db")
|
||||||
|
return db
|
||||||
|
|
||||||
|
@app.teardown_appcontext
|
||||||
|
def close_connection(exception):
|
||||||
|
db = getattr(g, '_database', None)
|
||||||
|
if db is not None:
|
||||||
|
db.close()
|
||||||
|
|
||||||
@app.route("/api/active_tables")
|
@app.route("/api/active_tables")
|
||||||
def hello():
|
def get_active_tables():
|
||||||
return jsonify([{
|
db = get_db()
|
||||||
"id": 1,
|
c = db.execute('select game_table_id as id, game_table_name as table_name, game_table_link as table_link, active from game_tables where active')
|
||||||
"table_name": "b",
|
v = c.fetchall()
|
||||||
"table_link": "potato",
|
c.close()
|
||||||
"active": False
|
|
||||||
}])
|
return jsonify([convert(x) for x in v])
|
||||||
|
|
||||||
@app.route("/api/tables")
|
@app.route("/api/tables")
|
||||||
def potato():
|
def get_tables():
|
||||||
return jsonify([{
|
db = get_db()
|
||||||
"id": 1,
|
c = db.execute('select game_table_id as id, game_table_name, game_table_link, active from game_tables')
|
||||||
"table_name": "Nuntis",
|
v = c.fetchall()
|
||||||
"table_link": "vtt.cyberpunkrush.com/nuntis",
|
c.close()
|
||||||
"active": False
|
|
||||||
}])
|
return jsonify([convert(x) for x in v])
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
app.run(debug=True)
|
||||||
|
|||||||
Reference in New Issue
Block a user