⚡ 启用压缩并兼容统一主题资源路径
为 FastAPI 应用启用 Gzip,并兼容 Vite 在 /theme-assets/ 基址下生成的 assets 子路径。 - 压缩超过 1KB 的静态资源和 API 响应,保留不可变缓存策略 - 同时兼容 /theme-assets/<file> 与 /theme-assets/assets/<file> - 保留 /assets 旧入口,避免影响历史主题 - 已通过 Python 编译检查
This commit is contained in:
@@ -10,6 +10,7 @@ from contextlib import asynccontextmanager
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import FileResponse, HTMLResponse, PlainTextResponse
|
||||
from starlette.middleware.gzip import GZipMiddleware
|
||||
from tortoise import Tortoise
|
||||
from tortoise.contrib.fastapi import register_tortoise
|
||||
|
||||
@@ -120,6 +121,8 @@ async def migrate_password_to_hash():
|
||||
|
||||
app = FastAPI(lifespan=lifespan)
|
||||
|
||||
app.add_middleware(GZipMiddleware, minimum_size=1024, compresslevel=6)
|
||||
|
||||
@app.middleware("http")
|
||||
async def refresh_settings_middleware(request, call_next):
|
||||
await refresh_settings()
|
||||
@@ -151,8 +154,11 @@ app.include_router(admin_api)
|
||||
@app.get("/theme-assets/{asset_path:path}")
|
||||
@app.get("/assets/{asset_path:path}")
|
||||
async def theme_asset(asset_path: str):
|
||||
# Vite with base=/theme-assets/ emits /theme-assets/assets/<file> while
|
||||
# legacy themes use /theme-assets/<file>. Keep both forms compatible.
|
||||
normalized_asset_path = asset_path.removeprefix("assets/")
|
||||
theme_assets = (BASE_DIR / settings.themesSelect / "assets").resolve()
|
||||
target = (theme_assets / asset_path).resolve()
|
||||
target = (theme_assets / normalized_asset_path).resolve()
|
||||
try:
|
||||
target.relative_to(theme_assets)
|
||||
except ValueError:
|
||||
|
||||
Reference in New Issue
Block a user