add callback for saas app

This commit is contained in:
dni ⚡ 2022-10-10 23:27:46 +02:00
parent 1cc54ff4b7
commit 761fc427de
4 changed files with 32 additions and 10 deletions

View File

@ -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)

View File

@ -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,

View File

@ -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"}

View File

@ -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