feat: add re-routing for upgraded extension APIs

This commit is contained in:
Vlad Stan 2022-11-30 10:59:01 +02:00
parent 0d61db13ec
commit 691e175370

View File

@ -87,24 +87,34 @@ class InstalledExtensionMiddleware:
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
pathname = scope["path"].split("/")[1] pathname = scope["path"].split("/")[1]
if pathname in settings.lnbits_disabled_extensions: if pathname in settings.lnbits_disabled_extensions:
path_elements = scope["path"].split("/")
if len(path_elements) > 2:
_, path_name, path_type, *rest = path_elements
else:
_, path_name = path_elements
path_type = None
# block path for all users if the extension is disabled
if path_name in settings.LNBITS_DISABLED_EXTENSIONS:
response = JSONResponse( response = JSONResponse(
status_code=HTTPStatus.NOT_FOUND, status_code=HTTPStatus.NOT_FOUND,
content={"detail": f"Extension '{pathname}' disabled"}, content={"detail": f"Extension '{path_name}' disabled"},
) )
await response(scope, receive, send) await response(scope, receive, send)
return return
# re-route trafic if the extension has been upgraded # re-route API trafic if the extension has been upgraded
upgraded_extensions = list( if path_type == "api":
filter( upgraded_extensions = list(
lambda ext: ext.endswith(f"/{pathname}"), filter(
g().config.LNBITS_UPGRADED_EXTENSIONS, lambda ext: ext.endswith(f"/{path_name}"),
g().config.LNBITS_UPGRADED_EXTENSIONS,
)
) )
) if len(upgraded_extensions) != 0:
if len(upgraded_extensions) != 0: upgrade_path = upgraded_extensions[0]
upgrade_path = upgraded_extensions[0] tail = "/".join(rest)
tail = "/".join(rest) scope["path"] = f"/upgrades/{upgrade_path}/{path_type}/{tail}"
scope["path"] = f"/upgrades/{upgrade_path}/{tail}"
await self.app(scope, receive, send) await self.app(scope, receive, send)