Last active
August 24, 2018 00:26
-
-
Save cieplak/415385623dbfc25408cfff11831e143d 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 #-} | |
-------------------------------------------------------------------------------- | |
module Echo where | |
-------------------------------------------------------------------------------- | |
import Blaze.ByteString.Builder.Char.Utf8 (fromString) | |
import Data.ByteString (ByteString) | |
import Network.Wai as Wai | |
import Network.Wai.Handler.Warp as Warp | |
import Network.HTTP.Types (status200) | |
import Network.HTTP.Types.Header (hContentType) | |
-------------------------------------------------------------------------------- | |
main = start 9000 | |
-------------------------------------------------------------------------------- | |
start :: Int -> IO () | |
start port = do | |
putStrLn ("Listening on port " ++ show port) | |
run port app | |
-------------------------------------------------------------------------------- | |
app :: Wai.Application | |
app req f = do | |
putStrLn (show req ++ "\n") | |
resp <- handler req | |
f resp | |
-------------------------------------------------------------------------------- | |
handler :: Request -> IO Response | |
handler req = do | |
let query = queryString req :: [(ByteString, Maybe ByteString)] | |
method = Wai.requestMethod req | |
headers = Wai.requestHeaders req | |
path = pathInfo req | |
rawpath = rawPathInfo req | |
body <- (Wai.requestBody req) | |
return | |
$ responseBuilder status200 | |
[(hContentType, "text/plain")] | |
$ fromString ( "\n" | |
++ "Method : " ++ (show method) ++ "\n" | |
++ "Path : " ++ (show path) ++ "\n" | |
++ "Raw Path : " ++ (show rawpath) ++ "\n" | |
++ "Headers : " ++ (show headers) ++ "\n" | |
++ "Query String : " ++ (show query) ++ "\n" | |
++ "Request Body : " ++ (show body) ++ "\n" | |
++ "\n" ) | |
-------------------------------------------------------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment