From b6efb73cba3adf2b396e18bfbe5a743b37d4b1d5 Mon Sep 17 00:00:00 2001 From: Julian Stirling Date: Sun, 10 May 2026 13:57:12 +0100 Subject: [PATCH] Fix data directory save location and mount path --- src/openflexure_microscope_server/server/__init__.py | 2 +- .../server/serve_static_files.py | 9 ++++++--- src/openflexure_microscope_server/things/__init__.py | 2 +- tests/unit_tests/test_serve_static_files.py | 7 +++++-- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/openflexure_microscope_server/server/__init__.py b/src/openflexure_microscope_server/server/__init__.py index 754a7799..52ee182f 100644 --- a/src/openflexure_microscope_server/server/__init__.py +++ b/src/openflexure_microscope_server/server/__init__.py @@ -86,7 +86,7 @@ def customise_server( ) add_v2_endpoints(server) - add_static_files(server.app, application_config.data_folder) + add_static_files(server, application_config.data_folder) # Configure logging to DEBUG if requested in CLI args. if debug: diff --git a/src/openflexure_microscope_server/server/serve_static_files.py b/src/openflexure_microscope_server/server/serve_static_files.py index 81338cc8..0d360a73 100644 --- a/src/openflexure_microscope_server/server/serve_static_files.py +++ b/src/openflexure_microscope_server/server/serve_static_files.py @@ -6,6 +6,8 @@ from fastapi import FastAPI from fastapi.responses import FileResponse, RedirectResponse from fastapi.staticfiles import StaticFiles +import labthings_fastapi as lt + THIS_DIR = os.path.dirname(os.path.abspath(__file__)) STATIC_PATH = os.path.normpath(os.path.join(THIS_DIR, "..", "static")) @@ -35,7 +37,7 @@ def add_static_file(app: FastAPI, fname: str, folder: str) -> None: ) -def add_static_files(app: FastAPI, data_folder: str) -> None: +def add_static_files(server: lt.ThingServer, data_folder: str) -> None: """Add the static files responsible for the webapp app to the FastAPI app. Note that any file in the root of the static dir will not be cached. However, the @@ -43,9 +45,10 @@ def add_static_files(app: FastAPI, data_folder: str) -> None: 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 server: The LabThings server. :param data_folder: The directory for any data. """ + app = server.app check_static_dir() @app.get("/", response_class=RedirectResponse) @@ -71,7 +74,7 @@ def add_static_files(app: FastAPI, data_folder: str) -> None: if not os.path.isdir(data_folder): os.makedirs(data_folder) app.mount( - "/data/", + server._api_prefix.rstrip("/") + "/data/", StaticFiles(directory=data_folder), name="data", ) diff --git a/src/openflexure_microscope_server/things/__init__.py b/src/openflexure_microscope_server/things/__init__.py index b35f1fdf..c497209f 100644 --- a/src/openflexure_microscope_server/things/__init__.py +++ b/src/openflexure_microscope_server/things/__init__.py @@ -31,7 +31,7 @@ class OFMThing(lt.Thing): raise ValueError("No application configuration was supplied.") app_data_dir = application_config["data_folder"] self._data_dir = os.path.join( - os.path.normpath(str(app_data_dir)), os.path.normpath(self.path.strip("/")) + os.path.normpath(str(app_data_dir)), os.path.normpath(self.name) ) return self diff --git a/tests/unit_tests/test_serve_static_files.py b/tests/unit_tests/test_serve_static_files.py index ba5fb693..62a741d0 100644 --- a/tests/unit_tests/test_serve_static_files.py +++ b/tests/unit_tests/test_serve_static_files.py @@ -124,10 +124,13 @@ def test_add_static_files(mock_static_dir, mocker): "openflexure_microscope_server.server.serve_static_files.STATIC_PATH", mock_static_dir, ) + mock_server = mocker.Mock() + mock_server.app = mock_app + mock_server._api_prefix = "/api/v3" # Get the wrapper function from the mocked decorator wrapper = mock_app.get.return_value with tempfile.TemporaryDirectory() as datadir: - serve_static_files.add_static_files(mock_app, data_folder=datadir) + serve_static_files.add_static_files(mock_server, data_folder=datadir) # Get should have been called twice to create a route for index assert mock_app.get.call_count == 2 @@ -158,5 +161,5 @@ def test_add_static_files(mock_static_dir, mocker): assert "/assets/" in mounted_path assert os.path.join(mock_static_dir, "assets") in mounted_dir - assert mock_app.mount.call_args_list[1].args[0] == "/data/" + assert mock_app.mount.call_args_list[1].args[0] == "/api/v3/data/" assert mock_app.mount.call_args_list[1].args[1].directory == datadir