| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- """
- Application configuration module.
- Manages OAuth/SSO settings from YAML configuration file.
- Supports dev/prod environments via APP_ENV environment variable.
- """
- import os
- import logging
- import yaml
- from pathlib import Path
- logger = logging.getLogger(__name__)
- def get_config_path() -> Path:
- """
- 根据 APP_ENV 环境变量获取配置文件路径
- APP_ENV=prod -> config.prod.yaml
- APP_ENV=dev -> config.dev.yaml
- 默认 -> config.dev.yaml
- """
- app_env = os.getenv("APP_ENV", "").lower()
- base_path = Path(__file__).parent
- if app_env == "prod":
- config_file = base_path / "config/config.prod.yaml"
- logger.info("使用生产环境配置: config.prod.yaml")
- elif app_env == "test":
- config_file = base_path / "config/config.test.yaml"
- logger.info("使用测试环境配置: config.test.yaml")
- elif app_env == "dev":
- config_file = base_path / "config/config.dev.yaml"
- logger.info("使用开发环境配置: config.dev.yaml")
- else:
- print("默认使用开发环境")
- config_file = base_path / "config/config.dev.yaml"
- if app_env:
- logger.warning(f"未知的 APP_ENV 值: {app_env},使用默认 config.dev.yaml")
- return config_file
- class Settings:
- """Application settings loaded from config YAML."""
- def __init__(self):
- """Load configuration from YAML file."""
- config_path = get_config_path()
- if not config_path.exists():
- raise FileNotFoundError(f"配置文件不存在: {config_path}")
- with open(config_path, 'r', encoding='utf-8') as f:
- config = yaml.safe_load(f)
- self.APP_ENV = os.getenv("APP_ENV", "default").lower()
- print(f"[Config] APP_ENV={self.APP_ENV}, 配置文件={config_path}")
- # Database Settings (MySQL only)
- db_config = config.get('database', {})
- mysql_config = db_config.get('mysql', {})
- self.MYSQL_HOST = mysql_config.get('host', 'localhost')
- self.MYSQL_PORT = mysql_config.get('port', 3306)
- self.MYSQL_USER = mysql_config.get('user', 'root')
- self.MYSQL_PASSWORD = mysql_config.get('password', '')
- self.MYSQL_DATABASE = mysql_config.get('database', 'annotation_platform')
- # SSO/SSO Settings
- sso_config = config.get('sso', {})
- self.SSO_ENABLED = sso_config.get('enabled', True)
- self.SSO_BASE_URL = sso_config.get('base_url', '')
- self.SSO_CLIENT_ID = sso_config.get('client_id', '')
- self.SSO_CLIENT_SECRET = sso_config.get('client_secret', '')
- self.SSO_REDIRECT_URI = sso_config.get('redirect_uri', '')
- self.SSO_SCOPE = sso_config.get('scope', 'profile email')
- self.SSO_LOGOUT_REDIRECT_URL = sso_config.get('logout_redirect_url', '')
- self.SSO_FRONTEND_URL = sso_config.get('frontend_url', '')
- # SSO Endpoints
- self.SSO_AUTHORIZE_ENDPOINT = sso_config.get('authorize_endpoint', '/oauth/authorize')
- self.SSO_TOKEN_ENDPOINT = sso_config.get('token_endpoint', '/oauth/token')
- self.SSO_USERINFO_ENDPOINT = sso_config.get('userinfo_endpoint', '/oauth/userinfo')
- self.SSO_REVOKE_ENDPOINT = sso_config.get('revoke_endpoint', '/oauth/revoke')
- # Server Settings
- server_config = config.get('server', {})
- self.SERVER_HOST = server_config.get('host', '0.0.0.0')
- self.SERVER_PORT = server_config.get('port', 8000)
- self.SERVER_RELOAD = server_config.get('reload', True)
- # JWT Settings (with defaults for backward compatibility)
- jwt_config = config.get('jwt', {})
- self.JWT_SECRET_KEY = jwt_config.get('secret_key', 'your-secret-key-change-in-production')
- self.JWT_ALGORITHM = jwt_config.get('algorithm', 'HS256')
- self.JWT_ACCESS_TOKEN_EXPIRE_MINUTES = jwt_config.get('access_token_expire_minutes', 30)
- # Create settings instance
- settings = Settings()
|