Skip to content

Commit

Permalink
Fix #1909: add example for middleware execution order
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov committed Aug 3, 2017
1 parent e9bf20d commit 9a9b39d
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions docs/web.rst
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,43 @@ if user has no permissions to access the underlying resource.
They may also render errors raised by the handler, perform some pre- or
post-processing like handling *CORS* and so on.

The following code demonstrates middlewares execution order::

from aiohttp import web
def test(request):
print('Handler function called')
return web.Response(text="Hello")

async def middleware1(app, handler):
async def middleware_handler(request):
print('Middleware 1 called')
response = await handler(request)
print('Middleware 1 finished')

return response
return middleware_handler

async def middleware2(app, handler):
async def middleware_handler(request):
print('Middleware 2 called')
response = await handler(request)
print('Middleware 2 finished')

return response
return middleware_handler


app = web.Application(middlewares=[middleware1, middleware2])
app.router.add_get('/', test)
web.run_app(app)

Produced output::

Middleware 1 called
Middleware 2 called
Handler function called
Middleware 2 finished
Middleware 1 finished

Example
^^^^^^^
Expand Down

0 comments on commit 9a9b39d

Please sign in to comment.