Fix IndentationError in SPA route
Browse files
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|