mirror of
https://github.com/lnbits/lnbits-legend.git
synced 2024-11-20 10:39:59 +01:00
74 lines
2.0 KiB
Python
74 lines
2.0 KiB
Python
import importlib
|
|
|
|
from quart import Quart, g
|
|
from quart_cors import cors # type: ignore
|
|
from quart_compress import Compress # type: ignore
|
|
from secure import SecureHeaders # type: ignore
|
|
|
|
from .commands import db_migrate
|
|
from .core import core_app
|
|
from .db import open_db
|
|
from .helpers import get_valid_extensions
|
|
from .proxy_fix import ProxyFix
|
|
|
|
secure_headers = SecureHeaders(hsts=False)
|
|
|
|
|
|
def create_app(config_object="lnbits.settings") -> Quart:
|
|
"""Create application factory.
|
|
:param config_object: The configuration object to use.
|
|
"""
|
|
app = Quart(__name__, static_folder="static")
|
|
app.config.from_object(config_object)
|
|
|
|
cors(app)
|
|
Compress(app)
|
|
ProxyFix(app)
|
|
|
|
register_blueprints(app)
|
|
register_filters(app)
|
|
register_commands(app)
|
|
register_request_hooks(app)
|
|
|
|
return app
|
|
|
|
|
|
def register_blueprints(app) -> None:
|
|
"""Register Flask blueprints / LNbits extensions."""
|
|
app.register_blueprint(core_app)
|
|
|
|
for ext in get_valid_extensions():
|
|
try:
|
|
ext_module = importlib.import_module(f"lnbits.extensions.{ext.code}")
|
|
app.register_blueprint(getattr(ext_module, f"{ext.code}_ext"), url_prefix=f"/{ext.code}")
|
|
except Exception:
|
|
raise ImportError(f"Please make sure that the extension `{ext.code}` follows conventions.")
|
|
|
|
|
|
def register_commands(app):
|
|
"""Register Click commands."""
|
|
app.cli.add_command(db_migrate)
|
|
|
|
|
|
def register_filters(app):
|
|
"""Jinja filters."""
|
|
app.jinja_env.globals["EXTENSIONS"] = get_valid_extensions()
|
|
app.jinja_env.globals["SITE_TITLE"] = app.config["LNBITS_SITE_TITLE"]
|
|
|
|
|
|
def register_request_hooks(app):
|
|
"""Open the core db for each request so everything happens in a big transaction"""
|
|
|
|
@app.before_request
|
|
async def before_request():
|
|
g.db = open_db()
|
|
|
|
@app.after_request
|
|
async def set_secure_headers(response):
|
|
secure_headers.quart(response)
|
|
return response
|
|
|
|
@app.teardown_request
|
|
async def after_request(exc):
|
|
g.db.__exit__(type(exc), exc, None)
|