Kaadan commited on
Commit
2fba0ae
·
1 Parent(s): 4540421
Files changed (1) hide show
  1. backend/main.py +16 -7
backend/main.py CHANGED
@@ -24,7 +24,7 @@ logger = logging.getLogger(__name__)
24
  # ----------------------------
25
  # Environment variables
26
  # ----------------------------
27
- PORT = int(os.environ.get("PORT", 7860)) # HF Spaces default
28
  HOST = os.environ.get("HOST", "0.0.0.0")
29
  DEBUG = os.getenv("DEBUG", str(settings.debug)).lower() == "true"
30
 
@@ -69,22 +69,31 @@ api_router.include_router(application_router)
69
  app.include_router(api_router)
70
 
71
  # ----------------------------
72
- # Mount React frontend at /static
73
  # ----------------------------
74
- app.mount("/static", StaticFiles(directory="static"), name="static")
 
 
 
 
 
75
 
76
  # ----------------------------
77
- # SPA fallback route for React
78
  # ----------------------------
79
  @app.get("/{full_path:path}")
80
  async def serve_spa(full_path: str, request: Request):
81
  """
82
- Serve React SPA for all routes not under /api or /static.
83
  """
84
- index_file = Path("static/index.html")
 
 
 
 
85
  if index_file.exists():
86
  return FileResponse(index_file)
87
- # If React build missing
88
  return {"detail": "SPA not found, build your frontend first."}
89
 
90
  logger.info("Application routes registered")
 
24
  # ----------------------------
25
  # Environment variables
26
  # ----------------------------
27
+ PORT = int(os.environ.get("PORT", 7860))
28
  HOST = os.environ.get("HOST", "0.0.0.0")
29
  DEBUG = os.getenv("DEBUG", str(settings.debug)).lower() == "true"
30
 
 
69
  app.include_router(api_router)
70
 
71
  # ----------------------------
72
+ # Mount React frontend (static files)
73
  # ----------------------------
74
+ # Assumes React build output is in /app/static
75
+ react_build_path = Path("static")
76
+ if react_build_path.exists():
77
+ app.mount("/", StaticFiles(directory=react_build_path, html=True), name="react")
78
+ else:
79
+ logger.warning("React build not found in /static. Make sure to build frontend.")
80
 
81
  # ----------------------------
82
+ # SPA fallback route (for React Router)
83
  # ----------------------------
84
  @app.get("/{full_path:path}")
85
  async def serve_spa(full_path: str, request: Request):
86
  """
87
+ Serve React SPA for all routes not under /api.
88
  """
89
+ # Skip /api routes
90
+ if request.url.path.startswith("/api"):
91
+ return {"detail": "API route not found."}
92
+
93
+ index_file = react_build_path / "index.html"
94
  if index_file.exists():
95
  return FileResponse(index_file)
96
+
97
  return {"detail": "SPA not found, build your frontend first."}
98
 
99
  logger.info("Application routes registered")