Spaces:
Sleeping
Sleeping
File size: 1,429 Bytes
ad5d266 bd1c43c ad5d266 bd1c43c ad5d266 bd1c43c ad5d266 bd1c43c ad5d266 |
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 |
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")
@app.get("/{path_name:path}")
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,
)
|