From 08993451e0b40ce82ec2c58865ca1c47f2121fc8 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sat, 1 Jun 2019 18:18:51 +0200 Subject: [PATCH] Woah, this is progress --- requirements.txt | 2 ++ royalnet/web/alchemyhandler.py | 18 ++++++++++++++++++ royalnet/web/flaskserver.py | 24 ++++++++++++++---------- 3 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 royalnet/web/alchemyhandler.py diff --git a/requirements.txt b/requirements.txt index 6c4fba73..c1993ac5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,3 +12,5 @@ ffmpeg-python>=0.1.17 Sphinx>=2.0.1 sphinx_rtd_theme>=0.4.3 PyNaCl>=1.3.0 +werkzeug>=0.15.4 +flask>=1.0.3 diff --git a/royalnet/web/alchemyhandler.py b/royalnet/web/alchemyhandler.py new file mode 100644 index 00000000..6a6a757b --- /dev/null +++ b/royalnet/web/alchemyhandler.py @@ -0,0 +1,18 @@ +import flask as f +import werkzeug.local + + +def get_alchemy_session(): + if "alchemy_session" not in f.g: + f.g.alchemy_session = f.current_app.config["RN_ALCHEMY"].Session() + return f.g.alchemy_session + + +@f.current_app.teardown_appcontext +def teardown_alchemy_session(*args, **kwargs): + _alchemy_session = f.g.pop("alchemy_session", None) + if _alchemy_session is not None: + _alchemy_session.close() + + +alchemy_session = werkzeug.local.LocalProxy(get_alchemy_session) diff --git a/royalnet/web/flaskserver.py b/royalnet/web/flaskserver.py index d1db0033..8bb0f1e8 100644 --- a/royalnet/web/flaskserver.py +++ b/royalnet/web/flaskserver.py @@ -1,15 +1,19 @@ -import typing import flask as f +import os from ..database import Alchemy -class RoyalFlask: - def __init__(self, config_obj: typing.Type): - self.app = f.Flask(__name__) - self.app.config.from_object(config_obj) - self.alchemy = Alchemy(self.app.config["RF_DATABASE_URI"], self.app.config["RF_REQUIRED_TABLES"]) - for blueprint in self.app.config["RF_BLUEPRINTS"]: - self.app.register_blueprint(blueprint) +app = f.Flask(__name__) +app.config["RN_ALCHEMY"] = Alchemy(os.environ["DB_PATH"], set()) +with app.app_context(): + from .alchemyhandler import alchemy_session as db_session - def debug(self): - self.app.run(host="127.0.0.1", port=1234, debug=True) + +@app.route("/") +def test(): + ... + return repr(db_session) + + +if __name__ == "__main__": + app.run()