feat: serve frontend SPA from FastAPI backend

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
CCS Admin 2026-02-24 10:10:11 +00:00
parent 1edf5835be
commit 4a3648a018

View file

@ -1,6 +1,10 @@
# backend/app/main.py # backend/app/main.py
from fastapi import FastAPI import os
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
from app.api.admin import router as admin_router from app.api.admin import router as admin_router
from app.api.auth import router as auth_router from app.api.auth import router as auth_router
@ -36,3 +40,20 @@ app.include_router(reports_router, prefix="/api/reports", tags=["reports"])
@app.get("/api/health") @app.get("/api/health")
def health_check(): def health_check():
return {"status": "ok"} return {"status": "ok"}
# --- Serve frontend SPA ---
_frontend_dist = os.path.join(
os.path.dirname(os.path.dirname(os.path.dirname(__file__))),
"frontend", "dist",
)
if os.path.isdir(_frontend_dist):
app.mount("/assets", StaticFiles(directory=os.path.join(_frontend_dist, "assets")), name="static")
@app.get("/{full_path:path}")
async def serve_spa(request: Request, full_path: str):
file_path = os.path.join(_frontend_dist, full_path)
if full_path and os.path.isfile(file_path):
return FileResponse(file_path)
return FileResponse(os.path.join(_frontend_dist, "index.html"))