-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Comments
Middlewares are nested too. |
I mean this is true the other way around as well. A middleware from sub app
affects on root application
|
Hmm. If it's true -- this is a regression and behavior should be fixed. |
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! |
The issue is wrong.
Obviously /admin/cabinet/ request is passed to subapp 1 (with its middlweares) and after that to nested subapp 2. Everything is working as expected. |
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. |
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
Your environment
python 3.6.3, aiohttp 2.3.1
The text was updated successfully, but these errors were encountered: