Seth0330 commited on
Commit
f975d5b
·
verified ·
1 Parent(s): 9b305a1

Update backend/app/main.py

Browse files
Files changed (1) hide show
  1. backend/app/main.py +33 -5
backend/app/main.py CHANGED
@@ -182,13 +182,41 @@ def get_history(db: Session = Depends(get_db)):
182
 
183
  # Static frontend mounting (used after we build React)
184
  # Dockerfile copies the Vite build into backend/frontend_dist
 
185
  frontend_dir = os.path.join(
186
  os.path.dirname(os.path.dirname(__file__)), "frontend_dist"
187
  )
188
 
189
  if os.path.isdir(frontend_dir):
190
- app.mount(
191
- "/",
192
- StaticFiles(directory=frontend_dir, html=True),
193
- name="frontend",
194
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
182
 
183
  # Static frontend mounting (used after we build React)
184
  # Dockerfile copies the Vite build into backend/frontend_dist
185
+ # IMPORTANT: API routes must be defined BEFORE this so they take precedence
186
  frontend_dir = os.path.join(
187
  os.path.dirname(os.path.dirname(__file__)), "frontend_dist"
188
  )
189
 
190
  if os.path.isdir(frontend_dir):
191
+ # Serve static files (JS, CSS, images, etc.) from assets directory
192
+ assets_dir = os.path.join(frontend_dir, "assets")
193
+ if os.path.isdir(assets_dir):
194
+ app.mount(
195
+ "/assets",
196
+ StaticFiles(directory=assets_dir),
197
+ name="assets",
198
+ )
199
+
200
+ # Catch-all route to serve index.html for React Router
201
+ # This must be last so API routes are matched first
202
+ @app.get("/{full_path:path}")
203
+ async def serve_frontend(full_path: str):
204
+ """
205
+ Serve React app for all non-API routes.
206
+ React Router will handle client-side routing.
207
+ """
208
+ # Skip API routes, docs, and static assets
209
+ if (full_path.startswith("api/") or
210
+ full_path.startswith("docs") or
211
+ full_path.startswith("openapi.json") or
212
+ full_path.startswith("assets/")):
213
+ from fastapi import HTTPException
214
+ raise HTTPException(status_code=404)
215
+
216
+ # Serve index.html for all other routes
217
+ from fastapi.responses import FileResponse
218
+ index_path = os.path.join(frontend_dir, "index.html")
219
+ if os.path.exists(index_path):
220
+ return FileResponse(index_path)
221
+ from fastapi import HTTPException
222
+ raise HTTPException(status_code=404)