Fix data directory save location and mount path

This commit is contained in:
Julian Stirling 2026-05-10 13:57:12 +01:00
parent fa5aaaf254
commit b6efb73cba
4 changed files with 13 additions and 7 deletions

View file

@ -86,7 +86,7 @@ def customise_server(
) )
add_v2_endpoints(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. # Configure logging to DEBUG if requested in CLI args.
if debug: if debug:

View file

@ -6,6 +6,8 @@ from fastapi import FastAPI
from fastapi.responses import FileResponse, RedirectResponse from fastapi.responses import FileResponse, RedirectResponse
from fastapi.staticfiles import StaticFiles from fastapi.staticfiles import StaticFiles
import labthings_fastapi as lt
THIS_DIR = os.path.dirname(os.path.abspath(__file__)) THIS_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_PATH = os.path.normpath(os.path.join(THIS_DIR, "..", "static")) 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. """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 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 The Vue CSS and JS are hashed, so if updated their filename will update. The most
important file not to cache is "index.html". 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. :param data_folder: The directory for any data.
""" """
app = server.app
check_static_dir() check_static_dir()
@app.get("/", response_class=RedirectResponse) @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): if not os.path.isdir(data_folder):
os.makedirs(data_folder) os.makedirs(data_folder)
app.mount( app.mount(
"/data/", server._api_prefix.rstrip("/") + "/data/",
StaticFiles(directory=data_folder), StaticFiles(directory=data_folder),
name="data", name="data",
) )

View file

@ -31,7 +31,7 @@ class OFMThing(lt.Thing):
raise ValueError("No application configuration was supplied.") raise ValueError("No application configuration was supplied.")
app_data_dir = application_config["data_folder"] app_data_dir = application_config["data_folder"]
self._data_dir = os.path.join( 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 return self

View file

@ -124,10 +124,13 @@ def test_add_static_files(mock_static_dir, mocker):
"openflexure_microscope_server.server.serve_static_files.STATIC_PATH", "openflexure_microscope_server.server.serve_static_files.STATIC_PATH",
mock_static_dir, 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 # Get the wrapper function from the mocked decorator
wrapper = mock_app.get.return_value wrapper = mock_app.get.return_value
with tempfile.TemporaryDirectory() as datadir: 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 # Get should have been called twice to create a route for index
assert mock_app.get.call_count == 2 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 "/assets/" in mounted_path
assert os.path.join(mock_static_dir, "assets") in mounted_dir 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 assert mock_app.mount.call_args_list[1].args[1].directory == datadir