haskell - Scotty:连接池作为单子(monad)阅读器

标签 haskell scotty haskell-wai

包括阅读器在内的数以万亿计的 monad 教程,当您阅读它时似乎一切都清楚了。但是当你真的需要写的时候,就另当别论了。

我从未使用过 Reader,只是在实践中从未使用过。因此,尽管我读过它,但我不知道该怎么做。

我需要在 Scotty 中实现一个简单的数据库连接池,以便每个操作都可以使用该池。池必须是“全局的”并且可以被所有操作函数访问。我读到这样做的方法是 Reader monad。如果还有其他方法请告诉我。

你能帮我看看如何正确使用阅读器吗?
如果我用我自己的例子看看它是如何完成的,我可能会学得更快。

{-# LANGUAGE OverloadedStrings #-}

module DB where

import Data.Pool
import Database.MongoDB

-- Get data from config
ip = "127.0.0.1"
db = "index"

--Create the connection pool
pool :: IO (Pool Pipe)
pool = createPool (runIOE $ connect $ host ip) close 1 300 5

-- Run a database action with connection pool
run :: Action IO a -> IO (Either Failure a)
run act = flip withResource (\x -> access x master db act) =<< pool

所以上面的很简单。我想在每个 Scotty 操作中使用“运行”功能来访问数据库连接池。现在,问题是如何将它包装在 Reader monad 中以使其可供所有函数访问?我知道对于所有 Scotty 操作函数,“池”变量必须“像全局”。

谢谢你。

更新

我正在使用完整的代码片段更新问题。我将“池”变量传递到函数链的位置。如果有人可以展示如何更改它以使用 monad Reader 请。
我不明白该怎么做。
{-# LANGUAGE OverloadedStrings #-}

module Main where

import Network.HTTP.Types
import Web.Scotty
import qualified Data.Text as T
import qualified Data.Text.Lazy as LT
import Data.Text.Lazy.Internal
import Data.Monoid (mconcat)
import Data.Aeson (object, (.=), encode)
import Network.Wai.Middleware.Static
import Data.Pool
import Database.MongoDB
import Control.Monad.Trans (liftIO,lift)

main = do
  -- Create connection pool to be accessible by all action functions
  pool <- createPool (runIOE $ connect $ host "127.0.0.1") close 1 300 5
  scotty 3000 (basal pool)

basal :: Pool Pipe -> ScottyM ()
basal pool = do
  middleware $ staticPolicy (noDots >-> addBase "static")
  get "/json" (showJson pool)

showJson :: Pool Pipe -> ActionM ()
showJson pool = do
  let run act = withResource pool (\pipe -> access pipe master "index" act) 
  d <- lift $ run $ fetch (select [] "tables")
  let r = either (const []) id d
  text $ LT.pack $ show r

谢谢。

更新 2

我尝试按照下面建议的方式进行操作,但它不起作用。
如果有人有任何想法,请。编译错误列表太长了,我什至不知道从哪里开始......
main = do
  pool <- createPool (runIOE $ connect $ host "127.0.0.1") close 1 300 5
  scotty 3000 $ runReaderT basal pool

basal :: ScottyT LT.Text (ReaderT (Pool Pipe) IO) ()
basal = do
  middleware $ staticPolicy (noDots >-> addBase "static")
  get "/json" $ showJson

showJson :: ActionT LT.Text (ReaderT (Pool Pipe) IO) ()
showJson = do
  p <- lift ask
  let rdb a = withResource p (\pipe -> access pipe master "index" a)
  j <- liftIO $ rdb $ fetch (select [] "tables")
  text $ LT.pack $ show j

更新 3

感谢 cdk 提供的想法,并感谢 Ivan Meredith 提供 scottyT 的建议。这个问题也有帮助:How do I add the Reader monad to Scotty's monad
这是编译的版本。我希望它可以帮助某人并节省一些时间。
import qualified Data.Text.Lazy as T
import qualified Data.Text.Lazy.Encoding as T
import           Data.Text.Lazy (Text)
import           Control.Monad.Reader
import           Web.Scotty.Trans
import           Data.Pool
import           Database.MongoDB

type ScottyD = ScottyT Text (ReaderT (Pool Pipe) IO)
type ActionD = ActionT Text (ReaderT (Pool Pipe) IO)

-- Get data from config
ip = "127.0.0.1"
db = "basal"

main = do
  pool <- createPool (runIOE $ connect $ host ip) close 1 300 5
  let read = \r -> runReaderT r pool
  scottyT 3000 read read basal

-- Application, meaddleware and routes
basal ::  ScottyD ()
basal = do
  get "/" shoot

-- Route action handlers
shoot ::  ActionD ()
shoot = do
  r <- rundb $ fetch $ select [] "computers"
  html $ T.pack $ show r

-- Database access shortcut
rundb :: Action IO a -> ActionD (Either Failure a)
rundb a = do
  pool <- lift ask
  liftIO $ withResource pool (\pipe -> access pipe master db a)

最佳答案

我一直试图自己找出这个确切的问题。感谢关于这个 SO question 的提示,以及我提出的其他研究,这些对我有用。您缺少的关键位是使用 scottyT
毫无疑问,有一种更漂亮的方式来编写 runDB,但我在 Haskell 方面没有太多经验,所以如果你能做得更好,请发布它。

type MCScottyM = ScottyT TL.Text (ReaderT (Pool Pipe) IO)
type MCActionM = ActionT TL.Text (ReaderT (Pool Pipe) IO)

main :: IO ()
main = do
  pool <- createPool (runIOE $ connect $ host "127.0.0.1") close 1 300 5  
  scottyT 3000 (f pool) (f pool) $ app
    where
      f = \p -> \r -> runReaderT r p

app :: MCScottyM ()
app = do
  middleware $ staticPolicy (noDots >-> addBase "public")
  get "/" $ do 
    p <- runDB dataSources 
    html $ TL.pack $ show p 

runDB :: Action IO a -> MCActionM (Either Failure a) 
runDB a = (lift ask) >>= (\p ->  liftIO $ withResource p (\pipe -> access pipe master "botland" a))

dataSources :: Action IO [Document]
dataSources = rest =<< find (select [] "datasources")

更新

我想这个更漂亮一些。
runDB :: Action IO a -> MCActionM (Either Failure a) 
runDB a = do
  p <- lift ask
  liftIO $ withResource p db
    where
       db pipe = access pipe master "botland" a

关于haskell - Scotty:连接池作为单子(monad)阅读器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22703289/

相关文章:

haskell - runghc 和 runhaskell 有什么区别?

haskell - Haskell中函数名末尾的'是什么意思/做什么?

haskell - 为什么 GHC 需要这么长时间才能链接?

haskell - 如何使用 Scotty 设置 wai-handler-devel 以自动重新加载代码?

apache - 将 yesod 应用程序作为(快速)CGI 服务

haskell - Windows 7 下的 haskell 编辑器具有自动完成功能?

haskell - 如何与 scotty 和 Selda 一起使用 monad 堆栈?

haskell - 什么时候泛型函数不是泛型的?

haskell - HSpec 中的多个 before 函数?

haskell - 在 Yesod 应用程序中添加 'always running' 线程的位置