This repository has been archived on 2026-10-02. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
NeteaseUrl/web/routers/index.py
T
snowy-yang 18c77296c2 feat(routes): 迁移首页/静态资源 + /auth/me 认证状态
- 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。
2026-09-26 09:51:32 +08:00

38 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- 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'},
)