SMTP 配置读取加固与日志降噪

1. 显式加载 backend/.env(不受工作目录影响),并以 utf-8-sig 读取兼容 Windows 记事本 BOM
2. 布尔配置容错:仅 false/0/no/off 视为关闭,SMTP_ENABLED 等拼写错误不再静默失效
3. 启动日志打印配置文件路径与缺失项;邮件测试/忘记密码报错直接列出缺少的配置
4. 认证中间件逐请求日志降为 DEBUG,减少'开始认证'刷屏
5. /api/config/deduction-rules 加入公开路径,修复页面 header 同步请求 401 与扣分规则回退默认值问题
This commit is contained in:
canglan committed 2026-09-20 20:46:25 +08:00
1 parent 1068bc6def
commit 05790c1882
6 files changed
+55 -30

No files matched your search

+18 -6
View File
@@ -13,14 +13,26 @@ import os
from dotenv import load_dotenv
from typing import List
load_dotenv()
# 始终加载 backend/.env(显式路径,避免受工作目录影响)
# encoding 用 utf-8-sig,兼容 Windows 记事本保存的 UTF-8 BOM
_ENV_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), ".env")
load_dotenv(_ENV_FILE, encoding="utf-8-sig")
def _env_bool(name: str, default: bool = False) -> bool:
"""布尔环境变量:仅显式的 false/0/no/off 视为关闭,其余(含拼写容错)视为开启"""
value = (os.getenv(name) or "").strip().lower()
if value == "":
return default
return value not in ("false", "0", "no", "off")
class Settings:
APP_NAME: str = os.getenv("APP_NAME", "班级操行分管理系统")
APP_ENV: str = os.getenv("APP_ENV", "production")
DEBUG: bool = os.getenv("DEBUG", "False").lower() == "true"
DEBUG: bool = _env_bool("DEBUG", False)
SECRET_KEY: str = os.getenv("SECRET_KEY", "")
API_VERSION: str = os.getenv("API_VERSION", "v1")
# 当前加载的配置文件路径(用于启动日志排查)
ENV_FILE: str = _ENV_FILE
DB_HOST: str = os.getenv("DB_HOST", "127.0.0.1")
DB_PORT: int = int(os.getenv("DB_PORT", "3306"))
@@ -47,13 +59,13 @@ class Settings:
JWT_IDLE_TIMEOUT_MINUTES: int = int(os.getenv("JWT_IDLE_TIMEOUT_MINUTES", "10"))
PASSWORD_SALT: str = os.getenv("PASSWORD_SALT", "")
DEBUG_ENABLED: bool = os.getenv("DEBUG_ENABLED", "False").lower() == "true"
DEBUG_ENABLED: bool = _env_bool("DEBUG_ENABLED", False)
DEBUG_PATH: str = os.getenv("DEBUG_PATH", "/debug_add_admin")
# ========== 站点地址与邮件(找回密码) ==========
# 前端站点地址,用于生成密码重置链接,如 https://class.example.com
FRONTEND_URL: str = os.getenv("FRONTEND_URL", "").rstrip("/")
SMTP_ENABLED: bool = os.getenv("SMTP_ENABLED", "False").strip().lower() in ("true", "1", "yes", "on")
SMTP_ENABLED: bool = _env_bool("SMTP_ENABLED", False)
SMTP_HOST: str = os.getenv("SMTP_HOST", "").strip()
SMTP_PORT: int = int(os.getenv("SMTP_PORT", "465"))
SMTP_USER: str = os.getenv("SMTP_USER", "").strip()
@@ -61,8 +73,8 @@ class Settings:
# 发件人邮箱默认与登录账号一致
SMTP_FROM: str = os.getenv("SMTP_FROM", "").strip() or SMTP_USER
SMTP_FROM_NAME: str = os.getenv("SMTP_FROM_NAME", os.getenv("APP_NAME", "班级操行分管理系统"))
SMTP_SSL: bool = os.getenv("SMTP_SSL", "True").lower() == "true"
SMTP_STARTTLS: bool = os.getenv("SMTP_STARTTLS", "False").lower() == "true"
SMTP_SSL: bool = _env_bool("SMTP_SSL", True)
SMTP_STARTTLS: bool = _env_bool("SMTP_STARTTLS", False)
# 密码重置链接有效期(分钟)
PASSWORD_RESET_EXPIRE_MINUTES: int = int(os.getenv("PASSWORD_RESET_EXPIRE_MINUTES", "30"))
# 邮箱验证码有效期(分钟)