0

I am still trying to understand how haskell syntax works. So, here's a dead simple wai/warp application.

{-# LANGUAGE OverloadedStrings #-}
import Network.Wai
import Network.HTTP.Types (status200)
import Network.Wai.Handler.Warp (run)

myApp _ respond = respond $
    responseLBS status200 [("Content-Type", "text/plain")] "Hello World" 

main = run 3000 myApp 

If I want to print out some text into stdout with putStrLn before returning the status 200 and "Hello World" plain text, how would I implement it?

Calvin Cheng
  • 35,640
  • 39
  • 116
  • 167

1 Answers1

1

myApp has this type:

myApp :: Request -> (Response -> IO ResponseReceived) -> IO ResponseReceived

so you can add your own IO action before returning the response like this:

myApp _ respond = do putStrLn "processing request"
                     respond $ responseLBS status200 [("Content-Type", "text/plain")] "Hello World"
ErikR
  • 51,541
  • 9
  • 73
  • 124
  • I am not sure what I am doing wrong as this is in fact what I did right from the get-go but it gives me `hellowai.hs:7:5: parse error on input ‘respond’` – Calvin Cheng Jan 05 '15 at 07:19
  • Damn, I realize that my tabs are not configured as space. My bad. This works great. – Calvin Cheng Jan 05 '15 at 07:21
  • Haskell is whitespace sensitive - make sure things line up and do not use tabs. Here is my code: http://lpaste.net/117798 – ErikR Jan 05 '15 at 07:22