|
| 1 | +import os |
| 2 | + |
| 3 | +import uvicorn |
| 4 | +from dotenv import load_dotenv |
| 5 | +from fastapi import FastAPI |
| 6 | +from fastapi.middleware.cors import CORSMiddleware |
| 7 | +from fastapi.responses import FileResponse, HTMLResponse |
| 8 | +from fastapi.staticfiles import StaticFiles |
| 9 | + |
| 10 | +# Load environment variables from .env file |
| 11 | +load_dotenv() |
| 12 | + |
| 13 | +app = FastAPI() |
| 14 | + |
| 15 | +app.add_middleware( |
| 16 | + CORSMiddleware, |
| 17 | + allow_origins=["*"], |
| 18 | + allow_methods=["*"], |
| 19 | + allow_headers=["*"], |
| 20 | +) |
| 21 | + |
| 22 | +# Build paths |
| 23 | +BUILD_DIR = os.path.join(os.path.dirname(__file__), "dist") |
| 24 | +INDEX_HTML = os.path.join(BUILD_DIR, "index.html") |
| 25 | + |
| 26 | +# Serve static files from build directory |
| 27 | +app.mount( |
| 28 | + "/assets", StaticFiles(directory=os.path.join(BUILD_DIR, "assets")), name="assets" |
| 29 | +) |
| 30 | + |
| 31 | + |
| 32 | +@app.get("/") |
| 33 | +async def serve_index(): |
| 34 | + return FileResponse(INDEX_HTML) |
| 35 | + |
| 36 | + |
| 37 | +@app.get("/config") |
| 38 | +async def get_config(): |
| 39 | + config = { |
| 40 | + "API_URL": os.getenv("API_URL", "API_URL not set"), |
| 41 | + "REACT_APP_MSAL_AUTH_CLIENTID": os.getenv( |
| 42 | + "REACT_APP_MSAL_AUTH_CLIENTID", "Client ID not set" |
| 43 | + ), |
| 44 | + "REACT_APP_MSAL_AUTH_AUTHORITY": os.getenv( |
| 45 | + "REACT_APP_MSAL_AUTH_AUTHORITY", "Authority not set" |
| 46 | + ), |
| 47 | + "REACT_APP_MSAL_REDIRECT_URL": os.getenv( |
| 48 | + "REACT_APP_MSAL_REDIRECT_URL", "Redirect URL not set" |
| 49 | + ), |
| 50 | + "REACT_APP_MSAL_POST_REDIRECT_URL": os.getenv( |
| 51 | + "REACT_APP_MSAL_POST_REDIRECT_URL", "Post Redirect URL not set" |
| 52 | + ), |
| 53 | + "ENABLE_AUTH": os.getenv("ENABLE_AUTH", "false"), |
| 54 | + } |
| 55 | + return config |
| 56 | + |
| 57 | + |
| 58 | +@app.get("/{full_path:path}") |
| 59 | +async def serve_app(full_path: str): |
| 60 | + # First check if file exists in build directory |
| 61 | + file_path = os.path.join(BUILD_DIR, full_path) |
| 62 | + if os.path.exists(file_path): |
| 63 | + return FileResponse(file_path) |
| 64 | + # Otherwise serve index.html for client-side routing |
| 65 | + return FileResponse(INDEX_HTML) |
| 66 | + |
| 67 | + |
| 68 | +if __name__ == "__main__": |
| 69 | + uvicorn.run(app, host="127.0.0.1", port=3000) |
0 commit comments