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()