From 5865a3f96a1ec591e05ecc75f6923e1c0e68b1c4 Mon Sep 17 00:00:00 2001 From: dni Date: Tue, 23 Jun 2026 15:40:06 +0200 Subject: [PATCH] refactor: get rid of url rewrites for extensions `/upgrades/` should open the way for custom root-path --- lnbits/app.py | 34 ++++++++++++++++++++++++---------- lnbits/helpers.py | 7 +------ lnbits/middleware.py | 8 -------- lnbits/settings.py | 6 ++---- tests/unit/test_helpers.py | 2 -- 5 files changed, 27 insertions(+), 30 deletions(-) diff --git a/lnbits/app.py b/lnbits/app.py index c4c2d5651..279c813e1 100644 --- a/lnbits/app.py +++ b/lnbits/app.py @@ -445,14 +445,6 @@ def register_ext_routes(app: FastAPI, ext: Extension) -> None: ext_route = getattr(ext_module, f"{ext.code}_ext") - if hasattr(ext_module, f"{ext.code}_static_files"): - ext_statics = getattr(ext_module, f"{ext.code}_static_files") - for s in ext_statics: - static_dir = Path( - settings.lnbits_extensions_path, "extensions", *s["path"].split("/") - ) - app.mount(s["path"], StaticFiles(directory=static_dir), s["name"]) - ext_redirects = ( getattr(ext_module, f"{ext.code}_redirect_paths") if hasattr(ext_module, f"{ext.code}_redirect_paths") @@ -461,9 +453,31 @@ def register_ext_routes(app: FastAPI, ext: Extension) -> None: settings.activate_extension_paths(ext.code, ext.upgrade_hash, ext_redirects) + # Remove existing routes for this extension before re-registering so that + # an upgraded extension replaces the old one at the same paths (no prefix). + ext_prefix = f"/{ext.code}" + app.router.routes = [ + r + for r in app.router.routes + if not ( + getattr(r, "path", "") == ext_prefix + or getattr(r, "path", "").startswith(f"{ext_prefix}/") + ) + ] + # Invalidate FastAPI's cached OpenAPI schema so the next /openapi.json + # request reflects the updated routes. + app.openapi_schema = None + + if hasattr(ext_module, f"{ext.code}_static_files"): + ext_statics = getattr(ext_module, f"{ext.code}_static_files") + for s in ext_statics: + static_dir = Path( + settings.lnbits_extensions_path, "extensions", *s["path"].split("/") + ) + app.mount(s["path"], StaticFiles(directory=static_dir), s["name"]) + logger.trace(f"Adding route for extension {ext_module}.") - prefix = f"/upgrades/{ext.upgrade_hash}" if ext.upgrade_hash != "" else "" - app.include_router(router=ext_route, prefix=prefix) + app.include_router(router=ext_route) async def check_and_register_extensions(app: FastAPI) -> None: diff --git a/lnbits/helpers.py b/lnbits/helpers.py index 88a001b34..b732f038b 100644 --- a/lnbits/helpers.py +++ b/lnbits/helpers.py @@ -310,12 +310,7 @@ def get_api_routes(routes: list) -> dict[str, str]: def path_segments(path: str) -> list[str]: path = path.strip("/") - segments = path.split("/") - if len(segments) < 2: - return segments - if segments[0] == "upgrades": - return segments[2:] - return segments[0:] + return path.split("/") def normalize_path(path: str | None) -> str: diff --git a/lnbits/middleware.py b/lnbits/middleware.py index dfa077885..b7b3c7d17 100644 --- a/lnbits/middleware.py +++ b/lnbits/middleware.py @@ -51,14 +51,6 @@ class InstalledExtensionMiddleware: await self.app(scope, receive, send) return - # re-route all trafic if the extension has been upgraded - if top_path in settings.lnbits_upgraded_extensions: - upgrade_path = ( - f"""{settings.lnbits_upgraded_extensions[top_path]}/{top_path}""" - ) - tail = "/".join(rest) - scope["path"] = f"/upgrades/{upgrade_path}/{tail}" - await self.app(scope, receive, send) def _response_by_accepted_type( diff --git a/lnbits/settings.py b/lnbits/settings.py index 8d9bf2404..911cc41e5 100644 --- a/lnbits/settings.py +++ b/lnbits/settings.py @@ -195,10 +195,8 @@ class InstalledExtensionsSettings(LNbitsSettings): ): self.lnbits_deactivated_extensions.discard(ext_id) - """ - Update the list of upgraded extensions. The middleware will perform - redirects based on this - """ + # Track upgrade hashes so that module names can be resolved for + # background-task start/stop (the module lives in the upgrades dir). if upgrade_hash: self.lnbits_upgraded_extensions[ext_id] = upgrade_hash diff --git a/tests/unit/test_helpers.py b/tests/unit/test_helpers.py index 499a58d6f..1d21598d8 100644 --- a/tests/unit/test_helpers.py +++ b/tests/unit/test_helpers.py @@ -259,9 +259,7 @@ def test_get_api_routes_extracts_v1_paths(): def test_path_and_case_helpers(): assert path_segments("/wallet/path") == ["wallet", "path"] - assert path_segments("/upgrades/ext/assets/app.js") == ["assets", "app.js"] assert normalize_path(None) == "/" - assert normalize_path("/upgrades/ext/assets/app.js") == "/assets/app.js" assert normalize_endpoint("example.com/") == "https://example.com" assert normalize_endpoint("ws://socket.example.com") == "ws://socket.example.com" assert (