Skip to content

Instantly share code, notes, and snippets.

@joastbg
Created October 29, 2013 12:39
Show Gist options
  • Select an option

  • Save joastbg/7213884 to your computer and use it in GitHub Desktop.

Select an option

Save joastbg/7213884 to your computer and use it in GitHub Desktop.
Haskell - Example using Happstack in Haskell
module Main where
import Control.Monad (msum)
import Data.Char (toLower)
import Happstack.Server (FromReqURI(..), dirs, dir, path, seeOther, nullConf, simpleHTTP, toResponse, ok)
--main :: IO ()
--main = simpleHTTP nullConf $ ok (toResponse "Hello, World!")
data Subject = World | Haskell
sayHello :: Subject -> String
sayHello World = "hello world!"
sayHello Haskell = "helloooo haskell!"
instance FromReqURI Subject where
fromReqURI sub =
case map toLower sub of
"haskell" -> Just Haskell
"world" -> Just World
_ -> Nothing
main :: IO ()
main = simpleHTTP nullConf $
msum [ dirs "hello/world" $ ok "Hello, World!"
, dirs "goodbye/moon" $ ok "Goodbye, Moon!"
, dir "hello" $ path $ \s -> ok $ "Hello, " ++ s
, dir "hoho" $ path $ \subject -> ok $ (sayHello subject)
, seeOther "/hello/world" "/hello/world"
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment