It's a peach

examples/global_middleware.py

"""
An example of global middleware.
"""

from bareasgi import (
    Application,
    Scope,
    Info,
    RouteMatches,
    Content,
    text_writer,
    HttpResponse,
    HttpRequestCallback
)


async def first_middleware(
        scope: Scope,
        info: Info,
        matches: RouteMatches,
        content: Content,
        handler: HttpRequestCallback
) -> HttpResponse:
    """The first part of a middleware chain"""
    print("First middleware - entry")
    info['message'] = 'This is first the middleware. '
    status, headers, response, pushes = await handler(
        scope,
        info,
        matches,
        content
    )
    print("First middleware - exit")
    return status, headers, response, pushes


async def second_middleware(
        scope: Scope,
        info: Info,
        matches: RouteMatches,
        content: Content,
        handler: HttpRequestCallback
) -> HttpResponse:
    """The second part of a middleware chain"""
    print("Second middleware - entry")
    info['message'] += 'This is the second middleware.'
    response = await handler(scope, info, matches, content)
    print("Second middleware - exit")
    return response


async def http_request_callback(
        _scope: Scope,
        info: Info,
        _matches: RouteMatches,
        _content: Content
) -> HttpResponse:
    """The final request handler"""
    return 200, [(b'content-type', b'text/plain')], text_writer(info['message'])


if __name__ == "__main__":
    import uvicorn

    app = Application(
        middlewares=[
            first_middleware,
            second_middleware
        ]
    )
    app.http_router.add(
        {'GET', 'POST', 'PUT', 'DELETE'},
        '/',
        http_request_callback
    )

    uvicorn.run(app, port=9009)