Skip to content

Commit

Permalink
fix: handle unnecessary checks for build files (#309)
Browse files Browse the repository at this point in the history

Does a few things

    @korikuzma noticed that the README description of copying build files had an incorrect path. However, this instruction is actually unnecessary (and impractical tbh). In development you'd be better off letting yarn start handle service of client files because it has hot-reloading on changes. I removed it.
    Rather than requiring client files to be present, catches + logs their absence if they're not there. This is better for development. Originally I added this code in the big VRS update PR but it should've been a separate issue. I would like to see us reexamine our logging initialization/setup in another issue, because it could be bad if it's not working properly.
    Adds some additional description of why the client service code is there + what it needs.
  • Loading branch information
jsstevenson authored and korikuzma committed Aug 12, 2024
1 parent 195b860 commit 381bd22
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 381bd22

Please sign in to comment.