2026-09-09 17:25:42 +08:00
|
|
|
|
"""Public-facing pages and routes: setup wizard, theme assets, index,
|
|
|
|
|
|
robots.txt, and the public config endpoints."""
|
|
|
|
|
|
import html
|
2026-10-23 09:18:24 +00:00
|
|
|
|
import re
|
|
|
|
|
|
from functools import lru_cache
|
|
|
|
|
|
from pathlib import Path
|
2026-09-09 17:25:42 +08:00
|
|
|
|
|
|
|
|
|
|
from fastapi import APIRouter, HTTPException, Request
|
2026-09-20 02:01:08 +08:00
|
|
|
|
from fastapi.responses import FileResponse, HTMLResponse, JSONResponse, RedirectResponse
|
2026-09-09 17:25:42 +08:00
|
|
|
|
|
|
|
|
|
|
from apps.base.config import initialize_system, is_runtime_initialized
|
|
|
|
|
|
from apps.base.setup_wizard import (
|
|
|
|
|
|
build_public_config,
|
|
|
|
|
|
build_public_meta,
|
|
|
|
|
|
build_setup_page,
|
|
|
|
|
|
build_setup_success_page,
|
|
|
|
|
|
parse_setup_options,
|
|
|
|
|
|
read_setup_payload,
|
|
|
|
|
|
setup_response,
|
|
|
|
|
|
)
|
|
|
|
|
|
from core.response import APIResponse
|
|
|
|
|
|
from core.settings import BASE_DIR, DEFAULT_CONFIG, settings
|
|
|
|
|
|
from core.version import APP_VERSION
|
|
|
|
|
|
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/setup", include_in_schema=False)
|
|
|
|
|
|
@router.get("/setup/", include_in_schema=False)
|
|
|
|
|
|
async def setup_page():
|
|
|
|
|
|
if is_runtime_initialized():
|
|
|
|
|
|
return RedirectResponse(url="/", status_code=303)
|
|
|
|
|
|
return setup_response(build_setup_page())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/setup", include_in_schema=False)
|
|
|
|
|
|
@router.post("/setup/", include_in_schema=False)
|
|
|
|
|
|
async def setup_submit(request: Request):
|
|
|
|
|
|
if is_runtime_initialized():
|
|
|
|
|
|
return RedirectResponse(url="/", status_code=303)
|
|
|
|
|
|
|
|
|
|
|
|
data = await read_setup_payload(request)
|
|
|
|
|
|
admin_password = str(data.get("admin_password") or "")
|
|
|
|
|
|
confirm_password = str(data.get("confirm_password") or "")
|
|
|
|
|
|
site_name = str(data.get("site_name") or "")
|
|
|
|
|
|
|
|
|
|
|
|
if admin_password != confirm_password:
|
|
|
|
|
|
return setup_response(build_setup_page("两次输入的管理员密码不一致", data), 400)
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
setup_options = parse_setup_options(data)
|
|
|
|
|
|
await initialize_system(
|
|
|
|
|
|
admin_password=admin_password,
|
|
|
|
|
|
site_name=site_name,
|
|
|
|
|
|
setup_options=setup_options,
|
|
|
|
|
|
)
|
|
|
|
|
|
except ValueError as exc:
|
|
|
|
|
|
return setup_response(build_setup_page(str(exc), data), 400)
|
|
|
|
|
|
|
|
|
|
|
|
if "application/json" in request.headers.get("accept", ""):
|
|
|
|
|
|
return APIResponse(detail={"ok": True, "admin": "/#/admin"})
|
|
|
|
|
|
return setup_response(build_setup_success_page())
|
|
|
|
|
|
|
|
|
|
|
|
def resolve_theme_root():
|
|
|
|
|
|
themes_root = (BASE_DIR / "themes").resolve()
|
2026-09-09 18:09:02 +08:00
|
|
|
|
theme_root = (BASE_DIR / str(settings.themes_select)).resolve()
|
2026-09-09 17:25:42 +08:00
|
|
|
|
try:
|
|
|
|
|
|
theme_root.relative_to(themes_root)
|
|
|
|
|
|
except ValueError:
|
2026-09-09 18:09:02 +08:00
|
|
|
|
theme_root = (BASE_DIR / DEFAULT_CONFIG["themes_select"]).resolve()
|
2026-09-09 17:25:42 +08:00
|
|
|
|
if not theme_root.exists():
|
2026-09-09 18:09:02 +08:00
|
|
|
|
theme_root = (BASE_DIR / DEFAULT_CONFIG["themes_select"]).resolve()
|
2026-09-09 17:25:42 +08:00
|
|
|
|
return theme_root
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def resolve_theme_file(*parts: str):
|
|
|
|
|
|
theme_root = resolve_theme_root()
|
|
|
|
|
|
file_path = theme_root.joinpath(*parts).resolve()
|
|
|
|
|
|
# 防止通过 /assets/../ 读取主题目录外的文件。
|
|
|
|
|
|
try:
|
|
|
|
|
|
file_path.relative_to(theme_root)
|
|
|
|
|
|
except ValueError:
|
|
|
|
|
|
raise HTTPException(status_code=404, detail="资源不存在")
|
|
|
|
|
|
if not file_path.is_file():
|
|
|
|
|
|
raise HTTPException(status_code=404, detail="资源不存在")
|
|
|
|
|
|
return file_path
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-10-23 09:18:24 +00:00
|
|
|
|
# Vite 产物带内容指纹(如 index-BX2s1QqP.js),内容变化时文件名必然变化,
|
|
|
|
|
|
# 可以安全地长缓存;public/ 下的未哈希资源原地升级时名字不变,只给短缓存,
|
|
|
|
|
|
# 过期后靠 FileResponse 自带的 ETag/Last-Modified 走 304。
|
|
|
|
|
|
_HASHED_ASSET_RE = re.compile(
|
|
|
|
|
|
r"-[A-Za-z0-9_-]{8,}\.(?:js|mjs|css|map|woff2?|ttf|otf|eot|svg|png|jpe?g|gif|webp|avif|ico)$",
|
|
|
|
|
|
re.IGNORECASE,
|
|
|
|
|
|
)
|
|
|
|
|
|
_IMMUTABLE_ASSET_CACHE = "public, max-age=31536000, immutable"
|
|
|
|
|
|
_DEFAULT_ASSET_CACHE = "public, max-age=86400"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _asset_cache_control(asset_path: str) -> str:
|
|
|
|
|
|
if _HASHED_ASSET_RE.search(asset_path):
|
|
|
|
|
|
return _IMMUTABLE_ASSET_CACHE
|
|
|
|
|
|
return _DEFAULT_ASSET_CACHE
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@lru_cache(maxsize=8)
|
|
|
|
|
|
def _render_index_html(
|
|
|
|
|
|
template_path: str,
|
|
|
|
|
|
inode: int,
|
|
|
|
|
|
mtime_ns: int,
|
|
|
|
|
|
size: int,
|
|
|
|
|
|
title: str,
|
|
|
|
|
|
description: str,
|
|
|
|
|
|
keywords: str,
|
|
|
|
|
|
opacity: str,
|
|
|
|
|
|
background: str,
|
|
|
|
|
|
) -> str:
|
|
|
|
|
|
"""Render index.html once per (theme file version, site config) pair.
|
|
|
|
|
|
|
|
|
|
|
|
The stat fields keep the cache honest when a theme is replaced in place;
|
|
|
|
|
|
config values are part of the key so admin edits are visible immediately
|
|
|
|
|
|
without any cross-module invalidation hook.
|
|
|
|
|
|
"""
|
|
|
|
|
|
return (
|
|
|
|
|
|
Path(template_path)
|
|
|
|
|
|
.read_text(encoding="utf-8")
|
|
|
|
|
|
.replace("{{title}}", html.escape(title))
|
|
|
|
|
|
.replace("{{description}}", html.escape(description))
|
|
|
|
|
|
.replace("{{keywords}}", html.escape(keywords))
|
|
|
|
|
|
.replace("{{opacity}}", html.escape(opacity))
|
|
|
|
|
|
.replace("{{background}}", html.escape(background))
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-09-09 17:25:42 +08:00
|
|
|
|
@router.get("/assets/{asset_path:path}", include_in_schema=False)
|
|
|
|
|
|
async def theme_asset(asset_path: str):
|
2026-10-23 09:18:24 +00:00
|
|
|
|
return FileResponse(
|
|
|
|
|
|
resolve_theme_file("assets", asset_path),
|
|
|
|
|
|
headers={"Cache-Control": _asset_cache_control(asset_path)},
|
|
|
|
|
|
)
|
2026-09-09 17:25:42 +08:00
|
|
|
|
|
|
|
|
|
|
|
2026-09-20 02:01:08 +08:00
|
|
|
|
async def not_found_handler(request, exc=None):
|
|
|
|
|
|
"""区分"浏览器导航到未知路径"与"API 处理器抛出的 404"。
|
|
|
|
|
|
|
|
|
|
|
|
Starlette 的 status-code handler 优先于 HTTPException 类 handler,
|
|
|
|
|
|
直接注册 index 会让全应用所有 HTTPException(404) 变成 200 HTML 页
|
|
|
|
|
|
(负路径测试抓到的存量 bug)。浏览器(Accept 含 text/html)仍拿到
|
|
|
|
|
|
主题首页做 SPA 兜底;API 客户端拿到 JSON 404。
|
|
|
|
|
|
"""
|
2026-09-20 02:05:57 +08:00
|
|
|
|
json_404 = JSONResponse(
|
2026-09-20 02:01:08 +08:00
|
|
|
|
status_code=404,
|
|
|
|
|
|
content={"code": 404, "message": "Not Found", "detail": "资源不存在"},
|
|
|
|
|
|
)
|
2026-09-20 02:05:57 +08:00
|
|
|
|
if request is not None and request.method in {"GET", "HEAD"} and "text/html" in (
|
|
|
|
|
|
request.headers.get("accept", "")
|
|
|
|
|
|
):
|
|
|
|
|
|
try:
|
|
|
|
|
|
return await index(request, exc)
|
|
|
|
|
|
except HTTPException:
|
|
|
|
|
|
return json_404
|
|
|
|
|
|
return json_404
|
2026-09-20 02:01:08 +08:00
|
|
|
|
|
|
|
|
|
|
|
2026-09-09 17:25:42 +08:00
|
|
|
|
@router.get("/")
|
|
|
|
|
|
async def index(request=None, exc=None):
|
|
|
|
|
|
# Site config is admin input (and during the setup window anyone can claim it);
|
|
|
|
|
|
# always escape before injecting into the theme template to prevent stored XSS
|
|
|
|
|
|
# (mirrors the setup page).
|
2026-10-23 09:18:24 +00:00
|
|
|
|
template_path = resolve_theme_file("index.html")
|
|
|
|
|
|
try:
|
|
|
|
|
|
stat_result = template_path.stat()
|
|
|
|
|
|
except OSError:
|
|
|
|
|
|
# resolve_theme_file 与 stat 之间文件被删除/替换时仍按 404 处理,
|
|
|
|
|
|
# 不能退回缓存内容。
|
|
|
|
|
|
raise HTTPException(status_code=404, detail="资源不存在")
|
|
|
|
|
|
content = _render_index_html(
|
|
|
|
|
|
str(template_path),
|
|
|
|
|
|
stat_result.st_ino,
|
|
|
|
|
|
stat_result.st_mtime_ns,
|
|
|
|
|
|
stat_result.st_size,
|
|
|
|
|
|
str(settings.name),
|
|
|
|
|
|
str(settings.description),
|
|
|
|
|
|
str(settings.keywords),
|
|
|
|
|
|
str(settings.opacity),
|
|
|
|
|
|
str(settings.background),
|
|
|
|
|
|
)
|
2026-09-09 17:25:42 +08:00
|
|
|
|
return HTMLResponse(
|
2026-10-23 09:18:24 +00:00
|
|
|
|
content=content,
|
2026-09-09 17:25:42 +08:00
|
|
|
|
media_type="text/html",
|
|
|
|
|
|
headers={"Cache-Control": "no-cache"},
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/robots.txt")
|
|
|
|
|
|
async def robots():
|
2026-09-09 18:09:02 +08:00
|
|
|
|
return HTMLResponse(content=settings.robots_text, media_type="text/plain")
|
2026-09-09 17:25:42 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/")
|
|
|
|
|
|
async def get_config():
|
|
|
|
|
|
return APIResponse(detail=build_public_config())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/api/v1/config")
|
|
|
|
|
|
async def get_public_config():
|
|
|
|
|
|
return APIResponse(
|
|
|
|
|
|
detail={
|
|
|
|
|
|
"config": build_public_config(),
|
|
|
|
|
|
"meta": build_public_meta(),
|
|
|
|
|
|
}
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/health")
|
|
|
|
|
|
async def health_check():
|
|
|
|
|
|
return APIResponse(
|
|
|
|
|
|
detail={
|
|
|
|
|
|
"status": "ok",
|
|
|
|
|
|
"version": APP_VERSION,
|
|
|
|
|
|
"storage": settings.file_storage,
|
2026-09-09 18:09:02 +08:00
|
|
|
|
"theme": settings.themes_select,
|
2026-09-09 17:25:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
)
|