ArshVerma commited on
Commit
cbe123c
Β·
1 Parent(s): a485e79

Fix Dashboard: Robust Catch-all SPA routing

Browse files
Files changed (1) hide show
  1. app.py +11 -15
app.py CHANGED
@@ -364,27 +364,23 @@ async def websocket_endpoint(websocket: WebSocket):
364
  finally:
365
  clients.discard(websocket)
366
 
367
- # ── Dashboard ─────────────────────────────────────────────────────────────────
368
  static_dir = os.path.join(os.path.dirname(__file__), "static", "dashboard")
369
- if os.path.exists(static_dir):
370
- app.mount("/dashboard/assets", StaticFiles(directory=os.path.join(static_dir, "assets")), name="dashboard-assets")
371
 
372
- @app.get("/", include_in_schema=False)
373
- @app.get("/dashboard", include_in_schema=False)
374
- @app.get("/dashboard/{full_path:path}", include_in_schema=False)
375
  @app.get("/{full_path:path}", include_in_schema=False)
376
- def dashboard(full_path: str = ""):
377
- """Serve the React dashboard SPA (index.html for all sub-paths)."""
378
- # 1. Check if the requested full_path is a specific static file (e.g. logo.svg)
379
  if full_path:
380
- static_file = os.path.join(os.path.dirname(__file__), "static", "dashboard", full_path)
381
- if os.path.exists(static_file) and os.path.isfile(static_file):
382
- return FileResponse(static_file)
383
 
384
- # 2. Fallback to index.html for SPA routing
385
- html_path = os.path.join(os.path.dirname(__file__), "static", "dashboard", "index.html")
386
  if not os.path.exists(html_path):
387
- raise HTTPException(status_code=404, detail="Dashboard not found. Run: cd dashboard && npm run build")
 
388
  return FileResponse(html_path)
389
 
390
  if __name__ == "__main__":
 
364
  finally:
365
  clients.discard(websocket)
366
 
367
+ # ── Dashboard & Static Files ─────────────────────────────────────────────────
368
  static_dir = os.path.join(os.path.dirname(__file__), "static", "dashboard")
 
 
369
 
 
 
 
370
  @app.get("/{full_path:path}", include_in_schema=False)
371
+ def serve_dashboard(full_path: str = ""):
372
+ """Catch-all for Root, Assets, and SPA routing."""
373
+ # 1. Check if the requested full_path is a specific static file (e.g. logo.svg, assets/index.js)
374
  if full_path:
375
+ local_file = os.path.join(static_dir, full_path)
376
+ if os.path.exists(local_file) and os.path.isfile(local_file):
377
+ return FileResponse(local_file)
378
 
379
+ # 2. Fallback to index.html for Root and SPA routes
380
+ html_path = os.path.join(static_dir, "index.html")
381
  if not os.path.exists(html_path):
382
+ # Fallback if dashboard isn't built
383
+ return {"status": "ready", "message": "API is online, dashboard not found locally."}
384
  return FileResponse(html_path)
385
 
386
  if __name__ == "__main__":