feat: re-route trafic when an extension is upgraded

This commit is contained in:
Vlad Stan 2022-11-29 18:16:28 +02:00
parent 3fbac927e9
commit 598d2a6939
3 changed files with 21 additions and 3 deletions

View File

@ -34,7 +34,7 @@ from genericpath import isfile
from loguru import logger
from pydantic import BaseModel
from pydantic.fields import Field
from sse_starlette.sse import EventSourceResponse
from sse_starlette.sse import EventSourceResponse, ServerSentEvent
from starlette.responses import StreamingResponse
from lnbits import bolt11, lnurl
@ -800,7 +800,13 @@ async def api_install_extension(
# todo: is admin only
# lnbits/extensions/satspay/upgrade/111/satspay/__init__.py
ext = Extension(code=extension.id, is_valid=True, is_admin_only=False, name=extension.name, version="111")
ext = Extension(
code=extension.id,
is_valid=True,
is_admin_only=False,
name=extension.name,
version="111",
)
# current_versions = await get_dbversions()
# current_version = current_versions.get(ext.code, 0)
@ -809,6 +815,7 @@ async def api_install_extension(
# disable by default
await update_user_extension(user_id=USER_ID_ALL, extension=ext_id, active=False)
settings.lnbits_disabled_extensions += [ext_id]
settings.LNBITS_UPGRADED_EXTENSIONS += [f"{ext.version}/{ext.code}"] #todo: re-visit
# mount routes at the very end
core_app_extra.register_new_ext_routes(ext)

View File

@ -94,6 +94,17 @@ class EnabledExtensionMiddleware:
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)
)
if len(upgraded_extensions) != 0:
upgrade_path = upgraded_extensions[0]
tail = "/".join(rest)
scope["path"] = f"/upgrades/{upgrade_path}/{tail}"
await self.app(scope, receive, send)

View File

@ -41,7 +41,7 @@ class UsersSettings(LNbitsSettings):
lnbits_admin_extensions: List[str] = Field(default=[])
lnbits_disabled_extensions: List[str] = Field(default=[])
lnbits_extensions_manifests: List[str] = Field(default=[])
# LNBITS_UPGRADED_EXTENSIONS: List[str] = [] # todo:
class ThemesSettings(LNbitsSettings):
lnbits_site_title: str = Field(default="LNbits")