每个处理程序读者的Scotty monad变换器

时间:2015-02-06 08:28:42

标签: haskell monad-transformers scotty

在问题Web, Scotty: connection pool as monad reader中,展示了如何使用ScottyT在堆栈中嵌入Reader monad来访问静态配置(在这种情况下,连接池)。 / p>

我有一个类似的问题,但更简单 - 或者至少我是这么认为的......

我想将Reader添加到单个处理程序(即ActionT),而不是整个应用程序。

我开始从上面的问题修改程序,但我无法弄清楚如何将ActionT Text (ReaderT String IO)转换为处理程序所需的ActionT Text IO。在摸索并试图使用打字孔希望看到如何构建这个之后我不得不放弃现在并寻求帮助。我真的觉得这应该很简单,但无法弄清楚如何做到这一点。

这是程序,其中突出显示的行:

{-# LANGUAGE OverloadedStrings #-}

import qualified Data.Text.Lazy as T
import           Data.Text.Lazy (Text)
import           Control.Monad.Reader
import           Web.Scotty.Trans

type ActionD = ActionT Text (ReaderT String IO)

main :: IO ()
main = do
  scottyT 3000 id id app

-- Application
app ::  ScottyT Text IO ()
app = do
  get "/foo" $ do
    h <- handler              -- ?
    runReaderT h "foo"        -- ?
--get "/bar" $ do
--  h <- handler
--  runReaderT h "bar"

-- Route action handler
handler ::  ActionD ()
handler = do
  config <- lift ask
  html $ T.pack $ show config

1 个答案:

答案 0 :(得分:4)

如果要在单独的阅读器中运行每个操作,则根本不需要更复杂的Scotty.Trans界面。你可以用另一种方式建立你的monad堆栈,顶部是ReaderT

import qualified Data.Text.Lazy as T
import           Control.Monad.Reader
import           Web.Scotty

type ActionD = ReaderT String ActionM

main :: IO ()
main = do
  scotty 3000 app

-- Application
app ::  ScottyM ()
app = do
  get "/foo" $ do
    runReaderT handler "foo"

-- Route action handler
handler ::  ActionD ()
handler = do
  config <- ask
  lift $ html $ T.pack $ show config
相关问题