@@ -2,6 +2,7 @@
22{-# LANGUAGE DataKinds #-}
33{-# LANGUAGE FlexibleContexts #-}
44{-# LANGUAGE KindSignatures #-}
5+{-# LANGUAGE RankNTypes #-}
56{-# LANGUAGE ScopedTypeVariables #-}
67{-# LANGUAGE TypeFamilies #-}
78{-# LANGUAGE TypeOperators #-}
@@ -11,6 +12,8 @@ module Servant.Server.Generic (
1112 AsServerT,
1213 AsServer,
1314 genericServe,
15+ genericServeT,
16+ genericServeT',
1417 genericServer,
1518 genericServerT,
1619 ) where
@@ -38,6 +41,45 @@ genericServe
3841 => routes AsServer -> Application
3942genericServe = serve (Proxy :: Proxy (ToServantApi routes)) . genericServer
4043
44+-- | Transform a record of routes with custom monad into a WAI 'Application',
45+-- by providing a transformation to bring each handler back in the 'Handler'
46+-- monad.
47+genericServeT
48+ :: forall (routes :: * -> *) (m :: * -> *).
49+ ( GenericServant routes (AsServerT m)
50+ , GenericServant routes AsApi
51+ , HasServer (ToServantApi routes) '[]
52+ , ServerT (ToServantApi routes) m ~ ToServant routes (AsServerT m)
53+ )
54+ => (forall a. m a -> Handler a) -- ^ 'hoistServer' argument to come back to 'Handler'
55+ -> routes (AsServerT m) -- ^ your record full of request handlers
56+ -> Application
57+genericServeT f server = serve p $ hoistServer p f (genericServerT server)
58+ where
59+ p = genericApi (Proxy :: Proxy routes)
60+
61+-- | Transform a record of routes with custom monad into a WAI 'Application',
62+-- while using the given 'Context' to serve the application (contexts are typically
63+-- used by auth-related combinators in servant, e.g to hold auth checks) and the given
64+-- transformation to map all the handlers back to the 'Handler' monad.
65+genericServeT'
66+ :: forall (routes :: * -> *) (m :: * -> *) (ctx :: [*]).
67+ ( GenericServant routes (AsServerT m)
68+ , GenericServant routes AsApi
69+ , HasServer (ToServantApi routes) ctx
70+ , ServerT (ToServantApi routes) m ~ ToServant routes (AsServerT m)
71+ )
72+ => (forall a. m a -> Handler a) -- ^ 'hoistServer' argument to come back to 'Handler'
73+ -> routes (AsServerT m) -- ^ your record full of request handlers
74+ -> Context ctx -- ^ the 'Context' to serve the application with
75+ -> Application
76+genericServeT' f server ctx =
77+ serveWithContext p ctx $
78+ hoistServerWithContext p pctx f (genericServerT server)
79+ where
80+ p = genericApi (Proxy :: Proxy routes)
81+ pctx = Proxy :: Proxy ctx
82+
4183-- | Transform record of endpoints into a 'Server'.
4284genericServer
4385 :: GenericServant routes AsServer