akkudoktoreos.server.dash.context.IngressMiddleware

class akkudoktoreos.server.dash.context.IngressMiddleware(app: Callable[[MutableMapping[str, Any], Callable[[], Awaitable[MutableMapping[str, Any]]], Callable[[MutableMapping[str, Any]], Awaitable[None]]], Awaitable[None]], dispatch: Callable[[Request, Callable[[Request], Awaitable[Response]]], Awaitable[Response]] | None = None)

Bases: BaseHTTPMiddleware

Middleware to handle Home Assistant Ingress path prefixes.

This middleware enables FastHTML applications to work seamlessly both with and without Home Assistant Ingress. When deployed as a Home Assistant add-on with Ingress enabled, it automatically handles the path prefix routing.

Home Assistant Ingress proxies add-on traffic through paths like /api/hassio_ingress/<token>/, which requires setting the application’s root_path for correct URL generation. This middleware detects the Ingress path from the X-Ingress-Path header and configures the request scope accordingly.

When running standalone (development or direct access), the middleware passes requests through unchanged, allowing normal operation.

None

Examples

>>> from fasthtml.common import FastHTML
>>> from starlette.middleware import Middleware
>>>
>>> app = FastHTML(middleware=[Middleware(IngressMiddleware)])
>>>
>>> @app.get("/")
>>> def home():
...     return "Hello World"

Notes

  • All htmx and route URLs should use relative paths (e.g., “/api/data”)

  • The middleware automatically adapts to both Ingress and direct access

  • No code changes needed when switching between deployment modes

__init__(app: Callable[[MutableMapping[str, Any], Callable[[], Awaitable[MutableMapping[str, Any]]], Callable[[MutableMapping[str, Any]], Awaitable[None]]], Awaitable[None]], dispatch: Callable[[Request, Callable[[Request], Awaitable[Response]]], Awaitable[Response]] | None = None) None

Methods

__init__(app[, dispatch])

dispatch(request, call_next)

Process the request and set root_path if running under Ingress.

async dispatch(request: Request, call_next: Callable[[Request], Awaitable[Response]]) Response

Process the request and set root_path if running under Ingress.

Parameters:
  • request – The incoming Starlette Request object.

  • call_next – Callable to invoke the next middleware or route handler.

Returns:

The response from the application after processing.

Return type:

Response

Note

The X-Ingress-Path header is automatically added by Home Assistant when proxying requests through Ingress.

__init__(app: Callable[[MutableMapping[str, Any], Callable[[], Awaitable[MutableMapping[str, Any]]], Callable[[MutableMapping[str, Any]], Awaitable[None]]], Awaitable[None]], dispatch: Callable[[Request, Callable[[Request], Awaitable[Response]]], Awaitable[Response]] | None = None) None