MiddlewareΒΆ

Middleware acts as a middleman between the web server and your app. The middleware wraps around the app, allowing it to modify a request before your app receives it or modify a response after your app processes it. An app can have several middleware, all wrapped around the app like the layers of an onion.

Add the following lines to demo.py:

from drakken.middleware import Middleware

class SimpleCustomMiddleware(Middleware):
    def process_request(self, request):
        print(f'Processing request: {request.url}')

    def process_response(self, request, response):
        print(f'Processing response: {request.url}')

app.add_middleware(SimpleCustomMiddleware)

Launch the app and visit http://127.0.0.1:8000. You should see the statements Processing request and Processing response in the terminal.