config.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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" # DEV (测试/开发环境) | PROD (生产正式环境)
  10. debug: bool = False
  11. # -----------------------
  12. # Database / Cache
  13. # -----------------------
  14. database_url: str = Field(..., description="Async database DSN")
  15. redis_url: str
  16. @property
  17. def is_prod(self) -> bool:
  18. """全局判断是否为生产环境"""
  19. return str(self.env).strip().upper() in ("PROD", "PRODUCTION", "LIVE")
  20. @property
  21. def is_dev(self) -> bool:
  22. """全局判断是否为测试/开发环境"""
  23. return not self.is_prod
  24. # -----------------------
  25. # Stripe 配置 (生产 / 测试)
  26. # -----------------------
  27. stripe_prod_api_key: str = Field("", description="Stripe Production API Key")
  28. stripe_prod_webhook_secret: str = Field("", description="Stripe Production Webhook Secret")
  29. stripe_test_api_key: str = Field("", description="Stripe Test API Key")
  30. stripe_test_webhook_secret: str = Field("", description="Stripe Test Webhook Secret")
  31. # -----------------------
  32. # Creem 配置 (生产 / 测试)
  33. # -----------------------
  34. creem_prod_api_key: str = Field("", description="Creem Production API Key")
  35. creem_prod_webhook_secret: str = Field("", description="Creem Production Webhook Secret")
  36. creem_prod_base_url: str = Field("https://api.creem.io", description="Creem Production Base URL")
  37. creem_prod_product_id: str = Field("", description="Creem Production Product ID")
  38. creem_test_api_key: str = Field("", description="Creem Test API Key")
  39. creem_test_webhook_secret: str = Field("", description="Creem Test Webhook Secret")
  40. creem_test_base_url: str = Field("https://test-api.creem.io", description="Creem Test Base URL")
  41. creem_test_product_id: str = Field("", description="Creem Test Product ID")
  42. # -----------------------
  43. # 确定性环境属性(根据 ENV 确定使用生产还是测试值)
  44. # -----------------------
  45. @property
  46. def active_stripe_api_key(self) -> str:
  47. return self.stripe_prod_api_key if self.is_prod else self.stripe_test_api_key
  48. @property
  49. def active_stripe_webhook_secret(self) -> str:
  50. return self.stripe_prod_webhook_secret if self.is_prod else self.stripe_test_webhook_secret
  51. @property
  52. def active_creem_api_key(self) -> str:
  53. return self.creem_prod_api_key if self.is_prod else self.creem_test_api_key
  54. @property
  55. def active_creem_webhook_secret(self) -> str:
  56. return self.creem_prod_webhook_secret if self.is_prod else self.creem_test_webhook_secret
  57. @property
  58. def active_creem_base_url(self) -> str:
  59. if self.is_prod:
  60. return (self.creem_prod_base_url or "https://api.creem.io").rstrip("/")
  61. return (self.creem_test_base_url or "https://test-api.creem.io").rstrip("/")
  62. @property
  63. def active_creem_product_id(self) -> str:
  64. return self.creem_prod_product_id if self.is_prod else self.creem_test_product_id
  65. telegram_api_token: str = Field("", description="Telegram bot API token")
  66. wechat_api_token: str = Field("", description="Wechat webhook API token")
  67. whatsapp_api_base_url: str = Field("https://waha.text.skin", description="Whatsapp API base url")
  68. whatsapp_api_key: str = Field("", description="Whatsapp API key")
  69. whatsapp_session: str = Field("default", description="Whatsapp session")
  70. # -----------------------
  71. # Storage (SeaweedFS / R2) & Network
  72. # -----------------------
  73. api_base_url: str = Field("https://api.text.skin", description="Public API base URL for resource links")
  74. seaweedfs_master_url: str = Field("http://seaweedfs-master:9333", description="SeaweedFS Master URL")
  75. seaweedfs_volume_override_host: str = Field("", description="Optional host/IP to override SeaweedFS Volume address for cross-network access")
  76. # Cloudflare R2
  77. r2_account_id: str = Field("f97ce2711cc348200b0de273e48f2230", description="Cloudflare Account ID")
  78. r2_access_key_id: str = Field("", description="Cloudflare R2 Access Key ID")
  79. r2_secret_access_key: str = Field("", description="Cloudflare R2 Secret Access Key")
  80. r2_endpoint_url: str = Field(
  81. "https://f97ce2711cc348200b0de273e48f2230.r2.cloudflarestorage.com",
  82. description="Cloudflare R2 S3 Endpoint URL",
  83. )
  84. r2_bucket_name: str = Field("", description="Default Cloudflare R2 Bucket Name")
  85. r2_public_domain: str = Field("", description="R2 Public Domain for accessing files (e.g. https://pub-xxx.r2.dev or custom domain)")
  86. # -----------------------
  87. # LLM Configuration (OpenAI-compatible)
  88. # -----------------------
  89. llm_api_key: str = Field("", description="LLM API Key")
  90. llm_base_url: str = Field("https://api.deepseek.com", description="OpenAI-compatible Base URL")
  91. llm_model: str = Field("deepseek-v4-flash", description="Default model name, e.g., deepseek-v4-flash, deepseek-v4-pro")
  92. llm_temperature: float = Field(0.0, description="LLM sampling temperature")
  93. llm_timeout_seconds: int = Field(60, description="LLM request timeout in seconds")
  94. model_config = SettingsConfigDict(
  95. env_file=".env",
  96. env_file_encoding="utf-8",
  97. case_sensitive=False,
  98. extra="ignore",
  99. )
  100. @lru_cache
  101. def get_settings() -> Settings:
  102. """
  103. 避免多次实例化 Settings(FastAPI 官方推荐)
  104. """
  105. return Settings()
  106. settings = get_settings()