Spaces:
Sleeping
Sleeping
Fix: Serve frontend and API from same FastAPI server on port 7860
Browse files- backend/app/main.py +42 -16
- 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 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
"
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
#
|
| 4 |
cd /app/backend
|
| 5 |
-
python3 -m uvicorn app.main:app --host 0.0.0.0 --port
|
| 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
|
|
|
|
|
|
|
|
|
|
|
|