4

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 ".")
Chris Stryczynski
  • 30,145
  • 48
  • 175
  • 286
Deck Pope
  • 231
  • 3
  • 10

2 Answers2

1

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

glguy
  • 1,090
  • 7
  • 8
  • thank you for suggestion it seem to make sense theoretically but i don't know how to use snaplets. – Deck Pope Feb 09 '13 at 15:09
  • You don't have to use snaplets to use this. Just use `rqServerName` and `getRequest`. – mightybyte Feb 13 '13 at 02:05
  • also i don't have a clue about the context in that they have to be. The most feedbacker stuff on `vhosts` is [this one](http://www.haskellshapes.com/haskell-web-programming-ii/) but it "only" emits status `ByteString` messages, any further implementation using forwarding to routes being abolished by the necessity for `vhosts` to receive an `Application`, then everything is broken. This is not in Snap, i know but is the most expressive example found out there. So, please, if you have a hint on how to do it in Snap please write a line. Or i\'m too new to Haskell. maybe – Deck Pope Feb 15 '13 at 15:07
0

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.

Deck Pope
  • 231
  • 3
  • 10