Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hostname-independent Yesod application

This is a very simple question: How do I run a Yesod application that can handle multiple hosts at the same time?

A default Yesod setup can handle incoming connections from any host, but all URLs are rendered with approot prepended to them. Overriding joinPath in the Yesod instance declaration would be possible, if there was a way of storing the current domain so that it is available to that function, but there is not.

The method described on this wiki page works, but is very hackish, since it forces routes to take an additional domain parameter (since I don't have a static set of domains to match against). For practical purposes (because I have a lot of routes, some of which are domain-independent), it would be better to have a solution that left routes untouched, since it's possible to still get the host name from the WAI request info inside of a handler function.

I use things that require absolute URLs (e.g. email, RSS, XML APIs) so approot _ = "" is not an option.

I don't and can't know all of the host names, that need to be supported, at compile-time.

So, how do I render URLs so that they use the host name of the incoming request, and not the approot variable?

like image 339
dflemstr Avatar asked Feb 07 '12 22:02

dflemstr


1 Answers

One approach would be to include the actual domain name in the foundation type, and use a middleware that farms out to the appropriate application depending on the Request, spawning new ones as necessary. However, this feels too heavy-weight.

I want to leave approot as-is, since it fits the general case, but perhaps we can add an extra method approotRequest that would be given the Request value as well. I'd like to move this discussion to the list.

Update: Change already landed in Git: https://github.com/yesodweb/yesod/commit/4dd9880389e3b61f976e44525ae97831ec578a17

like image 184
Michael Snoyman Avatar answered Sep 29 '22 22:09

Michael Snoyman