完善 /api/site/config 路由,修复数据库无法二次启动
This commit is contained in:
5
main.py
5
main.py
@@ -2,9 +2,11 @@ from fastapi import FastAPI
|
||||
from routers import routers
|
||||
from pkg.conf import appmeta
|
||||
from models.database import init_db
|
||||
from models.migration import init_default_settings
|
||||
from pkg.lifespan import lifespan
|
||||
|
||||
lifespan.add_startup(init_db)
|
||||
lifespan.add_startup(init_default_settings)
|
||||
|
||||
app = FastAPI(
|
||||
title=appmeta.APP_NAME,
|
||||
@@ -30,4 +32,5 @@ for router in routers.Router:
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
uvicorn.run(app='main:app', host="0.0.0.0", port=5213, reload=True)
|
||||
uvicorn.run(app=app, host="0.0.0.0", port=5213)
|
||||
# uvicorn.run(app='main:app', host="0.0.0.0", port=5213, reload=True)
|
||||
@@ -101,6 +101,13 @@ Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; verti
|
||||
async def init_default_settings() -> None:
|
||||
from .setting import Setting
|
||||
|
||||
try:
|
||||
# 检查是否已经存在版本设置
|
||||
ver = await Setting.get(type="version", name=f"db_version_{BackendVersion}")
|
||||
if ver == "installed":
|
||||
return
|
||||
else: raise ValueError("Database version mismatch or not installed.")
|
||||
except:
|
||||
for setting in default_settings:
|
||||
await Setting.add(
|
||||
type=setting.type,
|
||||
|
||||
@@ -99,7 +99,8 @@ class Setting(BaseModel, table=True):
|
||||
|
||||
async def get(
|
||||
type: SETTINGS_TYPE,
|
||||
name: str
|
||||
name: str,
|
||||
format: Literal['int', 'float', 'bool', 'str'] = 'str'
|
||||
) -> Optional['Setting']:
|
||||
"""
|
||||
从数据库中获取指定类型和名称的设置项。
|
||||
@@ -121,8 +122,21 @@ class Setting(BaseModel, table=True):
|
||||
Setting.name == name
|
||||
)
|
||||
|
||||
result = await session.exec(statment)
|
||||
return result.one_or_none()
|
||||
statment = await session.exec(statment)
|
||||
result = statment.one_or_none()
|
||||
result = result.value if result else None
|
||||
|
||||
# 根据 format 参数转换结果类型
|
||||
if format == 'int':
|
||||
return int(result) if result is not None else None
|
||||
elif format == 'float':
|
||||
return float(result) if result is not None else None
|
||||
elif format == 'bool':
|
||||
return result.lower() in ['true', '1'] if isinstance(result, str) else bool(result)
|
||||
elif format == 'str':
|
||||
return str(result) if result is not None else None
|
||||
else:
|
||||
raise ValueError(f"Unsupported format: {format}")
|
||||
|
||||
async def set(
|
||||
type: SETTINGS_TYPE,
|
||||
|
||||
@@ -42,11 +42,40 @@ def router_site_captcha():
|
||||
description='Get the configuration file.',
|
||||
response_model=ResponseModel,
|
||||
)
|
||||
def router_site_config():
|
||||
async def router_site_config():
|
||||
"""
|
||||
Get the configuration file.
|
||||
|
||||
Returns:
|
||||
dict: The site configuration.
|
||||
"""
|
||||
...
|
||||
from models.setting import Setting
|
||||
|
||||
return ResponseModel(
|
||||
data={
|
||||
"title": await Setting.get(type='basic', name='siteName'),
|
||||
"loginCaptcha": await Setting.get(type='login', name='login_captcha', format='bool'),
|
||||
"regCaptcha": await Setting.get(type='login', name='reg_captcha', format='bool'),
|
||||
"forgetCaptcha": await Setting.get(type='login', name='forget_captcha', format='bool'),
|
||||
"emailActive": await Setting.get(type='login', name='email_active', format='bool'),
|
||||
"QQLogin": None,
|
||||
"themes": await Setting.get(type='basic', name='themes'),
|
||||
"defaultTheme": await Setting.get(type='basic', name='defaultTheme'),
|
||||
"score_enabled": None,
|
||||
"share_score_rate": None,
|
||||
"home_view_method": await Setting.get(type='view', name='home_view_method'),
|
||||
"share_view_method": await Setting.get(type='view', name='share_view_method'),
|
||||
"authn": await Setting.get(type='authn', name='authn_enabled', format='bool'),
|
||||
"user": {},
|
||||
"captcha_type": None,
|
||||
"captcha_ReCaptchaKey": await Setting.get(type='captcha', name='captcha_ReCaptchaKey'),
|
||||
"captcha_CloudflareKey": await Setting.get(type='captcha', name='captcha_CloudflareKey'),
|
||||
"captcha_tcaptcha_appid": None,
|
||||
"site_notice": None,
|
||||
"registerEnabled": await Setting.get(type='register', name='register_enabled', format='bool'),
|
||||
"app_promotion": None,
|
||||
"wopi_exts": None,
|
||||
"app_feedback": None,
|
||||
"app_forum": None,
|
||||
}
|
||||
)
|
||||
Reference in New Issue
Block a user