update:1.切换数据库PostgreSQL
This commit is contained in:
+31
-6
@@ -3,6 +3,7 @@ from pydantic_settings import BaseSettings
|
||||
from typing import Optional
|
||||
from pathlib import Path
|
||||
import logging
|
||||
import os
|
||||
|
||||
# 获取项目根目录(从backend/app/config.py向上两级)
|
||||
PROJECT_ROOT = Path(__file__).parent.parent
|
||||
@@ -12,13 +13,15 @@ DATA_DIR.mkdir(exist_ok=True)
|
||||
# 配置模块使用标准logging(在logger.py初始化之前)
|
||||
config_logger = logging.getLogger(__name__)
|
||||
|
||||
# 数据库文件路径(绝对路径)
|
||||
# 数据库配置:支持PostgreSQL和SQLite
|
||||
# 优先使用环境变量DATABASE_URL,否则使用SQLite
|
||||
DB_FILE = DATA_DIR / "ai_story.db"
|
||||
DEFAULT_SQLITE_URL = f"sqlite+aiosqlite:///{str(DB_FILE.absolute()).replace(chr(92), '/')}"
|
||||
|
||||
# 生成数据库URL(在类外部生成,确保使用绝对路径)
|
||||
# 将Windows反斜杠转换为正斜杠,SQLite URL格式要求
|
||||
DATABASE_URL = f"sqlite+aiosqlite:///{str(DB_FILE.absolute()).replace(chr(92), '/')}"
|
||||
config_logger.debug(f"数据库文件路径: {DB_FILE}")
|
||||
# 从环境变量获取数据库URL,如果未设置则使用SQLite
|
||||
DATABASE_URL = os.getenv("DATABASE_URL", DEFAULT_SQLITE_URL)
|
||||
|
||||
config_logger.debug(f"数据库类型: {'PostgreSQL' if 'postgresql' in DATABASE_URL else 'SQLite'}")
|
||||
config_logger.debug(f"数据库URL: {DATABASE_URL}")
|
||||
|
||||
class Settings(BaseSettings):
|
||||
@@ -41,9 +44,31 @@ class Settings(BaseSettings):
|
||||
# CORS配置
|
||||
cors_origins: list[str] = ["http://localhost:8000", "http://127.0.0.1:8000"]
|
||||
|
||||
# 数据库配置 - 使用预先计算好的绝对路径URL
|
||||
# 数据库配置 - 支持PostgreSQL和SQLite
|
||||
database_url: str = DATABASE_URL
|
||||
|
||||
# PostgreSQL连接池配置(优化后支持80-150并发用户)
|
||||
database_pool_size: int = 30 # 核心连接池大小(从20提升到30)
|
||||
database_max_overflow: int = 20 # 最大溢出连接数(从10提升到20)
|
||||
database_pool_timeout: int = 60 # 连接池超时秒数(从30提升到60)
|
||||
database_pool_recycle: int = 1800 # 连接回收时间秒数(从3600降低到1800,30分钟)
|
||||
database_pool_pre_ping: bool = True # 连接前ping检测,确保连接有效
|
||||
database_pool_use_lifo: bool = True # 使用LIFO策略提高连接复用率
|
||||
|
||||
# 会话监控配置
|
||||
database_session_max_active: int = 50 # 活跃会话警告阈值(从100降低到50)
|
||||
database_session_leak_threshold: int = 100 # 会话泄漏严重告警阈值
|
||||
|
||||
# SQLite优化配置
|
||||
sqlite_cache_size_mb: int = 128 # SQLite缓存大小MB(从64提升到128)
|
||||
sqlite_mmap_size_mb: int = 256 # 内存映射I/O大小MB
|
||||
sqlite_wal_autocheckpoint: int = 1000 # WAL自动检查点间隔
|
||||
|
||||
# 数据库监控配置
|
||||
database_enable_slow_query_log: bool = True # 启用慢查询日志
|
||||
database_slow_query_threshold: float = 1.0 # 慢查询阈值(秒)
|
||||
database_enable_metrics: bool = True # 启用性能指标收集
|
||||
|
||||
# AI服务配置
|
||||
openai_api_key: Optional[str] = None
|
||||
openai_base_url: Optional[str] = None
|
||||
|
||||
Reference in New Issue
Block a user