Archived
- web/routers/index.py:GET / 首页,用 Jinja2 以 Flask 同口径渲染 (keep_trailing_newline=False)——输出与 Flask render_template 逐字节一致 - web/app.py:挂载 /static 静态资源(site_auth.js 等,与 Flask 同目录) - web/routers/auth_status.py:/api/v1/auth/me 认证状态 · user/duration_text/announcement/smtp_configured/register_credits 逐字段对齐 - core/repo.py:补 get_announcement / get_smtp_config / smtp_configured 异步读 契约奇偶校验:28 项全 OK(新增 index 逐字节、auth:me); 验证:pytest 51/51;ruff All checks passed;pyright 0 errors; uvicorn 实跑 / 与 /static 与 /api/v1/* 均 200。
38 lines
1.3 KiB
Python
38 lines
1.3 KiB
Python
# -*- 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'},
|
||
)
|