[web-devel] [Yesod] How to write approot to handle http and https.

Michael Snoyman michael at snoyman.com
Wed Feb 2 05:59:09 CET 2011

This is a good question. Right now, Yesod specifically does *not*
support this, essentially because it is non-RESTful (two URLs for the
same resource). In order to achieve this, the type signature for
approot would need to be modified to something like:

    approot :: master -> Request -> String

This actually introduces some other issues, such as making it
difficult for people to generate URLs outside of the context of a
specific request. There *is* a hack you could use right now that will
work, and I'm leaning towards recommending it. Essentially, create
your foundation type as:

    data MyApp = MyApp { dbpool :: DBPool, isHTTPS :: Bool }

Then in your withMyApp function, you could write something like:

    withMyApp f = do
        withDBPool $ \pool -> do
            http <- toWaiApp $ MyApp pool False -- make sure to call
toWaiApp before you receive a request, otherwise you will keep
reloading encryption keys at each request, which will kill performance
            https <- toWaiApp $ MyApp pool True
            f $ \req -> (if Network.Wai.isSecure req then https else http) req

Then you would write approot as:

    approot app = (if isHTTPS app then "https" else "http") ++


On Wed, Feb 2, 2011 at 12:04 AM, Katsutoshi Itoh <cutsea110 at gmail.com> wrote:
> Hi
> I'd like to host application for http and https.
> I set up apache and use rewrite rules.
> In this case, how do i write approot of Yesod instance?
> when client request by http://www.foo.com/a/b/c/,
> approot must be http://www.foo.com,
> one the other hand, when client request by https://www.foo.com/a/b/c/,
> approot must be https://www.foo.com.
> how do i write?
> _______________________________________________
> web-devel mailing list
> web-devel at haskell.org
> http://www.haskell.org/mailman/listinfo/web-devel

More information about the web-devel mailing list