chih.yikuan commited on
Commit
8f19122
·
1 Parent(s): 4d7cfb5

Fix: Improve report-demo route with better path resolution and error handling

Browse files
Files changed (1) hide show
  1. chatkit/backend/app/main.py +26 -8
chatkit/backend/app/main.py CHANGED
@@ -259,14 +259,32 @@ async def serve_report_template():
259
  @app.get("/report-demo")
260
  async def serve_report_demo():
261
  """Serve the HTML report template demo."""
262
- if REPORT_TEMPLATE_PATH and REPORT_TEMPLATE_PATH.exists():
263
- return FileResponse(
264
- REPORT_TEMPLATE_PATH,
265
- media_type="text/html",
266
- headers={"Cache-Control": "public, max-age=3600"}
267
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
268
  return Response(
269
- content="Report template not found",
270
  status_code=404,
271
  media_type="text/plain"
272
  )
@@ -297,7 +315,7 @@ if STATIC_DIR.exists():
297
  async def serve_spa_routes(full_path: str):
298
  """Catch-all route to serve React SPA for client-side routing."""
299
  # Don't serve SPA for API routes
300
- if full_path.startswith(("api/", "auth/", "chatkit")):
301
  return Response(status_code=404)
302
 
303
  # Check if it's a static file
 
259
  @app.get("/report-demo")
260
  async def serve_report_demo():
261
  """Serve the HTML report template demo."""
262
+ # Try multiple possible locations (in order of preference)
263
+ base_dir = Path(__file__).parent.parent # /home/user/app in Docker
264
+ possible_paths = [
265
+ base_dir / "report-template.html", # Docker: /home/user/app/report-template.html
266
+ REPORT_TEMPLATE_PATH, # From find_report_template() (fallback)
267
+ STATIC_DIR / "report-template.html" if STATIC_DIR.exists() else None, # Static dir
268
+ Path(__file__).parent.parent.parent / "report-template.html", # Dev fallback
269
+ ]
270
+
271
+ # Filter out None values
272
+ possible_paths = [p for p in possible_paths if p is not None]
273
+
274
+ for template_path in possible_paths:
275
+ if template_path.exists():
276
+ return FileResponse(
277
+ template_path,
278
+ media_type="text/html",
279
+ headers={"Cache-Control": "public, max-age=3600"}
280
+ )
281
+
282
+ # If not found, return helpful error message with debug info
283
+ current_dir = base_dir
284
+ files_in_dir = list(current_dir.glob("*")) if current_dir.exists() else []
285
+ searched_paths = [str(p) for p in possible_paths]
286
  return Response(
287
+ content=f"Report template not found.\nSearched: {searched_paths}\nCurrent dir: {current_dir}\nFiles in dir: {[str(f) for f in files_in_dir[:10]]}",
288
  status_code=404,
289
  media_type="text/plain"
290
  )
 
315
  async def serve_spa_routes(full_path: str):
316
  """Catch-all route to serve React SPA for client-side routing."""
317
  # Don't serve SPA for API routes
318
+ if full_path.startswith(("api/", "auth/", "chatkit", "report-demo")):
319
  return Response(status_code=404)
320
 
321
  # Check if it's a static file