补充提交

This commit is contained in:
zqc
2026-01-08 10:33:53 +08:00
parent f86effd63c
commit a183d650b3
4 changed files with 410 additions and 0 deletions

103
config.py Normal file
View File

@@ -0,0 +1,103 @@
"""
数据库配置模块
使用pydantic进行配置验证和管理
"""
from typing import Optional, List
from pydantic_settings import BaseSettings
from functools import lru_cache
from pydantic import PostgresDsn, field_validator
from pydantic_core.core_schema import FieldValidationInfo
class Settings(BaseSettings):
"""应用配置类"""
RTSP_ENABLED: bool = True
# API配置
API_V1_PREFIX: str = "/api/v1"
PROJECT_NAME: str = "algorithm-service"
PROJECT_VERSION: str = "1.0.0"
PROJECT_DESCRIPTION: str = "algorithm-service"
BACKEND_CORS_ORIGINS: List[str] = ["http://localhost:3000", "http://localhost:8000"]
# 数据库配置
DATABASE_HOST: str = "localhost"
DATABASE_PORT: int = 5432
DATABASE_USER: str = "postgres"
DATABASE_PASSWORD: str = "yipai123"
DATABASE_NAME: str = "pmms"
DATABASE_SCHEMA: str = "public"
# 连接池配置
DATABASE_POOL_SIZE: int = 10
DATABASE_MAX_OVERFLOW: int = 20
DATABASE_POOL_RECYCLE: int = 3600 # 连接回收时间(秒)
DATABASE_ECHO: bool = False # SQL日志生产环境设为False
# 应用配置
APP_NAME: str = "SurFaceFeature API"
APP_VERSION: str = "1.0.0"
DEBUG: bool = False
# 日志配置
LOG_LEVEL: str = "INFO"
LOG_FILE: Optional[str] = None
# 异步配置
ASYNC_MODE: bool = False
# 资源文件夹配置
FACE_REGISTER_IMAGE_RESOURCE_DIR: str = "D:/ruoyi/uploadPath/face"
VIDEO_RESOURCE_DIR: str = "D:/ruoyi/uploadPath/video"
FACE_CAL_FEATURE_TIMEOUT_HOURS: int = 10
FACE_MODEL_VERSION: int = 0 #insight_face_buffalo_l
FACE_USE_GPU: bool = True
FACE_USE_NPU: bool = False
SUR_CONFIG_TYPE_FACE: int = 0
SUR_CONFIG_SCOPE_GLOBAL: int = 0
# JWT配置预留
SECRET_KEY: str = "your-secret-key-here-change-in-production"
ALGORITHM: str = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES: int = 30
@property
def DATABASE_URL(self) -> str:
"""构建数据库连接URL"""
return f"postgresql://{self.DATABASE_USER}:{self.DATABASE_PASSWORD}@{self.DATABASE_HOST}:{self.DATABASE_PORT}/{self.DATABASE_NAME}"
@property
def ASYNC_DATABASE_URL(self) -> str:
"""构建异步数据库连接URL"""
return f"postgresql+asyncpg://{self.DATABASE_USER}:{self.DATABASE_PASSWORD}@{self.DATABASE_HOST}:{self.DATABASE_PORT}/{self.DATABASE_NAME}"
@field_validator("DATABASE_POOL_SIZE")
def validate_pool_size(cls, v):
"""验证连接池大小"""
if v < 1:
raise ValueError("DATABASE_POOL_SIZE must be at least 1")
if v > 100:
raise ValueError("DATABASE_POOL_SIZE cannot exceed 100")
return v
class Config:
env_file = ".env"
env_file_encoding = "utf-8"
case_sensitive = False
extra = "ignore"
@lru_cache()
def get_settings() -> Settings:
"""
获取配置单例
使用lru_cache避免重复加载.env文件
"""
return Settings()
# 导出配置实例
settings = get_settings()