config.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. telegram_api_token: str = Field("", description="Telegram bot API token")
  23. wechat_api_token: str = Field("", description="Wechat webhook API token")
  24. whatsapp_api_base_url: str = Field("https://waha.text.skin", description="Whatsapp API base url")
  25. whatsapp_api_key: str = Field("", description="Whatsapp API key")
  26. whatsapp_session: str = Field("default", description="Whatsapp session")
  27. model_config = SettingsConfigDict(
  28. env_file=".env",
  29. env_file_encoding="utf-8",
  30. case_sensitive=False,
  31. )
  32. @lru_cache
  33. def get_settings() -> Settings:
  34. """
  35. 避免多次实例化 Settings(FastAPI 官方推荐)
  36. """
  37. return Settings()
  38. settings = get_settings()