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

Middlewares and nested applications #2454

Closed
olexandr-klymenko opened this issue Oct 31, 2017 · 6 comments
Closed

Middlewares and nested applications #2454

olexandr-klymenko opened this issue Oct 31, 2017 · 6 comments
Labels
invalid This doesn't seem right outdated

Comments

@olexandr-klymenko
Copy link

Long story short

There is no clear notion about how middlewares should work in nested applications.
By common sense if middleware is mentioned in sub application it shouldn't be invoked for root application. But as the matter of fact it doesn't work the I expected. Every middleware is applied for every endpoint which is a bit wierd.

Expected behaviour

middlewares used for declare sub application shouldn't be invoked when root application enpoint is requested

Actual behaviour

every middleware is applied for every ebdpoint

Steps to reproduce

  1. Create root application
  2. Create route in root application
  3. Create sub app with middlewares
  4. Attach sub app to root application
  5. Request root application endpoint

Your environment

python 3.6.3, aiohttp 2.3.1

@asvetlov
Copy link
Member

Middlewares are nested too.
If endpoints is handled by subapp -- middlewares from main app will be applied first, than subapp middlewares.

@olexandr-klymenko
Copy link
Author

olexandr-klymenko commented Oct 31, 2017 via email

@asvetlov
Copy link
Member

Hmm. If it's true -- this is a regression and behavior should be fixed.

@asvetlov
Copy link
Member

Steps to reproduce:

A sub application is not working and it is not very convenient.

from aiohttp import web

async def index(request):
    print('*'*8, request.path, '*'*8)
    print('Index!')
    return web.Response(body=b'Index', content_type='text/plain')


async def login(request):
    print('*' * 8, request.path, '*' * 8)
    print('Login!')
    return web.Response(body=b'Login', content_type='text/plain')

async def cabinet(request):
    print('*' * 8, request.path, '*' * 8)
    print('Cabinet!')
    return web.Response(body=b'Cabinet', content_type='text/plain')


@web.middleware
async def auth_middleware(request, handler):
    print('m' * 8, request.path, 'm' * 8)
    print('Auth middleware!')
    return await handler(request)


root_app = web.Application()
admin_app = web.Application(
    middlewares=[auth_middleware],
)
admin_login_app = web.Application()

root_app.router.add_get('/', index, name='index')
admin_app.router.add_get('/cabinet/', cabinet, name='admin:cabinet')
admin_login_app.router.add_route('*', '/', login, name='login')

admin_app.add_subapp('/login', admin_login_app)
root_app.add_subapp('/admin/', admin_app)


web.run_app(root_app)
$ python server.py 
======== Running on http://0.0.0.0:8080 ========
(Press CTRL+C to quit)
******** / ********
Index!
mmmmmmmm /admin/login/ mmmmmmmm
Auth middleware!
******** /admin/login/ ********
Login!
mmmmmmmm /admin/cabinet/ mmmmmmmm
Auth middleware!
******** /admin/cabinet/ ********
Cabinet!

@asvetlov asvetlov added the invalid This doesn't seem right label Jan 17, 2018
@asvetlov
Copy link
Member

The issue is wrong.
There are two subapplications:

  1. For handling /admin/* requests
  2. Nested app for handling /admin/cabinet/

Obviously /admin/cabinet/ request is passed to subapp 1 (with its middlweares) and after that to nested subapp 2.

Everything is working as expected.

@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].
[new issue]: https://github.com/aio-libs/aiohttp/issues/new

@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
invalid This doesn't seem right outdated
Projects
None yet
Development

No branches or pull requests

2 participants