config.py 985 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. debug: bool = False
  10. # -----------------------
  11. # Database / Cache
  12. # -----------------------
  13. database_url: str = Field(..., description="Async database DSN")
  14. redis_url: str
  15. # -----------------------
  16. # Security / API Keys
  17. # -----------------------
  18. openai_api_key: str
  19. stripe_api_key: str
  20. model_config = SettingsConfigDict(
  21. env_file=".env",
  22. env_file_encoding="utf-8",
  23. case_sensitive=False,
  24. )
  25. @lru_cache
  26. def get_settings() -> Settings:
  27. """
  28. 避免多次实例化 Settings(FastAPI 官方推荐)
  29. """
  30. return Settings()
  31. settings = get_settings()
  32. # -----------------------
  33. # Global constants
  34. # -----------------------
  35. BASE_CURRENCY = "EUR"