ciyidogan commited on
Commit
d20563d
·
verified ·
1 Parent(s): 5a59c8d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -25
app.py CHANGED
@@ -253,9 +253,9 @@ async def get_metrics():
253
  return metrics
254
 
255
  # ---------------- Health probe (HF Spaces watchdog) -----------------
256
- @app.get("/")
257
  def health_check():
258
- """Health check with detailed status"""
259
  return {
260
  "status": "ok",
261
  "version": "2.0.0",
@@ -268,34 +268,59 @@ def health_check():
268
  async def conversation_websocket(websocket: WebSocket, session_id: str):
269
  await websocket_endpoint(websocket, session_id)
270
 
271
- # ---------------- Serve Angular UI if exists ------------------------
272
- static_dir = Path(__file__).parent / "static"
273
- if static_dir.exists():
274
- log_info("🎨 Serving Angular UI from /static directory")
 
275
 
276
- # Mount static files with custom handler for Angular routing
277
- @app.get("/{path:path}")
278
- async def serve_angular(path: str):
279
- # API routes should not be handled here
280
- if path.startswith("api/"):
281
- return {"error": "Not found"}, 404
282
-
283
- # Try to serve the exact file first
284
- file_path = static_dir / path
285
- if file_path.is_file():
286
- return FileResponse(file_path)
 
 
 
 
 
 
 
 
 
287
 
288
- # For Angular routes, always serve index.html
289
- index_path = static_dir / "index.html"
 
 
 
 
 
290
  if index_path.exists():
291
- return FileResponse(index_path)
292
 
293
- return {"error": "Not found"}, 404
294
-
295
- # Mount static files for assets
296
- app.mount("/", StaticFiles(directory=str(static_dir), html=True), name="static")
297
  else:
298
- log_info("⚠️ No UI found. Run 'cd flare-ui && npm run build' to build the UI.")
 
 
 
 
 
 
 
 
 
 
 
 
 
299
 
300
  if __name__ == "__main__":
301
  log_info("🌐 Starting Flare backend on port 7860...")
 
253
  return metrics
254
 
255
  # ---------------- Health probe (HF Spaces watchdog) -----------------
256
+ @app.get("/api/health")
257
  def health_check():
258
+ """Health check endpoint - moved to /api/health"""
259
  return {
260
  "status": "ok",
261
  "version": "2.0.0",
 
268
  async def conversation_websocket(websocket: WebSocket, session_id: str):
269
  await websocket_endpoint(websocket, session_id)
270
 
271
+ # ---------------- Serve static files ------------------------------------
272
+ # UI static files (production build)
273
+ static_path = Path(__file__).parent / "static"
274
+ if static_path.exists():
275
+ app.mount("/static", StaticFiles(directory=str(static_path)), name="static")
276
 
277
+ # Serve index.html for all non-API routes (SPA support)
278
+ @app.get("/", response_class=FileResponse)
279
+ async def serve_index():
280
+ """Serve Angular app"""
281
+ index_path = static_path / "index.html"
282
+ if index_path.exists():
283
+ return FileResponse(str(index_path))
284
+ else:
285
+ return JSONResponse(
286
+ status_code=404,
287
+ content={"error": "UI not found. Please build the Angular app first."}
288
+ )
289
+
290
+ # Catch-all route for SPA
291
+ @app.get("/{full_path:path}")
292
+ async def serve_spa(full_path: str):
293
+ """Serve Angular app for all routes"""
294
+ # Skip API routes
295
+ if full_path.startswith("api/"):
296
+ return JSONResponse(status_code=404, content={"error": "Not found"})
297
 
298
+ # Serve static files
299
+ file_path = static_path / full_path
300
+ if file_path.exists() and file_path.is_file():
301
+ return FileResponse(str(file_path))
302
+
303
+ # Fallback to index.html for SPA routing
304
+ index_path = static_path / "index.html"
305
  if index_path.exists():
306
+ return FileResponse(str(index_path))
307
 
308
+ return JSONResponse(status_code=404, content={"error": "Not found"})
 
 
 
309
  else:
310
+ log_warning(f"⚠️ Static files directory not found at {static_path}")
311
+ log_warning(" Run 'npm run build' in flare-ui directory to build the UI")
312
+
313
+ @app.get("/")
314
+ async def no_ui():
315
+ """No UI available"""
316
+ return JSONResponse(
317
+ status_code=503,
318
+ content={
319
+ "error": "UI not available",
320
+ "message": "Please build the Angular UI first. Run: cd flare-ui && npm run build",
321
+ "api_docs": "/docs"
322
+ }
323
+ )
324
 
325
  if __name__ == "__main__":
326
  log_info("🌐 Starting Flare backend on port 7860...")