from functools import lru_cache from pydantic_settings import BaseSettings, SettingsConfigDict from pydantic import Field class Settings(BaseSettings): # ----------------------- # App # ----------------------- app_name: str = "MyApp" env: str = "PROD" # DEV (测试/开发环境) | PROD (生产正式环境) debug: bool = False # ----------------------- # Database / Cache # ----------------------- database_url: str = Field(..., description="Async database DSN") redis_url: str @property def is_prod(self) -> bool: """全局判断是否为生产环境""" return str(self.env).strip().upper() in ("PROD", "PRODUCTION", "LIVE") @property def is_dev(self) -> bool: """全局判断是否为测试/开发环境""" return not self.is_prod # ----------------------- # Stripe 配置 (生产 / 测试) # ----------------------- stripe_prod_api_key: str = Field("", description="Stripe Production API Key") stripe_prod_webhook_secret: str = Field("", description="Stripe Production Webhook Secret") stripe_test_api_key: str = Field("", description="Stripe Test API Key") stripe_test_webhook_secret: str = Field("", description="Stripe Test Webhook Secret") # ----------------------- # Creem 配置 (生产 / 测试) # ----------------------- creem_prod_api_key: str = Field("", description="Creem Production API Key") creem_prod_webhook_secret: str = Field("", description="Creem Production Webhook Secret") creem_prod_base_url: str = Field("https://api.creem.io", description="Creem Production Base URL") creem_prod_product_id: str = Field("", description="Creem Production Product ID") creem_test_api_key: str = Field("", description="Creem Test API Key") creem_test_webhook_secret: str = Field("", description="Creem Test Webhook Secret") creem_test_base_url: str = Field("https://test-api.creem.io", description="Creem Test Base URL") creem_test_product_id: str = Field("", description="Creem Test Product ID") # ----------------------- # 确定性环境属性(根据 ENV 确定使用生产还是测试值) # ----------------------- @property def active_stripe_api_key(self) -> str: return self.stripe_prod_api_key if self.is_prod else self.stripe_test_api_key @property def active_stripe_webhook_secret(self) -> str: return self.stripe_prod_webhook_secret if self.is_prod else self.stripe_test_webhook_secret @property def active_creem_api_key(self) -> str: return self.creem_prod_api_key if self.is_prod else self.creem_test_api_key @property def active_creem_webhook_secret(self) -> str: return self.creem_prod_webhook_secret if self.is_prod else self.creem_test_webhook_secret @property def active_creem_base_url(self) -> str: if self.is_prod: return (self.creem_prod_base_url or "https://api.creem.io").rstrip("/") return (self.creem_test_base_url or "https://test-api.creem.io").rstrip("/") @property def active_creem_product_id(self) -> str: return self.creem_prod_product_id if self.is_prod else self.creem_test_product_id telegram_api_token: str = Field("", description="Telegram bot API token") wechat_api_token: str = Field("", description="Wechat webhook API token") whatsapp_api_base_url: str = Field("https://waha.text.skin", description="Whatsapp API base url") whatsapp_api_key: str = Field("", description="Whatsapp API key") whatsapp_session: str = Field("default", description="Whatsapp session") # ----------------------- # Storage (SeaweedFS / R2) & Network # ----------------------- api_base_url: str = Field("https://api.text.skin", description="Public API base URL for resource links") seaweedfs_master_url: str = Field("http://seaweedfs-master:9333", description="SeaweedFS Master URL") seaweedfs_volume_override_host: str = Field("", description="Optional host/IP to override SeaweedFS Volume address for cross-network access") # Cloudflare R2 r2_account_id: str = Field("f97ce2711cc348200b0de273e48f2230", description="Cloudflare Account ID") r2_access_key_id: str = Field("", description="Cloudflare R2 Access Key ID") r2_secret_access_key: str = Field("", description="Cloudflare R2 Secret Access Key") r2_endpoint_url: str = Field( "https://f97ce2711cc348200b0de273e48f2230.r2.cloudflarestorage.com", description="Cloudflare R2 S3 Endpoint URL", ) r2_bucket_name: str = Field("", description="Default Cloudflare R2 Bucket Name") r2_public_domain: str = Field("", description="R2 Public Domain for accessing files (e.g. https://pub-xxx.r2.dev or custom domain)") # ----------------------- # LLM Configuration (OpenAI-compatible) # ----------------------- llm_api_key: str = Field("", description="LLM API Key") llm_base_url: str = Field("https://api.deepseek.com", description="OpenAI-compatible Base URL") llm_model: str = Field("deepseek-v4-flash", description="Default model name, e.g., deepseek-v4-flash, deepseek-v4-pro") llm_temperature: float = Field(0.0, description="LLM sampling temperature") llm_timeout_seconds: int = Field(60, description="LLM request timeout in seconds") model_config = SettingsConfigDict( env_file=".env", env_file_encoding="utf-8", case_sensitive=False, extra="ignore", ) @lru_cache def get_settings() -> Settings: """ 避免多次实例化 Settings(FastAPI 官方推荐) """ return Settings() settings = get_settings()