refactor(backend): 后端重构初步设想

This commit is contained in:
sophie committed 2026-06-17 11:24:54 +08:00
1 parent b69d549b2c
commit 21b8c24815
96 files changed
+1592

No files matched your search

+6
View File
@@ -0,0 +1,6 @@
"""
backend/app/core/auth/__init__.py
用途:认证与当前用户解析包入口。
逻辑:api 层通过 get_current_user 获取 User;Wave 1 自 auth_jwt.py 迁入 jwt.py。
"""
+39
View File
@@ -0,0 +1,39 @@
"""
backend/app/core/auth/context.py
用途:从 Flask g/request 解析请求上下文(request_id、user),供 api 层薄封装。
逻辑:Wave 3 拆分 user/chat 路由时,将重复 auth 逻辑收敛到本模块。
"""
from __future__ import annotations
from typing import Any
def get_request_id() -> str | None:
"""
读取当前请求的 X-Request-Id 或 g.request_id。
核心逻辑:延迟 import flask.g,避免 services 层依赖。
Returns:
request_id 字符串,无上下文时返回 None。
"""
try:
from flask import g
return getattr(g, 'request_id', None)
except RuntimeError:
return None
def get_optional_config(key: str, default: Any = None) -> Any:
"""
读取 Flask app.config 项(仅 api / jobs context 使用)。
Args:
key: config 键名。
default: 无 app context 时的默认值。
"""
try:
from flask import current_app
return current_app.config.get(key, default)
except RuntimeError:
return default
+9
View File
@@ -0,0 +1,9 @@
"""
backend/app/core/auth/jwt.py
用途:Casdoor JWT 校验与当前用户解析(Wave 1 自 app/auth_jwt.py 迁入)。
逻辑:api 路由与 decorators 调用 get_current_user()。
"""
from app.auth_jwt import get_current_user # noqa: F401
__all__ = ['get_current_user']