From 240daa427834a2aeb5f5a1a56a516f4603e8b2a1 Mon Sep 17 00:00:00 2001 From: iamBadgers Date: Sun, 22 Mar 2026 12:51:42 -0700 Subject: [PATCH] read from the db --- src/main.py | 54 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 16 deletions(-) diff --git a/src/main.py b/src/main.py index 99f4f0c..e2a1eb8 100644 --- a/src/main.py +++ b/src/main.py @@ -1,23 +1,45 @@ -from flask import Flask, jsonify, request +from flask import Flask, g, jsonify, request import sqlite3 - 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") -def hello(): - return jsonify([{ - "id": 1, - "table_name": "b", - "table_link": "potato", - "active": False - }]) +def get_active_tables(): + db = get_db() + 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') + v = c.fetchall() + c.close() + + return jsonify([convert(x) for x in v]) @app.route("/api/tables") -def potato(): - return jsonify([{ - "id": 1, - "table_name": "Nuntis", - "table_link": "vtt.cyberpunkrush.com/nuntis", - "active": False - }]) +def get_tables(): + db = get_db() + c = db.execute('select game_table_id as id, game_table_name, game_table_link, active from game_tables') + v = c.fetchall() + c.close() + + return jsonify([convert(x) for x in v]) + +if __name__ == '__main__': + app.run(debug=True)