我正在使用servant-generic-0.1.0.3
并servant-server-0.13.0.1
执行以下操作:
data Site route = Site
{ page :: route :-
"page" :> Capture "x" Int :> Capture "y" Int :> Get '[JSON] [Int]
, home :: route :-
Raw
} deriving (Generic)
type API = ToServant (Site AsApi)
siteServer :: Pool Connection -> Site AsServer
siteServer pool = Site
{ page = \x y ->
liftIO $ withResource pool $ \conn -> someDbFunction conn x y
, home = serveDirectoryWebApp "static"
}
api :: Proxy API
api = Proxy
app :: Pool Connection -> Application
app pool = serve api (toServant $ siteServer pool)
效果很好,然后我尝试使用它ReaderT
来避免传递Pool Connection
到siteServer
,所以我添加AppM
和替换siteServer
如下:
type AppM = ReaderT (Pool Connection) IO
siteServer :: ServerT API AppM
siteServer = Site
{ page = do
pool <- ask
\x y ->
liftIO $ withResource pool $ \conn -> someDbFunction conn x y
, home = serveDirectoryWebApp "static"
}
但是当我尝试编译它时遇到了一堆错误。
我遵循了servant cookbook中显示的相同步骤,但我无法使用通用路由进行此操作,尽管它在使用常规路由时有效。
我是否错过了可以使这项工作发挥作用的东西?