Skip to content

Commit

Permalink
feat: catch-all route in FastAPI allows for deep links in prod (#25) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
holtgrewe authored Sep 1, 2023
1 parent 5459db5 commit 454f211
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions backend/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,18 @@ async def favicon():
return FileResponse(pathlib.Path(__file__).parent.parent.parent / "frontend/public/favicon.ico")


# Routes
# Server front-end (assets directory and index file for root ("/") entrypoint) when configured
if SERVE_FRONTEND: # pragma: no cover
print(f"SERVE_FRONTEND = {SERVE_FRONTEND}", file=sys.stderr)
print(f"serving front-end from {SERVE_FRONTEND}", file=sys.stderr)
app.mount("/assets", StaticFiles(directory=f"{SERVE_FRONTEND}/assets"), name="ui")

@app.get("/")
async def index():
"""Render the index.html page at the root URL"""
return FileResponse(f"{SERVE_FRONTEND}/index.html")

@app.api_route("/{path_name:path}", methods=["GET"])
async def catch_all(request: Request, path_name: str):
"""Catch-all route forwarding to frontend."""
_, _ = request, path_name
return FileResponse(f"{SERVE_FRONTEND}/index.html")

0 comments on commit 454f211

Please sign in to comment.