mirror of
https://gitlab.archlinux.org/archlinux/aurweb.git
synced 2025-02-03 10:43:03 +01:00
fix(deps): fastapi 0.92.0 upgrade
middleware must be added before startup: fixes: "RuntimeError: Cannot add middleware after an application has started" https://fastapi.tiangolo.com/release-notes/#0910 Signed-off-by: moson-mo <mo-son@mailbox.org>
This commit is contained in:
parent
c0390240bc
commit
52c962a590
2 changed files with 20 additions and 11 deletions
|
@ -38,6 +38,8 @@ logger = aur_logging.get_logger(__name__)
|
||||||
# Setup the FastAPI app.
|
# Setup the FastAPI app.
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
||||||
|
session_secret = aurweb.config.get("fastapi", "session_secret")
|
||||||
|
|
||||||
# Instrument routes with the prometheus-fastapi-instrumentator
|
# Instrument routes with the prometheus-fastapi-instrumentator
|
||||||
# library with custom collectors and expose /metrics.
|
# library with custom collectors and expose /metrics.
|
||||||
instrumentator().add(prometheus.http_api_requests_total())
|
instrumentator().add(prometheus.http_api_requests_total())
|
||||||
|
@ -68,7 +70,6 @@ async def app_startup():
|
||||||
f"Supported backends: {str(aurweb.db.DRIVERS.keys())}"
|
f"Supported backends: {str(aurweb.db.DRIVERS.keys())}"
|
||||||
)
|
)
|
||||||
|
|
||||||
session_secret = aurweb.config.get("fastapi", "session_secret")
|
|
||||||
if not session_secret:
|
if not session_secret:
|
||||||
raise Exception("[fastapi] session_secret must not be empty")
|
raise Exception("[fastapi] session_secret must not be empty")
|
||||||
|
|
||||||
|
@ -84,10 +85,6 @@ async def app_startup():
|
||||||
"/static/images", StaticFiles(directory="web/html/images"), name="static_images"
|
"/static/images", StaticFiles(directory="web/html/images"), name="static_images"
|
||||||
)
|
)
|
||||||
|
|
||||||
# Add application middlewares.
|
|
||||||
app.add_middleware(AuthenticationMiddleware, backend=BasicAuthBackend())
|
|
||||||
app.add_middleware(SessionMiddleware, secret_key=session_secret)
|
|
||||||
|
|
||||||
# Add application routes.
|
# Add application routes.
|
||||||
def add_router(module):
|
def add_router(module):
|
||||||
app.include_router(module.router)
|
app.include_router(module.router)
|
||||||
|
@ -320,3 +317,8 @@ async def id_redirect_middleware(request: Request, call_next: typing.Callable):
|
||||||
return RedirectResponse(f"{path}/{id}{qs}")
|
return RedirectResponse(f"{path}/{id}{qs}")
|
||||||
|
|
||||||
return await util.error_or_result(call_next, request)
|
return await util.error_or_result(call_next, request)
|
||||||
|
|
||||||
|
|
||||||
|
# Add application middlewares.
|
||||||
|
app.add_middleware(AuthenticationMiddleware, backend=BasicAuthBackend())
|
||||||
|
app.add_middleware(SessionMiddleware, secret_key=session_secret)
|
||||||
|
|
|
@ -68,12 +68,19 @@ async def test_asgi_startup_session_secret_exception(monkeypatch):
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_asgi_startup_exception(monkeypatch):
|
async def test_asgi_startup_exception():
|
||||||
with mock.patch.dict(os.environ, {"AUR_CONFIG": "conf/config.defaults"}):
|
# save proper session secret
|
||||||
aurweb.config.rehash()
|
prev_secret = aurweb.asgi.session_secret
|
||||||
|
|
||||||
|
# remove secret
|
||||||
|
aurweb.asgi.session_secret = None
|
||||||
|
|
||||||
|
# startup should fail
|
||||||
with pytest.raises(Exception):
|
with pytest.raises(Exception):
|
||||||
await aurweb.asgi.app_startup()
|
await aurweb.asgi.app_startup()
|
||||||
aurweb.config.rehash()
|
|
||||||
|
# restore previous session secret after test
|
||||||
|
aurweb.asgi.session_secret = prev_secret
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
|
|
Loading…
Add table
Reference in a new issue