Python Falcon – Middleware
”;
A “middleware” is a function that is processed with every request (before being processed by any specific responder) as well as with every response before returning it. This function takes each request that comes to your application.
A middleware works similar to hooks. However, unlike hooks, middleware methods apply globally to the entire App. It may perform some process with the request by running a code defined in it, and then passes the request to be processed the corresponding operation function. It can also process the response generated by the operation function before returning it.
A middleware is a class that implements one or more of the following even handler methods. For a WSGI app, the methods are −
-
process_request (self, req, resp) − This method processes the request before routing it.
-
process_resource (self, req, resp, resource, params) − processes the request after routing. A dict object representing any additional params derived from the route”s URI template fields may be passed.
-
process_response (self, req, resp, resource, req_succeeded) − This method is for post-processing of the response (after routing). The req_succeeded parameter is True if no exceptions were raised otherwise False.
In case of the ASGI app, in addition to the above methods, the middleware class may define some more methods.
To account for lifespan events, an optional part of ASGI specification, the startup and shutdown event handlers may be included.
-
process_startup (self, scope, event) − This method processes the ASGI lifespan startup event. It is invoked when the server is ready to start up and receive connections, but before it has started to do so.
-
process_shutdown(self, scope, event) − This method processes the ASGI lifespan shutdown event. It is invoked when the server has stopped accepting connections and closed all active connections.
Since the ASGI application also responds to the requests under Websocket protocol, the middleware may define following coroutine methods −
-
process_request_ws (self, req, ws) − This method processes a WebSocket handshake request before routing it.
-
process_resource_ws (self, req, ws, resource, params) − This method processes a WebSocket handshake request after routing. A dict object derived from the route”s URI template fields may be passed to the resource”s responder.
An instance of the middleware class has to be added to the Falcon application object at the time of initialization. For a WSGI Falcon app −
class MyMiddleware: def process_request(self, req, resp): pass def process_resource(self, req, resp, resource, params): pass def process_response(self, req, resp, resource, req_succeeded): pass from falcon import App app=App(middleware=[MyMiddleware()])
For the ASGI app −
class MyMiddleware: async def process_startup(self, scope, event): pass async def process_shutdown(self, scope, event): pass async def process_request(self, req, resp): pass async def process_resource(self, req, resp, resource, params): pass async def process_response(self, req, resp, resource, req_succeeded): pass async def process_request_ws(self, req, ws): pass async def process_resource_ws(self, req, ws, resource, params): pass from falcon.asgi import App app=App(middleware=[MyMiddleware()])
”;