refactor: 删除旧前端单体,React 产物成为唯一首页来源

为什么:React 迁移已验证完毕(浏览器冒烟全过、golden 刷新),
legacy 模板兜底分支指向已删除的文件只会制造幻觉路径。删除
templates/index.html(466KB)与 static/site_auth.js(80KB)及
/static 挂载;jinja2 随兜底分支失去唯一使用者,连同 markupsafe
一并移出依赖(63 包 -2)。_render_index 简化为直接读构建产物——
dist 已入库恒存在,无需分支。
This commit is contained in:
snowy-yang committed 2026-09-27 18:41:30 +08:00
1 parent 17b4a99680
commit 064a89d726
6 files changed
+6 -9236

No files matched your search

-5
View File
@@ -109,11 +109,6 @@ def create_app(api_prefix: str = '', *, start_monitor: bool = True) -> FastAPI:
# 响应侧统一处理:安全头 + 音频直链签名保护 + 版本指纹抹除
app.add_middleware(ResponseHygieneMiddleware)
# 静态资源(/static/site_auth.js 等)——与 Flask static_folder 同目录
_static_dir = Path(__file__).resolve().parent.parent / 'static'
if _static_dir.is_dir():
app.mount('/static', StaticFiles(directory=str(_static_dir)), name='static')
# React 构建产物(vite build → frontend/dist,hash 指纹资源挂 /assets)
_dist_dir = Path(__file__).resolve().parent.parent / 'frontend' / 'dist'
if (_dist_dir / 'assets').is_dir():
+6 -15
View File
@@ -1,9 +1,9 @@
# -*- coding: utf-8 -*-
"""首页与静态资源。
"""首页。
- GET / 首页:优先服务 React 构建产物 frontend/dist/index.html;
dist 未构建时回退旧版 templates/index.html(平滑过渡)
- 静态资源由 Starlette StaticFiles 挂载 /assets 与 /static(见 web/app.py)
- GET / 首页:服务 React 构建产物 frontend/dist/index.html
(vite 固定产物文件名,字节级稳定,golden 契约依赖此点)
- 构建产物经 /assets 挂载(见 web/app.py)
"""
from pathlib import Path
@@ -14,20 +14,11 @@ router = APIRouter()
_PROJECT_ROOT = Path(__file__).resolve().parent.parent.parent
_DIST_INDEX = _PROJECT_ROOT / 'frontend' / 'dist' / 'index.html'
_LEGACY_TEMPLATE = _PROJECT_ROOT / 'templates' / 'index.html'
def _render_index() -> str:
"""渲染首页 HTML:React 构建产物优先,缺失时回退旧模板。"""
if _DIST_INDEX.is_file():
return _DIST_INDEX.read_text(encoding='utf-8')
import jinja2
env = jinja2.Environment(
loader=jinja2.FileSystemLoader(str(_LEGACY_TEMPLATE.parent)),
autoescape=True,
)
return env.get_template(_LEGACY_TEMPLATE.name).render()
"""返回 React 构建产物原样字节(构建产物入库,恒存在)。"""
return _DIST_INDEX.read_text(encoding='utf-8')
@router.get('/', response_class=HTMLResponse)