From 761fc427defc590913124f1ad2f1b518633fbad4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Mon, 10 Oct 2022 23:27:46 +0200 Subject: [PATCH] add callback for saas app --- lnbits/app.py | 2 +- lnbits/extensions/admin/migrations.py | 3 +++ lnbits/extensions/admin/views_api.py | 5 ----- lnbits/settings.py | 32 +++++++++++++++++++++++---- 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/lnbits/app.py b/lnbits/app.py index 50f218b7..49ad8d77 100644 --- a/lnbits/app.py +++ b/lnbits/app.py @@ -64,7 +64,7 @@ def create_app() -> FastAPI: # TODO: why those 2? g().config = settings - # g().base_url = f"http://{settings.host}:{settings.port}" + g().base_url = f"http://{settings.host}:{settings.port}" app.add_middleware(GZipMiddleware, minimum_size=1000) diff --git a/lnbits/extensions/admin/migrations.py b/lnbits/extensions/admin/migrations.py index c4bc98d8..ea698c27 100644 --- a/lnbits/extensions/admin/migrations.py +++ b/lnbits/extensions/admin/migrations.py @@ -6,6 +6,9 @@ async def m001_create_admin_settings_table(db): debug TEXT, host TEXT, port INTEGER, + lnbits_saas_instance_id TEXT, + lnbits_saas_callback TEXT, + lnbits_saas_secret TEXT, lnbits_path TEXT, lnbits_commit TEXT, lnbits_admin_users TEXT, diff --git a/lnbits/extensions/admin/views_api.py b/lnbits/extensions/admin/views_api.py index 19b52e35..ae2959bc 100644 --- a/lnbits/extensions/admin/views_api.py +++ b/lnbits/extensions/admin/views_api.py @@ -53,8 +53,3 @@ async def api_delete_settings( ): await delete_settings() return {"status": "Success"} - - -@admin_ext.get("/api/v1/backup/", status_code=HTTPStatus.OK) -async def api_backup(user: User = Depends(check_admin)): - return {"status": "not implemented"} diff --git a/lnbits/settings.py b/lnbits/settings.py index ffcdcc0a..f183211c 100644 --- a/lnbits/settings.py +++ b/lnbits/settings.py @@ -1,6 +1,7 @@ import importlib import json import subprocess +import httpx from os import path from sqlite3 import Row from typing import List, Optional @@ -9,6 +10,7 @@ from loguru import logger from pydantic import BaseSettings, Field, validator + def list_parse_fallback(v): try: return json.loads(v) @@ -34,6 +36,11 @@ class Settings(BaseSettings): lnbits_path: str = Field(default=".") lnbits_commit: str = Field(default="unknown") + # saas + lnbits_saas_callback: Optional[str] = Field(default=None) + lnbits_saas_secret: Optional[str] = Field(default=None) + lnbits_saas_instance_id: Optional[str] = Field(default=None) + # users lnbits_admin_users: List[str] = Field(default=[]) lnbits_allowed_users: List[str] = Field(default=[]) @@ -230,11 +237,28 @@ async def check_admin_settings(): http = "https" if settings.lnbits_force_https else "http" user = settings.lnbits_admin_users[0] - logger.warning( - f" ✔️ Access admin user account at: {http}://{settings.host}:{settings.port}/wallet?usr={user}" - ) + + admin_url = f"{http}://{settings.host}:{settings.port}/wallet?usr={user}" + logger.warning(f"✔️ Access admin user account at: {admin_url}") + + if settings.lnbits_saas_callback and settings.lnbits_saas_secret and settings.lnbits_saas_instance_id: + with httpx.Client() as client: + headers = { + "Content-Type": "application/json; charset=utf-8", + "X-API-KEY": settings.lnbits_saas_secret + } + payload = { + "instance_id": settings.lnbits_saas_instance_id, + "adminuser": user + } + try: + r = client.post(settings.lnbits_saas_callback, headers=headers, json=payload) + logger.warning("sent admin user to saas application") + except: + logger.error(f"error sending admin user to saas: {settings.lnbits_saas_callback}") + except: - logger.warning("admin.settings tables does not exist.") + logger.error("admin.settings tables does not exist.") raise