配置从config.yaml中读取
This commit is contained in:
59
config.py
59
config.py
@@ -3,6 +3,8 @@
|
|||||||
使用pydantic进行配置验证和管理
|
使用pydantic进行配置验证和管理
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import yaml
|
||||||
|
import os
|
||||||
from typing import Optional, List
|
from typing import Optional, List
|
||||||
from pydantic_settings import BaseSettings
|
from pydantic_settings import BaseSettings
|
||||||
from functools import lru_cache
|
from functools import lru_cache
|
||||||
@@ -10,10 +12,22 @@ from pydantic import PostgresDsn, field_validator
|
|||||||
from pydantic_core.core_schema import FieldValidationInfo
|
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):
|
class Settings(BaseSettings):
|
||||||
"""应用配置类"""
|
"""应用配置类"""
|
||||||
|
|
||||||
RTSP_ENABLED: bool = True
|
yaml_config: dict = load_yaml_config()
|
||||||
|
|
||||||
|
RTSP_ENABLED: bool = yaml_config.get('rtsp_enabled', False)
|
||||||
|
|
||||||
# API配置
|
# API配置
|
||||||
API_V1_PREFIX: str = "/api/v1"
|
API_V1_PREFIX: str = "/api/v1"
|
||||||
@@ -24,40 +38,41 @@ class Settings(BaseSettings):
|
|||||||
|
|
||||||
|
|
||||||
# 数据库配置
|
# 数据库配置
|
||||||
DATABASE_HOST: str = "localhost"
|
DATABASE_HOST: str = yaml_config.get('database', {}).get('host', "localhost")
|
||||||
DATABASE_PORT: int = 5432
|
DATABASE_PORT: int = yaml_config.get('database', {}).get('port', 5432)
|
||||||
DATABASE_USER: str = "postgres"
|
DATABASE_USER: str = yaml_config.get('database', {}).get('user', "postgres")
|
||||||
DATABASE_PASSWORD: str = "yipai123"
|
DATABASE_PASSWORD: str = yaml_config.get('database', {}).get('password', "yipai123")
|
||||||
DATABASE_NAME: str = "pmms"
|
DATABASE_NAME: str = yaml_config.get('database', {}).get('name', "pmms")
|
||||||
DATABASE_SCHEMA: str = "public"
|
DATABASE_SCHEMA: str = yaml_config.get('database', {}).get('schema', "public")
|
||||||
|
|
||||||
# 连接池配置
|
# 连接池配置
|
||||||
DATABASE_POOL_SIZE: int = 10
|
DATABASE_POOL_SIZE: int = yaml_config.get('database', {}).get('pool_size', 10)
|
||||||
DATABASE_MAX_OVERFLOW: int = 20
|
DATABASE_MAX_OVERFLOW: int = yaml_config.get('database', {}).get('max_overflow', 20)
|
||||||
DATABASE_POOL_RECYCLE: int = 3600 # 连接回收时间(秒)
|
DATABASE_POOL_RECYCLE: int = yaml_config.get('database', {}).get('pool_recycle', 3600) # 连接回收时间(秒)
|
||||||
DATABASE_ECHO: bool = False # SQL日志,生产环境设为False
|
DATABASE_ECHO: bool = yaml_config.get('database', {}).get('echo', False) # SQL日志,生产环境设为False
|
||||||
|
|
||||||
# 应用配置
|
# 应用配置
|
||||||
APP_NAME: str = "SurFaceFeature API"
|
APP_NAME: str = "SurFaceFeature API"
|
||||||
APP_VERSION: str = "1.0.0"
|
APP_VERSION: str = "1.0.0"
|
||||||
DEBUG: bool = False
|
|
||||||
|
DEBUG: bool = yaml_config.get('app', {}).get('debug', False)
|
||||||
|
|
||||||
# 日志配置
|
# 日志配置
|
||||||
LOG_LEVEL: str = "INFO"
|
LOG_LEVEL: str = yaml_config.get('app', {}).get('log_level', "INFO")
|
||||||
LOG_FILE: Optional[str] = None
|
LOG_FILE: Optional[str] = yaml_config.get('app', {}).get('log_file')
|
||||||
|
|
||||||
# 异步配置
|
# 异步配置
|
||||||
ASYNC_MODE: bool = False
|
ASYNC_MODE: bool = False
|
||||||
|
|
||||||
# 资源文件夹配置
|
# 资源文件夹配置
|
||||||
FACE_REGISTER_IMAGE_RESOURCE_DIR: str = "D:/ruoyi/uploadPath/face"
|
FACE_REGISTER_IMAGE_RESOURCE_DIR: str = yaml_config.get('face', {}).get('face_register_image_dir', "D:/ruoyi/uploadPath/face")
|
||||||
VIDEO_RESOURCE_DIR: str = "D:/ruoyi/uploadPath/video"
|
VIDEO_RESOURCE_DIR: str = yaml_config.get('face', {}).get('video_dir', "D:/ruoyi/uploadPath/video")
|
||||||
FACE_CAL_FEATURE_TIMEOUT_HOURS: int = 10
|
FACE_CAL_FEATURE_TIMEOUT_HOURS: int = yaml_config.get('face', {}).get('face_cal_feature_timeout_hours', 10)
|
||||||
FACE_MODEL_VERSION: int = 0 #insight_face_buffalo_l
|
FACE_MODEL_VERSION: int = yaml_config.get('face', {}).get('face_model_version', 0) #insight_face_buffalo_l
|
||||||
FACE_USE_GPU: bool = True
|
FACE_USE_GPU: bool = yaml_config.get('face', {}).get('face_use_gpu', True)
|
||||||
FACE_USE_NPU: bool = False
|
FACE_USE_NPU: bool = yaml_config.get('face', {}).get('face_use_npu', False)
|
||||||
SUR_CONFIG_TYPE_FACE: int = 0
|
SUR_CONFIG_TYPE_FACE: int = yaml_config.get('face', {}).get('sur_config_type_face', 0)
|
||||||
SUR_CONFIG_SCOPE_GLOBAL: int = 0
|
SUR_CONFIG_SCOPE_GLOBAL: int = yaml_config.get('face', {}).get('sur_config_scope_global', 0)
|
||||||
|
|
||||||
# JWT配置(预留)
|
# JWT配置(预留)
|
||||||
SECRET_KEY: str = "your-secret-key-here-change-in-production"
|
SECRET_KEY: str = "your-secret-key-here-change-in-production"
|
||||||
|
|||||||
32
config.yaml
32
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:
|
cameras:
|
||||||
- id: 1
|
- id: 1
|
||||||
index: testindexcode
|
index: testindexcode
|
||||||
|
|||||||
Reference in New Issue
Block a user