Files
GitMa/backend/app/config.py
panw cd6bf9bb13 feat: add configuration management
- Add Settings class using pydantic-settings
- Load config from environment variables with GM_ prefix
- Support encrypt_key and api_token (required, no defaults for security)
- Provide defaults for data_dir, host, port
- Add computed properties for db_path, ssh_keys_dir, repos_dir
- Add tests for config defaults and environment variable overrides
- Add Base class to app.models to unblock conftest.py imports

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-30 15:07:55 +08:00

44 lines
986 B
Python

import os
from pathlib import Path
from typing import Literal
from pydantic_settings import BaseSettings, SettingsConfigDict
class Settings(BaseSettings):
"""应用配置,从环境变量加载."""
# 安全配置
encrypt_key: str # AES-256 密钥 (base64)
api_token: str # API 认证 Token
# 路径配置
data_dir: Path = Path('./data')
# 服务器配置
host: str = '0.0.0.0'
port: int = 8000
model_config = SettingsConfigDict(
env_prefix='GM_',
env_file='.env',
env_file_encoding='utf-8',
)
@property
def db_path(self) -> Path:
"""SQLite 数据库路径."""
return self.data_dir / 'git_manager.db'
@property
def ssh_keys_dir(self) -> Path:
"""SSH 密钥存储目录."""
return self.data_dir / 'ssh_keys'
@property
def repos_dir(self) -> Path:
"""仓库镜像存储目录."""
return self.data_dir / 'repos'
settings = Settings()