0

I am writing a website in Mojolicious which needs to route to certain files depending on the domain name. Ie:

mydomain.com/foo -> controllerA#foo
mydomain.es/foo  -> controllerB#foo

The documentation gives the following solution:

$r->get('/foo')->over(host => qr/mydomain\.com/)->to('controllerA#foo');
$r->get('/foo')->over(host => qr/mydomain\.es/)->to('controllerB#foo');

But also warns me, that this will disable route caching.

Is there a better way to do this? And if not, how bad is it that route caching gets disabled? This website needs to be able to handle a lot of requests (up to 10.000/hour), so I could imagine that route caching was preferred.

Thanks!

  • did you find an acceptable approach? – Chris Betti Sep 25 '13 at 19:28
  • Unfortunately not. I ended up writing a hook rewriting the url like you suggested before dispatching it to the router - but it is not really an ideal solution because all internal mojo calls (like url_for) now will return the wrong url (it will return mydomain.com/foo/es, not mydomain.es/foo). I think the root of the problem is simply the lack of cross-domain routing in mojo's caching module. – JeppeHallgren Sep 26 '13 at 07:22

2 Answers2

0

One way that comes to mind is to rewrite the request URI before mojolicious sees it. For example:

mydomain.com/foo -> mydomain.com/foo/com
mydomain.es/foo  -> mydomain.com/foo/es

Then add some routes:

$r->get('/foo/com')->to('controllerA#foo');
$r->get('/foo/es')->to('controllerB#foo');
Chris Betti
  • 2,721
  • 2
  • 27
  • 36
0

I don't know how you are deploying this app, but if you are using Apache or NGIX as a reverse proxy, you could handle the domain routing there and rewrite it.

arafeandur
  • 196
  • 1
  • 5