config.py 944 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. from functools import lru_cache
  2. from pydantic_settings import BaseSettings, SettingsConfigDict
  3. from pydantic import Field
  4. class Settings(BaseSettings):
  5. # -----------------------
  6. # App
  7. # -----------------------
  8. app_name: str = "MyApp"
  9. env: str = "PROD"
  10. debug: bool = False
  11. # -----------------------
  12. # Database / Cache
  13. # -----------------------
  14. database_url: str = Field(..., description="Async database DSN")
  15. redis_url: str
  16. # -----------------------
  17. # Security / API Keys
  18. # -----------------------
  19. openai_api_key: str
  20. stripe_api_key: str
  21. stripe_webhook_secret: str
  22. model_config = SettingsConfigDict(
  23. env_file=".env",
  24. env_file_encoding="utf-8",
  25. case_sensitive=False,
  26. )
  27. @lru_cache
  28. def get_settings() -> Settings:
  29. """
  30. 避免多次实例化 Settings(FastAPI 官方推荐)
  31. """
  32. return Settings()
  33. settings = get_settings()