配置从config.yaml中读取

This commit is contained in:
zqc
2026-02-03 09:13:44 +08:00
parent 7f18de94d0
commit 42d7d24c4d
2 changed files with 69 additions and 22 deletions

View File

@@ -3,6 +3,8 @@
使用pydantic进行配置验证和管理
"""
import yaml
import os
from typing import Optional, List
from pydantic_settings import BaseSettings
from functools import lru_cache
@@ -10,10 +12,22 @@ from pydantic import PostgresDsn, field_validator
from pydantic_core.core_schema import FieldValidationInfo
def load_yaml_config() -> dict:
"""从config.yaml加载配置"""
config_path = os.path.join(os.path.dirname(__file__), "config.yaml")
try:
with open(config_path, 'r', encoding='utf-8') as f:
return yaml.safe_load(f) or {}
except (FileNotFoundError, yaml.YAMLError):
return {}
class Settings(BaseSettings):
"""应用配置类"""
RTSP_ENABLED: bool = True
yaml_config: dict = load_yaml_config()
RTSP_ENABLED: bool = yaml_config.get('rtsp_enabled', False)
# API配置
API_V1_PREFIX: str = "/api/v1"
@@ -24,40 +38,41 @@ class Settings(BaseSettings):
# 数据库配置
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_HOST: str = yaml_config.get('database', {}).get('host', "localhost")
DATABASE_PORT: int = yaml_config.get('database', {}).get('port', 5432)
DATABASE_USER: str = yaml_config.get('database', {}).get('user', "postgres")
DATABASE_PASSWORD: str = yaml_config.get('database', {}).get('password', "yipai123")
DATABASE_NAME: str = yaml_config.get('database', {}).get('name', "pmms")
DATABASE_SCHEMA: str = yaml_config.get('database', {}).get('schema', "public")
# 连接池配置
DATABASE_POOL_SIZE: int = 10
DATABASE_MAX_OVERFLOW: int = 20
DATABASE_POOL_RECYCLE: int = 3600 # 连接回收时间(秒)
DATABASE_ECHO: bool = False # SQL日志生产环境设为False
DATABASE_POOL_SIZE: int = yaml_config.get('database', {}).get('pool_size', 10)
DATABASE_MAX_OVERFLOW: int = yaml_config.get('database', {}).get('max_overflow', 20)
DATABASE_POOL_RECYCLE: int = yaml_config.get('database', {}).get('pool_recycle', 3600) # 连接回收时间(秒)
DATABASE_ECHO: bool = yaml_config.get('database', {}).get('echo', False) # SQL日志生产环境设为False
# 应用配置
APP_NAME: str = "SurFaceFeature API"
APP_VERSION: str = "1.0.0"
DEBUG: bool = False
DEBUG: bool = yaml_config.get('app', {}).get('debug', False)
# 日志配置
LOG_LEVEL: str = "INFO"
LOG_FILE: Optional[str] = None
LOG_LEVEL: str = yaml_config.get('app', {}).get('log_level', "INFO")
LOG_FILE: Optional[str] = yaml_config.get('app', {}).get('log_file')
# 异步配置
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
FACE_REGISTER_IMAGE_RESOURCE_DIR: str = yaml_config.get('face', {}).get('face_register_image_dir', "D:/ruoyi/uploadPath/face")
VIDEO_RESOURCE_DIR: str = yaml_config.get('face', {}).get('video_dir', "D:/ruoyi/uploadPath/video")
FACE_CAL_FEATURE_TIMEOUT_HOURS: int = yaml_config.get('face', {}).get('face_cal_feature_timeout_hours', 10)
FACE_MODEL_VERSION: int = yaml_config.get('face', {}).get('face_model_version', 0) #insight_face_buffalo_l
FACE_USE_GPU: bool = yaml_config.get('face', {}).get('face_use_gpu', True)
FACE_USE_NPU: bool = yaml_config.get('face', {}).get('face_use_npu', False)
SUR_CONFIG_TYPE_FACE: int = yaml_config.get('face', {}).get('sur_config_type_face', 0)
SUR_CONFIG_SCOPE_GLOBAL: int = yaml_config.get('face', {}).get('sur_config_scope_global', 0)
# JWT配置预留
SECRET_KEY: str = "your-secret-key-here-change-in-production"