from pydantic_settings import BaseSettings from typing import Optional from urllib.parse import quote_plus class PostgreSQLSettings(BaseSettings): """PostgreSQL数据库配置""" host: str = "postgres" port: int = 5432 user: str = "admin" password: str = "PostgresAdmin@123" database: str = "booksystem" min_size: int = 5 max_size: int = 20 @property def url(self) -> str: """生成数据库连接URL""" encoded_password = quote_plus(self.password) return f"postgresql+asyncpg://{self.user}:{encoded_password}@{self.host}:{self.port}/{self.database}" class Config: env_prefix = "POSTGRES_" class RedisSettings(BaseSettings): """Redis配置""" host: str = "redis" port: int = 6379 password: Optional[str] = "RedisAdmin@123" db: int = 0 @property def url(self) -> str: """生成Redis连接URL""" if self.password: return f"redis://:{self.password}@{self.host}:{self.port}/{self.db}" return f"redis://{self.host}:{self.port}/{self.db}" class Config: env_prefix = "REDIS_" class AppSettings(BaseSettings): """应用配置""" name: str = "Book System API" version: str = "1.0.0" debug: bool = False secret_key: str = "BookSystemAPIQRAdmin123" algorithm: str = "HS256" access_token_expire_minutes: int = 10080 class Config: env_prefix = "APP_" # 创建设置实例 postgres_settings = PostgreSQLSettings() redis_settings = RedisSettings() app_settings = AppSettings()