fix(backend): serve frontend at root path and move meta info to /api/meta

This commit is contained in:
zly
2026-01-14 14:57:11 +08:00
parent 39765e4782
commit 96598b9d25

View File

@@ -47,8 +47,8 @@ app.include_router(upload.router, prefix=f"{settings.API_V1_STR}/upload", tags=[
app.include_router(results.router, prefix=f"{settings.API_V1_STR}/results", tags=["results"]) app.include_router(results.router, prefix=f"{settings.API_V1_STR}/results", tags=["results"])
@app.get("/") @app.get("/api/meta")
async def root(): async def root_meta():
return { return {
"name": settings.APP_NAME, "name": settings.APP_NAME,
"version": settings.APP_VERSION, "version": settings.APP_VERSION,
@@ -70,7 +70,17 @@ if frontend_dist_path.exists():
@app.get("/{full_path:path}", include_in_schema=False) @app.get("/{full_path:path}", include_in_schema=False)
async def serve_spa(full_path: str): async def serve_spa(full_path: str):
"""Serve frontend SPA - all routes go to index.html""" """Serve frontend SPA - all routes go to index.html"""
# 如果是 API 路径,且没有被前面路由捕获,返回 404
if full_path.startswith("api/"):
return {"detail": "Not Found"}, 404
# 处理根路径 ""
if full_path == "" or full_path == "/":
return FileResponse(frontend_dist_path / "index.html")
file_path = frontend_dist_path / full_path file_path = frontend_dist_path / full_path
if file_path.exists() and file_path.is_file(): if file_path.exists() and file_path.is_file():
return FileResponse(file_path) return FileResponse(file_path)
# SPA 路由:返回 index.html
return FileResponse(frontend_dist_path / "index.html") return FileResponse(frontend_dist_path / "index.html")