From 42d7d24c4d836db18b8f0c6854fa904de075b44e Mon Sep 17 00:00:00 2001 From: zqc <835569504@qq.com> Date: Tue, 3 Feb 2026 09:13:44 +0800 Subject: [PATCH] =?UTF-8?q?=E9=85=8D=E7=BD=AE=E4=BB=8Econfig.yaml=E4=B8=AD?= =?UTF-8?q?=E8=AF=BB=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.py | 59 +++++++++++++++++++++++++++++++++-------------------- config.yaml | 32 +++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 22 deletions(-) diff --git a/config.py b/config.py index 48a4bb6..0ccb9ed 100644 --- a/config.py +++ b/config.py @@ -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" diff --git a/config.yaml b/config.yaml index 9ff58b2..b221eb0 100644 --- a/config.yaml +++ b/config.yaml @@ -1,3 +1,35 @@ +rtsp_enabled: false + +# 应用配置 +app: + debug: false + log_level: INFO + log_file: "log/app.log" + +# 资源文件夹配置 +face: + face_register_image_dir: "D:/ruoyi/uploadPath/face" + video_dir: "D:/ruoyi/uploadPath/video" + face_cal_feature_timeout_hours: 10 + face_model_version: 0 + face_use_gpu: true + face_use_npu: false + sur_config_type_face: 0 + sur_config_scope_global: 0 + +# 数据库配置 +database: + host: localhost + port: 5432 + user: postgres + password: yipai123 + name: pmms + schema: public + pool_size: 10 + max_overflow: 20 + pool_recycle: 3600 + echo: false + cameras: - id: 1 index: testindexcode