app middlewares

This commit is contained in:
Jörn-Michael Miehe 2022-10-27 23:48:02 +00:00
parent cf812f23cd
commit 4567ab12f8
2 changed files with 31 additions and 0 deletions

View file

@ -1,4 +1,6 @@
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from .routers import router
from .settings import SETTINGS
@ -19,4 +21,32 @@ app = FastAPI(
redoc_url=SETTINGS.redoc_url,
)
@app.on_event("startup")
async def add_middlewares() -> None:
if SETTINGS.production_mode:
# Mount frontend in production mode
app.mount(
path="/",
app=StaticFiles(
directory=SETTINGS.ui_directory,
html=True,
),
name="frontend",
)
else:
# Allow CORS in debug mode
app.add_middleware(
CORSMiddleware,
allow_origins=[
"*",
],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
expose_headers=["*"],
)
app.include_router(router)

View file

@ -35,6 +35,7 @@ class Settings(BaseSettings):
#####
production_mode: bool = False
ui_directory: str = "/html"
#####
# openapi settings