| """Unified server for Hugging Face Spaces — serves both the Amaru API and the built frontend.""" |
|
|
| import os |
| import sys |
|
|
| sys.path.insert(0, "/app") |
|
|
| from pathlib import Path |
| from fastapi import FastAPI |
| from fastapi.staticfiles import StaticFiles |
| from fastapi.responses import FileResponse |
| from starlette.middleware.cors import CORSMiddleware |
|
|
| from amaru.app import app as amaru_app |
|
|
| app = FastAPI(title="Amaru — Full Stack") |
|
|
| app.add_middleware( |
| CORSMiddleware, |
| allow_origins=["*"], |
| allow_methods=["*"], |
| allow_headers=["*"], |
| ) |
|
|
| app.mount("/api/amaru", amaru_app) |
|
|
| STATIC_DIR = Path("/app/static") |
|
|
| if STATIC_DIR.exists(): |
| app.mount("/assets", StaticFiles(directory=str(STATIC_DIR / "assets")), name="assets") |
|
|
| @app.get("/{path:path}") |
| async def serve_spa(path: str): |
| file_path = STATIC_DIR / path |
| if file_path.exists() and file_path.is_file(): |
| return FileResponse(file_path) |
| return FileResponse(STATIC_DIR / "index.html") |
|
|
|
|
| if __name__ == "__main__": |
| import uvicorn |
| port = int(os.environ.get("PORT", "7860")) |
| uvicorn.run("serve:app", host="0.0.0.0", port=port, log_level="info") |
|
|