1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-27 13:34:28 +00:00

I somehow managed to get FLASK_DEBUG to work. Flask multithreading is weird.

This commit is contained in:
Steffo 2019-06-13 01:08:34 +02:00
parent 6ba220869f
commit 8e96cc0a07
8 changed files with 24 additions and 52 deletions

View file

@ -1,7 +1,7 @@
import typing import typing
from sqlalchemy import create_engine from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import sessionmaker, scoped_session
from contextlib import contextmanager, asynccontextmanager from contextlib import contextmanager, asynccontextmanager
from ..utils import asyncify from ..utils import asyncify
# noinspection PyUnresolvedReferences # noinspection PyUnresolvedReferences

View file

@ -1,21 +0,0 @@
import flask as f
from werkzeug.local import LocalProxy
alchemy = f.current_app.config["ALCHEMY"]
def get_alchemy_session():
if "alchemy_session" not in f.g:
f.g.alchemy_session = alchemy.Session()
return f.g.alchemy_session
@f.current_app.teardown_appcontext
def teardown_alchemy_session(*_, **__):
_alchemy_session = f.g.pop("alchemy_session", None)
if _alchemy_session is not None:
_alchemy_session.close()
alchemy_session = LocalProxy(get_alchemy_session)

View file

@ -1,7 +1,5 @@
from .helloworld import bp as helloworld
from .testing import bp as testing
from .home import bp as home from .home import bp as home
from .wikiview import bp as wikiview from .wikiview import bp as wikiview
from .tglogin import bp as tglogin from .tglogin import bp as tglogin
__all__ = ["helloworld", "testing", "home", "wikiview", "tglogin"] __all__ = ["home", "wikiview", "tglogin"]

View file

@ -1,10 +0,0 @@
import flask as f
from ... import Royalprint
bp = Royalprint("helloworld", __name__, url_prefix="/helloworld")
@bp.route("/")
def helloworld_index():
return "Hello world!"

View file

@ -1,13 +0,0 @@
import flask as f
from ... import Royalprint
from ....database.tables import Royal
bp = Royalprint("testing", __name__, url_prefix="/testing", required_tables={Royal})
@bp.route("/listroyals")
def testing_listroyals():
from ...alchemyhandler import alchemy, alchemy_session
royals = alchemy_session.query(alchemy.Royal).all()
return f'<body><script type="text/plain" style="display: block;">{repr(royals)}</script></body>'

View file

@ -18,7 +18,7 @@ def tglogin_index():
@bp.route("/done") @bp.route("/done")
def tglogin_done(): def tglogin_done():
from ...alchemyhandler import alchemy, alchemy_session alchemy, alchemy_session = f.current_app.config["ALCHEMY"], f.current_app.config["ALCHEMY_SESSION"]
data_check_string = "" data_check_string = ""
for field in sorted(list(f.request.args)): for field in sorted(list(f.request.args)):
if field == "hash": if field == "hash":

View file

@ -50,7 +50,7 @@ def prepare_page(page):
@bp.route("/") @bp.route("/")
def wikiview_index(): def wikiview_index():
from ...alchemyhandler import alchemy, alchemy_session alchemy, alchemy_session = f.current_app.config["ALCHEMY"], f.current_app.config["ALCHEMY_SESSION"]
pages = sorted(alchemy_session.query(alchemy.WikiPage).all(), key=lambda page: page.title) pages = sorted(alchemy_session.query(alchemy.WikiPage).all(), key=lambda page: page.title)
return f.render_template("wikiview_index.html", pages=pages) return f.render_template("wikiview_index.html", pages=pages)
@ -58,7 +58,7 @@ def wikiview_index():
@bp.route("/<uuid:page_id>", defaults={"title": ""}) @bp.route("/<uuid:page_id>", defaults={"title": ""})
@bp.route("/<uuid:page_id>/<title>") @bp.route("/<uuid:page_id>/<title>")
def wikiview_by_id(page_id: uuid.UUID, title: str): def wikiview_by_id(page_id: uuid.UUID, title: str):
from ...alchemyhandler import alchemy, alchemy_session alchemy, alchemy_session = f.current_app.config["ALCHEMY"], f.current_app.config["ALCHEMY_SESSION"]
page = alchemy_session.query(alchemy.WikiPage).filter(alchemy.WikiPage.page_id == page_id).one_or_none() page = alchemy_session.query(alchemy.WikiPage).filter(alchemy.WikiPage.page_id == page_id).one_or_none()
if page is None: if page is None:
return "No such page", 404 return "No such page", 404

View file

@ -1,6 +1,7 @@
import typing import typing
import flask as f import flask as f
import os import os
from sqlalchemy.orm import scoped_session
from ..database import Alchemy from ..database import Alchemy
from .royalprint import Royalprint from .royalprint import Royalprint
@ -9,9 +10,26 @@ def create_app(config_obj: typing.Type, blueprints: typing.List[Royalprint]):
app = f.Flask(__name__) app = f.Flask(__name__)
app.config.from_object(config_obj) app.config.from_object(config_obj)
app.secret_key = os.environ["SECRET_KEY"] app.secret_key = os.environ["SECRET_KEY"]
@app.teardown_request
def teardown_alchemy_session(*_, **__):
alchemy_session = app.config["ALCHEMY_SESSION"]
if alchemy_session is not None:
alchemy_session.remove()
# Load blueprints
required_tables = set() required_tables = set()
for blueprint in blueprints: for blueprint in blueprints:
required_tables = required_tables.union(blueprint.required_tables) required_tables = required_tables.union(blueprint.required_tables)
app.register_blueprint(blueprint) app.register_blueprint(blueprint)
app.config["ALCHEMY"] = Alchemy(app.config["DB_PATH"], required_tables)
# Init Alchemy
# Seems like a dirty hack to me, but experiments are fun, right?
if len(required_tables) > 0:
alchemy = Alchemy(app.config["DB_PATH"], required_tables)
app.config["ALCHEMY"] = alchemy
app.config["ALCHEMY_SESSION"] = scoped_session(alchemy.Session)
else:
app.config["ALCHEMY"] = None
app.config["ALCHEMY_SESSION"] = None
return app return app