74 lines
1.9 KiB
Python
74 lines
1.9 KiB
Python
"""全局配置:从项目根 .env 加载环境变量,通过 `settings` 单例访问。"""
|
|
from __future__ import annotations
|
|
|
|
from functools import lru_cache
|
|
from pathlib import Path
|
|
|
|
from pydantic import Field
|
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
|
|
# 项目根(backend/ 上一级)
|
|
ROOT_DIR = Path(__file__).resolve().parents[3]
|
|
|
|
|
|
class Settings(BaseSettings):
|
|
model_config = SettingsConfigDict(
|
|
env_file=str(ROOT_DIR / ".env"),
|
|
env_file_encoding="utf-8",
|
|
case_sensitive=True,
|
|
extra="ignore",
|
|
)
|
|
|
|
# App
|
|
APP_ENV: str = "dev"
|
|
APP_SECRET_KEY: str = "change-me"
|
|
APP_BASE_URL: str = "http://localhost:8000"
|
|
|
|
# JWT
|
|
JWT_ALG: str = "HS256"
|
|
JWT_ACCESS_TTL: int = 3600
|
|
JWT_REFRESH_TTL: int = 604800
|
|
|
|
# Database
|
|
DATABASE_URL: str = "postgresql+asyncpg://loom:loom@localhost:5432/loom"
|
|
|
|
# Redis / Celery
|
|
REDIS_URL: str = "redis://localhost:6379/0"
|
|
CELERY_BROKER_URL: str = "redis://localhost:6379/1"
|
|
CELERY_RESULT_BACKEND: str = "redis://localhost:6379/2"
|
|
|
|
# Lobster Agent(联调前留空)
|
|
LOBSTER_API_BASE: str = ""
|
|
LOBSTER_API_KEY: str = ""
|
|
LOBSTER_CALLBACK_SECRET: str = ""
|
|
LOBSTER_TIMEOUT_SECONDS: int = 30
|
|
LOBSTER_POLL_INTERVAL_SECONDS: int = 5
|
|
|
|
# OpenAI(联调前留空)
|
|
OPENAI_API_BASE: str = "https://api.openai.com/v1"
|
|
OPENAI_API_KEY: str = ""
|
|
OPENAI_DEFAULT_MODEL: str = "gpt-4o-mini"
|
|
OPENAI_TIMEOUT_SECONDS: int = 60
|
|
|
|
# 加密
|
|
FERNET_KEY: str = ""
|
|
|
|
# CORS
|
|
CORS_ALLOW_ORIGINS: str = "http://localhost:5173,http://127.0.0.1:5173"
|
|
|
|
@property
|
|
def cors_origins(self) -> list[str]:
|
|
return [o.strip() for o in self.CORS_ALLOW_ORIGINS.split(",") if o.strip()]
|
|
|
|
@property
|
|
def is_dev(self) -> bool:
|
|
return self.APP_ENV.lower() in {"dev", "development", "local"}
|
|
|
|
|
|
@lru_cache
|
|
def get_settings() -> Settings:
|
|
return Settings()
|
|
|
|
|
|
settings = get_settings()
|