zhenxun_bot/zhenxun/builtin_plugins/web_ui/__init__.py

124 lines
3.9 KiB
Python
Raw Normal View History

import asyncio
2024-08-26 19:20:22 +08:00
import secrets
from fastapi import APIRouter, FastAPI
import nonebot
2024-08-27 09:24:32 +08:00
from nonebot.log import default_filter, default_format
from nonebot.plugin import PluginMetadata
2024-08-27 09:24:32 +08:00
from zhenxun.configs.config import Config as gConfig
from zhenxun.configs.utils import PluginExtraData, RegisterConfig
from zhenxun.services.log import logger, logger_
from zhenxun.utils.enum import PluginType
:sparkles: 首次启动时提供使用web ui方式完全配置 (#1870) * :sparkles: 添加全局优先级hook * :sparkles: 添加基础配置api * :sparkles: 添加数据库连接测试 * :speech_balloon: 提示重启 * :adhesive_bandage: 填充过配置时友好提示 * :bug: 首次生成简易配置后自动加载 * :sparkles: 添加配置后重启接口 * :sparkles: 添加重启标志文件 * :sparkles: 添加重启脚本命令 * :sparkles: 添加重启系统限制 * :sparkles: 首次配置判断是否为win系统 * :fire: 移除bat * :sparkles: 添加关于菜单 * :sparkles: 支持整合包插件安装和添加整合包文档 * :adhesive_bandage: 检测数据库路径 * :adhesive_bandage: 修改数据库路径检测 * :adhesive_bandage: 修改数据库路径检测 * :adhesive_bandage: 修复路径注入 * :art: 显示添加优先级 * :bug: 修改PriorityLifecycle字典类名称 * :zap: 修复路径问题 * :zap: 修复路径检测 * ✨ 新增路径验证功能,确保用户输入的路径安全并在项目根目录内 * ✨ 优化路径验证功能,增加对非法字符和路径长度的检查,确保用户输入的路径更加安全 * :rotating_light: auto fix by pre-commit hooks * ✨ 优化获取文件列表的代码格式 * :memo: 修改README中webui示例图 * ✨ 更新PriorityLifecycle.on_startup装饰器 * ✨ 简化安装依赖的命令构建逻辑 * :rotating_light: auto fix by pre-commit hooks --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2025-06-16 09:11:41 +08:00
from zhenxun.utils.manager.priority_manager import PriorityLifecycle
:sparkles: 首次启动时提供使用web ui方式完全配置 (#1870) * :sparkles: 添加全局优先级hook * :sparkles: 添加基础配置api * :sparkles: 添加数据库连接测试 * :speech_balloon: 提示重启 * :adhesive_bandage: 填充过配置时友好提示 * :bug: 首次生成简易配置后自动加载 * :sparkles: 添加配置后重启接口 * :sparkles: 添加重启标志文件 * :sparkles: 添加重启脚本命令 * :sparkles: 添加重启系统限制 * :sparkles: 首次配置判断是否为win系统 * :fire: 移除bat * :sparkles: 添加关于菜单 * :sparkles: 支持整合包插件安装和添加整合包文档 * :adhesive_bandage: 检测数据库路径 * :adhesive_bandage: 修改数据库路径检测 * :adhesive_bandage: 修改数据库路径检测 * :adhesive_bandage: 修复路径注入 * :art: 显示添加优先级 * :bug: 修改PriorityLifecycle字典类名称 * :zap: 修复路径问题 * :zap: 修复路径检测 * ✨ 新增路径验证功能,确保用户输入的路径安全并在项目根目录内 * ✨ 优化路径验证功能,增加对非法字符和路径长度的检查,确保用户输入的路径更加安全 * :rotating_light: auto fix by pre-commit hooks * ✨ 优化获取文件列表的代码格式 * :memo: 修改README中webui示例图 * ✨ 更新PriorityLifecycle.on_startup装饰器 * ✨ 简化安装依赖的命令构建逻辑 * :rotating_light: auto fix by pre-commit hooks --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2025-06-16 09:11:41 +08:00
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
from .api.tabs.dashboard import router as dashboard_router
from .api.tabs.database import router as database_router
from .api.tabs.main import router as main_router
2024-08-27 09:24:32 +08:00
from .api.tabs.main import ws_router as status_routes
from .api.tabs.manage import router as manage_router
2024-08-13 22:37:44 +08:00
from .api.tabs.manage.chat import ws_router as chat_routes
from .api.tabs.plugin_manage import router as plugin_router
from .api.tabs.plugin_manage.store import router as store_router
from .api.tabs.system import router as system_router
from .auth import router as auth_router
from .public import init_public
2024-08-15 00:14:09 +08:00
__plugin_meta__ = PluginMetadata(
name="WebUi",
description="WebUi API",
2025-04-26 20:15:44 +08:00
usage='"""\n """.strip(),',
2024-08-15 00:14:09 +08:00
extra=PluginExtraData(
2024-08-26 19:20:22 +08:00
author="HibiKier",
version="0.1",
plugin_type=PluginType.HIDDEN,
2024-08-27 09:24:32 +08:00
configs=[
2024-08-26 19:20:22 +08:00
RegisterConfig(
module="web-ui",
key="username",
value="admin",
help="前端管理用户名",
type=str,
default_value="admin",
),
RegisterConfig(
module="web-ui",
key="password",
value=None,
help="前端管理密码",
type=str,
default_value=None,
),
RegisterConfig(
module="web-ui",
key="secret",
value=secrets.token_urlsafe(32),
help="JWT密钥",
type=str,
default_value=None,
),
2024-08-27 09:24:32 +08:00
],
).to_dict(),
2024-08-15 00:14:09 +08:00
)
driver = nonebot.get_driver()
gConfig.set_name("web-ui", "web-ui")
BaseApiRouter = APIRouter(prefix="/zhenxun/api")
BaseApiRouter.include_router(auth_router)
BaseApiRouter.include_router(store_router)
BaseApiRouter.include_router(dashboard_router)
BaseApiRouter.include_router(main_router)
BaseApiRouter.include_router(manage_router)
BaseApiRouter.include_router(database_router)
BaseApiRouter.include_router(plugin_router)
BaseApiRouter.include_router(system_router)
BaseApiRouter.include_router(menu_router)
:sparkles: 首次启动时提供使用web ui方式完全配置 (#1870) * :sparkles: 添加全局优先级hook * :sparkles: 添加基础配置api * :sparkles: 添加数据库连接测试 * :speech_balloon: 提示重启 * :adhesive_bandage: 填充过配置时友好提示 * :bug: 首次生成简易配置后自动加载 * :sparkles: 添加配置后重启接口 * :sparkles: 添加重启标志文件 * :sparkles: 添加重启脚本命令 * :sparkles: 添加重启系统限制 * :sparkles: 首次配置判断是否为win系统 * :fire: 移除bat * :sparkles: 添加关于菜单 * :sparkles: 支持整合包插件安装和添加整合包文档 * :adhesive_bandage: 检测数据库路径 * :adhesive_bandage: 修改数据库路径检测 * :adhesive_bandage: 修改数据库路径检测 * :adhesive_bandage: 修复路径注入 * :art: 显示添加优先级 * :bug: 修改PriorityLifecycle字典类名称 * :zap: 修复路径问题 * :zap: 修复路径检测 * ✨ 新增路径验证功能,确保用户输入的路径安全并在项目根目录内 * ✨ 优化路径验证功能,增加对非法字符和路径长度的检查,确保用户输入的路径更加安全 * :rotating_light: auto fix by pre-commit hooks * ✨ 优化获取文件列表的代码格式 * :memo: 修改README中webui示例图 * ✨ 更新PriorityLifecycle.on_startup装饰器 * ✨ 简化安装依赖的命令构建逻辑 * :rotating_light: auto fix by pre-commit hooks --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2025-06-16 09:11:41 +08:00
BaseApiRouter.include_router(configure_router)
WsApiRouter = APIRouter(prefix="/zhenxun/socket")
WsApiRouter.include_router(ws_log_routes)
WsApiRouter.include_router(status_routes)
WsApiRouter.include_router(chat_routes)
:sparkles: 首次启动时提供使用web ui方式完全配置 (#1870) * :sparkles: 添加全局优先级hook * :sparkles: 添加基础配置api * :sparkles: 添加数据库连接测试 * :speech_balloon: 提示重启 * :adhesive_bandage: 填充过配置时友好提示 * :bug: 首次生成简易配置后自动加载 * :sparkles: 添加配置后重启接口 * :sparkles: 添加重启标志文件 * :sparkles: 添加重启脚本命令 * :sparkles: 添加重启系统限制 * :sparkles: 首次配置判断是否为win系统 * :fire: 移除bat * :sparkles: 添加关于菜单 * :sparkles: 支持整合包插件安装和添加整合包文档 * :adhesive_bandage: 检测数据库路径 * :adhesive_bandage: 修改数据库路径检测 * :adhesive_bandage: 修改数据库路径检测 * :adhesive_bandage: 修复路径注入 * :art: 显示添加优先级 * :bug: 修改PriorityLifecycle字典类名称 * :zap: 修复路径问题 * :zap: 修复路径检测 * ✨ 新增路径验证功能,确保用户输入的路径安全并在项目根目录内 * ✨ 优化路径验证功能,增加对非法字符和路径长度的检查,确保用户输入的路径更加安全 * :rotating_light: auto fix by pre-commit hooks * ✨ 优化获取文件列表的代码格式 * :memo: 修改README中webui示例图 * ✨ 更新PriorityLifecycle.on_startup装饰器 * ✨ 简化安装依赖的命令构建逻辑 * :rotating_light: auto fix by pre-commit hooks --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2025-06-16 09:11:41 +08:00
@PriorityLifecycle.on_startup(priority=0)
2024-08-23 18:41:35 +08:00
async def _():
try:
2025-04-26 20:15:44 +08:00
# 存储任务引用的列表,防止任务被垃圾回收
_tasks = []
async def log_sink(message: str):
loop = None
if not loop:
try:
loop = asyncio.get_running_loop()
except Exception as e:
logger.warning("Web Ui log_sink", e=e)
if not loop:
loop = asyncio.new_event_loop()
2025-04-26 20:15:44 +08:00
# 存储任务引用到外部列表中
_tasks.append(loop.create_task(LOG_STORAGE.add(message.rstrip("\n"))))
logger_.add(
log_sink, colorize=True, filter=default_filter, format=default_format
)
app: FastAPI = nonebot.get_app()
app.include_router(BaseApiRouter)
app.include_router(WsApiRouter)
2024-08-23 18:41:35 +08:00
await init_public(app)
logger.info("<g>API启动成功</g>", "WebUi")
except Exception as e:
logger.error("<g>API启动失败</g>", "WebUi", e=e)