Search the package for static files
This isn't foolproof: a zipped package will break it. If that's something we want to support, we might need to extract the folder to a temp location.
This commit is contained in:
parent
e3e248c46e
commit
aa49cfcba3
1 changed files with 13 additions and 2 deletions
|
|
@ -2,6 +2,7 @@ from fastapi.responses import FileResponse, RedirectResponse
|
||||||
from fastapi.staticfiles import StaticFiles
|
from fastapi.staticfiles import StaticFiles
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
import os
|
import os
|
||||||
|
import pathlib
|
||||||
|
|
||||||
def add_static_file(app: FastAPI, fname: str, folder: str):
|
def add_static_file(app: FastAPI, fname: str, folder: str):
|
||||||
print(f"Adding route for /{fname}")
|
print(f"Adding route for /{fname}")
|
||||||
|
|
@ -16,8 +17,18 @@ 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/")
|
||||||
#TODO: don't hard code this!
|
#TODO: don't hard code this!
|
||||||
static_path = "/var/openflexure/application/openflexure-microscope-server/src/openflexure_microscope_server/static"
|
search_paths = [
|
||||||
if not os.path.isdir(static_path):
|
"/var/openflexure/application/openflexure-microscope-server/src/openflexure_microscope_server/static",
|
||||||
|
pathlib.Path().absolute() / "application/openflexure-microscope-server/src/openflexure_microscope_server/static",
|
||||||
|
]
|
||||||
|
if __file__:
|
||||||
|
search_paths.append(pathlib.Path(__file__).parent.parent / "static")
|
||||||
|
|
||||||
|
for static_path in search_paths:
|
||||||
|
if os.path.isdir(static_path):
|
||||||
|
break # stop once one of the paths exists
|
||||||
|
else:
|
||||||
|
# If we get to the else: block, no pat was found.
|
||||||
raise RuntimeError("Can't find static files :(")
|
raise RuntimeError("Can't find static files :(")
|
||||||
|
|
||||||
@app.get("/", response_class=RedirectResponse)
|
@app.get("/", response_class=RedirectResponse)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue