2020-10-02 22:12:49 +02:00
|
|
|
|
import importlib
|
2020-10-13 03:25:55 +02:00
|
|
|
|
import warnings
|
2020-09-05 08:00:44 +02:00
|
|
|
|
|
2020-10-04 02:57:14 +02:00
|
|
|
|
from quart import g
|
|
|
|
|
from quart_trio import QuartTrio
|
2020-09-14 02:31:05 +02:00
|
|
|
|
from quart_cors import cors # type: ignore
|
|
|
|
|
from quart_compress import Compress # type: ignore
|
|
|
|
|
from secure import SecureHeaders # type: ignore
|
2020-09-05 08:00:44 +02:00
|
|
|
|
|
2020-09-28 23:11:59 +02:00
|
|
|
|
from .commands import db_migrate, handle_assets
|
2020-09-05 08:00:44 +02:00
|
|
|
|
from .core import core_app
|
2020-09-15 20:54:05 +02:00
|
|
|
|
from .helpers import get_valid_extensions, get_js_vendored, get_css_vendored, url_for_vendored
|
2020-09-28 05:21:33 +02:00
|
|
|
|
from .proxy_fix import ASGIProxyFix
|
2020-10-22 20:36:37 +02:00
|
|
|
|
from .tasks import run_deferred_async, invoice_listener, internal_invoice_listener, webhook_handler, grab_app_for_later
|
2020-10-13 03:25:55 +02:00
|
|
|
|
from .settings import WALLET
|
2020-09-05 08:00:44 +02:00
|
|
|
|
|
2020-09-14 02:31:05 +02:00
|
|
|
|
secure_headers = SecureHeaders(hsts=False)
|
2020-09-05 08:00:44 +02:00
|
|
|
|
|
2020-09-14 02:31:05 +02:00
|
|
|
|
|
2020-10-04 02:57:14 +02:00
|
|
|
|
def create_app(config_object="lnbits.settings") -> QuartTrio:
|
2020-09-14 02:31:05 +02:00
|
|
|
|
"""Create application factory.
|
2020-09-05 08:00:44 +02:00
|
|
|
|
:param config_object: The configuration object to use.
|
|
|
|
|
"""
|
2020-10-04 02:57:14 +02:00
|
|
|
|
app = QuartTrio(__name__, static_folder="static")
|
2020-09-05 08:00:44 +02:00
|
|
|
|
app.config.from_object(config_object)
|
2020-09-28 05:21:33 +02:00
|
|
|
|
app.asgi_http_class = ASGIProxyFix
|
2020-09-05 08:00:44 +02:00
|
|
|
|
|
2020-09-14 02:31:05 +02:00
|
|
|
|
cors(app)
|
|
|
|
|
Compress(app)
|
|
|
|
|
|
2020-10-13 03:25:55 +02:00
|
|
|
|
check_funding_source(app)
|
2020-09-15 20:54:05 +02:00
|
|
|
|
register_assets(app)
|
2020-09-05 08:00:44 +02:00
|
|
|
|
register_blueprints(app)
|
|
|
|
|
register_filters(app)
|
|
|
|
|
register_commands(app)
|
2020-09-07 05:47:13 +02:00
|
|
|
|
register_request_hooks(app)
|
2020-09-28 04:12:55 +02:00
|
|
|
|
register_async_tasks(app)
|
2020-10-06 05:39:54 +02:00
|
|
|
|
grab_app_for_later(app)
|
2020-09-05 08:00:44 +02:00
|
|
|
|
|
|
|
|
|
return app
|
|
|
|
|
|
|
|
|
|
|
2020-10-13 03:25:55 +02:00
|
|
|
|
def check_funding_source(app: QuartTrio) -> None:
|
|
|
|
|
@app.before_serving
|
|
|
|
|
async def check_wallet_status():
|
|
|
|
|
error_message, balance = WALLET.status()
|
|
|
|
|
if error_message:
|
|
|
|
|
warnings.warn(
|
|
|
|
|
f" × The backend for {WALLET.__class__.__name__} isn't working properly: '{error_message}'",
|
|
|
|
|
RuntimeWarning,
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
print(f" ✔️ {WALLET.__class__.__name__} seems to be connected and with a balance of {balance} msat.")
|
|
|
|
|
|
|
|
|
|
|
2020-10-04 02:57:14 +02:00
|
|
|
|
def register_blueprints(app: QuartTrio) -> None:
|
2020-09-05 08:00:44 +02:00
|
|
|
|
"""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}")
|
2020-10-02 22:12:49 +02:00
|
|
|
|
bp = getattr(ext_module, f"{ext.code}_ext")
|
|
|
|
|
|
|
|
|
|
@bp.teardown_request
|
|
|
|
|
async def after_request(exc):
|
2020-11-21 22:04:39 +01:00
|
|
|
|
await ext_module.db.close_session()
|
2020-10-02 22:12:49 +02:00
|
|
|
|
|
|
|
|
|
app.register_blueprint(bp, url_prefix=f"/{ext.code}")
|
2020-09-05 08:00:44 +02:00
|
|
|
|
except Exception:
|
|
|
|
|
raise ImportError(f"Please make sure that the extension `{ext.code}` follows conventions.")
|
|
|
|
|
|
|
|
|
|
|
2020-10-04 02:57:14 +02:00
|
|
|
|
def register_commands(app: QuartTrio):
|
2020-09-05 08:00:44 +02:00
|
|
|
|
"""Register Click commands."""
|
2020-09-14 02:31:05 +02:00
|
|
|
|
app.cli.add_command(db_migrate)
|
2020-09-28 23:11:59 +02:00
|
|
|
|
app.cli.add_command(handle_assets)
|
2020-09-05 08:00:44 +02:00
|
|
|
|
|
|
|
|
|
|
2020-10-04 02:57:14 +02:00
|
|
|
|
def register_assets(app: QuartTrio):
|
2020-09-15 20:54:05 +02:00
|
|
|
|
"""Serve each vendored asset separately or a bundle."""
|
|
|
|
|
|
|
|
|
|
@app.before_request
|
|
|
|
|
async def vendored_assets_variable():
|
|
|
|
|
if app.config["DEBUG"]:
|
|
|
|
|
g.VENDORED_JS = map(url_for_vendored, get_js_vendored())
|
|
|
|
|
g.VENDORED_CSS = map(url_for_vendored, get_css_vendored())
|
|
|
|
|
else:
|
|
|
|
|
g.VENDORED_JS = ["/static/bundle.js"]
|
|
|
|
|
g.VENDORED_CSS = ["/static/bundle.css"]
|
|
|
|
|
|
|
|
|
|
|
2020-10-04 02:57:14 +02:00
|
|
|
|
def register_filters(app: QuartTrio):
|
2020-09-05 08:00:44 +02:00
|
|
|
|
"""Jinja filters."""
|
|
|
|
|
app.jinja_env.globals["SITE_TITLE"] = app.config["LNBITS_SITE_TITLE"]
|
2020-09-15 20:54:05 +02:00
|
|
|
|
app.jinja_env.globals["EXTENSIONS"] = get_valid_extensions()
|
2020-09-07 05:47:13 +02:00
|
|
|
|
|
|
|
|
|
|
2020-10-04 02:57:14 +02:00
|
|
|
|
def register_request_hooks(app: QuartTrio):
|
2020-09-07 05:47:13 +02:00
|
|
|
|
"""Open the core db for each request so everything happens in a big transaction"""
|
|
|
|
|
|
|
|
|
|
@app.before_request
|
2020-09-14 02:31:05 +02:00
|
|
|
|
async def before_request():
|
2020-10-06 05:39:54 +02:00
|
|
|
|
g.nursery = app.nursery
|
2020-09-07 05:47:13 +02:00
|
|
|
|
|
2020-11-21 22:04:39 +01:00
|
|
|
|
@app.teardown_request
|
|
|
|
|
async def after_request(exc):
|
|
|
|
|
from lnbits.core import db
|
|
|
|
|
|
|
|
|
|
await db.close_session()
|
|
|
|
|
|
2020-09-14 02:31:05 +02:00
|
|
|
|
@app.after_request
|
|
|
|
|
async def set_secure_headers(response):
|
|
|
|
|
secure_headers.quart(response)
|
|
|
|
|
return response
|
|
|
|
|
|
2020-09-28 04:12:55 +02:00
|
|
|
|
|
|
|
|
|
def register_async_tasks(app):
|
2020-09-29 05:52:27 +02:00
|
|
|
|
@app.route("/wallet/webhook", methods=["GET", "POST", "PUT", "PATCH", "DELETE"])
|
2020-09-28 04:12:55 +02:00
|
|
|
|
async def webhook_listener():
|
|
|
|
|
return await webhook_handler()
|
|
|
|
|
|
|
|
|
|
@app.before_serving
|
|
|
|
|
async def listeners():
|
2020-10-06 06:50:55 +02:00
|
|
|
|
run_deferred_async(app.nursery)
|
2020-10-04 02:57:14 +02:00
|
|
|
|
app.nursery.start_soon(invoice_listener)
|
2020-10-22 20:36:37 +02:00
|
|
|
|
app.nursery.start_soon(internal_invoice_listener)
|
2020-09-28 04:12:55 +02:00
|
|
|
|
|
|
|
|
|
@app.after_serving
|
|
|
|
|
async def stop_listeners():
|
|
|
|
|
pass
|