PMFlow/backend/app/core/config.py

59 lines
2.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
核心配置
集中管理环境变量和应用配置,遵循 12-factor 原则。
"""
import os
from pathlib import Path
from typing import Optional
from dotenv import load_dotenv
# 统一从 backend/.env 加载配置,避免因启动目录不同读到错误环境
BACKEND_DIR = Path(__file__).resolve().parents[2]
PROJECT_ROOT = BACKEND_DIR.parent
load_dotenv(BACKEND_DIR / ".env", override=False)
load_dotenv(PROJECT_ROOT / ".env", override=False)
# 默认数据库固定到 backend/neural_sync.db,避免相对路径导致多库混用
DEFAULT_SQLITE_DB = BACKEND_DIR / "neural_sync.db"
DEFAULT_DATABASE_URL = f"sqlite+aiosqlite:///{DEFAULT_SQLITE_DB.as_posix()}"
class Settings:
"""应用配置(从环境变量读取)"""
# 应用
app_name: str = os.getenv("APP_NAME", "Neural Sync API")
debug: bool = os.getenv("DEBUG", "false").lower() in ("true", "1", "yes")
# 数据库
database_url: str = os.getenv("DATABASE_URL", DEFAULT_DATABASE_URL)
db_pool_size: int = int(os.getenv("DB_POOL_SIZE", "20"))
db_max_overflow: int = int(os.getenv("DB_MAX_OVERFLOW", "40"))
db_pool_timeout: int = int(os.getenv("DB_POOL_TIMEOUT", "30"))
db_pool_recycle: int = int(os.getenv("DB_POOL_RECYCLE", "1800"))
# JWT
secret_key: str = os.getenv("SECRET_KEY", "your-secret-key-change-in-production")
algorithm: str = os.getenv("JWT_ALGORITHM", "HS256")
access_token_expire_minutes: int = int(os.getenv("ACCESS_TOKEN_EXPIRE_MINUTES", str(60 * 24 * 7)))
# CORS
cors_origins: str = os.getenv("CORS_ORIGINS", "*")
# LLM
llm_provider: str = os.getenv("LLM_PROVIDER", "ollama").lower()
ollama_host: str = os.getenv("OLLAMA_HOST", "http://127.0.0.1:11434")
business_context: Optional[str] = os.getenv("BUSINESS_CONTEXT")
# Redis(可选,用于 AI 结果缓存)
redis_url: Optional[str] = os.getenv("REDIS_URL")
# 多租户(预留)
default_tenant_id: str = os.getenv("DEFAULT_TENANT_ID", "default")
# 全局配置实例
settings = Settings()