asphalt.web.asgi3
- class asphalt.web.asgi3.ASGIComponent(components=None, *, app, host='127.0.0.1', port=8000, middlewares=())
Bases:
ContainerComponent
,Generic
[T_Application
]A component that serves the given ASGI 3.0 application via Uvicorn.
- Parameters:
app (asgiref.typing.ASGI3Application | str) – the ASGI application to serve, or a module:varname reference to one
host – the IP address to bind to
port – the port to bind to
middlewares – list of callables or dicts to be added as middleware using
add_middleware()
- add_middleware(middleware)
Add middleware to the application.
- Parameters:
middleware – either a callable that takes the application object and returns an ASGI 3.0 application, or a dictionary containing a reference to such a callable. This dictionary must contain the key
type
which is a non-async callable (or a module:varname reference to one) and which will be called with the application object as the first positional argument and the rest of the keys in the dict as keyword arguments.
- async start_server(ctx)
Start the HTTP server.
This method is called by the component after the subcomponents have been started. If you need to add any middleware that requires resources provided by subcomponents, you can override this method and call the superclass implementation after the middleware has been added.
- Return type:
AsyncGenerator[None, Exception | None]
- class asphalt.web.asgi3.AsphaltMiddleware(app)
Bases:
object
Generic ASGI middleware for Asphalt integration.
This middleware wraps both HTTP requests and websocket connections in their own contexts and exposes the ASGI scope object as a resource.
- Parameters:
app (asgiref.typing.ASGI3Application) – an ASGI 3.0 application