Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I perform IO inside a WAI (Warp) Application

Tags:

io

haskell

I have a simple WAI application (Warp in this case) that responds to all web requests with "Hi". I also want it to display "Said hi" on the server each time a request is processed. How do I perform IO inside my WAI response handler? Here's 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"
like image 950
mndrix Avatar asked Oct 14 '11 17:10

mndrix


1 Answers

The type of a WAI application is:

type Application = Request -> Iteratee ByteString IO Response

This means that a WAI application runs in an Iteratee monad transformer over IO, so you'll have to use liftIO to perform regular IO actions.

import Control.Monad.Trans

app _ = do
    liftIO $ putStrLn "Said hi"
    return hello
like image 95
hammar Avatar answered Oct 21 '22 12:10

hammar