将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

@@ -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")