rasAli02 commited on
Commit
04f87a5
·
1 Parent(s): de195a4

Fix IndentationError in SPA route

Browse files
Files changed (1) hide show
  1. app.py +20 -1
app.py CHANGED
@@ -510,7 +510,26 @@ with gr.Blocks(title="ForgeSight Admin") as demo:
510
  # Mount Gradio
511
  app = gr.mount_gradio_app(app, demo, path="/gradio")
512
 
513
- return FileResponse("build/index.html")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
514
 
515
  if __name__ == "__main__":
516
  import uvicorn
 
510
  # Mount Gradio
511
  app = gr.mount_gradio_app(app, demo, path="/gradio")
512
 
513
+ # SPA Fallback for React Router
514
+ from fastapi.responses import FileResponse
515
+ from fastapi.responses import JSONResponse
516
+
517
+ @app.get("/{full_path:path}")
518
+ async def serve_spa(full_path: str):
519
+ if full_path.startswith("api/") or full_path.startswith("gradio/"):
520
+ return JSONResponse({"detail": "Not found"}, status_code=404)
521
+
522
+ build_dir = "build" if os.path.exists("build") else "frontend/build"
523
+ path = os.path.join(build_dir, full_path)
524
+
525
+ if os.path.isfile(path):
526
+ return FileResponse(path)
527
+
528
+ index_file = os.path.join(build_dir, "index.html")
529
+ if os.path.exists(index_file):
530
+ return FileResponse(index_file)
531
+
532
+ return JSONResponse({"detail": "Not found"}, status_code=404)
533
 
534
  if __name__ == "__main__":
535
  import uvicorn