From 860172916ed36dab4445db93fcffbe02bca5602a Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sat, 1 Jun 2019 18:42:44 +0200 Subject: [PATCH] Well, this works --- royalnet/royalgamesweb.py | 12 ++++++++++++ royalnet/web/__init__.py | 3 +++ royalnet/web/alchemyhandler.py | 4 ++-- royalnet/web/blueprints/helloworld.py | 9 +++++++++ royalnet/web/flaskserver.py | 24 +++++++++--------------- 5 files changed, 35 insertions(+), 17 deletions(-) create mode 100644 royalnet/royalgamesweb.py create mode 100644 royalnet/web/blueprints/helloworld.py diff --git a/royalnet/royalgamesweb.py b/royalnet/royalgamesweb.py new file mode 100644 index 00000000..2a010031 --- /dev/null +++ b/royalnet/royalgamesweb.py @@ -0,0 +1,12 @@ +import os +from .web import create_app +from .web.blueprints import helloworld + + +class TestConfig: + DB_PATH = os.environ["DB_PATH"] + REQUIRED_TABLES = set() + + +app = create_app(TestConfig, [helloworld.bp]) +app.run() diff --git a/royalnet/web/__init__.py b/royalnet/web/__init__.py index e69de29b..3501800d 100644 --- a/royalnet/web/__init__.py +++ b/royalnet/web/__init__.py @@ -0,0 +1,3 @@ +from .flaskserver import create_app + +__all__ = ["create_app"] diff --git a/royalnet/web/alchemyhandler.py b/royalnet/web/alchemyhandler.py index 6a6a757b..46d850a8 100644 --- a/royalnet/web/alchemyhandler.py +++ b/royalnet/web/alchemyhandler.py @@ -4,12 +4,12 @@ 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() + f.g.alchemy_session = f.g.alchemy.Session() return f.g.alchemy_session @f.current_app.teardown_appcontext -def teardown_alchemy_session(*args, **kwargs): +def teardown_alchemy_session(*_, **__): _alchemy_session = f.g.pop("alchemy_session", None) if _alchemy_session is not None: _alchemy_session.close() diff --git a/royalnet/web/blueprints/helloworld.py b/royalnet/web/blueprints/helloworld.py new file mode 100644 index 00000000..13cb720a --- /dev/null +++ b/royalnet/web/blueprints/helloworld.py @@ -0,0 +1,9 @@ +import flask as f + + +bp = f.Blueprint("helloworld", __name__, url_prefix="/helloworld") + + +@bp.route("/") +def helloworld(): + return "Hello world!" diff --git a/royalnet/web/flaskserver.py b/royalnet/web/flaskserver.py index 8bb0f1e8..4cac9ca2 100644 --- a/royalnet/web/flaskserver.py +++ b/royalnet/web/flaskserver.py @@ -1,19 +1,13 @@ +import typing import flask as f -import os from ..database import Alchemy -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 - - -@app.route("/") -def test(): - ... - return repr(db_session) - - -if __name__ == "__main__": - app.run() +def create_app(config_obj: typing.Type, blueprints: typing.List[f.Blueprint]): + app = f.Flask(__name__) + app.config.from_object(config_obj) + with app.app_context(): + f.g.alchemy = Alchemy(app.config["DB_PATH"], app.config["REQUIRED_TABLES"]) + for blueprint in blueprints: + app.register_blueprint(blueprint) + return app