2021-08-30 19:55:02 +02:00
|
|
|
import asyncio
|
2022-12-22 15:30:37 +01:00
|
|
|
import glob
|
2021-05-07 04:22:02 +02:00
|
|
|
import importlib
|
2022-12-22 15:30:37 +01:00
|
|
|
import os
|
2023-01-18 15:25:44 +01:00
|
|
|
import shutil
|
2021-08-27 20:54:42 +02:00
|
|
|
import sys
|
2024-04-05 07:05:26 +02:00
|
|
|
from contextlib import asynccontextmanager
|
2023-09-25 12:44:29 +02:00
|
|
|
from pathlib import Path
|
2024-01-22 11:18:12 +01:00
|
|
|
from typing import Callable, List, Optional
|
2020-09-05 08:00:44 +02:00
|
|
|
|
2024-05-13 17:58:48 +02:00
|
|
|
from fastapi import FastAPI
|
2021-08-22 20:07:24 +02:00
|
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
from fastapi.staticfiles import StaticFiles
|
2022-07-15 19:54:12 +02:00
|
|
|
from loguru import logger
|
2023-06-20 11:26:33 +02:00
|
|
|
from slowapi import Limiter
|
|
|
|
from slowapi.util import get_remote_address
|
2023-12-12 11:38:19 +01:00
|
|
|
from starlette.middleware.sessions import SessionMiddleware
|
2020-09-05 08:00:44 +02:00
|
|
|
|
2024-05-28 13:07:33 +02:00
|
|
|
from lnbits.core.crud import (
|
|
|
|
get_dbversions,
|
|
|
|
get_installed_extensions,
|
|
|
|
update_installed_extension_state,
|
|
|
|
)
|
2023-01-18 17:35:02 +01:00
|
|
|
from lnbits.core.helpers import migrate_extension_database
|
2024-03-22 12:29:05 +01:00
|
|
|
from lnbits.core.tasks import ( # watchdog_task
|
|
|
|
killswitch_task,
|
|
|
|
wait_for_paid_invoices,
|
2023-06-20 11:26:33 +02:00
|
|
|
)
|
2024-05-13 17:58:48 +02:00
|
|
|
from lnbits.exceptions import register_exception_handlers
|
2023-06-27 16:11:00 +02:00
|
|
|
from lnbits.settings import settings
|
2024-03-22 12:29:05 +01:00
|
|
|
from lnbits.tasks import (
|
|
|
|
cancel_all_tasks,
|
|
|
|
create_permanent_task,
|
|
|
|
register_invoice_listener,
|
|
|
|
)
|
2023-08-28 11:56:59 +02:00
|
|
|
from lnbits.utils.cache import cache
|
2024-05-09 18:51:18 +02:00
|
|
|
from lnbits.utils.logger import (
|
|
|
|
configure_logger,
|
|
|
|
initialize_server_websocket_logger,
|
|
|
|
log_server_info,
|
|
|
|
)
|
2024-04-15 09:02:21 +02:00
|
|
|
from lnbits.wallets import get_funding_source, set_funding_source
|
2022-10-03 16:42:18 +02:00
|
|
|
|
2024-02-09 13:10:51 +01:00
|
|
|
from .commands import migrate_databases
|
2023-09-12 12:25:05 +02:00
|
|
|
from .core import init_core_routers
|
|
|
|
from .core.db import core_app_extra
|
2023-09-11 15:48:49 +02:00
|
|
|
from .core.services import check_admin_settings, check_webpush_settings
|
2024-03-28 08:59:28 +01:00
|
|
|
from .core.views.extension_api import add_installed_extension
|
2023-10-18 13:11:58 +02:00
|
|
|
from .extension_manager import (
|
|
|
|
Extension,
|
|
|
|
InstallableExtension,
|
|
|
|
get_valid_extensions,
|
|
|
|
version_parse,
|
|
|
|
)
|
2023-06-20 11:26:33 +02:00
|
|
|
from .middleware import (
|
2023-08-18 12:05:14 +02:00
|
|
|
CustomGZipMiddleware,
|
2023-06-20 11:26:33 +02:00
|
|
|
ExtensionsRedirectMiddleware,
|
|
|
|
InstalledExtensionMiddleware,
|
2024-01-25 14:33:40 +01:00
|
|
|
add_first_install_middleware,
|
2023-06-20 11:26:33 +02:00
|
|
|
add_ip_block_middleware,
|
|
|
|
add_ratelimit_middleware,
|
|
|
|
)
|
2021-08-27 20:54:42 +02:00
|
|
|
from .requestvars import g
|
2021-10-17 19:33:29 +02:00
|
|
|
from .tasks import (
|
|
|
|
check_pending_payments,
|
2024-07-23 14:01:34 +02:00
|
|
|
create_task,
|
2021-10-17 19:33:29 +02:00
|
|
|
internal_invoice_listener,
|
|
|
|
invoice_listener,
|
|
|
|
)
|
2021-08-27 20:54:42 +02:00
|
|
|
|
2020-09-05 08:00:44 +02:00
|
|
|
|
2024-04-05 07:05:26 +02:00
|
|
|
async def startup(app: FastAPI):
|
2024-04-22 11:33:53 +02:00
|
|
|
settings.lnbits_running = True
|
|
|
|
|
2024-04-05 07:05:26 +02:00
|
|
|
# wait till migration is done
|
|
|
|
await migrate_databases()
|
|
|
|
|
|
|
|
# setup admin settings
|
|
|
|
await check_admin_settings()
|
|
|
|
await check_webpush_settings()
|
|
|
|
|
|
|
|
log_server_info()
|
|
|
|
|
|
|
|
# initialize WALLET
|
|
|
|
try:
|
2024-04-15 09:02:21 +02:00
|
|
|
set_funding_source()
|
2024-04-05 07:05:26 +02:00
|
|
|
except Exception as e:
|
|
|
|
logger.error(f"Error initializing {settings.lnbits_backend_wallet_class}: {e}")
|
|
|
|
set_void_wallet_class()
|
|
|
|
|
|
|
|
# initialize funding source
|
|
|
|
await check_funding_source()
|
|
|
|
|
|
|
|
# register core routes
|
|
|
|
init_core_routers(app)
|
|
|
|
|
|
|
|
# initialize tasks
|
2024-07-23 14:01:34 +02:00
|
|
|
register_async_tasks(app)
|
2024-04-05 07:05:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
async def shutdown():
|
2024-04-22 11:33:53 +02:00
|
|
|
logger.warning("LNbits shutting down...")
|
|
|
|
settings.lnbits_running = False
|
|
|
|
|
2024-04-05 07:05:26 +02:00
|
|
|
# shutdown event
|
|
|
|
cancel_all_tasks()
|
|
|
|
|
|
|
|
# wait a bit to allow them to finish, so that cleanup can run without problems
|
|
|
|
await asyncio.sleep(0.1)
|
2024-04-15 09:02:21 +02:00
|
|
|
funding_source = get_funding_source()
|
|
|
|
await funding_source.cleanup()
|
2024-04-05 07:05:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
@asynccontextmanager
|
|
|
|
async def lifespan(app: FastAPI):
|
|
|
|
await startup(app)
|
|
|
|
yield
|
|
|
|
await shutdown()
|
|
|
|
|
|
|
|
|
2022-10-03 16:42:18 +02:00
|
|
|
def create_app() -> FastAPI:
|
2022-07-07 16:24:36 +02:00
|
|
|
configure_logger()
|
2022-07-27 19:20:36 +02:00
|
|
|
app = FastAPI(
|
2023-08-24 11:52:12 +02:00
|
|
|
title=settings.lnbits_title,
|
2023-08-18 11:23:57 +02:00
|
|
|
description=(
|
|
|
|
"API for LNbits, the free and open source bitcoin wallet and "
|
|
|
|
"accounts system with plugins."
|
|
|
|
),
|
2023-03-30 14:19:45 +02:00
|
|
|
version=settings.version,
|
2024-04-05 07:05:26 +02:00
|
|
|
lifespan=lifespan,
|
2022-07-27 19:20:36 +02:00
|
|
|
license_info={
|
|
|
|
"name": "MIT License",
|
2022-12-05 12:18:59 +01:00
|
|
|
"url": "https://raw.githubusercontent.com/lnbits/lnbits/main/LICENSE",
|
2022-07-27 19:20:36 +02:00
|
|
|
},
|
|
|
|
)
|
2022-09-22 11:47:24 +02:00
|
|
|
|
2023-09-25 11:54:02 +02:00
|
|
|
# Allow registering new extensions routes without direct access to the `app` object
|
2024-04-17 13:11:51 +02:00
|
|
|
core_app_extra.register_new_ext_routes = register_new_ext_routes(app)
|
|
|
|
core_app_extra.register_new_ratelimiter = register_new_ratelimiter(app)
|
2023-09-25 11:54:02 +02:00
|
|
|
|
2023-10-27 13:50:49 +02:00
|
|
|
# register static files
|
|
|
|
static_path = Path("lnbits", "static")
|
|
|
|
static = StaticFiles(directory=static_path)
|
|
|
|
app.mount("/static", static, name="static")
|
2021-08-22 20:07:24 +02:00
|
|
|
|
2022-11-24 11:35:03 +01:00
|
|
|
g().base_url = f"http://{settings.host}:{settings.port}"
|
|
|
|
|
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
|
|
|
|
2023-08-18 12:05:14 +02:00
|
|
|
app.add_middleware(
|
|
|
|
CustomGZipMiddleware, minimum_size=1000, exclude_paths=["/api/v1/payments/sse"]
|
|
|
|
)
|
2023-02-22 10:12:16 +01:00
|
|
|
|
2023-12-12 11:38:19 +01:00
|
|
|
# required for SSO login
|
|
|
|
app.add_middleware(SessionMiddleware, secret_key=settings.auth_secret_key)
|
|
|
|
|
2023-02-22 10:12:16 +01:00
|
|
|
# order of these two middlewares is important
|
2022-11-29 17:19:33 +01:00
|
|
|
app.add_middleware(InstalledExtensionMiddleware)
|
2023-02-22 10:12:16 +01:00
|
|
|
app.add_middleware(ExtensionsRedirectMiddleware)
|
2020-09-14 02:31:05 +02:00
|
|
|
|
2023-09-25 12:44:29 +02:00
|
|
|
register_custom_extensions_path()
|
|
|
|
|
2024-01-25 14:33:40 +01:00
|
|
|
add_first_install_middleware(app)
|
|
|
|
|
2023-09-25 11:54:02 +02:00
|
|
|
# adds security middleware
|
|
|
|
add_ip_block_middleware(app)
|
|
|
|
add_ratelimit_middleware(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:
|
2024-04-15 09:02:21 +02:00
|
|
|
funding_source = get_funding_source()
|
2023-03-02 12:03:58 +01:00
|
|
|
|
2024-04-11 18:29:25 +02:00
|
|
|
max_retries = settings.funding_source_max_retries
|
2023-03-02 12:03:58 +01:00
|
|
|
retry_counter = 0
|
|
|
|
|
2024-04-22 11:33:53 +02:00
|
|
|
while settings.lnbits_running:
|
2022-10-03 16:42:18 +02:00
|
|
|
try:
|
2024-04-15 09:02:21 +02:00
|
|
|
logger.info(f"Connecting to backend {funding_source.__class__.__name__}...")
|
|
|
|
error_message, balance = await funding_source.status()
|
2022-10-03 16:42:18 +02:00
|
|
|
if not error_message:
|
2023-03-02 12:03:58 +01:00
|
|
|
retry_counter = 0
|
2024-04-11 14:19:09 +02:00
|
|
|
logger.success(
|
2024-04-15 09:02:21 +02:00
|
|
|
f"✔️ Backend {funding_source.__class__.__name__} connected "
|
2024-04-11 14:19:09 +02:00
|
|
|
f"and with a balance of {balance} msat."
|
|
|
|
)
|
2022-10-03 16:42:18 +02:00
|
|
|
break
|
|
|
|
logger.error(
|
2024-04-15 09:02:21 +02:00
|
|
|
f"The backend for {funding_source.__class__.__name__} isn't "
|
2023-08-18 11:23:57 +02:00
|
|
|
f"working properly: '{error_message}'",
|
2022-10-03 16:42:18 +02:00
|
|
|
RuntimeWarning,
|
|
|
|
)
|
2023-08-23 08:59:39 +02:00
|
|
|
except Exception as e:
|
2024-04-15 09:02:21 +02:00
|
|
|
logger.error(
|
|
|
|
f"Error connecting to {funding_source.__class__.__name__}: {e}"
|
|
|
|
)
|
2023-03-02 12:03:58 +01:00
|
|
|
|
2024-04-11 18:29:25 +02:00
|
|
|
if retry_counter >= max_retries:
|
2023-08-18 11:23:57 +02:00
|
|
|
set_void_wallet_class()
|
2024-04-15 09:02:21 +02:00
|
|
|
funding_source = get_funding_source()
|
2023-03-02 12:03:58 +01:00
|
|
|
break
|
|
|
|
|
2024-04-11 14:19:09 +02:00
|
|
|
retry_counter += 1
|
2024-04-11 18:29:25 +02:00
|
|
|
sleep_time = 0.25 * (2**retry_counter)
|
2024-04-11 14:19:09 +02:00
|
|
|
logger.warning(
|
|
|
|
f"Retrying connection to backend in {sleep_time} seconds... "
|
|
|
|
f"({retry_counter}/{max_retries})"
|
|
|
|
)
|
|
|
|
await asyncio.sleep(sleep_time)
|
2023-08-18 11:23:57 +02:00
|
|
|
|
|
|
|
|
|
|
|
def set_void_wallet_class():
|
|
|
|
logger.warning(
|
|
|
|
"Fallback to VoidWallet, because the backend for "
|
|
|
|
f"{settings.lnbits_backend_wallet_class} isn't working properly"
|
2022-10-03 16:42:18 +02:00
|
|
|
)
|
2024-04-15 09:02:21 +02:00
|
|
|
set_funding_source("VoidWallet")
|
2020-10-13 03:25:55 +02:00
|
|
|
|
|
|
|
|
2023-01-18 16:16:15 +01:00
|
|
|
async def check_installed_extensions(app: FastAPI):
|
2022-12-22 15:30:37 +01:00
|
|
|
"""
|
2023-08-18 11:23:57 +02:00
|
|
|
Check extensions that have been installed, but for some reason no longer present in
|
|
|
|
the 'lnbits/extensions' directory. One reason might be a docker-container that was
|
|
|
|
re-created. The 'data' directory (where the '.zip' files live) is expected to
|
|
|
|
persist state. Zips that are missing will be re-downloaded.
|
2022-12-22 15:30:37 +01:00
|
|
|
"""
|
2023-01-19 10:12:48 +01:00
|
|
|
shutil.rmtree(os.path.join("lnbits", "upgrades"), True)
|
2024-01-22 11:18:12 +01:00
|
|
|
installed_extensions = await build_all_installed_extensions_list(False)
|
2023-01-18 15:25:44 +01:00
|
|
|
|
|
|
|
for ext in installed_extensions:
|
|
|
|
try:
|
2024-02-22 14:16:41 +01:00
|
|
|
installed = await check_installed_extension_files(ext)
|
2023-01-18 17:35:02 +01:00
|
|
|
if not installed:
|
2023-01-19 10:12:48 +01:00
|
|
|
await restore_installed_extension(app, ext)
|
2023-05-24 12:58:11 +02:00
|
|
|
logger.info(
|
2023-08-18 11:23:57 +02:00
|
|
|
"✔️ Successfully re-installed extension: "
|
|
|
|
f"{ext.id} ({ext.installed_version})"
|
2023-05-24 12:58:11 +02:00
|
|
|
)
|
2023-01-23 10:51:53 +01:00
|
|
|
except Exception as e:
|
|
|
|
logger.warning(e)
|
2023-05-24 12:58:11 +02:00
|
|
|
logger.warning(
|
|
|
|
f"Failed to re-install extension: {ext.id} ({ext.installed_version})"
|
|
|
|
)
|
2023-01-18 16:16:15 +01:00
|
|
|
|
2023-08-18 11:13:37 +02:00
|
|
|
logger.info(f"Installed Extensions ({len(installed_extensions)}):")
|
|
|
|
for ext in installed_extensions:
|
|
|
|
logger.info(f"{ext.id} ({ext.installed_version})")
|
|
|
|
|
2022-12-22 15:30:37 +01:00
|
|
|
|
2024-01-22 11:18:12 +01:00
|
|
|
async def build_all_installed_extensions_list(
|
|
|
|
include_deactivated: Optional[bool] = True,
|
|
|
|
) -> List[InstallableExtension]:
|
2023-02-16 09:48:12 +01:00
|
|
|
"""
|
|
|
|
Returns a list of all the installed extensions plus the extensions that
|
|
|
|
MUST be installed by default (see LNBITS_EXTENSIONS_DEFAULT_INSTALL).
|
|
|
|
"""
|
|
|
|
installed_extensions = await get_installed_extensions()
|
2024-05-21 12:17:02 +02:00
|
|
|
settings.lnbits_all_extensions_ids = {e.id for e in installed_extensions}
|
2023-02-16 09:48:12 +01:00
|
|
|
|
|
|
|
for ext_id in settings.lnbits_extensions_default_install:
|
2024-05-21 12:17:02 +02:00
|
|
|
if ext_id in settings.lnbits_all_extensions_ids:
|
2023-02-16 09:48:12 +01:00
|
|
|
continue
|
|
|
|
|
|
|
|
ext_releases = await InstallableExtension.get_extension_releases(ext_id)
|
2023-10-18 13:11:58 +02:00
|
|
|
ext_releases = sorted(
|
|
|
|
ext_releases, key=lambda r: version_parse(r.version), reverse=True
|
|
|
|
)
|
|
|
|
|
|
|
|
release = next((e for e in ext_releases if e.is_version_compatible), None)
|
2023-02-16 09:48:12 +01:00
|
|
|
|
|
|
|
if release:
|
|
|
|
ext_info = InstallableExtension(
|
|
|
|
id=ext_id, name=ext_id, installed_release=release, icon=release.icon
|
|
|
|
)
|
|
|
|
installed_extensions.append(ext_info)
|
|
|
|
|
2024-01-22 11:18:12 +01:00
|
|
|
if include_deactivated:
|
|
|
|
return installed_extensions
|
|
|
|
|
|
|
|
if settings.lnbits_extensions_deactivate_all:
|
|
|
|
return []
|
|
|
|
|
|
|
|
return [
|
|
|
|
e
|
|
|
|
for e in installed_extensions
|
|
|
|
if e.id not in settings.lnbits_deactivated_extensions
|
|
|
|
]
|
2023-02-16 09:48:12 +01:00
|
|
|
|
|
|
|
|
2024-02-22 14:16:41 +01:00
|
|
|
async def check_installed_extension_files(ext: InstallableExtension) -> bool:
|
2023-01-25 14:00:39 +01:00
|
|
|
if ext.has_installed_version:
|
|
|
|
return True
|
|
|
|
|
2023-09-25 12:44:29 +02:00
|
|
|
zip_files = glob.glob(os.path.join(settings.lnbits_data_folder, "zips", "*.zip"))
|
2023-01-18 15:25:44 +01:00
|
|
|
|
2024-04-17 07:36:22 +02:00
|
|
|
if f"./{ext.zip_path!s}" not in zip_files:
|
2024-02-22 14:16:41 +01:00
|
|
|
await ext.download_archive()
|
2023-01-20 14:55:58 +01:00
|
|
|
ext.extract_archive()
|
2023-01-20 14:59:44 +01:00
|
|
|
|
2023-01-18 16:16:15 +01:00
|
|
|
return False
|
2022-12-22 15:30:37 +01:00
|
|
|
|
|
|
|
|
2023-01-19 10:12:48 +01:00
|
|
|
async def restore_installed_extension(app: FastAPI, ext: InstallableExtension):
|
2023-05-26 11:48:40 +02:00
|
|
|
await add_installed_extension(ext)
|
|
|
|
await update_installed_extension_state(ext_id=ext.id, active=True)
|
|
|
|
|
2023-01-19 10:12:48 +01:00
|
|
|
extension = Extension.from_installable_ext(ext)
|
|
|
|
register_ext_routes(app, extension)
|
|
|
|
|
2024-02-09 13:10:51 +01:00
|
|
|
current_version = (await get_dbversions()).get(ext.id, 0)
|
2023-01-19 10:12:48 +01:00
|
|
|
await migrate_extension_database(extension, current_version)
|
|
|
|
|
|
|
|
# mount routes for the new version
|
|
|
|
core_app_extra.register_new_ext_routes(extension)
|
2024-05-21 12:17:02 +02:00
|
|
|
ext.notify_upgrade(extension.upgrade_hash)
|
2023-01-19 10:12:48 +01:00
|
|
|
|
|
|
|
|
2023-09-25 12:44:29 +02:00
|
|
|
def register_custom_extensions_path():
|
|
|
|
if settings.has_default_extension_path:
|
|
|
|
return
|
|
|
|
default_ext_path = os.path.join("lnbits", "extensions")
|
|
|
|
if os.path.isdir(default_ext_path) and len(os.listdir(default_ext_path)) != 0:
|
|
|
|
logger.warning(
|
|
|
|
"You are using a custom extensions path, "
|
|
|
|
+ "but the default extensions directory is not empty. "
|
|
|
|
+ f"Please clean-up the '{default_ext_path}' directory."
|
|
|
|
)
|
|
|
|
logger.warning(
|
|
|
|
f"You can move the existing '{default_ext_path}' directory to: "
|
|
|
|
+ f" '{settings.lnbits_extensions_path}/extensions'"
|
|
|
|
)
|
|
|
|
|
|
|
|
sys.path.append(str(Path(settings.lnbits_extensions_path, "extensions")))
|
|
|
|
sys.path.append(str(Path(settings.lnbits_extensions_path, "upgrades")))
|
|
|
|
|
|
|
|
|
2022-12-22 15:30:37 +01:00
|
|
|
def register_new_ext_routes(app: FastAPI) -> Callable:
|
2023-01-20 16:48:52 +01:00
|
|
|
# Returns a function that registers new routes for an extension.
|
2023-08-18 11:23:57 +02:00
|
|
|
# The returned function encapsulates (creates a closure around)
|
|
|
|
# the `app` object but does expose it.
|
2022-12-22 15:30:37 +01:00
|
|
|
def register_new_ext_routes_fn(ext: Extension):
|
|
|
|
register_ext_routes(app, ext)
|
|
|
|
|
|
|
|
return register_new_ext_routes_fn
|
|
|
|
|
|
|
|
|
2023-06-20 11:26:33 +02:00
|
|
|
def register_new_ratelimiter(app: FastAPI) -> Callable:
|
|
|
|
def register_new_ratelimiter_fn():
|
|
|
|
limiter = Limiter(
|
|
|
|
key_func=get_remote_address,
|
|
|
|
default_limits=[
|
|
|
|
f"{settings.lnbits_rate_limit_no}/{settings.lnbits_rate_limit_unit}"
|
|
|
|
],
|
|
|
|
)
|
|
|
|
app.state.limiter = limiter
|
|
|
|
|
|
|
|
return register_new_ratelimiter_fn
|
|
|
|
|
|
|
|
|
2022-12-22 15:30:37 +01:00
|
|
|
def register_ext_routes(app: FastAPI, ext: Extension) -> None:
|
|
|
|
"""Register FastAPI routes for extension."""
|
2022-11-29 17:14:53 +01:00
|
|
|
ext_module = importlib.import_module(ext.module_name)
|
2022-11-29 16:03:46 +01:00
|
|
|
|
2022-12-22 15:30:37 +01:00
|
|
|
ext_route = getattr(ext_module, f"{ext.code}_ext")
|
|
|
|
|
|
|
|
if hasattr(ext_module, f"{ext.code}_start"):
|
|
|
|
ext_start_func = getattr(ext_module, f"{ext.code}_start")
|
|
|
|
ext_start_func()
|
|
|
|
|
|
|
|
if hasattr(ext_module, f"{ext.code}_static_files"):
|
|
|
|
ext_statics = getattr(ext_module, f"{ext.code}_static_files")
|
|
|
|
for s in ext_statics:
|
2023-09-25 12:44:29 +02:00
|
|
|
static_dir = Path(
|
|
|
|
settings.lnbits_extensions_path, "extensions", *s["path"].split("/")
|
|
|
|
)
|
|
|
|
app.mount(s["path"], StaticFiles(directory=static_dir), s["name"])
|
2022-12-22 15:30:37 +01:00
|
|
|
|
2023-02-22 10:12:16 +01:00
|
|
|
if hasattr(ext_module, f"{ext.code}_redirect_paths"):
|
|
|
|
ext_redirects = getattr(ext_module, f"{ext.code}_redirect_paths")
|
|
|
|
settings.lnbits_extensions_redirects = [
|
|
|
|
r for r in settings.lnbits_extensions_redirects if r["ext_id"] != ext.code
|
|
|
|
]
|
|
|
|
for r in ext_redirects:
|
|
|
|
r["ext_id"] = ext.code
|
|
|
|
settings.lnbits_extensions_redirects.append(r)
|
|
|
|
|
2022-12-22 15:30:37 +01:00
|
|
|
logger.trace(f"adding route for extension {ext_module}")
|
2022-11-29 17:14:53 +01:00
|
|
|
|
2023-01-25 13:32:41 +01:00
|
|
|
prefix = f"/upgrades/{ext.upgrade_hash}" if ext.upgrade_hash != "" else ""
|
2022-11-29 17:14:53 +01:00
|
|
|
app.include_router(router=ext_route, prefix=prefix)
|
2022-12-22 15:30:37 +01:00
|
|
|
|
|
|
|
|
2024-07-23 14:01:34 +02:00
|
|
|
async def check_and_register_extensions(app: FastAPI):
|
|
|
|
await check_installed_extensions(app)
|
2024-03-26 11:44:33 +01:00
|
|
|
for ext in get_valid_extensions(False):
|
|
|
|
try:
|
|
|
|
register_ext_routes(app, ext)
|
2024-07-23 14:01:34 +02:00
|
|
|
except Exception as exc:
|
|
|
|
logger.error(f"Could not load extension `{ext.code}`: {exc!s}")
|
2024-03-26 11:44:33 +01:00
|
|
|
|
|
|
|
|
2024-07-23 14:01:34 +02:00
|
|
|
def register_async_tasks(app: FastAPI):
|
|
|
|
|
|
|
|
# check extensions after restart
|
|
|
|
if not settings.lnbits_extensions_deactivate_all:
|
|
|
|
create_task(check_and_register_extensions(app))
|
|
|
|
|
2024-04-05 07:05:26 +02:00
|
|
|
create_permanent_task(check_pending_payments)
|
|
|
|
create_permanent_task(invoice_listener)
|
|
|
|
create_permanent_task(internal_invoice_listener)
|
|
|
|
create_permanent_task(cache.invalidate_forever)
|
2024-03-22 12:29:05 +01:00
|
|
|
|
2024-04-05 07:05:26 +02:00
|
|
|
# core invoice listener
|
2024-07-23 14:01:34 +02:00
|
|
|
invoice_queue: asyncio.Queue = asyncio.Queue(5)
|
2024-04-05 07:05:26 +02:00
|
|
|
register_invoice_listener(invoice_queue, "core")
|
|
|
|
create_permanent_task(lambda: wait_for_paid_invoices(invoice_queue))
|
2024-03-22 12:29:05 +01:00
|
|
|
|
2024-04-05 07:05:26 +02:00
|
|
|
# TODO: implement watchdog properly
|
|
|
|
# create_permanent_task(watchdog_task)
|
|
|
|
create_permanent_task(killswitch_task)
|
2021-08-22 20:07:24 +02:00
|
|
|
|
2024-05-09 18:51:18 +02:00
|
|
|
# server logs for websocket
|
|
|
|
if settings.lnbits_admin_ui:
|
|
|
|
server_log_task = initialize_server_websocket_logger()
|
|
|
|
create_permanent_task(server_log_task)
|