| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- """API 配置模型。
- 集中管理服务运行参数,并提供从环境变量读取配置的能力。
- """
- import os
- from dataclasses import dataclass
- from depth_common import Settings
- @dataclass(frozen=True)
- class ApiConfig:
- """服务配置。
- 字段说明:
- - `sample_count`: 单次测量期望采样帧数;
- - `frame_timeout_ms`: 每次等待帧的超时时间;
- - `sample_timeout_sec`: 整体采样总超时;
- - `max_saved_images`: 本地最多保留图片数量;
- - `request_log_*`: 请求日志相关限制;
- - `api_host` / `api_port`: API 监听地址;
- - `settings`: 深度处理参数集合。
- """
- sample_count: int
- frame_timeout_ms: int
- sample_timeout_sec: int
- max_saved_images: int
- request_log_max_len: int
- request_log_max_bytes: int
- request_log_backup_count: int
- api_host: str
- api_port: int
- settings: Settings
- @classmethod
- def from_env(cls) -> "ApiConfig":
- """从环境变量构建配置,未设置时使用默认值。"""
- return cls(
- sample_count=int(os.getenv("SAMPLE_COUNT", "10")),
- frame_timeout_ms=int(os.getenv("FRAME_TIMEOUT_MS", "200")),
- sample_timeout_sec=int(os.getenv("SAMPLE_TIMEOUT_SEC", "8")),
- max_saved_images=int(os.getenv("MAX_SAVED_IMAGES", "1000")),
- request_log_max_len=int(os.getenv("REQUEST_LOG_MAX_LEN", "1000")),
- request_log_max_bytes=int(os.getenv("REQUEST_LOG_MAX_BYTES", str(20 * 1024 * 1024))),
- request_log_backup_count=int(os.getenv("REQUEST_LOG_BACKUP_COUNT", "10")),
- api_host=os.getenv("API_HOST", "0.0.0.0"),
- api_port=int(os.getenv("API_PORT", "8080")),
- settings=Settings.from_env(),
- )
|