firebreak/web/app/middleware.py
ZTatman 8b7f2664d0 refactor(web): expose app settings as required/optional FastAPI deps
Mirror the session.py dependency trio: current_app_settings (honest
optional accessor), get_app_settings (required; raises 500 if reached
unconfigured), and set_app_settings (single write path). Middleware reads
through the accessor instead of poking app.state directly.

get_app_settings takes request and calls current_app_settings internally
rather than declaring it as a sub-Depends — a Depends param typed with a
TYPE_CHECKING-only forward ref makes FastAPI drop the marker and treat it
as a query param.
2026-06-10 17:03:10 -04:00

23 lines
928 B
Python

from starlette.middleware.base import BaseHTTPMiddleware
from starlette.requests import Request
from starlette.responses import RedirectResponse, Response
from app.config import current_app_settings
class SetupRequiredMiddleware(BaseHTTPMiddleware):
"""Redirect all requests to /setup when the app has not been configured yet."""
async def dispatch(self, request: Request, call_next) -> Response:
path = request.url.path
if path.startswith("/setup") or path.startswith("/static") or path == "/favicon.ico":
return await call_next(request)
if current_app_settings(request) is None:
if request.headers.get("HX-Request"):
response = Response(status_code=200)
response.headers["HX-Redirect"] = "/setup"
return response
return RedirectResponse("/setup", status_code=302)
return await call_next(request)