将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,6 +1,10 @@
from ..Convention.Runtime.Config import *
from ..Convention.Runtime.Architecture import Architecture
from ..Convention.Runtime.GlobalConfig import ProjectConfig, ConsoleFrontColor
from ..CoreModules.flags import set_internal_verbose, get_internal_debug
Architecture.Register(ProjectConfig, ProjectConfig(), lambda: None)
from ..CoreModules.flags import set_internal_verbose
from .web import app
from argparse import ArgumentParser
from typing import *
@@ -11,7 +15,7 @@ except ImportError as ex:
ImportingThrow(ex, "Internal", ["uvicorn"])
def main() -> int:
config = ProjectConfig()
config: ProjectConfig = Architecture.Get(ProjectConfig)
parser = ArgumentParser()
parser.add_argument("--host", type=str, default=config.FindItem("host", "0.0.0.0"))

View File

@@ -8,7 +8,7 @@ from ..CoreRouters import callback, health
from ..Convention.Runtime.GlobalConfig import *
from ..Convention.Runtime.Architecture import Architecture
config = ProjectConfig()
logger: ProjectConfig = Architecture.Get(ProjectConfig)
APP_CONFIG = {
"docs_url": "/docs",
}
@@ -17,30 +17,31 @@ APP_CONFIG = {
async def lifespan(app: FastAPI):
"""应用生命周期管理"""
# 启动
config.Log("Info", "应用启动中...")
logger.Log("Info", "应用启动中...")
# 初始化数据
config.Log("Info", "初始化数据...")
logger.Log("Info", "初始化数据...")
# 启动后台清理
config.Log("Info", "启动后台清理...")
logger.Log("Info", "启动后台清理...")
yield
# 关闭
try:
config.Log("Info", "关闭应用...")
logger.Log("Info", "关闭应用...")
# await cleanup_task
except asyncio.CancelledError:
pass
finally:
config.Log("Info", "关闭应用完成...")
logger.Log("Info", "关闭应用完成...")
# db.close()
def generate_app(kwargs: dict) -> FastAPI:
'''
生成FastAPI应用
'''
config: ProjectConfig = Architecture.Get(ProjectConfig)
kwargs.update(**APP_CONFIG)
app = FastAPI(**kwargs, lifespan=lifespan)
@@ -59,11 +60,12 @@ def generate_app(kwargs: dict) -> FastAPI:
return app
app: FastAPI = generate_app(config.FindItem("app_config", {}))
app: FastAPI = generate_app(logger.FindItem("app_config", {}))
@app.get("/")
async def root():
"""根路径"""
config: ProjectConfig = Architecture.Get(ProjectConfig)
app_name = config.FindItem("app_name", "Application")
app_version = config.FindItem("app_version", "0.0.0")
config.SaveProperties()
@@ -76,11 +78,13 @@ async def root():
@app.exception_handler(Exception)
async def global_exception_handler(request, exc):
"""全局异常处理"""
config.Log("Error", f"未捕获的异常: {exc}\n{format_traceback_info()}")
logger.Log("Error", f"未捕获的异常: {exc}\n{format_traceback_info()}")
return JSONResponse(
status_code=500,
content={"error": "Internal Server Error", "detail": str(exc)}
)
logger.SaveProperties()
# 除了从本模块导出的app使用API实例外, 还可以从Architecture.Get(FastAPI)获取
__all__ = ["app"]

View File

@@ -5,8 +5,9 @@ from ..Convention.Runtime.GlobalConfig import ProjectConfig, ConsoleFrontColor
from ..Convention.Runtime.Architecture import Architecture
from ..Convention.Runtime.File import ToolFile
config = ProjectConfig()
DATABASE_PATH = config.GetFile(config.FindItem("database_path", "db.db"), False).GetFullPath()
logger: ProjectConfig = Architecture.Get(ProjectConfig)
DATABASE_PATH = logger.GetFile(logger.FindItem("database_path", "db.db"), False).GetFullPath()
logger.SaveProperties()
class Database:
"""数据库管理类"""
@@ -47,9 +48,9 @@ class Database:
self._conn.execute("PRAGMA cache_size=1000")
self._conn.execute("PRAGMA temp_store=MEMORY")
config.Log("Info", f"{ConsoleFrontColor.GREEN}数据库连接成功: {self.db_path}{ConsoleFrontColor.RESET}")
logger.Log("Info", f"{ConsoleFrontColor.GREEN}数据库连接成功: {self.db_path}{ConsoleFrontColor.RESET}")
except Exception as e:
config.Log("Error", f"{ConsoleFrontColor.RED}数据库连接失败: {e}{ConsoleFrontColor.RESET}")
logger.Log("Error", f"{ConsoleFrontColor.RED}数据库连接失败: {e}{ConsoleFrontColor.RESET}")
raise
return self._conn
@@ -75,7 +76,7 @@ class Database:
if not self._table_exists(table_name):
cursor = self.conn.cursor()
cursor.execute(f"CREATE TABLE IF NOT EXISTS {table_name} (id INTEGER PRIMARY KEY AUTOINCREMENT)")
config.Log("Info", f"{ConsoleFrontColor.GREEN}为表 {table_name} 创建{ConsoleFrontColor.RESET}")
logger.Log("Info", f"{ConsoleFrontColor.GREEN}为表 {table_name} 创建{ConsoleFrontColor.RESET}")
return self
def _column_exists(self, table_name: str, column_name: str) -> bool:
@@ -105,9 +106,9 @@ class Database:
try:
cursor = self.conn.cursor()
cursor.execute(f"ALTER TABLE {table_name} ADD COLUMN {column_name} {column_def}")
config.Log("Info", f"{ConsoleFrontColor.GREEN}为表 {table_name} 添加列 {column_name}{ConsoleFrontColor.RESET}")
logger.Log("Info", f"{ConsoleFrontColor.GREEN}为表 {table_name} 添加列 {column_name}{ConsoleFrontColor.RESET}")
except Exception as e:
config.Log("Warning", f"{ConsoleFrontColor.YELLOW}添加列失败: {e}{ConsoleFrontColor.RESET}")
logger.Log("Warning", f"{ConsoleFrontColor.YELLOW}添加列失败: {e}{ConsoleFrontColor.RESET}")
def define_column(self, table_name: str, column_name: str, column_def: str):
"""定义列
@@ -129,7 +130,7 @@ class Database:
if self._conn:
self._conn.close()
self._conn = None
config.Log("Info", f"{ConsoleFrontColor.GREEN}数据库连接已关闭{ConsoleFrontColor.RESET}")
logger.Log("Info", f"{ConsoleFrontColor.GREEN}数据库连接已关闭{ConsoleFrontColor.RESET}")
def get_db() -> Database:
"""获取全局数据库实例(单例模式)"""

View File

@@ -2,7 +2,7 @@ from ..Convention.Runtime.Architecture import *
from ..Convention.Runtime.GlobalConfig import ProjectConfig
from pydantic import *
config = ProjectConfig()
logger: ProjectConfig = Architecture.Get(ProjectConfig)
class DebugFlags(BaseModel):
debug: bool = Field(default=False)
@@ -10,7 +10,7 @@ class DebugFlags(BaseModel):
class VerboseFlags(BaseModel):
verbose: bool = Field(default=False)
Architecture.Register(DebugFlags, DebugFlags(debug=config.FindItem("debug", False)), lambda: None)
Architecture.Register(DebugFlags, DebugFlags(debug=logger.FindItem("debug", False)), lambda: None)
Architecture.Register(VerboseFlags, VerboseFlags(verbose=False), lambda: None)
def set_internal_debug(debug:bool) -> None:
@@ -23,6 +23,6 @@ def set_internal_verbose(verbose:bool) -> None:
def get_internal_verbose() -> bool:
return Architecture.Get(VerboseFlags).verbose
config.SaveProperties()
logger.SaveProperties()
__all__ = ["set_internal_debug", "get_internal_debug", "set_internal_verbose", "get_internal_verbose"]

View File

@@ -4,9 +4,11 @@ from starlette.middleware.base import BaseHTTPMiddleware
from starlette.requests import Request
from starlette.responses import Response
from ..Convention.Runtime.GlobalConfig import *
from ..Convention.Runtime.Architecture import Architecture
config = ProjectConfig()
MAX_CONCURRENT_REQUESTS = config.FindItem("max_concurrent_requests", 100)
logger: ProjectConfig = Architecture.Get(ProjectConfig)
MAX_CONCURRENT_REQUESTS = logger.FindItem("max_concurrent_requests", 100)
logger.SaveProperties()
class ConcurrencyLimitMiddleware(BaseHTTPMiddleware):
"""并发限制中间件 - 防止内存爆炸"""
@@ -15,7 +17,7 @@ class ConcurrencyLimitMiddleware(BaseHTTPMiddleware):
super().__init__(app)
self.semaphore = asyncio.Semaphore(max_concurrent)
self.max_concurrent = max_concurrent
config.Log("Info", f"并发限制中间件已启用,最大并发数:{max_concurrent}")
logger.Log("Info", f"并发限制中间件已启用,最大并发数:{max_concurrent}")
async def dispatch(self, request: Request, call_next) -> Response:
"""处理请求"""
@@ -24,7 +26,7 @@ class ConcurrencyLimitMiddleware(BaseHTTPMiddleware):
response = await call_next(request)
return response
except Exception as e:
config.Log("Error", f"{ConsoleFrontColor.RED}请求处理错误: {e}{ConsoleFrontColor.RESET}")
logger.Log("Error", f"{ConsoleFrontColor.RED}请求处理错误: {e}{ConsoleFrontColor.RESET}")
return Response(
content='{"error": "Internal Server Error"}',
status_code=500,

View File

@@ -12,7 +12,7 @@ from abc import ABC
import importlib
import os
config = ProjectConfig()
config: ProjectConfig = Architecture.Get(ProjectConfig)
class DatabaseModel(BaseModel):
table_name: str = Field(default="main_table")

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()