2021-08-30 19:55:02 +02:00
|
|
|
import asyncio
|
2021-05-07 04:22:02 +02:00
|
|
|
import importlib
|
2022-07-15 19:54:12 +02:00
|
|
|
import logging
|
2022-08-13 14:46:47 +02:00
|
|
|
import signal
|
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
|
2022-06-13 22:43:17 +02:00
|
|
|
from http import HTTPStatus
|
2020-09-05 08:00:44 +02:00
|
|
|
|
2022-06-13 22:43:17 +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
|
2022-06-13 16:18:14 +02:00
|
|
|
from fastapi.responses import JSONResponse
|
2021-08-22 20:07:24 +02:00
|
|
|
from fastapi.staticfiles import StaticFiles
|
2022-07-15 19:54:12 +02:00
|
|
|
from loguru import logger
|
2020-09-05 08:00:44 +02:00
|
|
|
|
2021-12-30 17:09:12 +01:00
|
|
|
from lnbits.core.tasks import register_task_listeners
|
2022-10-03 16:42:18 +02:00
|
|
|
from lnbits.settings import WALLET, check_admin_settings, settings
|
|
|
|
|
|
|
|
from .commands import migrate_databases
|
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
|
2021-10-17 19:33:29 +02:00
|
|
|
from .tasks import (
|
|
|
|
catch_everything_and_restart,
|
|
|
|
check_pending_payments,
|
|
|
|
internal_invoice_listener,
|
|
|
|
invoice_listener,
|
|
|
|
webhook_handler,
|
|
|
|
)
|
2021-08-27 20:54:42 +02:00
|
|
|
|
2020-09-05 08:00:44 +02:00
|
|
|
|
2022-10-03 16:42:18 +02:00
|
|
|
def create_app() -> FastAPI:
|
|
|
|
|
2022-07-07 16:24:36 +02:00
|
|
|
configure_logger()
|
2022-07-07 14:30:16 +02:00
|
|
|
|
2022-07-27 19:20:36 +02:00
|
|
|
app = FastAPI(
|
|
|
|
title="LNbits API",
|
|
|
|
description="API for LNbits, the free and open source bitcoin wallet and accounts system with plugins.",
|
|
|
|
license_info={
|
|
|
|
"name": "MIT License",
|
|
|
|
"url": "https://raw.githubusercontent.com/lnbits/lnbits-legend/main/LICENSE",
|
|
|
|
},
|
|
|
|
)
|
2022-09-22 11:47:24 +02:00
|
|
|
|
2022-07-08 10:49:38 +02:00
|
|
|
app.mount("/static", StaticFiles(packages=[("lnbits", "static")]), name="static")
|
2021-10-17 19:33:29 +02:00
|
|
|
app.mount(
|
2022-07-27 16:45:17 +02:00
|
|
|
"/core/static",
|
|
|
|
StaticFiles(packages=[("lnbits.core", "static")]),
|
|
|
|
name="core_static",
|
2021-10-17 19:33:29 +02:00
|
|
|
)
|
2021-08-22 20:07:24 +02:00
|
|
|
|
|
|
|
app.add_middleware(
|
2022-10-03 22:14:07 +02:00
|
|
|
CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"]
|
2021-08-22 20:07:24 +02:00
|
|
|
)
|
2021-08-27 20:54:42 +02:00
|
|
|
|
2022-10-03 22:14:07 +02:00
|
|
|
# TODO: why those 2?
|
2022-10-03 16:42:18 +02:00
|
|
|
g().config = settings
|
2022-10-03 22:14:07 +02:00
|
|
|
# g().base_url = f"http://{settings.host}:{settings.port}"
|
2021-08-22 20:07:24 +02:00
|
|
|
|
|
|
|
app.add_middleware(GZipMiddleware, minimum_size=1000)
|
2020-09-14 02:31:05 +02:00
|
|
|
|
2022-10-03 16:42:18 +02:00
|
|
|
register_startup(app)
|
2020-09-15 20:54:05 +02:00
|
|
|
register_assets(app)
|
2021-08-22 20:07:24 +02:00
|
|
|
register_routes(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
|
|
|
|
|
2022-09-22 10:46:11 +02:00
|
|
|
|
2022-10-03 16:42:18 +02:00
|
|
|
async def check_funding_source() -> None:
|
|
|
|
# original_sigint_handler = signal.getsignal(signal.SIGINT)
|
2022-09-22 10:46:11 +02:00
|
|
|
|
2022-10-03 16:42:18 +02:00
|
|
|
# def signal_handler(signal, frame):
|
|
|
|
# logger.debug(f"SIGINT received, terminating LNbits.")
|
|
|
|
# sys.exit(1)
|
|
|
|
# signal.signal(signal.SIGINT, signal_handler)
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
error_message, balance = await WALLET.status()
|
|
|
|
if not error_message:
|
|
|
|
break
|
|
|
|
logger.error(
|
|
|
|
f"The backend for {WALLET.__class__.__name__} isn't working properly: '{error_message}'",
|
|
|
|
RuntimeWarning,
|
|
|
|
)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
logger.info("Retrying connection to backend in 5 seconds...")
|
|
|
|
await asyncio.sleep(5)
|
|
|
|
# signal.signal(signal.SIGINT, original_sigint_handler)
|
|
|
|
logger.info(
|
|
|
|
f"✔️ Backend {WALLET.__class__.__name__} 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
|
|
|
|
2022-07-07 14:30:16 +02:00
|
|
|
logger.trace(f"adding route for extension {ext_module}")
|
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:
|
2022-07-07 14:30:16 +02:00
|
|
|
logger.error(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
|
|
|
|
|
|
|
|
2022-10-03 16:42:18 +02:00
|
|
|
def register_startup(app: FastAPI):
|
|
|
|
@app.on_event("startup")
|
|
|
|
async def lnbits_startup():
|
|
|
|
|
|
|
|
# 1. wait till migration is done
|
|
|
|
await migrate_databases()
|
|
|
|
|
|
|
|
# 2. setup admin settings
|
|
|
|
await check_admin_settings()
|
|
|
|
|
|
|
|
logger.info("Starting LNbits")
|
|
|
|
logger.info(f"Host: {settings.host}")
|
|
|
|
logger.info(f"Port: {settings.port}")
|
|
|
|
logger.info(f"Debug: {settings.debug}")
|
|
|
|
logger.info(f"Site title: {settings.lnbits_site_title}")
|
|
|
|
logger.info(f"Funding source: {settings.lnbits_backend_wallet_class}")
|
|
|
|
logger.info(f"Data folder: {settings.lnbits_data_folder}")
|
|
|
|
logger.info(f"Git version: {settings.lnbits_commit}")
|
|
|
|
|
|
|
|
db_url = settings.lnbits_database_url
|
|
|
|
database = (
|
|
|
|
"PostgreSQL"
|
|
|
|
if db_url and db_url.startswith("postgres://")
|
|
|
|
else "CockroachDB"
|
|
|
|
if db_url and db_url.startswith("cockroachdb://")
|
|
|
|
else "SQLite"
|
|
|
|
)
|
|
|
|
logger.info(f"Database: {database}")
|
|
|
|
logger.info(f"Service fee: {settings.lnbits_service_fee}")
|
|
|
|
|
|
|
|
# 3. initialize funding source
|
|
|
|
await check_funding_source()
|
|
|
|
|
|
|
|
|
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():
|
2022-10-03 16:42:18 +02:00
|
|
|
if g().config.debug:
|
2021-08-22 20:07:24 +02:00
|
|
|
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()
|
2022-10-04 09:51:47 +02:00
|
|
|
# await run_deferred_async() # calle: doesn't do anyting?
|
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):
|
2022-07-07 14:30:16 +02:00
|
|
|
logger.error("handled error", traceback.format_exc())
|
|
|
|
logger.error("ERROR:", err)
|
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()
|
2022-06-15 17:37:10 +02:00
|
|
|
|
2022-08-14 23:17:53 +02:00
|
|
|
if (
|
|
|
|
request.headers
|
|
|
|
and "accept" in request.headers
|
|
|
|
and "text/html" in request.headers["accept"]
|
|
|
|
):
|
2022-06-15 17:20:20 +02:00
|
|
|
return template_renderer().TemplateResponse(
|
|
|
|
"error.html", {"request": request, "err": err}
|
|
|
|
)
|
2022-06-15 17:49:39 +02:00
|
|
|
|
2022-06-15 17:20:20 +02:00
|
|
|
return JSONResponse(
|
|
|
|
status_code=HTTPStatus.NO_CONTENT,
|
2022-06-15 17:49:39 +02:00
|
|
|
content={"detail": err},
|
2021-10-17 19:33:29 +02:00
|
|
|
)
|
2022-07-07 14:30:16 +02:00
|
|
|
|
2022-10-03 16:42:18 +02:00
|
|
|
@app.exception_handler(RequestValidationError)
|
|
|
|
async def validation_exception_handler(
|
|
|
|
request: Request, exc: RequestValidationError
|
|
|
|
):
|
|
|
|
# Only the browser sends "text/html" request
|
|
|
|
# not fail proof, but everything else get's a JSON response
|
|
|
|
|
|
|
|
if (
|
|
|
|
request.headers
|
|
|
|
and "accept" in request.headers
|
|
|
|
and "text/html" in request.headers["accept"]
|
|
|
|
):
|
|
|
|
return template_renderer().TemplateResponse(
|
|
|
|
"error.html",
|
|
|
|
{"request": request, "err": f"{exc.errors()} is not a valid UUID."},
|
|
|
|
)
|
|
|
|
|
|
|
|
return JSONResponse(
|
|
|
|
status_code=HTTPStatus.NO_CONTENT,
|
|
|
|
content={"detail": exc.errors()},
|
|
|
|
)
|
|
|
|
|
2022-07-07 14:30:16 +02:00
|
|
|
|
2022-07-07 16:24:36 +02:00
|
|
|
def configure_logger() -> None:
|
2022-07-07 14:30:16 +02:00
|
|
|
logger.remove()
|
2022-10-03 16:42:18 +02:00
|
|
|
log_level: str = "DEBUG" if settings.debug else "INFO"
|
2022-07-15 19:54:12 +02:00
|
|
|
formatter = Formatter()
|
|
|
|
logger.add(sys.stderr, level=log_level, format=formatter.format)
|
|
|
|
|
|
|
|
logging.getLogger("uvicorn").handlers = [InterceptHandler()]
|
|
|
|
logging.getLogger("uvicorn.access").handlers = [InterceptHandler()]
|
|
|
|
|
|
|
|
|
|
|
|
class Formatter:
|
|
|
|
def __init__(self):
|
|
|
|
self.padding = 0
|
|
|
|
self.minimal_fmt: str = "<green>{time:YYYY-MM-DD HH:mm:ss.SS}</green> | <level>{level}</level> | <level>{message}</level>\n"
|
2022-10-03 16:42:18 +02:00
|
|
|
if settings.debug:
|
2022-07-15 19:54:12 +02:00
|
|
|
self.fmt: str = "<green>{time:YYYY-MM-DD HH:mm:ss.SS}</green> | <level>{level: <4}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> | <level>{message}</level>\n"
|
|
|
|
else:
|
|
|
|
self.fmt: str = self.minimal_fmt
|
|
|
|
|
|
|
|
def format(self, record):
|
|
|
|
function = "{function}".format(**record)
|
|
|
|
if function == "emit": # uvicorn logs
|
|
|
|
return self.minimal_fmt
|
|
|
|
return self.fmt
|
|
|
|
|
|
|
|
|
|
|
|
class InterceptHandler(logging.Handler):
|
|
|
|
def emit(self, record):
|
|
|
|
try:
|
|
|
|
level = logger.level(record.levelname).name
|
|
|
|
except ValueError:
|
|
|
|
level = record.levelno
|
|
|
|
logger.log(level, record.getMessage())
|