Middlewares

Note

This topic describes middleware support in Micro.Web

Concept

The aim of middleware design in Micro.Web is to provide framework for writing isolated, consistent and testable middlewares. Instead of traditional wsgi-like approach, which, for example, aiohttp provide, Micro.Web middlewares are inspired by Django middlewares .

This way, each middleware declares several hooks, which are invoked in process of request handling.

User-defined middlewares should inherit base class Middleware, and re-define its methods for custom processing of the hooks.

The middleware hooks should return None. If the hook return the subclass of aiohttp.web_reqrep.StreamResponse, this terminates middleware processing flow, and the response will bypass all other middleware hooks except response hook, which is invoked for every response.

See Middleware module reference for details.

Hooks

This describes the hooks which are invoked during request handling flow.

Request hook

micro.web.middleware.request(request)
Parameters:request (micro.web.request.Request) – An incoming request instance.
Returns:None or response to write

Request hook is invoked right after the request initialization, before routing call. Every request to Micro.Web will pass this hook, even if there is no route to match it.

Route match hook

micro.web.middleware.match_info(request)
Parameters:
  • request (micro.web.request.Request) – An incoming request instance.
  • match_info (aiohttp.web_urldispatcher.UrlMappingMatchInfo) – An aiohttp`s match info object
Returns:

None or response to write

Request hook nvoked right after the routing initialization, if the matching route is found. This hook will be executed only for requests for valid routes (i.e. that which declared by the application)

Response hook

micro.web.middleware.response(request, response, result)
Parameters:
  • request (micro.web.request.Request) – An incoming request instance
  • response (aiohttp.web_reqrep.StreamResponse) – An outcoming response instance, StreamResponse subclass
  • result – A result of request handler, simply the return value of the handler function or any middleware processing hooks.
Returns:

None or response to write

Response hook invoked for every response.

Exception hook

micro.web.middleware.response(request)
Parameters:
Returns:

None or response to write

Processes the exception happened during the request handling.

Installation

Middlewares can be enabled globally (for any request handler), or just for particular endpoints.

Function for installing global middlewares:

micro.web.middleware.install_global(middleware_class)[source]

Install global middleware class

Parameters:middleware_class (micro.web.middleware.Middleware inheritor) – A middleware class to install
micro.web.middleware.install_for_endpoint(middleware_class, endpoint_name=None)[source]

Install middleware class to be run for endpoint

Parameters:
  • middleware_class (micro.web.middleware.Middleware inheritor) – A middleware class to install
  • endpoint_name – Name of endpoint which we must decorate in middleware

Also, there is convenient Decorator for enabling middlewares for single request handler function