Fix Dashboard: Robust Catch-all SPA routing
Browse files
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
|
| 377 |
-
"""
|
| 378 |
-
# 1. Check if the requested full_path is a specific static file (e.g. logo.svg)
|
| 379 |
if full_path:
|
| 380 |
-
|
| 381 |
-
if os.path.exists(
|
| 382 |
-
return FileResponse(
|
| 383 |
|
| 384 |
-
# 2. Fallback to index.html for SPA
|
| 385 |
-
html_path = os.path.join(
|
| 386 |
if not os.path.exists(html_path):
|
| 387 |
-
|
|
|
|
| 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__":
|