from pathlib import Path from pydantic import model_validator from pydantic_settings import BaseSettings, SettingsConfigDict class Settings(BaseSettings): model_config = SettingsConfigDict(env_prefix="HN_", extra="ignore", hide_input_in_errors=True) environment: str = "production" role: str = "connector" redis_url: str = "redis://127.0.0.1:6379/0" data_dir: Path = Path("runtime/connector") database_url: str = "" admin_secret: str = "" internal_secret: str = "" ca_cert: Path = Path("runtime/pki/ca.pem") connector_key: Path = Path("runtime/pki/connector.key") connector_public_key: Path = Path("runtime/pki/connector.pub") public_url: str = "https://relay.example.invalid" router_url: str = "wss://relay.example.invalid/device/ws" device_domain: str = "" feishu_api_base: str = "https://open.feishu.cn" feishu_authorize_url: str = "https://open.feishu.cn/open-apis/authen/v1/authorize" feishu_app_id: str = "" feishu_app_secret: str = "" feishu_encrypt_key: str = "" feishu_verification_token: str = "" feishu_enabled: bool = False proxy_enabled: bool = False # A fixed allowlist, never a user supplied upstream URL. proxy_paths: str = "/open-apis/drive/v1/files,/open-apis/docx/v1/documents" request_timeout: int = 45 max_body: int = 262144 max_pending: int = 1000 max_devices: int = 1000 max_connections: int = 1000 max_result: int = 16000 allow_insecure_local: bool = False @model_validator(mode="after") def validate_runtime(self): if self.environment not in {"production", "test", "development"}: raise ValueError("invalid environment") if self.role == "connector" and min(len(self.admin_secret), len(self.internal_secret)) < 32: raise ValueError("admin/internal secrets must each be at least 32 characters") if self.role == "longconn" and len(self.internal_secret) < 32: raise ValueError("internal credential must be at least 32 characters") if self.role == "connector" and self.admin_secret == self.internal_secret: raise ValueError("admin and internal credentials must be different") if self.environment == "production": if self.allow_insecure_local or not self.public_url.startswith("https://"): raise ValueError("production requires HTTPS") if not self.router_url.startswith("wss://"): raise ValueError("production requires WSS") if not self.feishu_api_base.startswith("https://"): raise ValueError("platform API requires HTTPS") if self.feishu_enabled and not all((self.feishu_app_id, self.feishu_app_secret, self.feishu_encrypt_key, self.feishu_verification_token)): raise ValueError("complete platform credentials are required") return self