""" 应用配置文件 按领域驱动的配置管理设计 """ from pydantic_settings import BaseSettings from pydantic import ConfigDict from typing import Optional, List from functools import lru_cache import os class ServerSettings(BaseSettings): """服务器配置""" model_config = ConfigDict(env_prefix="SERVER_", env_file="D:\work\code\python\demo\demo07\backend\.env") HOST: str = "0.0.0.0" PORT: int = 8000 WORKERS: int = 1 LOG_LEVEL: str = "info" class DatabaseSettings(BaseSettings): """数据库配置""" model_config = ConfigDict(env_prefix="DB_", env_file="D:\work\code\python\demo\demo07\backend\.env") # MySQL连接格式: mysql+pymysql://username:password@host:port/database_name?charset=utf8mb4 DATABASE_URL: str = "mysql+pymysql://root:taiyi1224@localhost:3306/baoxiang?charset=utf8mb4" # SQLite开发配置 (可选) # SQLite连接格式: sqlite:///./database.db # DATABASE_URL: str = "sqlite:///./treasure_box_game.db" # 数据库连接池配置 DB_POOL_SIZE: int = 20 DB_MAX_OVERFLOW: int = 30 DB_POOL_TIMEOUT: int = 30 DB_POOL_RECYCLE: int = 3600 class SecuritySettings(BaseSettings): """安全配置""" model_config = ConfigDict(env_prefix="SECURITY_", env_file="D:\work\code\python\demo\demo07\backend\.env") SECRET_KEY: str = "your-secret-key-change-in-production" ALGORITHM: str = "HS256" ACCESS_TOKEN_EXPIRE_MINUTES: int = 10080 # 7天 PASSWORD_MAX_LENGTH: int = 72 # 密码哈希最大长度 WS_CLOSE_CODE: int = 4001 # WebSocket关闭码 class RedisSettings(BaseSettings): """Redis配置""" model_config = ConfigDict(env_prefix="REDIS_", env_file="D:\work\code\python\demo\demo07\backend\.env") REDIS_HOST: str = "localhost" REDIS_PORT: int = 6379 REDIS_DB: int = 0 REDIS_PASSWORD: Optional[str] = None REDIS_TIMEOUT: int = 5 class CORSSettings(BaseSettings): """CORS跨域配置""" model_config = ConfigDict(env_prefix="CORS_", env_file="D:\work\code\python\demo\demo07\backend\.env") ALLOW_ORIGINS: str = "http://localhost:3000,http://127.0.0.1:3000,http://localhost:3001,http://127.0.0.1:3001,http://bx.youxidaren.cn,https://bx.youxidaren.cn,http://199.68.217.236:3000,http://199.68.217.236:3001,http://199.68.217.236:8000,*" ALLOW_CREDENTIALS: bool = True ALLOW_METHODS: str = "GET,POST,PUT,DELETE,PATCH,OPTIONS" ALLOW_HEADERS: str = "Authorization,Content-Type,Accept,X-Requested-With" def get_allow_origins(self) -> List[str]: """解析允许的源列表""" return [origin.strip() for origin in self.ALLOW_ORIGINS.split(",")] def get_allow_methods(self) -> List[str]: """解析允许的方法列表""" return [method.strip() for method in self.ALLOW_METHODS.split(",")] def get_allow_headers(self) -> List[str]: """解析允许的头部列表""" return [header.strip() for header in self.ALLOW_HEADERS.split(",")] class PaginationSettings(BaseSettings): """分页配置""" model_config = ConfigDict(env_prefix="PAGINATION_", env_file="D:\work\code\python\demo\demo07\backend\.env") DEFAULT_PAGE_SIZE: int = 20 MAX_PAGE_SIZE: int = 100 ADMIN_PAGE_SIZE: int = 20 ANNOUNCEMENT_PAGE_SIZE: int = 10 ANNOUNCEMENT_LIMIT: int = 5 USER_LIST_LIMIT: int = 100 STREAMER_LIST_LIMIT: int = 100 class GameSettings(BaseSettings): """游戏配置""" model_config = ConfigDict(env_prefix="GAME_", env_file="D:\work\code\python\demo\demo07\backend\.env") # 经济配置 NEW_USER_REWARD: int = 100000 # 新用户注册奖励(分) DAILY_ALLOWANCE: int = 5000 # 每日低保(分) ALLOWANCE_THRESHOLD: int = 1000 # 低保门槛(分) # 抽水配置 HOUSE_EDGE: float = 0.10 # 总抽水10% STREAMER_SHARE: float = 0.05 # 主播分润5% PLATFORM_SHARE: float = 0.05 # 平台分润5% # 游戏逻辑配置 DEFAULT_COUNTDOWN: int = 300 # 默认倒计时(秒) MIN_COUNTDOWN: int = 10 # 最小倒计时(秒) MAX_COUNTDOWN: int = 3600 # 最大倒计时(秒) DEFAULT_MAX_ACTIVE_CHESTS: int = 10 # 默认最大活跃宝箱数 MIN_COMMISSION_RATE: float = 0.0 # 最小主播抽成(%) MAX_COMMISSION_RATE: float = 100.0 # 最大主播抽成(%) DEFAULT_COMMISSION_RATE: float = 5.0 # 默认主播抽成(%) class WebSocketSettings(BaseSettings): """WebSocket配置""" model_config = ConfigDict(env_prefix="WS_", env_file="D:\work\code\python\demo\demo07\backend\.env") BROADCAST_INTERVAL: int = 200 # 广播间隔(毫秒) HEARTBEAT_INTERVAL: int = 30 # 心跳间隔(秒) CONNECTION_TIMEOUT: int = 60 # 连接超时(秒) class AdminSettings(BaseSettings): """管理员配置""" model_config = ConfigDict(env_prefix="ADMIN_", env_file="D:\work\code\python\demo\demo07\backend\.env") DEFAULT_ADMIN_PASSWORD: str = "admin123" DEFAULT_STREAMER_PASSWORD: str = "streamer123" DEFAULT_USER_PASSWORD: str = "user123" DEFAULT_ADMIN_BALANCE: int = 1000000 # 默认管理员余额(分) DEFAULT_STREAMER_BALANCE: int = 500000 # 默认主播余额(分) DEFAULT_USER_BALANCE: int = 200000 # 默认用户余额(分) class AnnouncementSettings(BaseSettings): """公告配置""" model_config = ConfigDict(env_prefix="ANNOUNCEMENT_", env_file="D:\work\code\python\demo\demo07\backend\.env") DEFAULT_PRIORITY: int = 0 MAX_PRIORITY: int = 100 HIGH_PRIORITY: int = 100 class Settings(BaseSettings): """ 根配置类 - 组合所有子配置 """ model_config = ConfigDict( env_file="./.env", extra='ignore' # 忽略.env文件中未定义的变量 ) server: ServerSettings = ServerSettings() database: DatabaseSettings = DatabaseSettings() security: SecuritySettings = SecuritySettings() redis: RedisSettings = RedisSettings() cors: CORSSettings = CORSSettings() pagination: PaginationSettings = PaginationSettings() game: GameSettings = GameSettings() websocket: WebSocketSettings = WebSocketSettings() admin: AdminSettings = AdminSettings() announcement: AnnouncementSettings = AnnouncementSettings() @lru_cache() def get_settings() -> Settings: """获取配置实例 (单例模式 + 缓存)""" return Settings() # 为了向后兼容,提供全局settings实例 settings = get_settings()