from fastapi.responses import JSONResponse, FileResponse import os async def custom_404_handler(request, exc): # Check if the request is for an API endpoint if request.url.path.startswith("/api/") or request.url.path.startswith("/ws/"): return JSONResponse({"detail": "Not Found"}, status_code=404) # Otherwise/for frontend routes, return index.html for React Router to handle frontend_dist = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "frontend", "dist") html_path = os.path.join(frontend_dist, "index.html") if os.path.exists(html_path): return FileResponse(html_path) return JSONResponse({"detail": "Frontend not built"}, status_code=404)