From a563c634a3f522b3758ddbe48ef80482640fa34b Mon Sep 17 00:00:00 2001 From: Joe Knapper Date: Thu, 15 May 2025 16:30:37 +0000 Subject: [PATCH] Update serve static files to serve correct directory and to add docstrings --- .../server/serve_static_files.py | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/openflexure_microscope_server/server/serve_static_files.py b/src/openflexure_microscope_server/server/serve_static_files.py index c00fda32..d87e5dec 100644 --- a/src/openflexure_microscope_server/server/serve_static_files.py +++ b/src/openflexure_microscope_server/server/serve_static_files.py @@ -4,18 +4,30 @@ from fastapi.responses import FileResponse, RedirectResponse from fastapi.staticfiles import StaticFiles from fastapi import FastAPI +THIS_DIR = os.path.dirname(os.path.abspath(__file__)) -def add_static_file(app: FastAPI, fname: str, folder: str): + +def add_static_file(app: FastAPI, fname: str, folder: str) -> None: + """Add a single file to the root of the FastAPI app + The file with name `fname` will be mounted at `/fname` - the + `folder` does not affect where it is mounted in the app. + + app: The FastAPI app to add to, in this case the OpenFlexure server + fname: the name of the file to add + folder: the containing folder of the file + """ p = os.path.join(folder, fname) app.get(f"/{fname}", response_class=FileResponse, include_in_schema=False)( lambda: FileResponse(p) ) -def add_static_files(app: FastAPI): - static_path = os.path.abspath(os.path.join(__file__, "..", "static")) - if not os.path.isdir(static_path): - raise RuntimeError("Can't find static files") +def add_static_files(app: FastAPI) -> None: + """Add the static files responsible for the webapp app to the FastAPI app + + app: The FastAPI app to add to, in this case the OpenFlexure server + """ + static_path = os.path.normpath(os.path.join(THIS_DIR, "..", "static")) @app.get("/", response_class=RedirectResponse) async def redirect_fastapi():