Kacemath commited on
Commit
88f3adc
·
1 Parent(s): 5d491ed

Fix: Serve frontend and API from same FastAPI server on port 7860

Browse files
Files changed (2) hide show
  1. backend/app/main.py +42 -16
  2. start.sh +2 -6
backend/app/main.py CHANGED
@@ -1,6 +1,9 @@
1
  """FastAPI application entry point."""
 
2
  from fastapi import FastAPI
3
  from fastapi.middleware.cors import CORSMiddleware
 
 
4
  import uvicorn
5
 
6
  from .api.routes import router
@@ -21,26 +24,49 @@ app.add_middleware(
21
  allow_headers=["*"],
22
  )
23
 
24
- # Include routes
25
  app.include_router(router)
26
 
 
 
 
 
 
27
 
28
- @app.get("/")
29
- async def root():
30
- """Root endpoint."""
31
- return {
32
- "name": "Package Delivery Search API",
33
- "version": "1.0.0",
34
- "endpoints": {
35
- "health": "/api/health",
36
- "algorithms": "/api/algorithms",
37
- "generate": "/api/grid/generate",
38
- "path": "/api/search/path",
39
- "plan": "/api/search/plan",
40
- "compare": "/api/search/compare",
41
- "visualize": "ws://localhost:8000/ws/visualize"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  }
43
- }
44
 
45
 
46
  if __name__ == "__main__":
 
1
  """FastAPI application entry point."""
2
+ from pathlib import Path
3
  from fastapi import FastAPI
4
  from fastapi.middleware.cors import CORSMiddleware
5
+ from fastapi.staticfiles import StaticFiles
6
+ from fastapi.responses import FileResponse
7
  import uvicorn
8
 
9
  from .api.routes import router
 
24
  allow_headers=["*"],
25
  )
26
 
27
+ # Include API routes
28
  app.include_router(router)
29
 
30
+ # Serve static frontend files
31
+ static_dir = Path("/app/frontend/dist")
32
+ if static_dir.exists():
33
+ # Mount assets directory
34
+ app.mount("/assets", StaticFiles(directory=str(static_dir / "assets")), name="assets")
35
 
36
+ # Serve index.html for root and all non-API routes
37
+ @app.get("/")
38
+ async def serve_root():
39
+ return FileResponse(str(static_dir / "index.html"))
40
+
41
+ @app.get("/{full_path:path}")
42
+ async def serve_frontend(full_path: str):
43
+ # Don't intercept API routes
44
+ if full_path.startswith("api/"):
45
+ return {"error": "API endpoint not found"}
46
+
47
+ # Try to serve the requested file
48
+ file_path = static_dir / full_path
49
+ if file_path.is_file():
50
+ return FileResponse(str(file_path))
51
+
52
+ # Otherwise serve index.html (SPA routing)
53
+ return FileResponse(str(static_dir / "index.html"))
54
+ else:
55
+ @app.get("/")
56
+ async def root():
57
+ """Root endpoint."""
58
+ return {
59
+ "name": "Package Delivery Search API",
60
+ "version": "1.0.0",
61
+ "endpoints": {
62
+ "health": "/api/health",
63
+ "algorithms": "/api/algorithms",
64
+ "generate": "/api/grid/generate",
65
+ "path": "/api/search/path",
66
+ "plan": "/api/search/plan",
67
+ "compare": "/api/search/compare",
68
+ }
69
  }
 
70
 
71
 
72
  if __name__ == "__main__":
start.sh CHANGED
@@ -1,9 +1,5 @@
1
  #!/bin/bash
2
 
3
- # Start backend in background
4
  cd /app/backend
5
- python3 -m uvicorn app.main:app --host 0.0.0.0 --port 8000 &
6
-
7
- # Serve frontend built files using Python's HTTP server on port 7860
8
- cd /app/frontend/dist
9
- python3 -m http.server 7860
 
1
  #!/bin/bash
2
 
3
+ # Run FastAPI backend on port 7860 - serves both API and frontend
4
  cd /app/backend
5
+ python3 -m uvicorn app.main:app --host 0.0.0.0 --port 7860