config.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. """
  2. Application configuration module.
  3. Manages OAuth/SSO settings from YAML configuration file.
  4. Supports dev/prod environments via APP_ENV environment variable.
  5. """
  6. import os
  7. import logging
  8. import yaml
  9. from pathlib import Path
  10. logger = logging.getLogger(__name__)
  11. def get_config_path() -> Path:
  12. """
  13. 根据 APP_ENV 环境变量获取配置文件路径
  14. APP_ENV=prod -> config.prod.yaml
  15. APP_ENV=dev -> config.dev.yaml
  16. 默认 -> config.dev.yaml
  17. """
  18. app_env = os.getenv("APP_ENV", "").lower()
  19. base_path = Path(__file__).parent
  20. if app_env == "prod":
  21. config_file = base_path / "config.prod.yaml"
  22. logger.info("使用生产环境配置: config.prod.yaml")
  23. elif app_env == "dev":
  24. config_file = base_path / "config.dev.yaml"
  25. logger.info("使用开发环境配置: config.dev.yaml")
  26. else:
  27. print("默认使用开发环境")
  28. config_file = base_path / "config.dev.yaml"
  29. if app_env:
  30. logger.warning(f"未知的 APP_ENV 值: {app_env},使用默认 config.dev.yaml")
  31. return config_file
  32. class Settings:
  33. """Application settings loaded from config YAML."""
  34. def __init__(self):
  35. """Load configuration from YAML file."""
  36. config_path = get_config_path()
  37. if not config_path.exists():
  38. raise FileNotFoundError(f"配置文件不存在: {config_path}")
  39. with open(config_path, 'r', encoding='utf-8') as f:
  40. config = yaml.safe_load(f)
  41. self.APP_ENV = os.getenv("APP_ENV", "default").lower()
  42. print(f"[Config] APP_ENV={self.APP_ENV}, 配置文件={config_path}")
  43. # Database Settings (MySQL only)
  44. db_config = config.get('database', {})
  45. mysql_config = db_config.get('mysql', {})
  46. self.MYSQL_HOST = mysql_config.get('host', 'localhost')
  47. self.MYSQL_PORT = mysql_config.get('port', 3306)
  48. self.MYSQL_USER = mysql_config.get('user', 'root')
  49. self.MYSQL_PASSWORD = mysql_config.get('password', '')
  50. self.MYSQL_DATABASE = mysql_config.get('database', 'annotation_platform')
  51. # OAuth/SSO Settings
  52. oauth_config = config.get('oauth', {})
  53. self.OAUTH_ENABLED = oauth_config.get('enabled', False)
  54. self.OAUTH_BASE_URL = oauth_config.get('base_url', '')
  55. self.OAUTH_CLIENT_ID = oauth_config.get('client_id', '')
  56. self.OAUTH_CLIENT_SECRET = oauth_config.get('client_secret', '')
  57. self.OAUTH_REDIRECT_URI = oauth_config.get('redirect_uri', '')
  58. self.OAUTH_SCOPE = oauth_config.get('scope', 'profile email')
  59. # OAuth Endpoints
  60. self.OAUTH_AUTHORIZE_ENDPOINT = oauth_config.get('authorize_endpoint', '/oauth/authorize')
  61. self.OAUTH_TOKEN_ENDPOINT = oauth_config.get('token_endpoint', '/oauth/token')
  62. self.OAUTH_USERINFO_ENDPOINT = oauth_config.get('userinfo_endpoint', '/oauth/userinfo')
  63. self.OAUTH_REVOKE_ENDPOINT = oauth_config.get('revoke_endpoint', '/oauth/revoke')
  64. # Token Cache TTL (seconds)
  65. self.TOKEN_CACHE_TTL = oauth_config.get('token_cache_ttl', 300)
  66. # Server Settings
  67. server_config = config.get('server', {})
  68. self.SERVER_HOST = server_config.get('host', '0.0.0.0')
  69. self.SERVER_PORT = server_config.get('port', 8000)
  70. self.SERVER_RELOAD = server_config.get('reload', True)
  71. # Create settings instance
  72. settings = Settings()