unis_manager/app/config.py

61 lines
2.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

"""全局配置。
数据库默认使用 SQLite,迁移 MySQL 只需修改 DATABASE_URL:
mysql+pymysql://user:pwd@host:3306/unis_board?charset=utf8mb4
其余代码无需改动(SQLAlchemy 负责方言差异)。
"""
from __future__ import annotations
import os
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent.parent
DATA_DIR = BASE_DIR / "data"
DATA_DIR.mkdir(parents=True, exist_ok=True)
UPLOAD_DIR = DATA_DIR / "uploads"
UPLOAD_DIR.mkdir(parents=True, exist_ok=True)
def _load_dotenv() -> None:
path = BASE_DIR / ".env"
if not path.exists():
return
for line in path.read_text(encoding="utf-8").splitlines():
line = line.strip()
if not line or line.startswith("#") or "=" not in line:
continue
key, value = line.split("=", 1)
os.environ.setdefault(key.strip(), value.strip().strip('"').strip("'"))
_load_dotenv()
DATABASE_URL = os.getenv("DATABASE_URL", f"sqlite:///{DATA_DIR / 'board.db'}")
AI_API_BASE = os.getenv("AI_API_BASE", "https://api.deepseek.com/v1").rstrip("/")
AI_API_KEY = os.getenv("AI_API_KEY", "")
AI_MODEL = os.getenv("AI_MODEL", "deepseek-chat")
AI_TEMPERATURE = float(os.getenv("AI_TEMPERATURE", "0.2"))
AI_TIMEOUT = float(os.getenv("AI_TIMEOUT", "90"))
PORT = int(os.getenv("PORT", "8770"))
# 首次启动自动创建的管理员账号(务必上线后改掉密码)
ADMIN_USERNAME = os.getenv("ADMIN_USERNAME", "admin").strip() or "admin"
ADMIN_PASSWORD = os.getenv("ADMIN_PASSWORD", "admin123").strip() or "admin123"
# 登录态有效天数
SESSION_DAYS = int(os.getenv("SESSION_DAYS", "30"))
# DEV=1 启动时开启开发模式(代码热加载 + 浏览器自动刷新)
DEV = os.getenv("DEV", "").strip().lower() in ("1", "true", "yes", "on")
# 允许在页面上被覆盖的 AI 配置键
AI_SETTING_KEYS = ("ai_api_base", "ai_api_key", "ai_model", "ai_temperature")
DEFAULT_AI_SETTINGS = {
"ai_api_base": AI_API_BASE,
"ai_api_key": AI_API_KEY,
"ai_model": AI_MODEL,
"ai_temperature": str(AI_TEMPERATURE),
}