如何在 WAI (Warp) 应用程序内执行 IO
我有一个简单的 WAI 应用程序(在本例中为 Warp),它用“Hi”响应所有 Web 请求。我还希望它在每次处理请求时在服务器上显示“Said hi”。如何在 WAI 响应处理程序中执行 IO?这是我的申请:
{-# 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"
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"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
WAI 应用程序的类型是:
这意味着 WAI 应用程序在
IO
上的Iteratee
monad 转换器中运行,因此您必须使用liftIO
code> 执行常规 IO 操作。The type of a WAI application is:
This means that a WAI application runs in an
Iteratee
monad transformer overIO
, so you'll have to useliftIO
to perform regularIO
actions.