Serve static web app at /
This commit is contained in:
parent
da711a7d8c
commit
b9804cdc2d
3 changed files with 46 additions and 3 deletions
5
.gitignore
vendored
5
.gitignore
vendored
|
|
@ -78,4 +78,7 @@ packages.png
|
|||
openflexure_microscope/cobertura.xml
|
||||
|
||||
# labthings settings
|
||||
/settings/
|
||||
/settings/
|
||||
|
||||
# web app build
|
||||
/src/openflexure_microscope_server/static/
|
||||
32
src/openflexure_microscope_server/serve_static_files.py
Normal file
32
src/openflexure_microscope_server/serve_static_files.py
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
from fastapi.responses import FileResponse, RedirectResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from fastapi import FastAPI
|
||||
import os
|
||||
|
||||
def add_static_file(app: FastAPI, fname: str, folder: str):
|
||||
print(f"Adding route for /{fname}")
|
||||
p=os.path.join(folder, fname)
|
||||
app.get(
|
||||
f"/{fname}",
|
||||
response_class=FileResponse,
|
||||
include_in_schema=False
|
||||
)(lambda: FileResponse(p))
|
||||
|
||||
def add_static_files(app: FastAPI):
|
||||
#with importlib.resources.as_file(openflexure_microscope_server) as p:
|
||||
# static_path = p.join("/static/")
|
||||
static_path = "./src/openflexure_microscope_server/static"
|
||||
if not os.path.isdir(static_path):
|
||||
raise RuntimeError("Can't find static files :(")
|
||||
|
||||
for folder in ["css", "js", "fonts"]:
|
||||
app.mount(
|
||||
f"/{folder}/",
|
||||
StaticFiles(directory=os.path.join(static_path, folder)),
|
||||
name=f"static_{folder}",
|
||||
)
|
||||
@app.get("/", response_class=RedirectResponse)
|
||||
async def redirect_fastapi():
|
||||
return "/index.html"
|
||||
for fname in os.listdir(static_path):
|
||||
add_static_file(app, fname, static_path)
|
||||
|
|
@ -1,18 +1,26 @@
|
|||
from __future__ import annotations
|
||||
import logging
|
||||
import importlib.resources
|
||||
import os.path
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from fastapi.responses import FileResponse, RedirectResponse
|
||||
from labthings_fastapi.thing_server import ThingServer
|
||||
from labthings_sangaboard import SangaboardThing
|
||||
from labthings_sangaboard.proscan import ProScan
|
||||
from labthings_picamera2.thing import StreamingPiCamera2
|
||||
|
||||
from .things.autofocus import AutofocusThing
|
||||
from .things.camera_stage_mapping import CameraStageMapper
|
||||
from .serve_static_files import add_static_files
|
||||
import openflexure_microscope_server
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
thing_server = ThingServer()
|
||||
thing_server.add_thing(StreamingPiCamera2(), "/camera/")
|
||||
thing_server.add_thing(SangaboardThing(), "/stage/")
|
||||
thing_server.add_thing(ProScan(), "/stage/")
|
||||
thing_server.add_thing(AutofocusThing(), "/autofocus/")
|
||||
thing_server.add_thing(CameraStageMapper(), "/camera_stage_mapping/")
|
||||
add_static_files(thing_server.app)
|
||||
|
||||
|
||||
app = thing_server.app
|
||||
Loading…
Add table
Add a link
Reference in a new issue