将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.Config import *
from ..Convention.Runtime.Architecture import Architecture
from ..Convention.Runtime.GlobalConfig import ProjectConfig, ConsoleFrontColor 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 .web import app
from argparse import ArgumentParser from argparse import ArgumentParser
from typing import * from typing import *
@@ -11,7 +15,7 @@ except ImportError as ex:
ImportingThrow(ex, "Internal", ["uvicorn"]) ImportingThrow(ex, "Internal", ["uvicorn"])
def main() -> int: def main() -> int:
config = ProjectConfig() config: ProjectConfig = Architecture.Get(ProjectConfig)
parser = ArgumentParser() parser = ArgumentParser()
parser.add_argument("--host", type=str, default=config.FindItem("host", "0.0.0.0")) 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.GlobalConfig import *
from ..Convention.Runtime.Architecture import Architecture from ..Convention.Runtime.Architecture import Architecture
config = ProjectConfig() logger: ProjectConfig = Architecture.Get(ProjectConfig)
APP_CONFIG = { APP_CONFIG = {
"docs_url": "/docs", "docs_url": "/docs",
} }
@@ -17,30 +17,31 @@ APP_CONFIG = {
async def lifespan(app: FastAPI): async def lifespan(app: FastAPI):
"""应用生命周期管理""" """应用生命周期管理"""
# 启动 # 启动
config.Log("Info", "应用启动中...") logger.Log("Info", "应用启动中...")
# 初始化数据 # 初始化数据
config.Log("Info", "初始化数据...") logger.Log("Info", "初始化数据...")
# 启动后台清理 # 启动后台清理
config.Log("Info", "启动后台清理...") logger.Log("Info", "启动后台清理...")
yield yield
# 关闭 # 关闭
try: try:
config.Log("Info", "关闭应用...") logger.Log("Info", "关闭应用...")
# await cleanup_task # await cleanup_task
except asyncio.CancelledError: except asyncio.CancelledError:
pass pass
finally: finally:
config.Log("Info", "关闭应用完成...") logger.Log("Info", "关闭应用完成...")
# db.close() # db.close()
def generate_app(kwargs: dict) -> FastAPI: def generate_app(kwargs: dict) -> FastAPI:
''' '''
生成FastAPI应用 生成FastAPI应用
''' '''
config: ProjectConfig = Architecture.Get(ProjectConfig)
kwargs.update(**APP_CONFIG) kwargs.update(**APP_CONFIG)
app = FastAPI(**kwargs, lifespan=lifespan) app = FastAPI(**kwargs, lifespan=lifespan)
@@ -59,11 +60,12 @@ def generate_app(kwargs: dict) -> FastAPI:
return app return app
app: FastAPI = generate_app(config.FindItem("app_config", {})) app: FastAPI = generate_app(logger.FindItem("app_config", {}))
@app.get("/") @app.get("/")
async def root(): async def root():
"""根路径""" """根路径"""
config: ProjectConfig = Architecture.Get(ProjectConfig)
app_name = config.FindItem("app_name", "Application") app_name = config.FindItem("app_name", "Application")
app_version = config.FindItem("app_version", "0.0.0") app_version = config.FindItem("app_version", "0.0.0")
config.SaveProperties() config.SaveProperties()
@@ -76,11 +78,13 @@ async def root():
@app.exception_handler(Exception) @app.exception_handler(Exception)
async def global_exception_handler(request, exc): 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( return JSONResponse(
status_code=500, status_code=500,
content={"error": "Internal Server Error", "detail": str(exc)} content={"error": "Internal Server Error", "detail": str(exc)}
) )
logger.SaveProperties()
# 除了从本模块导出的app使用API实例外, 还可以从Architecture.Get(FastAPI)获取 # 除了从本模块导出的app使用API实例外, 还可以从Architecture.Get(FastAPI)获取
__all__ = ["app"] __all__ = ["app"]

View File

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

View File

@@ -2,7 +2,7 @@ from ..Convention.Runtime.Architecture import *
from ..Convention.Runtime.GlobalConfig import ProjectConfig from ..Convention.Runtime.GlobalConfig import ProjectConfig
from pydantic import * from pydantic import *
config = ProjectConfig() logger: ProjectConfig = Architecture.Get(ProjectConfig)
class DebugFlags(BaseModel): class DebugFlags(BaseModel):
debug: bool = Field(default=False) debug: bool = Field(default=False)
@@ -10,7 +10,7 @@ class DebugFlags(BaseModel):
class VerboseFlags(BaseModel): class VerboseFlags(BaseModel):
verbose: bool = Field(default=False) 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) Architecture.Register(VerboseFlags, VerboseFlags(verbose=False), lambda: None)
def set_internal_debug(debug:bool) -> None: def set_internal_debug(debug:bool) -> None:
@@ -23,6 +23,6 @@ def set_internal_verbose(verbose:bool) -> None:
def get_internal_verbose() -> bool: def get_internal_verbose() -> bool:
return Architecture.Get(VerboseFlags).verbose return Architecture.Get(VerboseFlags).verbose
config.SaveProperties() logger.SaveProperties()
__all__ = ["set_internal_debug", "get_internal_debug", "set_internal_verbose", "get_internal_verbose"] __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.requests import Request
from starlette.responses import Response from starlette.responses import Response
from ..Convention.Runtime.GlobalConfig import * from ..Convention.Runtime.GlobalConfig import *
from ..Convention.Runtime.Architecture import Architecture
config = ProjectConfig() logger: ProjectConfig = Architecture.Get(ProjectConfig)
MAX_CONCURRENT_REQUESTS = config.FindItem("max_concurrent_requests", 100) MAX_CONCURRENT_REQUESTS = logger.FindItem("max_concurrent_requests", 100)
logger.SaveProperties()
class ConcurrencyLimitMiddleware(BaseHTTPMiddleware): class ConcurrencyLimitMiddleware(BaseHTTPMiddleware):
"""并发限制中间件 - 防止内存爆炸""" """并发限制中间件 - 防止内存爆炸"""
@@ -15,7 +17,7 @@ class ConcurrencyLimitMiddleware(BaseHTTPMiddleware):
super().__init__(app) super().__init__(app)
self.semaphore = asyncio.Semaphore(max_concurrent) self.semaphore = asyncio.Semaphore(max_concurrent)
self.max_concurrent = 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: async def dispatch(self, request: Request, call_next) -> Response:
"""处理请求""" """处理请求"""
@@ -24,7 +26,7 @@ class ConcurrencyLimitMiddleware(BaseHTTPMiddleware):
response = await call_next(request) response = await call_next(request)
return response return response
except Exception as e: except Exception as e:
config.Log("Error", f"{ConsoleFrontColor.RED}请求处理错误: {e}{ConsoleFrontColor.RESET}") logger.Log("Error", f"{ConsoleFrontColor.RED}请求处理错误: {e}{ConsoleFrontColor.RESET}")
return Response( return Response(
content='{"error": "Internal Server Error"}', content='{"error": "Internal Server Error"}',
status_code=500, status_code=500,

View File

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

View File

@@ -1,5 +1,6 @@
"""Callback路由处理""" """Callback路由处理"""
from ..Convention.Runtime.GlobalConfig import * from ..Convention.Runtime.GlobalConfig import *
from ..Convention.Runtime.Architecture import Architecture
from fastapi import APIRouter, Request from fastapi import APIRouter, Request
from fastapi.responses import JSONResponse from fastapi.responses import JSONResponse
@@ -7,15 +8,17 @@ from fastapi.responses import JSONResponse
from ..CoreModules.models import CallbackRequest from ..CoreModules.models import CallbackRequest
from ..CoreModules.plugin_interface import PluginInterface 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 = APIRouter()
@router.get("/callback") @router.get("/callback")
async def callback_verify(): async def callback_verify():
"""Callback可用性校验""" """Callback可用性校验"""
config.Log("Info", "收到Callback验证请求") logger.Log("Info", "收到Callback验证请求")
return JSONResponse({"result": "ok"}) return JSONResponse({"result": "ok"})
@@ -27,7 +30,7 @@ async def callback_receive_construct(callback_data: CallbackRequest):
content = callback_data.content content = callback_data.content
command = content.split(" ")[0] command = content.split(" ")[0]
message = content[len(command):].strip() 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) 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"}) return JSONResponse({"result": "ok"})
except Exception as e: except Exception as e:
config.Log("Error", f"处理Callback异常: {e}") logger.Log("Error", f"处理Callback异常: {e}")
if ALWAYS_RETURN_OK: if ALWAYS_RETURN_OK:
return JSONResponse({"result": "ok"}) return JSONResponse({"result": "ok"})
else: else:
@@ -49,13 +52,13 @@ async def callback_receive(request: Request):
try: try:
# 解析请求数据 # 解析请求数据
data = await request.json() data = await request.json()
config.Log("Info", f"完整callback数据: {data}") logger.Log("Info", f"完整callback数据: {data}")
# 验证请求 # 验证请求
try: try:
callback_data = CallbackRequest(**data) callback_data = CallbackRequest(**data)
except Exception as e: except Exception as e:
config.Log("Error", f"请求数据验证失败: {e}") logger.Log("Error", f"请求数据验证失败: {e}")
if ALWAYS_RETURN_OK: if ALWAYS_RETURN_OK:
return JSONResponse({"result": "ok"}) return JSONResponse({"result": "ok"})
else: else:
@@ -65,7 +68,7 @@ async def callback_receive(request: Request):
content = callback_data.content content = callback_data.content
command = content.split(" ")[0] command = content.split(" ")[0]
message = content[len(command):].strip() 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) 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"}) return JSONResponse({"result": "ok"})
except Exception as e: except Exception as e:
config.Log("Error", f"处理Callback异常: {e}") logger.Log("Error", f"处理Callback异常: {e}")
if ALWAYS_RETURN_OK: if ALWAYS_RETURN_OK:
return JSONResponse({"result": "ok"}) return JSONResponse({"result": "ok"})
else: else:
@@ -98,11 +101,11 @@ async def handle_command(command: str, message: str,
try: try:
plugin = PluginInterface.plugin_instances.get(command, None) plugin = PluginInterface.plugin_instances.get(command, None)
if plugin: 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) return await plugin.callback(message, chat_id, user_id)
else: else:
return f"❌ 未识别指令: {command}" return f"❌ 未识别指令: {command}"
except Exception as e: 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)}" return f"❌ 处理指令时出错: {str(e)}"

View File

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