Merge branch 'config-scan-dir' into 'v3'

Add scan directory to json config

Closes #393

See merge request openflexure/openflexure-microscope-server!283
This commit is contained in:
Joe Knapper 2025-06-09 13:42:27 +00:00
commit ae2ec1864f
6 changed files with 58 additions and 19 deletions

View file

@ -10,17 +10,33 @@ from .legacy_api import add_v2_endpoints
from ..logging import configure_logging, retrieve_log, retrieve_log_from_file
def customise_server(server: ThingServer, log_folder: str):
def customise_server(server: ThingServer, log_folder: str, scans_folder: Optional[str]):
"""Customise the server with additional endpoints, etc."""
configure_logging(log_folder)
add_v2_endpoints(server)
add_static_files(server.app)
add_static_files(server.app, scans_folder)
# Add an endpoint to get the logs - (directly calling the FastAPI decorator)
server.app.get("/log/")(retrieve_log)
server.app.get("/logfile/")(retrieve_log_from_file)
def _get_scans_dir(config: dict) -> Optional[str]:
"""
Read the config and return the scans directory.
Return is None if there is no /smart_scan/ thing loaded.
"""
if "/smart_scan/" in config["things"]:
try:
return config["things"]["/smart_scan/"]["kwargs"]["scans_folder"]
except KeyError as e:
msg = "Configuration error smart scan should have scans_folder kwarg set"
raise RuntimeError(msg) from e
return None
def serve_from_cli(argv: Optional[list[str]] = None):
"""Start the server from the command line"""
args = cli.parse_args(argv)
@ -29,11 +45,16 @@ def serve_from_cli(argv: Optional[list[str]] = None):
log_config["loggers"]["uvicorn"]["propagate"] = True
log_config["loggers"]["uvicorn.access"]["propagate"] = True
# Create server and config vars before trying to configure so they are defined
# if fallback is needed before they are set.
config = None
server = None
try:
config = cli.config_from_args(args)
log_folder = config.get("log_folder", "./openflexure/logs")
scans_folder = _get_scans_dir(config)
server = cli.server_from_config(config)
customise_server(server, log_folder)
customise_server(server, log_folder, scans_folder)
uvicorn.run(
server.app,
host=args.host,

View file

@ -1,4 +1,5 @@
import os
from typing import Optional
from fastapi.responses import FileResponse, RedirectResponse
from fastapi.staticfiles import StaticFiles
@ -22,7 +23,7 @@ def add_static_file(app: FastAPI, fname: str, folder: str) -> None:
)
def add_static_files(app: FastAPI) -> 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
@ -45,9 +46,13 @@ def add_static_files(app: FastAPI) -> None:
name=f"static_{fname}",
)
# Mount the scan directory to .../scans/, to allow dzi viewing
app.mount(
"/scans/",
StaticFiles(directory="/var/openflexure/scans/"),
name="scans",
)
# If scans folder is None, there is not smart scan thing. So nothing to mount.
if scans_folder is not None:
# Mount the scan directory to .../scans/, to allow dzi viewing
if not os.path.isdir(scans_folder):
os.makedirs(scans_folder)
app.mount(
"/scans/",
StaticFiles(directory=scans_folder),
name="scans",
)

View file

@ -114,7 +114,8 @@ def _scan_running(method):
class SmartScanThing(Thing):
def __init__(self):
def __init__(self, scans_folder):
self._scans_folder = scans_folder
self._preview_stitch_popen = None
self._preview_stitch_popen_lock = threading.Lock()
self._scan_lock = threading.Lock()
@ -270,11 +271,8 @@ class SmartScanThing(Thing):
@property
def base_scan_dir(self) -> str:
"""This directory will hold all the scans we do."""
# TODO: This should be determined using sensible configuration.
# If the working directory is `/var/openflexure` this will result
# in scans being saved at `/var/openflexure/scans/`
return "scans"
"""The path of the directory where the scans are saved."""
return self._scans_folder
@thing_property
def latest_scan_name(self) -> Optional[str]: