Spaces:
Sleeping

basel00omar commited on
Commit
a255a2f
·
1 Parent(s): 4600797

fix: use custom routes instead of StaticFiles to avoid HF proxy crashing

Browse files
Files changed (1) hide show
  1. api/main.py +40 -1
api/main.py CHANGED
@@ -338,7 +338,46 @@ elif os.path.isdir(_frontend_dist):
338
  return HTMLResponse(content=html_content)
339
  return {"detail": "Not Found"}
340
 
341
- app.mount("/", StaticFiles(directory=_frontend_dist, html=False), name="frontend")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
342
 
343
  if __name__ == "__main__":
344
  import uvicorn
 
338
  return HTMLResponse(content=html_content)
339
  return {"detail": "Not Found"}
340
 
341
+ @app.get("/assets/{file_path:path}")
342
+ async def serve_assets(file_path: str):
343
+ full_path = os.path.join(_frontend_dist, "assets", file_path)
344
+ if os.path.isfile(full_path):
345
+ from fastapi.responses import Response
346
+ ext = os.path.splitext(full_path)[1].lower()
347
+ if ext == ".js": mime_type = "application/javascript"
348
+ elif ext == ".css": mime_type = "text/css"
349
+ elif ext == ".png": mime_type = "image/png"
350
+ elif ext == ".svg": mime_type = "image/svg+xml"
351
+ elif ext == ".html": mime_type = "text/html"
352
+ elif ext == ".json": mime_type = "application/json"
353
+ elif ext == ".ico": mime_type = "image/x-icon"
354
+ else: mime_type = "application/octet-stream"
355
+
356
+ with open(full_path, "rb") as f:
357
+ content = f.read()
358
+ return Response(content=content, media_type=mime_type)
359
+ return {"detail": "Not Found"}
360
+
361
+ @app.get("/{file_name}")
362
+ async def serve_root_files(file_name: str):
363
+ # Serve files like vite.svg or favicon.ico in the root of dist
364
+ full_path = os.path.join(_frontend_dist, file_name)
365
+ if os.path.isfile(full_path):
366
+ from fastapi.responses import Response
367
+ ext = os.path.splitext(full_path)[1].lower()
368
+ if ext == ".js": mime_type = "application/javascript"
369
+ elif ext == ".css": mime_type = "text/css"
370
+ elif ext == ".png": mime_type = "image/png"
371
+ elif ext == ".svg": mime_type = "image/svg+xml"
372
+ elif ext == ".html": mime_type = "text/html"
373
+ elif ext == ".json": mime_type = "application/json"
374
+ elif ext == ".ico": mime_type = "image/x-icon"
375
+ else: mime_type = "application/octet-stream"
376
+
377
+ with open(full_path, "rb") as f:
378
+ content = f.read()
379
+ return Response(content=content, media_type=mime_type)
380
+ return {"detail": "Not Found"}
381
 
382
  if __name__ == "__main__":
383
  import uvicorn