Deck Pope
Deck Pope

Reputation: 231

How to do subdomain routing using Haskell

Using Hakyll that uses snap i started working on a routing server. Given the following code from their tutorials i can see the routing but i would like to have some different applications on their own subdomains like oneapp.mysite.com. Is this possible using snap or any other Haskell server?

site :: Snap ()
site =
    ifTop (writeBS "hello world") <|>
    route [ ("foo", writeBS "bar")
          , ("echo/:echoparam", echoHandler)
          ] <|>
    dir "static" (serveDirectory ".")

Upvotes: 4

Views: 350

Answers (2)

Deck Pope
Deck Pope

Reputation: 231

Thank you both for suggestions, i made it. I didn't used snaplets but i did use fmap rqServerName getRequest and the if-then-else statements. This is a piece of code.

skite :: Snap ()
skite = do
    req <- fmap rqServerName getRequest
    routes req
  where
    routes req =
        if (req == "www.site1.ro") then (site1) else pass <|>
        if (req == "site1.ro") then (site1) else pass <|>
        if (req == "www.site2.ro") then (writeBS req) else pass <|>
        if (req == "site2.ro") then (writeBS "Nowhere to be found") else pass <|>
        ifTop (writeBS req)

I also created a gist with the full code here For further suggestions you are welcome.

Upvotes: 0

glguy
glguy

Reputation: 1090

I haven't done this before, but this is what I would try:

Use the wrapSite function to conditionally use the routes for your subdomain and you can test which subdomain was requested with fmap rqServerName getRequest

http://hackage.haskell.org/packages/archive/snap/0.11.0/doc/html/Snap-Snaplet.html#g:7 http://hackage.haskell.org/packages/archive/snap-core/0.9.2.2/doc/html/Snap-Core.html#g:5 http://hackage.haskell.org/packages/archive/snap-core/0.9.2.2/doc/html/Snap-Core.html#g:10

Upvotes: 1

Related Questions