98 lines
2.6 KiB
Python
98 lines
2.6 KiB
Python
"""
|
||
配置管理模块
|
||
从环境变量加载配置
|
||
"""
|
||
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
|
||
|
||
# CORS配置
|
||
cors_origins: str = "http://localhost:8000,http://127.0.0.1:8000"
|
||
|
||
class Config:
|
||
env_file = find_env_file()
|
||
case_sensitive = False
|
||
|
||
|
||
@lru_cache()
|
||
def get_settings() -> Settings:
|
||
"""获取配置单例"""
|
||
return Settings()
|