2021-08-30 19:55:02 +02:00
|
|
|
|
import asyncio
|
2021-05-07 04:22:02 +02:00
|
|
|
|
import importlib
|
2021-08-27 20:54:42 +02:00
|
|
|
|
import sys
|
2021-05-07 04:22:02 +02:00
|
|
|
|
import traceback
|
2021-08-27 20:54:42 +02:00
|
|
|
|
import warnings
|
2020-09-05 08:00:44 +02:00
|
|
|
|
|
2021-08-23 20:19:43 +02:00
|
|
|
|
from fastapi import FastAPI, Request
|
2021-08-27 20:54:42 +02:00
|
|
|
|
from fastapi.exceptions import RequestValidationError
|
2021-08-22 20:07:24 +02:00
|
|
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
|
from fastapi.middleware.gzip import GZipMiddleware
|
|
|
|
|
from fastapi.staticfiles import StaticFiles
|
2020-09-05 08:00:44 +02:00
|
|
|
|
|
2021-08-27 20:54:42 +02:00
|
|
|
|
import lnbits.settings
|
2021-12-30 17:09:12 +01:00
|
|
|
|
from lnbits.core.tasks import register_task_listeners
|
2021-08-27 20:54:42 +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
|
2021-08-23 21:17:46 +02:00
|
|
|
|
from .core.views.generic import core_html_routes
|
2021-10-17 19:33:29 +02:00
|
|
|
|
from .helpers import (
|
|
|
|
|
get_css_vendored,
|
|
|
|
|
get_js_vendored,
|
|
|
|
|
get_valid_extensions,
|
|
|
|
|
template_renderer,
|
|
|
|
|
url_for_vendored,
|
|
|
|
|
)
|
2021-08-27 20:54:42 +02:00
|
|
|
|
from .requestvars import g
|
|
|
|
|
from .settings import WALLET
|
2021-10-17 19:33:29 +02:00
|
|
|
|
from .tasks import (
|
|
|
|
|
catch_everything_and_restart,
|
|
|
|
|
check_pending_payments,
|
|
|
|
|
internal_invoice_listener,
|
|
|
|
|
invoice_listener,
|
|
|
|
|
run_deferred_async,
|
|
|
|
|
webhook_handler,
|
|
|
|
|
)
|
2021-08-27 20:54:42 +02:00
|
|
|
|
|
2020-09-05 08:00:44 +02:00
|
|
|
|
|
2021-08-30 19:55:02 +02:00
|
|
|
|
def create_app(config_object="lnbits.settings") -> FastAPI:
|
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.
|
|
|
|
|
"""
|
2021-08-22 20:07:24 +02:00
|
|
|
|
app = FastAPI()
|
|
|
|
|
app.mount("/static", StaticFiles(directory="lnbits/static"), name="static")
|
2021-10-17 19:33:29 +02:00
|
|
|
|
app.mount(
|
|
|
|
|
"/core/static", StaticFiles(directory="lnbits/core/static"), name="core_static"
|
|
|
|
|
)
|
2021-08-22 20:07:24 +02:00
|
|
|
|
|
2021-12-30 17:09:12 +01:00
|
|
|
|
origins = ["*"]
|
2021-08-22 20:07:24 +02:00
|
|
|
|
|
|
|
|
|
app.add_middleware(
|
|
|
|
|
CORSMiddleware,
|
|
|
|
|
allow_origins=origins,
|
|
|
|
|
allow_methods=["*"],
|
|
|
|
|
allow_headers=["*"],
|
|
|
|
|
)
|
2021-10-17 19:33:29 +02:00
|
|
|
|
|
2021-08-22 20:07:24 +02:00
|
|
|
|
g().config = lnbits.settings
|
2021-08-27 20:54:42 +02:00
|
|
|
|
g().base_url = f"http://{lnbits.settings.HOST}:{lnbits.settings.PORT}"
|
|
|
|
|
|
|
|
|
|
@app.exception_handler(RequestValidationError)
|
2021-10-17 19:33:29 +02:00
|
|
|
|
async def validation_exception_handler(
|
|
|
|
|
request: Request, exc: RequestValidationError
|
|
|
|
|
):
|
|
|
|
|
return template_renderer().TemplateResponse(
|
|
|
|
|
"error.html",
|
|
|
|
|
{"request": request, "err": f"`{exc.errors()}` is not a valid UUID."},
|
|
|
|
|
)
|
|
|
|
|
|
2021-08-27 20:54:42 +02:00
|
|
|
|
# return HTMLResponse(
|
|
|
|
|
# status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
|
|
|
|
# content=jsonable_encoder({"detail": exc.errors(), "body": exc.body}),
|
|
|
|
|
# )
|
2021-08-22 20:07:24 +02:00
|
|
|
|
|
|
|
|
|
app.add_middleware(GZipMiddleware, minimum_size=1000)
|
|
|
|
|
# app.add_middleware(ASGIProxyFix)
|
2020-09-14 02:31:05 +02:00
|
|
|
|
|
2020-10-13 03:25:55 +02:00
|
|
|
|
check_funding_source(app)
|
2020-09-15 20:54:05 +02:00
|
|
|
|
register_assets(app)
|
2021-08-22 20:07:24 +02:00
|
|
|
|
register_routes(app)
|
|
|
|
|
# register_commands(app)
|
2020-09-28 04:12:55 +02:00
|
|
|
|
register_async_tasks(app)
|
2021-09-30 19:16:38 +02:00
|
|
|
|
register_exception_handlers(app)
|
2020-09-05 08:00:44 +02:00
|
|
|
|
|
|
|
|
|
return app
|
|
|
|
|
|
2021-10-17 19:33:29 +02:00
|
|
|
|
|
2021-08-22 20:07:24 +02:00
|
|
|
|
def check_funding_source(app: FastAPI) -> None:
|
|
|
|
|
@app.on_event("startup")
|
2020-10-13 03:25:55 +02:00
|
|
|
|
async def check_wallet_status():
|
2021-03-24 05:01:09 +01:00
|
|
|
|
error_message, balance = await WALLET.status()
|
2020-10-13 03:25:55 +02:00
|
|
|
|
if error_message:
|
|
|
|
|
warnings.warn(
|
|
|
|
|
f" × The backend for {WALLET.__class__.__name__} isn't working properly: '{error_message}'",
|
|
|
|
|
RuntimeWarning,
|
|
|
|
|
)
|
2021-03-24 13:49:43 +01:00
|
|
|
|
|
|
|
|
|
sys.exit(4)
|
2020-10-13 03:25:55 +02:00
|
|
|
|
else:
|
2021-03-24 04:40:32 +01:00
|
|
|
|
print(
|
|
|
|
|
f" ✔️ {WALLET.__class__.__name__} seems to be connected and with a balance of {balance} msat."
|
|
|
|
|
)
|
2020-10-13 03:25:55 +02:00
|
|
|
|
|
|
|
|
|
|
2021-08-22 20:07:24 +02:00
|
|
|
|
def register_routes(app: FastAPI) -> None:
|
2021-08-23 21:17:46 +02:00
|
|
|
|
"""Register FastAPI routes / LNbits extensions."""
|
2021-08-22 20:07:24 +02:00
|
|
|
|
app.include_router(core_app)
|
2021-08-23 21:17:46 +02:00
|
|
|
|
app.include_router(core_html_routes)
|
2020-09-05 08:00:44 +02:00
|
|
|
|
|
|
|
|
|
for ext in get_valid_extensions():
|
|
|
|
|
try:
|
|
|
|
|
ext_module = importlib.import_module(f"lnbits.extensions.{ext.code}")
|
2021-08-22 20:07:24 +02:00
|
|
|
|
ext_route = getattr(ext_module, f"{ext.code}_ext")
|
2021-10-17 19:33:29 +02:00
|
|
|
|
|
2021-09-29 20:44:00 +02:00
|
|
|
|
if hasattr(ext_module, f"{ext.code}_start"):
|
|
|
|
|
ext_start_func = getattr(ext_module, f"{ext.code}_start")
|
|
|
|
|
ext_start_func()
|
|
|
|
|
|
2021-09-28 21:10:51 +02:00
|
|
|
|
if hasattr(ext_module, f"{ext.code}_static_files"):
|
|
|
|
|
ext_statics = getattr(ext_module, f"{ext.code}_static_files")
|
|
|
|
|
for s in ext_statics:
|
|
|
|
|
app.mount(s["path"], s["app"], s["name"])
|
2020-10-02 22:12:49 +02:00
|
|
|
|
|
2021-08-22 20:07:24 +02:00
|
|
|
|
app.include_router(ext_route)
|
2021-09-16 19:42:05 +02:00
|
|
|
|
except Exception as e:
|
|
|
|
|
print(str(e))
|
2021-03-24 04:40:32 +01:00
|
|
|
|
raise ImportError(
|
|
|
|
|
f"Please make sure that the extension `{ext.code}` follows conventions."
|
|
|
|
|
)
|
2020-09-05 08:00:44 +02:00
|
|
|
|
|
|
|
|
|
|
2021-08-22 20:07:24 +02:00
|
|
|
|
def register_commands(app: FastAPI):
|
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
|
|
|
|
|
|
|
|
|
|
2021-08-22 20:07:24 +02:00
|
|
|
|
def register_assets(app: FastAPI):
|
2020-09-15 20:54:05 +02:00
|
|
|
|
"""Serve each vendored asset separately or a bundle."""
|
|
|
|
|
|
2021-08-22 20:07:24 +02:00
|
|
|
|
@app.on_event("startup")
|
2020-09-15 20:54:05 +02:00
|
|
|
|
async def vendored_assets_variable():
|
2021-08-22 20:07:24 +02:00
|
|
|
|
if g().config.DEBUG:
|
|
|
|
|
g().VENDORED_JS = map(url_for_vendored, get_js_vendored())
|
|
|
|
|
g().VENDORED_CSS = map(url_for_vendored, get_css_vendored())
|
2020-09-15 20:54:05 +02:00
|
|
|
|
else:
|
2021-08-22 20:07:24 +02:00
|
|
|
|
g().VENDORED_JS = ["/static/bundle.js"]
|
|
|
|
|
g().VENDORED_CSS = ["/static/bundle.css"]
|
2020-09-07 05:47:13 +02:00
|
|
|
|
|
|
|
|
|
|
2020-09-28 04:12:55 +02:00
|
|
|
|
def register_async_tasks(app):
|
2021-08-22 20:07:24 +02:00
|
|
|
|
@app.route("/wallet/webhook")
|
2020-09-28 04:12:55 +02:00
|
|
|
|
async def webhook_listener():
|
|
|
|
|
return await webhook_handler()
|
|
|
|
|
|
2021-08-22 20:07:24 +02:00
|
|
|
|
@app.on_event("startup")
|
2020-09-28 04:12:55 +02:00
|
|
|
|
async def listeners():
|
2021-08-30 19:55:02 +02:00
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
|
loop.create_task(catch_everything_and_restart(check_pending_payments))
|
|
|
|
|
loop.create_task(catch_everything_and_restart(invoice_listener))
|
|
|
|
|
loop.create_task(catch_everything_and_restart(internal_invoice_listener))
|
|
|
|
|
await register_task_listeners()
|
|
|
|
|
await run_deferred_async()
|
2021-08-22 20:07:24 +02:00
|
|
|
|
|
|
|
|
|
@app.on_event("shutdown")
|
2020-09-28 04:12:55 +02:00
|
|
|
|
async def stop_listeners():
|
|
|
|
|
pass
|
2021-05-07 04:22:02 +02:00
|
|
|
|
|
2021-10-17 19:33:29 +02:00
|
|
|
|
|
2021-09-30 19:16:38 +02:00
|
|
|
|
def register_exception_handlers(app: FastAPI):
|
|
|
|
|
@app.exception_handler(Exception)
|
2021-08-23 20:19:43 +02:00
|
|
|
|
async def basic_error(request: Request, err):
|
2021-07-31 13:15:28 +02:00
|
|
|
|
print("handled error", traceback.format_exc())
|
2021-09-30 19:16:38 +02:00
|
|
|
|
etype, _, tb = sys.exc_info()
|
2021-05-07 04:22:02 +02:00
|
|
|
|
traceback.print_exception(etype, err, tb)
|
|
|
|
|
exc = traceback.format_exc()
|
2021-10-17 19:33:29 +02:00
|
|
|
|
return template_renderer().TemplateResponse(
|
|
|
|
|
"error.html", {"request": request, "err": err}
|
|
|
|
|
)
|