Skip to content

Commit

Permalink
fix: handle unnecessary checks for build files
Browse files Browse the repository at this point in the history
  • Loading branch information
jsstevenson committed Aug 7, 2024
1 parent 9f64b22 commit 10a84db
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 29 deletions.
7 changes: 0 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,6 @@ You can run:
yarn install --ignore-engines
```

Next, run the following commands:

```
yarn build
mv build/ ../server/curfu/build
```

Then start the development server:

```commandline
Expand Down
61 changes: 39 additions & 22 deletions server/src/curfu/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Provide FastAPI application and route declarations."""

import logging
from collections.abc import AsyncGenerator
from contextlib import asynccontextmanager

Expand All @@ -25,6 +26,8 @@
validate,
)

_logger = logging.getLogger(__name__)

fastapi_app = FastAPI(
title="Fusion Curation API",
description="Provide data functions to support [VICC Fusion Curation interface](fusion-builder.cancervariants.org/).",
Expand Down Expand Up @@ -66,32 +69,46 @@


def serve_react_app(app: FastAPI) -> FastAPI:
"""Wrap application initialization in Starlette route param converter.
"""Wrap application initialization in Starlette route param converter. This ensures
that the static web client files can be served from the backend.
Client source must be available at the location specified by `BUILD_DIR` in a
production environment. However, this may not be necessary during local development,
so the `RuntimeError` is simply caught and logged.
For the live service, `.ebextensions/01_build.config` includes code to build a
production version of the client and move it to the proper location.
:param app: FastAPI application instance
:return: application with React frontend mounted
"""
app.mount(
"/static/",
StaticFiles(directory=BUILD_DIR / "static"),
name="React application static files",
)
templates = Jinja2Templates(directory=BUILD_DIR.as_posix())

@app.get("/{full_path:path}", include_in_schema=False)
async def serve_react_app(request: Request, full_path: str) -> TemplateResponse: # noqa: ARG001
"""Add arbitrary path support to FastAPI service.
React-router provides something akin to client-side routing based out
of the Javascript embedded in index.html. However, FastAPI will intercede
and handle all client requests, and will 404 on any non-server-defined paths.
This function reroutes those otherwise failed requests against the React-Router
client, allowing it to redirect the client to the appropriate location.
:param request: client request object
:param full_path: request path
:return: Starlette template response object
"""
return templates.TemplateResponse("index.html", {"request": request})
try:
static_files = StaticFiles(directory=BUILD_DIR / "static")
except RuntimeError:
_logger.error("Unable to access static build files -- does the folder exist?")
else:
app.mount(
"/static/",
static_files,
name="React application static files",
)
templates = Jinja2Templates(directory=BUILD_DIR.as_posix())

@app.get("/{full_path:path}", include_in_schema=False)
async def serve_react_app(request: Request, full_path: str) -> TemplateResponse: # noqa: ARG001
"""Add arbitrary path support to FastAPI service.
React-router provides something akin to client-side routing based out
of the Javascript embedded in index.html. However, FastAPI will intercede
and handle all client requests, and will 404 on any non-server-defined paths.
This function reroutes those otherwise failed requests against the React-Router
client, allowing it to redirect the client to the appropriate location.
:param request: client request object
:param full_path: request path
:return: Starlette template response object
"""
return templates.TemplateResponse("index.html", {"request": request})

return app

Expand Down

0 comments on commit 10a84db

Please sign in to comment.