INNER CODE UNIT · Python
_mount_spa
ohdearquant/lionagi · lionagi/studio/app.py:325
def _mount_spa(application: FastAPI, dist: Path) -> None:
"""Mount static assets and register an SPA 404 fallback. Uses a 404
exception handler, not a catch-all route (which would intercept
/api/shows before FastAPI's trailing-slash redirect fires).
"""
assets_dir = dist / "assets"
if assets_dir.is_dir():
application.mount("/assets", StaticFiles(directory=str(assets_dir)), name="spa-assets")
index_path = dist / "index.html"
@application.exception_handler(404)
async def _spa_fallback(request: Request, exc: Exception) -> FileResponse | JSONResponse:
# /api/* paths stay 404 JSON here, not the SPA HTML shell.
path = request.url.path
if path.startswith("/api/") or path == "/api":
return JSONResponse({"detail": "Not Found"}, status_code=404)
if request.method not in ("GET", "HEAD"):