How to do IO in WAI (Warp) application

I have a simple WAI application (Warp in this case) that answers all web requests with "Hello." I also want it to display "Said hi" on the server every time the request is processed. How to do IO inside my WAI response handler? Here is my application:

{-# LANGUAGE OverloadedStrings #-} import Network.Wai import Network.HTTP.Types (status200) import Network.Wai.Handler.Warp (run) main :: IO () main = do putStrLn "http://localhost:3000/" run 3000 app app :: Application app _ = return hello hello = responseLBS status200 [("Content-Type", "text/plain")] "Hi" 
+8
io haskell
source share
1 answer

WAI Application Type:

 type Application = Request -> Iteratee ByteString IO Response 

This means that the WAI application is running in an Iteratee transformer interleaved over the IO , so you will need to use liftIO to perform regular IO actions.

 import Control.Monad.Trans app _ = do liftIO $ putStrLn "Said hi" return hello 
+13
source share

All Articles