How do you add logic into the following `myApp` haskell function?

88 Views Asked by At

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?

1

There are 1 best solutions below

3
On BEST ANSWER

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"