Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sub app middlewares are called before main app, #1853

Closed
Anvil opened this issue Apr 28, 2017 · 1 comment · Fixed by #1854
Closed

Sub app middlewares are called before main app, #1853

Anvil opened this issue Apr 28, 2017 · 1 comment · Fixed by #1854
Labels

Comments

@Anvil
Copy link
Contributor

Anvil commented Apr 28, 2017

Long story short

According to http://aiohttp.readthedocs.io/en/stable/web.html#nested-applications,
[I]f URL is '/admin/something' middlewares from [main application] are applied first and [sub application] middlewares are the next in the call chain.

It turns out it's actually the other way.

Steps to reproduce

Run this code saved as foo.py:

#!/usr/bin/python3 -tt

import aiohttp
import aiohttp.web

async def middleware1(app, next_handler):

    async def handler(request):
        print("middleware1")
        return await next_handler(request)

    return handler
    
async def middleware2(app, next_handler):

    async def handler(request):
        print("middleware2")
        return await next_handler(request)

    return handler

async def middleware3(app, next_handler):

    async def handler(request):
        print("middleware3")
        return await next_handler(request)

    return handler

app = aiohttp.web.Application(
    middlewares=[middleware1])

subapp1 = aiohttp.web.Application(middlewares=[middleware2])
app.add_subapp("/foo", subapp1)

subapp2 = aiohttp.web.Application(middlewares=[middleware3])
subapp1.add_subapp("/foo/bar", subapp2)

aiohttp.web.run_app(app, host='127.0.0.1', port=4096)

And the run wget http://127.0.0.1:4096/foo/bar.

Current Result:

$ python3 foo.py
======== Running on http://127.0.0.1:4096 ========
(Press CTRL+C to quit)
middleware3
middleware2
middleware1

Expected output:

$ python3 foo.py
======== Running on http://127.0.0.1:4096 ========
(Press CTRL+C to quit)
middleware1
middleware2
middleware3

Your environment

Ubuntu Xenial (python 3.5.2) with aiohttp 2.0.7

Anvil pushed a commit to Anvil/aiohttp that referenced this issue Apr 28, 2017
Anvil pushed a commit to Anvil/aiohttp that referenced this issue May 11, 2017
@lock
Copy link

lock bot commented Oct 28, 2019

This thread has been automatically locked since there has not been
any recent activity after it was closed. Please open a new issue for
related bugs.

If you feel like there's important points made in this discussion,
please include those exceprts into that new issue.

@lock lock bot added the outdated label Oct 28, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant