Spaces:
Sleeping
Sleeping
File size: 2,129 Bytes
e067c2d 47bba68 88f3adc e067c2d 88f3adc e067c2d 88f3adc e067c2d 88f3adc 47bba68 e067c2d 88f3adc 47bba68 88f3adc 47bba68 88f3adc 47bba68 e067c2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
"""FastAPI application entry point."""
from pathlib import Path
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
import uvicorn
from .api.routes import router
# Create FastAPI app
app = FastAPI(
title="Package Delivery Search API",
description="Search algorithms for package delivery optimization",
version="1.0.0",
)
# Configure CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Include API routes
app.include_router(router)
# Serve static frontend files
static_dir = Path("/app/frontend/dist")
if static_dir.exists():
# Mount assets directory
app.mount(
"/assets", StaticFiles(directory=str(static_dir / "assets")), name="assets"
)
# Serve index.html for root and all non-API routes
@app.get("/")
async def serve_root():
return FileResponse(str(static_dir / "index.html"))
@app.get("/{full_path:path}")
async def serve_frontend(full_path: str):
# Don't intercept API routes
if full_path.startswith("api/"):
return {"error": "API endpoint not found"}
# Try to serve the requested file
file_path = static_dir / full_path
if file_path.is_file():
return FileResponse(str(file_path))
# Otherwise serve index.html (SPA routing)
return FileResponse(str(static_dir / "index.html"))
else:
@app.get("/")
async def root():
"""Root endpoint."""
return {
"name": "Package Delivery Search API",
"version": "1.0.0",
"endpoints": {
"health": "/api/health",
"algorithms": "/api/algorithms",
"generate": "/api/grid/generate",
"path": "/api/search/path",
"plan": "/api/search/plan",
"compare": "/api/search/compare",
},
}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)
|