将ProjectConfig加入Architecture

This commit is contained in:
2025-11-07 23:47:33 +08:00
parent c49f55808e
commit 9899387697
8 changed files with 56 additions and 41 deletions

View File

@@ -1,5 +1,6 @@
"""Callback路由处理"""
from ..Convention.Runtime.GlobalConfig import *
from ..Convention.Runtime.Architecture import Architecture
from fastapi import APIRouter, Request
from fastapi.responses import JSONResponse
@@ -7,15 +8,17 @@ from fastapi.responses import JSONResponse
from ..CoreModules.models import CallbackRequest
from ..CoreModules.plugin_interface import PluginInterface
config = ProjectConfig()
ALWAYS_RETURN_OK = config.FindItem("always_return_ok", True) # 返回ok可以避免重试
logger: ProjectConfig = Architecture.Get(ProjectConfig)
ALWAYS_RETURN_OK = logger.FindItem("always_return_ok", True) # 返回ok可以避免重试
logger.SaveProperties()
router = APIRouter()
@router.get("/callback")
async def callback_verify():
"""Callback可用性校验"""
config.Log("Info", "收到Callback验证请求")
logger.Log("Info", "收到Callback验证请求")
return JSONResponse({"result": "ok"})
@@ -27,7 +30,7 @@ async def callback_receive_construct(callback_data: CallbackRequest):
content = callback_data.content
command = content.split(" ")[0]
message = content[len(command):].strip()
config.Log("Info", f"识别指令: command={command}")
logger.Log("Info", f"识别指令: command={command}")
# 处理指令
result = await handle_command(command, message, callback_data.chatid, callback_data.creator)
@@ -37,7 +40,7 @@ async def callback_receive_construct(callback_data: CallbackRequest):
return JSONResponse({"result": "ok"})
except Exception as e:
config.Log("Error", f"处理Callback异常: {e}")
logger.Log("Error", f"处理Callback异常: {e}")
if ALWAYS_RETURN_OK:
return JSONResponse({"result": "ok"})
else:
@@ -49,13 +52,13 @@ async def callback_receive(request: Request):
try:
# 解析请求数据
data = await request.json()
config.Log("Info", f"完整callback数据: {data}")
logger.Log("Info", f"完整callback数据: {data}")
# 验证请求
try:
callback_data = CallbackRequest(**data)
except Exception as e:
config.Log("Error", f"请求数据验证失败: {e}")
logger.Log("Error", f"请求数据验证失败: {e}")
if ALWAYS_RETURN_OK:
return JSONResponse({"result": "ok"})
else:
@@ -65,7 +68,7 @@ async def callback_receive(request: Request):
content = callback_data.content
command = content.split(" ")[0]
message = content[len(command):].strip()
config.Log("Info", f"识别指令: command={command}")
logger.Log("Info", f"识别指令: command={command}")
# 处理指令
result = await handle_command(command, message, callback_data.chatid, callback_data.creator)
@@ -75,7 +78,7 @@ async def callback_receive(request: Request):
return JSONResponse({"result": "ok"})
except Exception as e:
config.Log("Error", f"处理Callback异常: {e}")
logger.Log("Error", f"处理Callback异常: {e}")
if ALWAYS_RETURN_OK:
return JSONResponse({"result": "ok"})
else:
@@ -98,11 +101,11 @@ async def handle_command(command: str, message: str,
try:
plugin = PluginInterface.plugin_instances.get(command, None)
if plugin:
config.Log("Info", f"已找到插件注册指令: {command}, class: {plugin.__class__.__name__}")
logger.Log("Info", f"已找到插件注册指令: {command}, class: {plugin.__class__.__name__}")
return await plugin.callback(message, chat_id, user_id)
else:
return f"❌ 未识别指令: {command}"
except Exception as e:
config.Log("Error", f"{ConsoleFrontColor.RED}处理指令异常: {e}{ConsoleFrontColor.RESET}")
logger.Log("Error", f"{ConsoleFrontColor.RED}处理指令异常: {e}{ConsoleFrontColor.RESET}")
return f"❌ 处理指令时出错: {str(e)}"

View File

@@ -1,5 +1,6 @@
"""健康检查路由"""
from ..Convention.Runtime.Config import *
from ..Convention.Runtime.Architecture import Architecture
try:
import psutil
except ImportError:
@@ -10,17 +11,16 @@ from fastapi.responses import JSONResponse
from ..CoreModules.database import get_db
from ..Convention.Runtime.GlobalConfig import ProjectConfig, ConsoleFrontColor
config = ProjectConfig()
logger: ProjectConfig = Architecture.Get(ProjectConfig)
router = APIRouter()
@router.get("/health")
async def health_check():
"""健康检查"""
return JSONResponse({
"status": "healthy",
"service": config.FindItem("app_name", "Application")
"service": logger.FindItem("app_name", "Application")
})
@@ -53,9 +53,10 @@ async def system_stats():
}
})
except Exception as e:
config.Log("Error", f"{ConsoleFrontColor.RED}获取系统统计失败: {e}{ConsoleFrontColor.RESET}")
logger.Log("Error", f"{ConsoleFrontColor.RED}获取系统统计失败: {e}{ConsoleFrontColor.RESET}")
return JSONResponse(
status_code=500,
content={"error": str(e)}
)
logger.SaveProperties()