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

Route for subdomains #1342

Closed
alikzao opened this issue Oct 27, 2016 · 30 comments
Closed

Route for subdomains #1342

alikzao opened this issue Oct 27, 2016 · 30 comments

Comments

@alikzao
Copy link

alikzao commented Oct 27, 2016

Describe

Sometimes there are situations when it is necessary for the current project to create a subdomain just as another rout.
And hang on to this subdomain a handler that is already there.

Expected behaviour

For example it might look something like this:

app.router.add_route('GET',  '/',  main_page)
app.router.add_route('GET',  '/',  handler, subdomain="domain")

def handler():
      print('text')
@asvetlov
Copy link
Member

After #1301 it's possible to register subapplications by prefix.

I suggest the same technique but for resolving by subdomain.

@cynecx
Copy link

cynecx commented Oct 27, 2016

I am currently using the approach of adding a custom middleware which specifically filters out unknown hostnames, the only issue I am currently having is that it quite hard to support real micro-services with different domains, but this isn't an issue anymore especially after @asvetlov has landed sub-applications :).

@asvetlov
Copy link
Member

I mean we could add a method router.add_subdomain('sub.domain.com', subapp) like recently added router.add_subapp('/prefix', subapp).

@cynecx
Copy link

cynecx commented Oct 27, 2016

@asvetlov Do you also consider supporting wildcards in the domain name as well a falback so unknown domains are handled too?

@asvetlov
Copy link
Member

Why not?

@cynecx
Copy link

cynecx commented Oct 27, 2016

@asvetlov Well, by looking at your example I thought you would like to keep it very simple, so in that case I assumed it would come without wildcard and fallback domain support. But yeah, it's good to know that you will include that too :).

@ghost
Copy link

ghost commented Mar 19, 2017

Is it implemented?

@fafhrd91
Copy link
Member

no, it is not

@ghost
Copy link

ghost commented Mar 20, 2017

Very sad :(

@fafhrd91
Copy link
Member

Would you provide PR?

@ghost
Copy link

ghost commented Mar 20, 2017

Nah, I'm not familiar with aiohttp

@alikzao
Copy link
Author

alikzao commented Mar 20, 2017

@TiberiumPY I did not wait for decisions, so switched to another framework

@ghost
Copy link

ghost commented Mar 20, 2017

@alikzao what framework?

@fafhrd91
Copy link
Member

You have plenty of options. Pyramid, flask.

@espositofulvio
Copy link

If this has not been done yet, I'd like to have add_domain instead of add_subdomain. It would be a nice feature which could easily enable multi-site support for aiohttp applications. Further, having remove_subapp() and remove_domain() would complete the feature allowing to dinamically add and remove features to a website and in my opinion could be a nice thing to have (paid features come to mind but also a multi-site cms where user can actually load the features they want could be handled this way easily).

@alex-eri
Copy link
Contributor

I suggest to rewrite domain to prefix on front web-server (like nginx)

@amirouche
Copy link

@alex-eri it doesn't work well outside production, except if you also have to configure nginx on the development host.

@amirouche
Copy link

I am willing to work on this.

I mean we could add a method router.add_subdomain('sub.domain.com', subapp) like recently added router.add_subapp('/prefix', subapp).

And prolly:

wildcards in the domain name as well a falback so unknown domains are handled too

@amirouche
Copy link

To be perfect, aiohttp needs a replacement for eoe:

*.hostname.local dns resolver, pow.cx replacement wanna-be.

@asvetlov
Copy link
Member

To be perfect, aiohttp needs a replacement for eoe:

Sorry, I don't follow

@amirouche
Copy link

Sorry, please forget about the eoe thing.

@mlyko
Copy link

mlyko commented Jan 19, 2018

Hi there,

I'd like to ask you if you going to add this feature to the aiohttp official release at all? I'm asking because the corresponding pull request seems to be abandoned by an author.

And second question. Why don't you want to add an ordinary routing by the 'Host' header to aiohttp, like it is available almost in all modern web framework (e.g. tornado, sanic)? Like this:
router.add_route('GET', '/path/', handler, host='example.com')

I could provide an appropriate pull request.

@asvetlov
Copy link
Member

Currently nobody works on the feature AFAIK but we very appreciate the contribution.

Regarding to second question (router.add_route('GET', '/path/', handler, host='example.com')).
I think it is not necessary: the code mixes routing by host and bare requests.
Maybe better to support explicit sub-app with host (domain) specified.

@mlyko
Copy link

mlyko commented Jan 19, 2018

Sorry, but I don't get it, what do you mean writing that 'the code mixes routing by host and bare requests'?
Please take a look at a concept of such routing which I've already implemented:
https://github.com/mlyko/aiohttp/commit/9054d0b6f2ab69b42d80e5f6d8642a19e6d09382
and give a feedback.

I don't like the concept with sub-apps in this case because I'll have to create sub-apps to use the routing by host feature. Whereas sub-apps in aiohttp are for solving problem with big projects, not for routing purposes.

@asvetlov
Copy link
Member

Now in aiohttp router is conceptually a table of regular expressions (let's assume that PlainResource can be converted to regex easy).
It allows future optimizations like https://github.com/pyrates/autoroutes or https://github.com/intel/hyperscan to replace iteration over resources with single call to highly optimized structure for a set of pre-compiled regular expressions.
Adding a host to resource match criteria complicates such optimization.

I prefer to have a route table per host.

@mlyko
Copy link

mlyko commented Jan 19, 2018

Ok, thanks for clarifying. So it means that futher developing an implementation of routing by host proposed by me doesn't make any sense :)

@asvetlov
Copy link
Member

But explicit domain/subdomain support does make sense :)

@roganov
Copy link
Contributor

roganov commented Apr 17, 2018

It's probably a little tricky, but route and subdomain rules can be combined in a single regular expression. I believe werkzeug does this.

@asvetlov
Copy link
Member

I pretty sure the combining is an implementation artifact, not the main intention.
As an alternative we can consider NGINX (or Apache) config. The config has server and location directives, pretty easy and understandable.

@samuelcolvin
Copy link
Member

I believe this should have been closed by #2809

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants