Merge branch 'update_serve_static_files' into 'v3'
Update serve static files See merge request openflexure/openflexure-microscope-server!265
This commit is contained in:
commit
8f45a9e9a9
1 changed files with 17 additions and 5 deletions
|
|
@ -4,18 +4,30 @@ from fastapi.responses import FileResponse, RedirectResponse
|
||||||
from fastapi.staticfiles import StaticFiles
|
from fastapi.staticfiles import StaticFiles
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
|
|
||||||
|
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
|
||||||
def add_static_file(app: FastAPI, fname: str, folder: str):
|
|
||||||
|
def add_static_file(app: FastAPI, fname: str, folder: str) -> None:
|
||||||
|
"""Add a single file to the root of the FastAPI app
|
||||||
|
The file with name `fname` will be mounted at `/fname` - the
|
||||||
|
`folder` does not affect where it is mounted in the app.
|
||||||
|
|
||||||
|
app: The FastAPI app to add to, in this case the OpenFlexure server
|
||||||
|
fname: the name of the file to add
|
||||||
|
folder: the containing folder of the file
|
||||||
|
"""
|
||||||
p = os.path.join(folder, fname)
|
p = os.path.join(folder, fname)
|
||||||
app.get(f"/{fname}", response_class=FileResponse, include_in_schema=False)(
|
app.get(f"/{fname}", response_class=FileResponse, include_in_schema=False)(
|
||||||
lambda: FileResponse(p)
|
lambda: FileResponse(p)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def add_static_files(app: FastAPI):
|
def add_static_files(app: FastAPI) -> None:
|
||||||
static_path = os.path.abspath(os.path.join(__file__, "..", "static"))
|
"""Add the static files responsible for the webapp app to the FastAPI app
|
||||||
if not os.path.isdir(static_path):
|
|
||||||
raise RuntimeError("Can't find static files")
|
app: The FastAPI app to add to, in this case the OpenFlexure server
|
||||||
|
"""
|
||||||
|
static_path = os.path.normpath(os.path.join(THIS_DIR, "..", "static"))
|
||||||
|
|
||||||
@app.get("/", response_class=RedirectResponse)
|
@app.get("/", response_class=RedirectResponse)
|
||||||
async def redirect_fastapi():
|
async def redirect_fastapi():
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue