ソースを参照

修复前端问题

xueyiming 4 日 前
コミット
f2adde2e5c
1 ファイル変更20 行追加1 行削除
  1. 20 1
      api/app.py

+ 20 - 1
api/app.py

@@ -9,6 +9,7 @@ from fastapi.middleware.cors import CORSMiddleware
 from fastapi.staticfiles import StaticFiles
 from pydantic import BaseModel, Field
 from sqlalchemy import text
+from starlette.exceptions import HTTPException as StarletteHTTPException
 
 from api.routers.pipeline import router as pipeline_router
 from api.services.agent_catalog import (
@@ -36,6 +37,24 @@ from supply_infra.config import get_infra_settings
 from supply_infra.db import dispose_engine, get_session, init_db
 
 
+class SPAStaticFiles(StaticFiles):
+    """Serve index.html for browser routes handled by Vue Router."""
+
+    async def get_response(self, path: str, scope):
+        try:
+            return await super().get_response(path, scope)
+        except StarletteHTTPException as exc:
+            is_backend_path = (
+                path == "api"
+                or path.startswith("api/")
+                or path == "health"
+                or path.startswith("health/")
+            )
+            if exc.status_code != 404 or is_backend_path or Path(path).suffix:
+                raise
+            return await super().get_response("index.html", scope)
+
+
 @asynccontextmanager
 async def lifespan(_app: FastAPI):
     if get_infra_settings().database_auto_create:
@@ -309,4 +328,4 @@ def agent_document_injection(
 
 _web_dist = Path(__file__).resolve().parent.parent / "web" / "dist"
 if _web_dist.is_dir():
-    app.mount("/", StaticFiles(directory=_web_dist, html=True), name="web")
+    app.mount("/", SPAStaticFiles(directory=_web_dist, html=True), name="web")