Created
May 6, 2015 17:47
Revisions
-
joehillen created this gist
May 6, 2015 .There are no files selected for viewing
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 charactersOriginal 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]