Merge branch 'check-static-files' into 'v3'

Check static files exist or throw a useful error

See merge request openflexure/openflexure-microscope-server!368
This commit is contained in:
Julian Stirling 2025-08-15 11:22:41 +00:00
commit 46802db308
2 changed files with 155 additions and 7 deletions

View file

@ -8,6 +8,7 @@ from fastapi.staticfiles import StaticFiles
from fastapi import FastAPI
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_PATH = os.path.normpath(os.path.join(THIS_DIR, "..", "static"))
NO_CACHE_HEADERS = {
"Cache-Control": "no-store, no-cache, must-revalidate, max-age=0",
@ -27,7 +28,7 @@ def add_static_file(app: FastAPI, fname: str, folder: str) -> None:
folder: the containing folder of the file
"""
file_path = os.path.join(folder, fname)
# Cache font files, but not other static files.
# Cache font files, but not other static files in the root static directory.
headers = {} if fname.endswith(".woff2") else NO_CACHE_HEADERS
app.get(f"/{fname}", response_class=FileResponse, include_in_schema=False)(
@ -38,19 +39,25 @@ def add_static_file(app: FastAPI, fname: str, folder: str) -> None:
def add_static_files(app: FastAPI, scans_folder: Optional[str]) -> 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
Note that any file in the root of the static dir will not be cached. However, the
files in mounted subdirectories are not sent with no-cache headers.
The Vue CSS and JS are hashed, so if updated their filename will update. The most
important file not to cache is "index.html".
:param app: The FastAPI app to add to, in this case the OpenFlexure server
:param scans_folder: The directory for the scans.
"""
static_path = os.path.normpath(os.path.join(THIS_DIR, "..", "static"))
check_static_dir()
@app.get("/", response_class=RedirectResponse)
async def redirect_fastapi():
return "/index.html"
# Mounting the webapp at / file by file to allow other endpoints to be created
for fname in os.listdir(static_path):
fpath = os.path.join(static_path, fname)
for fname in os.listdir(STATIC_PATH):
fpath = os.path.join(STATIC_PATH, fname)
if os.path.isfile(fpath):
add_static_file(app, fname, static_path)
add_static_file(app, fname, STATIC_PATH)
elif os.path.isdir(fpath):
app.mount(
f"/{fname}/",
@ -68,3 +75,22 @@ def add_static_files(app: FastAPI, scans_folder: Optional[str]) -> None:
StaticFiles(directory=scans_folder),
name="scans",
)
def check_static_dir():
"""Check that the static dir exists and contains expected files and dirs."""
if not os.path.isdir(STATIC_PATH):
raise FileNotFoundError(
"The static directory does not exist. You will need to pull or compile the"
"web app."
)
expected = [
os.path.isdir(os.path.join(STATIC_PATH, "js")),
os.path.isdir(os.path.join(STATIC_PATH, "css")),
os.path.isfile(os.path.join(STATIC_PATH, "index.html")),
]
if not all(expected):
raise FileNotFoundError(
"The static directory does not contain a valid web app. You will need to "
"pull or compile the web app."
)