Skip to content

Instantly share code, notes, and snippets.

@joehillen
Created May 6, 2015 17:47

Revisions

  1. joehillen created this gist May 6, 2015.
    38 changes: 38 additions & 0 deletions request.hs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    #!/usr/bin/env runhaskell

    {-# LANGUAGE OverloadedStrings #-}

    import Prelude hiding (interact)
    import Data.Aeson
    import Data.Text (Text)
    import Data.Text.Encoding (decodeUtf8)
    import qualified Data.Text as T
    import qualified Data.Text.IO as TIO
    import qualified Data.ByteString.Lazy.Char8 as BSL
    import Control.Applicative ((<$>))

    data Log = Log { request :: Text } deriving (Show)

    instance FromJSON Log where
    parseJSON (Object obj) = Log <$> obj .: "request"

    main :: IO ()
    main =
    interact
    $ T.unlines
    . (filter dropBlank)
    . (map getRequest)
    . BSL.lines

    interact :: (BSL.ByteString -> Text) -> IO ()
    interact f = do
    r <- BSL.getContents
    TIO.putStr $ f r

    dropBlank :: Text -> Bool
    dropBlank x = T.length x > 0

    getRequest :: BSL.ByteString -> Text
    getRequest x = case eitherDecode x of
    (Right l) -> request l
    (Left err) -> decodeUtf8 . BSL.toStrict $ BSL.intercalate " " ["###", (BSL.pack err), x]