From 691e1753707bc5f51c6b2eeaae019970525bb2e5 Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Wed, 30 Nov 2022 10:59:01 +0200 Subject: [PATCH] feat: add re-routing for upgraded extension APIs --- lnbits/helpers.py | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/lnbits/helpers.py b/lnbits/helpers.py index ecb895f4..766fbcd2 100644 --- a/lnbits/helpers.py +++ b/lnbits/helpers.py @@ -87,24 +87,34 @@ class InstalledExtensionMiddleware: async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: pathname = scope["path"].split("/")[1] 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( status_code=HTTPStatus.NOT_FOUND, - content={"detail": f"Extension '{pathname}' disabled"}, + content={"detail": f"Extension '{path_name}' disabled"}, ) await response(scope, receive, send) return - # re-route trafic if the extension has been upgraded - upgraded_extensions = list( - filter( - lambda ext: ext.endswith(f"/{pathname}"), - g().config.LNBITS_UPGRADED_EXTENSIONS, + # re-route API trafic if the extension has been upgraded + if path_type == "api": + upgraded_extensions = list( + filter( + lambda ext: ext.endswith(f"/{path_name}"), + g().config.LNBITS_UPGRADED_EXTENSIONS, + ) ) - ) - if len(upgraded_extensions) != 0: - upgrade_path = upgraded_extensions[0] - tail = "/".join(rest) - scope["path"] = f"/upgrades/{upgrade_path}/{tail}" + if len(upgraded_extensions) != 0: + upgrade_path = upgraded_extensions[0] + tail = "/".join(rest) + scope["path"] = f"/upgrades/{upgrade_path}/{path_type}/{tail}" await self.app(scope, receive, send)