""" 配置管理模块 从环境变量加载配置 """ import os from pathlib import Path from typing import Optional from pydantic_settings import BaseSettings from functools import lru_cache # 查找.env文件的位置 def find_env_file(): """查找.env文件,支持从backend目录或项目根目录启动""" current_dir = Path.cwd() # 尝试当前目录 env_path = current_dir / ".env" if env_path.exists(): print(f"[Config] 找到.env文件: {env_path}") return str(env_path) # 尝试父目录(项目根目录) env_path = current_dir.parent / ".env" if env_path.exists(): print(f"[Config] 找到.env文件: {env_path}") return str(env_path) # 尝试backend的父目录 if current_dir.name == "backend": env_path = current_dir.parent / ".env" if env_path.exists(): print(f"[Config] 找到.env文件: {env_path}") return str(env_path) # 尝试从环境变量指定的路径 if os.getenv('ENV_FILE'): env_path = Path(os.getenv('ENV_FILE')) if env_path.exists(): print(f"[Config] 从ENV_FILE环境变量找到.env文件: {env_path}") return str(env_path) print(f"[Config] 警告:未找到.env文件,当前目录: {current_dir}") # 默认返回当前目录的.env return ".env" class Settings(BaseSettings): """应用配置""" # Tushare配置 tushare_token: str = "" # LLM配置 zhipuai_api_key: str = "" deepseek_api_key: str = "" # 数据库配置 database_url: str = "sqlite:///./stock_agent.db" # API配置 api_host: str = "0.0.0.0" api_port: int = 8000 debug: bool = True # 安全配置 secret_key: str = "change-this-secret-key-in-production" rate_limit: str = "100/minute" # JWT配置 jwt_algorithm: str = "HS256" jwt_expire_days: int = 7 # 腾讯云短信配置 tencent_sms_app_id: str = "1400961527" tencent_sms_secret_id: str = "AKIDxnbGj281iHtKallqqzvlV5YxBCrPltnS" # 腾讯云SecretId tencent_sms_secret_key: str = "ta6PXTMBsX7dzA7IN6uYUFn8F9uTovoU" # 腾讯云SecretKey tencent_sms_sign_id: str = "629073" tencent_sms_template_id: str = "2353142" # 验证码配置 code_expire_minutes: int = 5 code_resend_seconds: int = 60 code_max_per_hour: int = 10 # 白名单手机号(无需验证码即可登录) whitelist_phones: str = "18583366860,18583926860" # CORS配置 cors_origins: str = "http://localhost:8000,http://127.0.0.1:8000" # Binance 配置(公开数据不需要 API 密钥) binance_api_key: str = "" binance_api_secret: str = "" # 飞书机器人配置 feishu_webhook_url: str = "https://open.feishu.cn/open-apis/bot/v2/hook/8a1dcf69-6753-41e2-a393-edc4f7822db0" feishu_enabled: bool = True # 是否启用飞书通知 # Telegram 机器人配置 telegram_bot_token: str = "" # 从 @BotFather 获取 telegram_channel_id: str = "" # 频道 ID,如 @your_channel 或 -1001234567890 telegram_enabled: bool = True # 是否启用 Telegram 通知 # 加密货币交易智能体配置 crypto_symbols: str = "BTCUSDT,ETHUSDT" # 监控的交易对,逗号分隔 crypto_analysis_interval: int = 60 # 分析间隔(秒) crypto_llm_threshold: float = 0.7 # 触发 LLM 分析的置信度阈值 # Brave Search API 配置 brave_api_key: str = "" # 模拟交易配置 paper_trading_enabled: bool = True # 是否启用模拟交易 paper_trading_position_a: float = 1000 # A级信号仓位 (USDT) paper_trading_position_b: float = 500 # B级信号仓位 (USDT) paper_trading_position_c: float = 200 # C级信号仓位 (USDT) class Config: env_file = find_env_file() case_sensitive = False @lru_cache() def get_settings() -> Settings: """获取配置单例""" return Settings()