Last active
September 4, 2015 21:01
-
-
Save qmmdb/37a9721641c79c9cf749 to your computer and use it in GitHub Desktop.
This file contains 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
{-# LANGUAGE OverloadedStrings #-} | |
import Control.Exception (SomeException) | |
import Control.Exception.Lifted (handle) | |
import Control.Monad.IO.Class (liftIO) | |
import Data.Aeson (Value, encode, object, (.=)) | |
import Data.Aeson.Parser (json) | |
import Data.ByteString (ByteString) | |
import Data.Conduit (($$)) | |
import Data.Conduit.Attoparsec (sinkParser) | |
import Network.HTTP.Types (status200, status400) | |
import Network.Wai (Application, Response, responseLBS) | |
import Network.Wai.Conduit (sourceRequestBody) | |
import Network.Wai.Handler.Warp (run) | |
main :: IO () | |
main = run 3000 app | |
app :: Application | |
app req sendResponse = handle (sendResponse . invalidJson) $ do | |
value <- sourceRequestBody req $$ sinkParser json | |
newValue <- liftIO $ modValue value | |
sendResponse $ responseLBS | |
status200 | |
[("Content-Type", "application/json")] | |
$ encode newValue | |
invalidJson :: SomeException -> Response | |
invalidJson ex = responseLBS | |
status400 | |
[("Content-Type", "application/json")] | |
$ encode $ object | |
[ ("message" .= show ex) | |
] | |
-- Application-specific logic would go here. | |
modValue :: Value -> IO Value | |
modValue = return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment