Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI, HTTPException | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from fastapi.staticfiles import StaticFiles | |
| from fastapi.responses import FileResponse | |
| from dotenv import load_dotenv | |
| from pathlib import Path | |
| import os | |
| load_dotenv() | |
| app = FastAPI() | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # Mount the static directory | |
| app.mount("/static", StaticFiles(directory="frontend/out", html=True), name="static") | |
| async def catch_all(path_name: str): | |
| # Default route for the root | |
| if path_name == "": | |
| path_name = "index" | |
| # Construct file path with .html extension | |
| file_path = Path("frontend/out") / f"{path_name}.html" | |
| if file_path.is_file(): | |
| # Serve the .html file if it exists | |
| return FileResponse(file_path) | |
| else: | |
| # If the specific .html file does not exist, raise a 404 error | |
| raise HTTPException(status_code=404, detail="Item not found") | |
| # Run the application using Uvicorn | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run( | |
| "server:app", | |
| host="0.0.0.0", | |
| port=int(os.getenv('FAST_API_PORT', 8000)), | |
| reload=os.getenv('FAST_API_RELOAD', 'false').lower() == 'true', | |
| #ssl_certfile=args.ssl_certfile, | |
| #ssl_keyfile=args.ssl_keyfile, | |
| ) | |