在问题Web,Scotty:connection pool as monad reader中,展示了如何使用ScottyT
将Reader
monad 嵌入堆栈以访问静态配置(在这种情况下为连接池)。
我有一个类似的问题,但更简单——或者至少我是这么认为的……</p>
我想将 a 添加Reader
到单个处理程序(即 a ActionT
),而不是整个应用程序。
我从上面的问题开始修改程序,但我不知道如何将 anActionT 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