This commit is contained in:
dni ⚡ 2022-09-22 10:46:11 +02:00
parent c32e0cbecb
commit e4c310d197
6 changed files with 43 additions and 21 deletions

View File

@ -115,24 +115,28 @@ def create_app(config_object="lnbits.settings") -> FastAPI:
return app return app
def check_settings(app: FastAPI): def check_settings(app: FastAPI):
@app.on_event("startup") @app.on_event("startup")
async def check_settings_admin(): async def check_settings_admin():
while True: while True:
admin_set = await get_admin_settings() admin_set = await get_admin_settings()
if admin_set : if admin_set:
break break
print("Waiting for admin settings... retrying in 5 seconds!") print("Waiting for admin settings... retrying in 5 seconds!")
await asyncio.sleep(5) await asyncio.sleep(5)
admin_set.admin_users = removeEmptyString(admin_set.admin_users.split(',')) admin_set.admin_users = removeEmptyString(admin_set.admin_users.split(","))
admin_set.allowed_users = removeEmptyString(admin_set.allowed_users.split(',')) admin_set.allowed_users = removeEmptyString(admin_set.allowed_users.split(","))
admin_set.admin_ext = removeEmptyString(admin_set.admin_ext.split(',')) admin_set.admin_ext = removeEmptyString(admin_set.admin_ext.split(","))
admin_set.disabled_ext = removeEmptyString(admin_set.disabled_ext.split(',')) admin_set.disabled_ext = removeEmptyString(admin_set.disabled_ext.split(","))
admin_set.theme = removeEmptyString(admin_set.theme.split(',')) admin_set.theme = removeEmptyString(admin_set.theme.split(","))
admin_set.ad_space = removeEmptyString(admin_set.ad_space.split(',')) admin_set.ad_space = removeEmptyString(admin_set.ad_space.split(","))
g().admin_conf = conf.copy(update=admin_set.dict()) g().admin_conf = conf.copy(update=admin_set.dict())
print(f" ✔️ Access admin user account at: http://{lnbits.settings.HOST}:{lnbits.settings.PORT}/wallet?usr={admin_set.user}") print(
f" ✔️ Access admin user account at: http://{lnbits.settings.HOST}:{lnbits.settings.PORT}/wallet?usr={admin_set.user}"
)
def check_funding_source(app: FastAPI) -> None: def check_funding_source(app: FastAPI) -> None:
@app.on_event("startup") @app.on_event("startup")

View File

