openflexure-microscope-server/src/openflexure_microscope_server/server/__init__.py
2025-04-07 23:45:19 +01:00

63 lines
2 KiB
Python

from __future__ import annotations
from typing import Optional
from labthings_fastapi.server import cli, ThingServer
import uvicorn
from .serve_static_files import add_static_files
from .legacy_api import add_v2_endpoints
from ..logging import configure_logging, retrieve_log, retrieve_log_from_file
def customise_server(server: ThingServer):
"""Customise the server with additional endpoints, etc."""
configure_logging()
add_v2_endpoints(server)
try:
add_static_files(server.app)
except RuntimeError:
print("Failed to add static files - you will have to do without them!")
# 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 serve_from_cli(argv: Optional[list[str]] = None):
"""Start the server from the command line"""
args = cli.parse_args(argv)
try:
config, server = None, None
config = cli.config_from_args(args)
server = cli.server_from_config(config)
customise_server(server)
uvicorn.run(
server.app,
host=args.host,
port=args.port,
log_config={
"version": 1,
"disable_existing_loggers": False,
},
)
except BaseException as e:
if args.fallback:
print(f"Error: {e}")
fallback_server = "labthings_fastapi.server.fallback:app"
print(f"Starting fallback server {fallback_server}.")
app = cli.object_reference_to_object(fallback_server)
app.labthings_config = config
app.labthings_server = server
app.labthings_error = e
uvicorn.run(
app,
host=args.host,
port=args.port,
log_config={
"version": 1,
"disable_existing_loggers": False,
},
)
else:
raise e