refactor: get rid of url rewrites for extensions /upgrades/
should open the way for custom root-path
This commit is contained in:
+24
-10
@@ -445,14 +445,6 @@ def register_ext_routes(app: FastAPI, ext: Extension) -> None:
|
|||||||
|
|
||||||
ext_route = getattr(ext_module, f"{ext.code}_ext")
|
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 = (
|
ext_redirects = (
|
||||||
getattr(ext_module, f"{ext.code}_redirect_paths")
|
getattr(ext_module, f"{ext.code}_redirect_paths")
|
||||||
if hasattr(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)
|
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}.")
|
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)
|
||||||
app.include_router(router=ext_route, prefix=prefix)
|
|
||||||
|
|
||||||
|
|
||||||
async def check_and_register_extensions(app: FastAPI) -> None:
|
async def check_and_register_extensions(app: FastAPI) -> None:
|
||||||
|
|||||||
+1
-6
@@ -310,12 +310,7 @@ def get_api_routes(routes: list) -> dict[str, str]:
|
|||||||
|
|
||||||
def path_segments(path: str) -> list[str]:
|
def path_segments(path: str) -> list[str]:
|
||||||
path = path.strip("/")
|
path = path.strip("/")
|
||||||
segments = path.split("/")
|
return path.split("/")
|
||||||
if len(segments) < 2:
|
|
||||||
return segments
|
|
||||||
if segments[0] == "upgrades":
|
|
||||||
return segments[2:]
|
|
||||||
return segments[0:]
|
|
||||||
|
|
||||||
|
|
||||||
def normalize_path(path: str | None) -> str:
|
def normalize_path(path: str | None) -> str:
|
||||||
|
|||||||
@@ -51,14 +51,6 @@ class InstalledExtensionMiddleware:
|
|||||||
await self.app(scope, receive, send)
|
await self.app(scope, receive, send)
|
||||||
return
|
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)
|
await self.app(scope, receive, send)
|
||||||
|
|
||||||
def _response_by_accepted_type(
|
def _response_by_accepted_type(
|
||||||
|
|||||||
+2
-4
@@ -195,10 +195,8 @@ class InstalledExtensionsSettings(LNbitsSettings):
|
|||||||
):
|
):
|
||||||
self.lnbits_deactivated_extensions.discard(ext_id)
|
self.lnbits_deactivated_extensions.discard(ext_id)
|
||||||
|
|
||||||
"""
|
# Track upgrade hashes so that module names can be resolved for
|
||||||
Update the list of upgraded extensions. The middleware will perform
|
# background-task start/stop (the module lives in the upgrades dir).
|
||||||
redirects based on this
|
|
||||||
"""
|
|
||||||
if upgrade_hash:
|
if upgrade_hash:
|
||||||
self.lnbits_upgraded_extensions[ext_id] = upgrade_hash
|
self.lnbits_upgraded_extensions[ext_id] = upgrade_hash
|
||||||
|
|
||||||
|
|||||||
@@ -259,9 +259,7 @@ def test_get_api_routes_extracts_v1_paths():
|
|||||||
|
|
||||||
def test_path_and_case_helpers():
|
def test_path_and_case_helpers():
|
||||||
assert path_segments("/wallet/path") == ["wallet", "path"]
|
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(None) == "/"
|
||||||
assert normalize_path("/upgrades/ext/assets/app.js") == "/assets/app.js"
|
|
||||||
assert normalize_endpoint("example.com/") == "https://example.com"
|
assert normalize_endpoint("example.com/") == "https://example.com"
|
||||||
assert normalize_endpoint("ws://socket.example.com") == "ws://socket.example.com"
|
assert normalize_endpoint("ws://socket.example.com") == "ws://socket.example.com"
|
||||||
assert (
|
assert (
|
||||||
|
|||||||
Reference in New Issue
Block a user