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): | |
if path_name == "": | |
return FileResponse("frontend/out/index.html") | |
file_path = Path("frontend/out") / path_name | |
if file_path.is_file(): | |
return file_path | |
html_file_path = file_path.with_suffix(".html") | |
if html_file_path.is_file(): | |
return FileResponse(html_file_path) | |
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, | |
) | |