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

Web is working #38 (#59)

* Una possibile idea che sto per eliminare

* Woah, this is progress

* Well, this works

* im lost

* OMFG IT WORKS
This commit is contained in:
Steffo 2019-06-01 21:09:21 +02:00 committed by GitHub
parent c388863609
commit a512da0058
9 changed files with 102 additions and 0 deletions

View file

@ -12,3 +12,5 @@ ffmpeg-python>=0.1.17
Sphinx>=2.0.1 Sphinx>=2.0.1
sphinx_rtd_theme>=0.4.3 sphinx_rtd_theme>=0.4.3
PyNaCl>=1.3.0 PyNaCl>=1.3.0
werkzeug>=0.15.4
flask>=1.0.3

15
royalnet/royalgamesweb.py Normal file
View file

@ -0,0 +1,15 @@
import os
from .web import create_app
from .web.blueprints import helloworld, testing
class TestConfig:
DB_PATH = os.environ["DB_PATH"]
REQUIRED_TABLES = set()
app = create_app(TestConfig, [helloworld, testing])
if __name__ == "__main__":
app.run()

5
royalnet/web/__init__.py Normal file
View file

@ -0,0 +1,5 @@
from .flaskserver import create_app
from .royalprint import Royalprint
from . import blueprints
__all__ = ["create_app", "Royalprint", "blueprints"]

View file

@ -0,0 +1,21 @@
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

@ -0,0 +1,4 @@
from .helloworld import bp as helloworld
from .testing import bp as testing
__all__ = ["helloworld", "testing"]

View file

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

View file

@ -0,0 +1,13 @@
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 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

@ -0,0 +1,15 @@
import typing
import flask as f
from ..database import Alchemy
from .royalprint import Royalprint
def create_app(config_obj: typing.Type, blueprints: typing.List[Royalprint]):
app = f.Flask(__name__)
app.config.from_object(config_obj)
required_tables = set()
for blueprint in blueprints:
required_tables = required_tables.union(blueprint.required_tables)
app.register_blueprint(blueprint)
app.config["ALCHEMY"] = Alchemy(app.config["DB_PATH"], required_tables)
return app

View file

@ -0,0 +1,16 @@
import typing
import flask as f
class Royalprint(f.Blueprint):
def __init__(self, name, import_name, static_folder=None,
static_url_path=None, template_folder=None,
url_prefix=None, subdomain=None, url_defaults=None,
root_path=None, required_tables: typing.Optional[set] = None):
super().__init__(name, import_name, static_folder=static_folder,
static_url_path=static_url_path, template_folder=template_folder,
url_prefix=url_prefix, subdomain=subdomain, url_defaults=url_defaults,
root_path=root_path)
self.required_tables = required_tables
if self.required_tables is None:
self.required_tables = set()