# -*- coding: utf-8 -*- """首页与静态资源(FastAPI 栈)。 - GET / 首页:渲染同一份 templates/index.html,禁止缓存(与 Flask 一致) - 静态资源由 Starlette StaticFiles 挂载 /static(见 web/app.py) 说明:首页在 Flask fallback 模式下由 Flask 兜底提供;本端点用于「关闭 Flask 后」 FastAPI 独立提供首页(迁移完成的形态)。两者渲染的是同一模板,故内容一致。 """ from pathlib import Path from fastapi import APIRouter, Request from fastapi.responses import HTMLResponse router = APIRouter() _TEMPLATE = Path(__file__).resolve().parent.parent.parent / 'templates' / 'index.html' @router.get('/', response_class=HTMLResponse) async def index(request: Request) -> HTMLResponse: """首页(禁止缓存,确保前端更新即时生效)。 用 Jinja2 以 Flask 相同的默认口径渲染(keep_trailing_newline=False), 使输出与 Flask render_template 逐字节一致。 """ import jinja2 env = jinja2.Environment( loader=jinja2.FileSystemLoader(str(_TEMPLATE.parent)), autoescape=True, ) html = env.get_template(_TEMPLATE.name).render() return HTMLResponse( content=html, headers={'Cache-Control': 'no-cache, no-store, must-revalidate'}, )