@ -5,6 +5,7 @@ import re
import warnings import warnings
import click import click
from genericpath import exists
from loguru import logger from loguru import logger
from .core import db as core_db from .core import db as core_db
@ -52,6 +53,7 @@ def bundle_vendored():
with open(outputpath, "w") as f: with open(outputpath, "w") as f:
f.write(output) f.write(output)
async def get_admin_settings(): async def get_admin_settings():
from lnbits.extensions.admin.models import Admin from lnbits.extensions.admin.models import Admin
@ -61,6 +63,7 @@ async def get_admin_settings():
return False return False
async with ext_db.connect() as conn: async with ext_db.connect() as conn:
if conn.type == SQLITE: if conn.type == SQLITE:
exists = await conn.fetchone( exists = await conn.fetchone(
"SELECT * FROM sqlite_master WHERE type='table' AND name='admin'" "SELECT * FROM sqlite_master WHERE type='table' AND name='admin'"
@ -69,7 +72,7 @@ async def get_admin_settings():
exists = await conn.fetchone( exists = await conn.fetchone(
"SELECT * FROM information_schema.tables WHERE table_name = 'admin'" "SELECT * FROM information_schema.tables WHERE table_name = 'admin'"
) )
if not exists: if not exists:
return False return False
@ -77,6 +80,7 @@ async def get_admin_settings():
return Admin(**row) if row else None return Admin(**row) if row else None
async def migrate_databases(): async def migrate_databases():
"""Creates the necessary databases if they don't exist already; or migrates them.""" """Creates the necessary databases if they don't exist already; or migrates them."""

View File

@ -6,17 +6,19 @@ from typing import List, Optional
from pydantic import BaseSettings, Field from pydantic import BaseSettings, Field
wallets_module = importlib.import_module("lnbits.wallets") wallets_module = importlib.import_module("lnbits.wallets")
wallet_class = getattr( wallet_class = getattr(
wallets_module, getenv("LNBITS_BACKEND_WALLET_CLASS", "VoidWallet") wallets_module, getenv("LNBITS_BACKEND_WALLET_CLASS", "VoidWallet")
) )
WALLET = wallet_class() WALLET = wallet_class()
def list_parse_fallback(v): def list_parse_fallback(v):
try: try:
return json.loads(v) return json.loads(v)
except Exception as e: except Exception as e:
return v.replace(' ','').split(',') return v.replace(" ", "").split(",")
class Settings(BaseSettings): class Settings(BaseSettings):
admin_ui: bool = Field(default=True, env="LNBITS_ADMIN_UI") admin_ui: bool = Field(default=True, env="LNBITS_ADMIN_UI")
@ -24,7 +26,9 @@ class Settings(BaseSettings):
admin_users: List[str] = Field(default_factory=list, env="LNBITS_ADMIN_USERS") admin_users: List[str] = Field(default_factory=list, env="LNBITS_ADMIN_USERS")
allowed_users: List[str] = Field(default_factory=list, env="LNBITS_ALLOWED_USERS") allowed_users: List[str] = Field(default_factory=list, env="LNBITS_ALLOWED_USERS")
admin_ext: List[str] = Field(default_factory=list, env="LNBITS_ADMIN_EXTENSIONS") admin_ext: List[str] = Field(default_factory=list, env="LNBITS_ADMIN_EXTENSIONS")
disabled_ext: List[str] = Field(default_factory=list, env="LNBITS_DISABLED_EXTENSIONS") disabled_ext: List[str] = Field(
default_factory=list, env="LNBITS_DISABLED_EXTENSIONS"
)
funding_source: str = Field(default="VoidWallet", env="LNBITS_BACKEND_WALLET_CLASS") funding_source: str = Field(default="VoidWallet", env="LNBITS_BACKEND_WALLET_CLASS")
# ops # ops
data_folder: str = Field(default=None, env="LNBITS_DATA_FOLDER") data_folder: str = Field(default=None, env="LNBITS_DATA_FOLDER")
@ -37,10 +41,17 @@ class Settings(BaseSettings):
denomination: str = Field(default="sats", env="LNBITS_DENOMINATION") denomination: str = Field(default="sats", env="LNBITS_DENOMINATION")
# Change theme # Change theme
site_title: str = Field(default="LNbits", env="LNBITS_SITE_TITLE") site_title: str = Field(default="LNbits", env="LNBITS_SITE_TITLE")
site_tagline: str = Field(default="free and open-source lightning wallet", env="LNBITS_SITE_TAGLINE") site_tagline: str = Field(
default="free and open-source lightning wallet", env="LNBITS_SITE_TAGLINE"
)
site_description: str = Field(default=None, env="LNBITS_SITE_DESCRIPTION") site_description: str = Field(default=None, env="LNBITS_SITE_DESCRIPTION")
default_wallet_name: str = Field(default="LNbits wallet", env="LNBITS_DEFAULT_WALLET_NAME") default_wallet_name: str = Field(
theme: List[str] = Field(default=["classic, flamingo, mint, salvador, monochrome, autumn"], env="LNBITS_THEME_OPTIONS") default="LNbits wallet", env="LNBITS_DEFAULT_WALLET_NAME"
)
theme: List[str] = Field(
default=["classic, flamingo, mint, salvador, monochrome, autumn"],
env="LNBITS_THEME_OPTIONS",
)
custom_logo: str = Field(default=None, env="LNBITS_CUSTOM_LOGO") custom_logo: str = Field(default=None, env="LNBITS_CUSTOM_LOGO")
ad_space: List[str] = Field(default_factory=list, env="LNBITS_AD_SPACE") ad_space: List[str] = Field(default_factory=list, env="LNBITS_AD_SPACE")
# .env # .env
@ -48,7 +59,7 @@ class Settings(BaseSettings):
debug: Optional[str] debug: Optional[str]
host: Optional[str] host: Optional[str]
port: Optional[str] port: Optional[str]
lnbits_path: Optional[str] = path.dirname(path.realpath(__file__)) lnbits_path: Optional[str] = path.dirname(path.realpath(__file__))
# @validator('admin_users', 'allowed_users', 'admin_ext', 'disabled_ext', pre=True) # @validator('admin_users', 'allowed_users', 'admin_ext', 'disabled_ext', pre=True)
# def validate(cls, val): # def validate(cls, val):

View File

@ -39,6 +39,7 @@ from ..services import pay_invoice, redeem_lnurl_withdraw
core_html_routes: APIRouter = APIRouter(tags=["Core NON-API Website Routes"]) core_html_routes: APIRouter = APIRouter(tags=["Core NON-API Website Routes"])
@core_html_routes.get("/favicon.ico", response_class=FileResponse) @core_html_routes.get("/favicon.ico", response_class=FileResponse)
async def favicon(): async def favicon():
return FileResponse("lnbits/core/static/favicon.ico") return FileResponse("lnbits/core/static/favicon.ico")

View File

@ -235,7 +235,7 @@ async def check_user_exists(usr: UUID4) -> User:
raise HTTPException( raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="User does not exist." status_code=HTTPStatus.NOT_FOUND, detail="User does not exist."
) )
if LNBITS_ADMIN_UI: if LNBITS_ADMIN_UI:
LNBITS_ADMIN_USERS = g().admin_conf.admin_users LNBITS_ADMIN_USERS = g().admin_conf.admin_users
LNBITS_ALLOWED_USERS = g().admin_conf.allowed_users LNBITS_ALLOWED_USERS = g().admin_conf.allowed_users

View File

@ -157,11 +157,13 @@ def url_for(endpoint: str, external: Optional[bool] = False, **params: Any) -> s
url = f"{base}{endpoint}{url_params}" url = f"{base}{endpoint}{url_params}"
return url return url
def removeEmptyString(arr): def removeEmptyString(arr):
return list(filter(None, arr)) return list(filter(None, arr))
def template_renderer(additional_folders: List = []) -> Jinja2Templates: def template_renderer(additional_folders: List = []) -> Jinja2Templates:
if(settings.LNBITS_ADMIN_UI): if settings.LNBITS_ADMIN_UI:
_ = g().admin_conf _ = g().admin_conf
settings.LNBITS_AD_SPACE = _.ad_space settings.LNBITS_AD_SPACE = _.ad_space
settings.LNBITS_HIDE_API = _.hide_api settings.LNBITS_HIDE_API = _.hide_api
@ -170,8 +172,8 @@ def template_renderer(additional_folders: List = []) -> Jinja2Templates:
settings.LNBITS_SITE_TAGLINE = _.site_tagline settings.LNBITS_SITE_TAGLINE = _.site_tagline
settings.LNBITS_SITE_DESCRIPTION = _.site_description settings.LNBITS_SITE_DESCRIPTION = _.site_description
settings.LNBITS_THEME_OPTIONS = _.theme settings.LNBITS_THEME_OPTIONS = _.theme
settings.LNBITS_CUSTOM_LOGO = _.custom_logo settings.LNBITS_CUSTOM_LOGO = _.custom_logo
t = Jinja2Templates( t = Jinja2Templates(
loader=jinja2.FileSystemLoader( loader=jinja2.FileSystemLoader(
["lnbits/templates", "lnbits/core/templates", *additional_folders] ["lnbits/templates", "lnbits/core/templates", *additional_folders]