config.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. stripe_api_key: str
  20. stripe_webhook_secret: str
  21. creem_api_key: str = Field("", description="Creem API Key")
  22. creem_webhook_secret: str = Field("", description="Creem Webhook Secret")
  23. creem_base_url: str = Field("https://test-api.creem.io", description="Creem Base URL (or https://api.creem.io)")
  24. creem_product_id: str = Field("", description="Default Creem Product ID (optional)")
  25. telegram_api_token: str = Field("", description="Telegram bot API token")
  26. wechat_api_token: str = Field("", description="Wechat webhook API token")
  27. whatsapp_api_base_url: str = Field("https://waha.text.skin", description="Whatsapp API base url")
  28. whatsapp_api_key: str = Field("", description="Whatsapp API key")
  29. whatsapp_session: str = Field("default", description="Whatsapp session")
  30. # -----------------------
  31. # Storage (SeaweedFS / R2) & Network
  32. # -----------------------
  33. api_base_url: str = Field("https://api.text.skin", description="Public API base URL for resource links")
  34. seaweedfs_master_url: str = Field("http://seaweedfs-master:9333", description="SeaweedFS Master URL")
  35. seaweedfs_volume_override_host: str = Field("", description="Optional host/IP to override SeaweedFS Volume address for cross-network access")
  36. # Cloudflare R2
  37. r2_account_id: str = Field("f97ce2711cc348200b0de273e48f2230", description="Cloudflare Account ID")
  38. r2_access_key_id: str = Field("", description="Cloudflare R2 Access Key ID")
  39. r2_secret_access_key: str = Field("", description="Cloudflare R2 Secret Access Key")
  40. r2_endpoint_url: str = Field(
  41. "https://f97ce2711cc348200b0de273e48f2230.r2.cloudflarestorage.com",
  42. description="Cloudflare R2 S3 Endpoint URL",
  43. )
  44. r2_bucket_name: str = Field("", description="Default Cloudflare R2 Bucket Name")
  45. r2_public_domain: str = Field("", description="R2 Public Domain for accessing files (e.g. https://pub-xxx.r2.dev or custom domain)")
  46. # -----------------------
  47. # LLM Configuration (OpenAI-compatible)
  48. # -----------------------
  49. llm_api_key: str = Field("", description="LLM API Key")
  50. llm_base_url: str = Field("https://api.deepseek.com", description="OpenAI-compatible Base URL")
  51. llm_model: str = Field("deepseek-v4-flash", description="Default model name, e.g., deepseek-v4-flash, deepseek-v4-pro")
  52. llm_temperature: float = Field(0.0, description="LLM sampling temperature")
  53. llm_timeout_seconds: int = Field(60, description="LLM request timeout in seconds")
  54. model_config = SettingsConfigDict(
  55. env_file=".env",
  56. env_file_encoding="utf-8",
  57. case_sensitive=False,
  58. extra="ignore",
  59. )
  60. @lru_cache
  61. def get_settings() -> Settings:
  62. """
  63. 避免多次实例化 Settings(FastAPI 官方推荐)
  64. """
  65. return Settings()
  66. settings = get_settings()