Add scan directory to json config
This commit is contained in:
parent
13c63f8481
commit
b45f5651ab
6 changed files with 54 additions and 19 deletions
|
|
@ -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)
|
||||
|
|
@ -32,8 +48,9 @@ def serve_from_cli(argv: Optional[list[str]] = 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,
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue