mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2025-12-15 14:22:55 +08:00
✨ 添加数据库连接测试
This commit is contained in:
parent
da1ccffe45
commit
c263c58740
@ -8,6 +8,7 @@ import nonebot
|
||||
from zhenxun.configs.config import BotConfig, Config
|
||||
|
||||
from ...base_model import Result
|
||||
from .data_source import test_db_connection
|
||||
from .model import Setting
|
||||
|
||||
router = APIRouter(prefix="/configure")
|
||||
@ -48,3 +49,16 @@ async def _(setting: Setting) -> Result:
|
||||
Config.set_config("web-ui", "password", setting.password)
|
||||
env_file.write_text(env_text, encoding="utf-8")
|
||||
return Result.ok(info="基础配置设置完成!")
|
||||
|
||||
|
||||
@router.get(
|
||||
"/test_db",
|
||||
response_model=Result,
|
||||
response_class=JSONResponse,
|
||||
description="设置基础配置",
|
||||
)
|
||||
async def _(db_url: str) -> Result:
|
||||
result = await test_db_connection(db_url)
|
||||
if isinstance(result, str):
|
||||
return Result.fail(result)
|
||||
return Result.ok(info="数据库连接成功!")
|
||||
|
||||
18
zhenxun/builtin_plugins/web_ui/api/configure/data_source.py
Normal file
18
zhenxun/builtin_plugins/web_ui/api/configure/data_source.py
Normal file
@ -0,0 +1,18 @@
|
||||
from tortoise import Tortoise
|
||||
|
||||
|
||||
async def test_db_connection(db_url: str) -> bool | str:
|
||||
try:
|
||||
# 初始化 Tortoise ORM
|
||||
await Tortoise.init(
|
||||
db_url=db_url,
|
||||
modules={"models": ["__main__"]}, # 这里不需要实际模型
|
||||
)
|
||||
# 测试连接
|
||||
await Tortoise.get_connection("default").execute_query("SELECT 1")
|
||||
return True
|
||||
except Exception as e:
|
||||
return str(e)
|
||||
finally:
|
||||
# 关闭连接
|
||||
await Tortoise.close_connections()
|
||||
@ -54,9 +54,9 @@ async def init():
|
||||
# raise DbUrlIsNode("数据库配置为空,请在.env.dev中配置DB_URL...")
|
||||
error = f"""
|
||||
**********************************************************************
|
||||
🌟 ****************************** 配置为空 ************************** 🌟
|
||||
🌟 **************************** 配置为空 ************************* 🌟
|
||||
🚀 请打开 WebUi 进行基础配置 🚀
|
||||
🌐 配置地址:http://{driver.config.host}:{driver.config.port}/configure 🌐
|
||||
🌐 配置地址:http://{driver.config.host}:{driver.config.port}/#/configure 🌐
|
||||
***********************************************************************
|
||||
***********************************************************************
|
||||
"""
|
||||
|
||||
Loading…
Reference in New Issue
Block a user