config.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. """
  2. Application configuration module.
  3. Manages JWT and OAuth settings from YAML configuration file.
  4. Supports dev/prod environments via APP_ENV environment variable.
  5. """
  6. import os
  7. import secrets
  8. import logging
  9. import yaml
  10. from pathlib import Path
  11. from typing import Dict, Any
  12. logger = logging.getLogger(__name__)
  13. def get_config_path() -> Path:
  14. """
  15. 根据 APP_ENV 环境变量获取配置文件路径
  16. APP_ENV=prod -> config.prod.yaml
  17. APP_ENV=dev -> config.dev.yaml
  18. 默认 -> config.yaml (兼容旧配置)
  19. """
  20. app_env = os.getenv("APP_ENV", "").lower()
  21. base_path = Path(__file__).parent
  22. if app_env == "prod":
  23. config_file = base_path / "config.prod.yaml"
  24. logger.info("使用生产环境配置: config.prod.yaml")
  25. elif app_env == "dev":
  26. config_file = base_path / "config.dev.yaml"
  27. logger.info("使用开发环境配置: config.dev.yaml")
  28. else:
  29. # 兼容旧的 config.yaml
  30. print("默认使用开发环境")
  31. config_file = base_path / "config.dev.yaml"
  32. if app_env:
  33. logger.warning(f"未知的 APP_ENV 值: {app_env},使用默认 config.yaml")
  34. return config_file
  35. class Settings:
  36. """Application settings loaded from config.yaml."""
  37. def __init__(self):
  38. """Load configuration from YAML file."""
  39. config_path = get_config_path()
  40. if not config_path.exists():
  41. raise FileNotFoundError(f"配置文件不存在: {config_path}")
  42. with open(config_path, 'r', encoding='utf-8') as f:
  43. config = yaml.safe_load(f)
  44. # 记录当前环境(统一转小写)
  45. self.APP_ENV = os.getenv("APP_ENV", "default").lower()
  46. print(f"[Config] APP_ENV={self.APP_ENV}, 配置文件={config_path}")
  47. # JWT Settings
  48. jwt_config = config.get('jwt', {})
  49. self.JWT_SECRET_KEY = jwt_config.get('secret_key', secrets.token_urlsafe(32))
  50. self.JWT_ALGORITHM = jwt_config.get('algorithm', 'HS256')
  51. self.ACCESS_TOKEN_EXPIRE_MINUTES = jwt_config.get('access_token_expire_minutes', 15)
  52. self.REFRESH_TOKEN_EXPIRE_DAYS = jwt_config.get('refresh_token_expire_days', 7)
  53. # Database Settings
  54. db_config = config.get('database', {})
  55. self.DATABASE_TYPE = db_config.get('type', 'sqlite')
  56. self.DATABASE_PATH = db_config.get('path', 'annotation_platform.db')
  57. # MySQL Settings
  58. mysql_config = db_config.get('mysql', {})
  59. self.MYSQL_HOST = mysql_config.get('host', 'localhost')
  60. self.MYSQL_PORT = mysql_config.get('port', 3306)
  61. self.MYSQL_USER = mysql_config.get('user', 'root')
  62. self.MYSQL_PASSWORD = mysql_config.get('password', '')
  63. self.MYSQL_DATABASE = mysql_config.get('database', 'annotation_platform')
  64. # OAuth Settings
  65. oauth_config = config.get('oauth', {})
  66. self.OAUTH_ENABLED = oauth_config.get('enabled', False)
  67. self.OAUTH_BASE_URL = oauth_config.get('base_url', '')
  68. self.OAUTH_CLIENT_ID = oauth_config.get('client_id', '')
  69. self.OAUTH_CLIENT_SECRET = oauth_config.get('client_secret', '')
  70. self.OAUTH_REDIRECT_URI = oauth_config.get('redirect_uri', '')
  71. self.OAUTH_SCOPE = oauth_config.get('scope', 'profile email')
  72. # OAuth Endpoints
  73. self.OAUTH_AUTHORIZE_ENDPOINT = oauth_config.get('authorize_endpoint', '/oauth/authorize')
  74. self.OAUTH_TOKEN_ENDPOINT = oauth_config.get('token_endpoint', '/oauth/token')
  75. self.OAUTH_USERINFO_ENDPOINT = oauth_config.get('userinfo_endpoint', '/oauth/userinfo')
  76. self.OAUTH_REVOKE_ENDPOINT = oauth_config.get('revoke_endpoint', '/oauth/revoke')
  77. # Server Settings
  78. server_config = config.get('server', {})
  79. self.SERVER_HOST = server_config.get('host', '0.0.0.0')
  80. self.SERVER_PORT = server_config.get('port', 8000)
  81. self.SERVER_RELOAD = server_config.get('reload', True)
  82. # Warn if using default JWT secret in production
  83. if self.APP_ENV == "prod" and self.JWT_SECRET_KEY in ['your-secret-key-here', 'CHANGE_THIS_TO_A_SECURE_RANDOM_KEY']:
  84. logger.warning("生产环境使用默认 JWT_SECRET_KEY,请立即修改 config.prod.yaml!")
  85. elif self.JWT_SECRET_KEY == 'your-secret-key-here':
  86. logger.warning(f"使用默认 JWT_SECRET_KEY,生产环境请修改配置文件!(当前环境: {self.APP_ENV})")
  87. # Create settings instance
  88. settings = Settings()