myst/web/app/middleware.py
ZTatman 442261f403 fix(web): mypy fixes, Starlette exception handler, disable CTA without PAT
- Fix return type annotations to use Response for TemplateResponse returns
- Fix db.scalar nullability with `or 0` for mypy
- Catch StarletteHTTPException for 404/405 error page rendering
- Disable "Create share link" button when PAT not registered
- Formatter/linter cleanup

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-06-05 11:19:36 -04:00

21 lines
884 B
Python

from starlette.middleware.base import BaseHTTPMiddleware
from starlette.requests import Request
from starlette.responses import RedirectResponse, Response
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 request.app.state.app_settings 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)