# -*- coding: utf-8 -*- """首页与静态资源。 - GET / 首页:渲染 templates/index.html,禁止缓存 - 静态资源由 Starlette StaticFiles 挂载 /static(见 web/app.py) """ 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 以与历史模板渲染一致的默认口径(keep_trailing_newline=False), 使输出逐字节不变。 """ 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'}, )