It's now May 2011, just an update on current trends.
I think most web development today is done with yesod or the snap frame work. both are very good and very nice developed (thanks to all the people who are involved!!). further there is also the wrap package.
My small REST example (or resful server). (ok maybe the example is not a real restful server, but it shows how you can handle GET/PUT requests, the rest is up to you..)
If you open http://localhost:8000/mytest in a browser, then "Get request" is displayed. If you make a PUT request with a rest-client (also to localhost:8000/mytest), the content of the request body is stored in "/tmp/restrq.txt".
This code is part of the Site.hs file of the Snap-Framework:
- | Constants
tempFilePath :: String
tempFilePath = "/tmp/restrq.txt"
-- | Helper Functions
-- Bytestring Conversion
strictToLazy :: B.ByteString -> BL.ByteString
strictToLazy x
| B.null x = BL.Empty
| otherwise = BL.Chunk x BL.Empty
lazyToStrict :: BL.ByteString -> B.ByteString
lazyToStrict = B.concat . BL.toChunks
getRequestString :: MonadSnap m => m B.ByteString
getRequestString = do message <- getRequestBody
return (lazyToStrict message)
-- | Action for PUT request
action :: Application ()
action = do message <- getRequestString
liftIO $ B.writeFile tempFilePath (B8.append (B8.pack "--- REST BODY ---\n") message)
-- | /mytest (GET and PUT requests possible)
mytest :: Application ()
mytest = method GET (writeBS "Get request") <|> method PUT action
-- | The main entry point handler.
site :: Application ()
site = route [ ("/", index)
, ("/mytest", mytest)
]
<|> serveDirectory "resources/static"