开发者

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

开发者 https://www.devze.com 2023-04-13 06:44 出处:网络
I have a 开发者_JS百科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

I have a 开发者_JS百科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"


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号