mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2025-12-15 14:22:55 +08:00
✨ 添加基础配置api
This commit is contained in:
parent
94ed4cd067
commit
da1ccffe45
@ -12,6 +12,7 @@ from zhenxun.services.log import logger, logger_
|
||||
from zhenxun.utils.enum import PluginType
|
||||
from zhenxun.utils.manager.priority_manager import HookPriorityManager
|
||||
|
||||
from .api.configure import router as configure_router
|
||||
from .api.logs import router as ws_log_routes
|
||||
from .api.logs.log_manager import LOG_STORAGE
|
||||
from .api.menu import router as menu_router
|
||||
@ -83,6 +84,7 @@ BaseApiRouter.include_router(database_router)
|
||||
BaseApiRouter.include_router(plugin_router)
|
||||
BaseApiRouter.include_router(system_router)
|
||||
BaseApiRouter.include_router(menu_router)
|
||||
BaseApiRouter.include_router(configure_router)
|
||||
|
||||
|
||||
WsApiRouter = APIRouter(prefix="/zhenxun/socket")
|
||||
@ -92,7 +94,7 @@ WsApiRouter.include_router(status_routes)
|
||||
WsApiRouter.include_router(chat_routes)
|
||||
|
||||
|
||||
@HookPriorityManager.on_startup()
|
||||
@HookPriorityManager.on_startup(0)
|
||||
async def _():
|
||||
try:
|
||||
|
||||
|
||||
@ -1,2 +1,3 @@
|
||||
from .configure import * # noqa: F403
|
||||
from .menu import * # noqa: F403f
|
||||
from .tabs import * # noqa: F403f
|
||||
|
||||
50
zhenxun/builtin_plugins/web_ui/api/configure/__init__.py
Normal file
50
zhenxun/builtin_plugins/web_ui/api/configure/__init__.py
Normal file
@ -0,0 +1,50 @@
|
||||
from pathlib import Path
|
||||
import re
|
||||
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from fastapi.responses import JSONResponse
|
||||
import nonebot
|
||||
|
||||
from zhenxun.configs.config import BotConfig, Config
|
||||
|
||||
from ...base_model import Result
|
||||
from .model import Setting
|
||||
|
||||
router = APIRouter(prefix="/configure")
|
||||
|
||||
driver = nonebot.get_driver()
|
||||
|
||||
|
||||
@router.post(
|
||||
"/set_configure",
|
||||
response_model=Result,
|
||||
response_class=JSONResponse,
|
||||
description="设置基础配置",
|
||||
)
|
||||
async def _(setting: Setting) -> Result:
|
||||
password = Config.get_config("web-ui", "password")
|
||||
if password or BotConfig.db_url:
|
||||
raise HTTPException(
|
||||
status_code=400, detail="Configuration can only be set once"
|
||||
)
|
||||
env_file = Path() / ".env.dev"
|
||||
if not env_file.exists():
|
||||
raise HTTPException(status_code=400, detail="Configuration file not found")
|
||||
env_text = env_file.read_text(encoding="utf-8")
|
||||
if setting.superusers:
|
||||
superusers = ", ".join([f'"{s}"' for s in setting.superusers])
|
||||
env_text = re.sub(r"SUPERUSERS=\[.*?\]", f"SUPERUSERS=[{superusers}]", env_text)
|
||||
if setting.host:
|
||||
env_text = env_text.replace("HOST = 127.0.0.1", f"HOST = {setting.host}")
|
||||
if setting.port:
|
||||
env_text = env_text.replace("PORT = 8080", f"PORT = {setting.port}")
|
||||
if setting.db_url:
|
||||
if setting.db_url.startswith("sqlite"):
|
||||
db_path = Path(setting.db_url.split(":")[-1])
|
||||
db_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
env_text.replace('DB_URL = ""', f"DB_URL = {setting.db_url}")
|
||||
if setting.username:
|
||||
Config.set_config("web-ui", "username", setting.username)
|
||||
Config.set_config("web-ui", "password", setting.password)
|
||||
env_file.write_text(env_text, encoding="utf-8")
|
||||
return Result.ok(info="基础配置设置完成!")
|
||||
16
zhenxun/builtin_plugins/web_ui/api/configure/model.py
Normal file
16
zhenxun/builtin_plugins/web_ui/api/configure/model.py
Normal file
@ -0,0 +1,16 @@
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class Setting(BaseModel):
|
||||
superusers: list[str]
|
||||
"""超级用户列表"""
|
||||
db_url: str
|
||||
"""数据库地址"""
|
||||
host: str
|
||||
"""主机地址"""
|
||||
port: int
|
||||
"""端口"""
|
||||
username: str
|
||||
"""前端用户名"""
|
||||
password: str
|
||||
"""前端密码"""
|
||||
@ -53,9 +53,10 @@ async def init():
|
||||
if not BotConfig.db_url:
|
||||
# raise DbUrlIsNode("数据库配置为空,请在.env.dev中配置DB_URL...")
|
||||
error = f"""
|
||||
***********************************************************************
|
||||
**********************************配置为空******************************
|
||||
请打开WebUi进行基础配置,配置地址:http://{driver.config.host}:{driver.config.port}
|
||||
**********************************************************************
|
||||
🌟 ****************************** 配置为空 ************************** 🌟
|
||||
🚀 请打开 WebUi 进行基础配置 🚀
|
||||
🌐 配置地址:http://{driver.config.host}:{driver.config.port}/configure 🌐
|
||||
***********************************************************************
|
||||
***********************************************************************
|
||||
"""
|
||||
|
||||
Loading…
Reference in New Issue
Block a user