Created
October 29, 2013 12:39
-
-
Save joastbg/7213884 to your computer and use it in GitHub Desktop.
Haskell - Example using Happstack in Haskell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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