Added v2 API routes and hard-coded static files to support Connect

Connect looks for /routes and /api/v2/streams/snapshot
These are now present and working.

I've also hard coded the static files directory - fixing this
properly is a work in progress.
This commit is contained in:
Richard Bowman 2023-12-11 23:59:11 +00:00
parent 60f854b7db
commit 753514c24b
2 changed files with 25 additions and 2 deletions

View file

@ -15,7 +15,8 @@ def add_static_file(app: FastAPI, fname: str, folder: str):
def add_static_files(app: FastAPI): def add_static_files(app: FastAPI):
#with importlib.resources.as_file(openflexure_microscope_server) as p: #with importlib.resources.as_file(openflexure_microscope_server) as p:
# static_path = p.join("/static/") # static_path = p.join("/static/")
static_path = "./src/openflexure_microscope_server/static" #TODO: don't hard code this!
static_path = "/var/openflexure/application/openflexure-microscope-server/src/openflexure_microscope_server/static"
if not os.path.isdir(static_path): if not os.path.isdir(static_path):
raise RuntimeError("Can't find static files :(") raise RuntimeError("Can't find static files :(")

View file

@ -2,6 +2,7 @@ from __future__ import annotations
import logging import logging
import importlib.resources import importlib.resources
import os.path import os.path
from fastapi import Response
from fastapi.staticfiles import StaticFiles from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse, RedirectResponse from fastapi.responses import FileResponse, RedirectResponse
from labthings_fastapi.thing_server import ThingServer from labthings_fastapi.thing_server import ThingServer
@ -30,4 +31,25 @@ except RuntimeError:
print("Failed to add static files - you will have to do without them!") print("Failed to add static files - you will have to do without them!")
app = thing_server.app app = thing_server.app
# TODO: update openflexure connect to make this unnecessary!!
# The endpoints below fool OpenFlexure Connect into thinking we are a
# v2 microscope, so we show up correctly.
# This is necessary until Connect is rebuilt.
@app.get("/routes")
def routes_stub() -> dict[str, dict]:
fake_routes = [
"/api/v2/",
"/api/v2/streams/snapshot",
]
return {url: {"url": url, "methods": ["GET"]} for url in fake_routes}
class JPEGResponse(Response):
media_type = "image/jpeg"
@app.get("/api/v2/streams/snapshot")
@app.head("/api/v2/streams/snapshot")
async def thumbnail() -> JPEGResponse:
blob = await thing_server.things["/camera/"].lores_mjpeg_stream.grab_frame()
return JPEGResponse(blob)