From d9711522d5f3f1eb3ea4954ae47fd2e1cee52f59 Mon Sep 17 00:00:00 2001 From: Richard Bowman Date: Tue, 12 Dec 2023 22:55:50 +0000 Subject: [PATCH] Detect static directories Simplified code and removed hard-coded list of static dirs. --- .../serve_static_files.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/openflexure_microscope_server/serve_static_files.py b/src/openflexure_microscope_server/serve_static_files.py index 6476393b..55dbeb97 100644 --- a/src/openflexure_microscope_server/serve_static_files.py +++ b/src/openflexure_microscope_server/serve_static_files.py @@ -20,14 +20,16 @@ def add_static_files(app: FastAPI): if not os.path.isdir(static_path): raise RuntimeError("Can't find static files :(") - for folder in ["css", "js", "fonts"]: - app.mount( - f"/{folder}/", - StaticFiles(directory=os.path.join(static_path, folder)), - name=f"static_{folder}", - ) @app.get("/", response_class=RedirectResponse) async def redirect_fastapi(): return "/index.html" for fname in os.listdir(static_path): - add_static_file(app, fname, static_path) + fpath = os.path.join(static_path, fname) + if os.path.isfile(fpath): + add_static_file(app, fname, static_path) + elif os.path.isdir(fpath): + app.mount( + f"/{fname}/", + StaticFiles(directory=fpath), + name=f"static_{fname}", + )