from functools import lru_cache from pathlib import Path from urllib.parse import quote_plus from pydantic import Field from pydantic_settings import BaseSettings, SettingsConfigDict class Settings(BaseSettings): model_config = SettingsConfigDict(env_file=".env", env_file_encoding="utf-8", extra="ignore") app_env: str = "development" app_timezone: str = "Asia/Shanghai" jwt_secret: str = Field(default="change_me_to_a_long_random_string") token_expire_minutes: int = 60 * 24 * 30 auto_create_tables: bool = False background_tasks_enabled: bool | None = None mysql_host: str = "127.0.0.1" mysql_port: int = 3306 mysql_database: str = "jiaheng_erp" mysql_user: str = "root" mysql_password: str = "" wechat_app_id: str = "" wechat_app_secret: str = "" wechat_qr_env_version: str = "release" public_base_url: str = "http://127.0.0.1:8000" upload_dir: str = "uploads" @property def database_url(self) -> str: user = quote_plus(self.mysql_user) password = quote_plus(self.mysql_password) database = quote_plus(self.mysql_database) return ( f"mysql+pymysql://{user}:{password}@" f"{self.mysql_host}:{self.mysql_port}/{database}?charset=utf8mb4" ) @property def upload_path(self) -> Path: return Path(self.upload_dir).resolve() @lru_cache def get_settings() -> Settings: return Settings() settings = get_settings()