我想知道是否有人可以指出我应该如何在数据库池中使用Wai / Warp的正确方向。
经线run
的类型为Port -> Application -> IO ()
,其中Application
为Request -> (Response -> IO ResponseReceived) -> IO ResponseReceived
假设我以某种方式建立了数据库连接,我看不到如何将Application
包装在每个请求共享ConnectionPool
的周围。
我在想也许要做这样的事情。那行得通吗?
{-# LANGUAGE OverloadedStrings #-}
import Network.Wai
import Network.HTTP.Types
import Network.Wai.Handler.Warp (run)
import Database.MySQL.Base
createPool = do
connect
defaultConnectInfo
{ ciUser = "username"
, ciPassword = "password"
, ciDatabase = "dbname" }
app :: IO MySQLConn -> Application
app conn _ respond = do
respond $ responseLBS
status200
[("Content-Type", "text/plain")]
"Hello, Web!"
main :: IO ()
main = do
putStrLn $ "http://localhost:8080/"
run 8080 $ app $ createPool