Skip to content

Instantly share code, notes, and snippets.

@joehillen
Created May 6, 2015 17:47
Show Gist options
  • Save joehillen/3f5a95e217365596c7f3 to your computer and use it in GitHub Desktop.
Save joehillen/3f5a95e217365596c7f3 to your computer and use it in GitHub Desktop.
#!/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]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment