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

Adding routes to subapps is broken after attaching subapp to the parent app #2656

Closed
webknjaz opened this issue Jan 11, 2018 · 2 comments
Closed

Comments

@webknjaz
Copy link
Member

Long story short

If one adds routes to subapp before app.add_subapp accessing them works well, otherwise it results in 404.

Expected behaviour

It's naturally expected that routing will work well regardless of the order of function calls.

Actual behaviour

Given sub_app_cbv.py:

import sys
import aiohttp.web

API_URL_PREFIX = r'/api/v1'

class PingView(aiohttp.web.View):
    async def get(self):
        return aiohttp.web.Response(text='pong')

def just_app_with_routes_def(app, api_app):
    app.router.add_route('GET', API_URL_PREFIX + r'/ping/', PingView)

def sub_app_after_routes_def(app, api_app):
    api_app.router.add_route('GET', r'/ping/', PingView)
    app.add_subapp(API_URL_PREFIX, api_app)

def sub_app_before_routes_def(app, api_app):
    app.add_subapp(API_URL_PREFIX, api_app)
    api_app.router.add_route('GET', r'/ping/', PingView)

def run_app(coro_name):
    print(f'Going to run {coro_name}')
    setup_app = globals()[coro_name]
    app = aiohttp.web.Application()
    api_app = aiohttp.web.Application()
    setup_app(app, api_app)
    aiohttp.web.run_app(app)

def main():
    if len(sys.argv) < 2:
        raise RuntimeError('Supply one cli argument plz [just_app_with_routes_def|sub_app_after_routes_def|sub_app_before_routes_def]')
    coro_name = sys.argv[1]
    run_app(coro_name)

__name__ == '__main__' and main()
Demo 1
$ python sub_app_cbv.py just_app_with_routes_def
Going to run just_app_with_routes_def
======== Running on http://0.0.0.0:8080 ========
(Press CTRL+C to quit)
$ http :8080/api/v1/ping/
HTTP/1.1 200 OK
Content-Length: 4
Content-Type: text/plain; charset=utf-8
Date: Thu, 11 Jan 2018 23:17:52 GMT
Server: Python/3.6 aiohttp/2.3.7
pong
Demo 2
$ python sub_app_cbv.py sub_app_after_routes_def
Going to run sub_app_after_routes_def
======== Running on http://0.0.0.0:8080 ========
(Press CTRL+C to quit)
$ http :8080/api/v1/ping/
HTTP/1.1 200 OK
Content-Length: 4
Content-Type: text/plain; charset=utf-8
Date: Thu, 11 Jan 2018 23:20:58 GMT
Server: Python/3.6 aiohttp/2.3.7
pong
Demo 3
$ python sub_app_cbv.py sub_app_before_routes_def
Going to run sub_app_before_routes_def
======== Running on http://0.0.0.0:8080 ========
(Press CTRL+C to quit)
$ http :8080/api/v1/ping/
HTTP/1.1 404 Not Found
Content-Length: 14
Content-Type: text/plain; charset=utf-8
Date: Thu, 11 Jan 2018 23:21:41 GMT
Server: Python/3.6 aiohttp/2.3.7
404: Not Found

Steps to reproduce

See above.

Your environment

Gentoo laptop, pyenv, 3.6. Not really env-related.

@asvetlov
Copy link
Member

No.
I consider a subapp as frozen after adding to parent application.
PR for fix is #2657

@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.
Projects
None yet
Development

No branches or pull requests

2 participants