@@ -1,6 +1,7 @@
1−{-# LANGUAGE TypeFamilies #-}
2−{-# LANGUAGE FlexibleContexts #-}
1+{-# LANGUAGE CPP #-}
2+{-# LANGUAGE FlexibleContexts #-}
33{-# LANGUAGE OverloadedStrings #-}
4+{-# LANGUAGE TypeFamilies #-}
45
56-- | This module lets you implement 'Server's for defined APIs. You'll
67-- most likely just need 'serve'.
@@ -17,8 +18,27 @@ module Servant.Server
1718 , ServerT
1819
1920 -- * Enter
20− -- Applying functions to all handlers
21− , Enter(..)
21+ -- $enterDoc
22+
23+ -- ** Basic functions and datatypes
24+ , enter
25+ , (:~>)(..)
26+ -- ** `Nat` utilities
27+ , liftNat
28+ , runReaderTNat
29+ , evalStateTLNat
30+ , evalStateTSNat
31+ , logWriterTLNat
32+ , logWriterTSNat
33+#if MIN_VERSION_mtl(2,2,1)
34+ , fromExceptT
35+#endif
36+ -- ** Functions based on <https://hackage.haskell.org/package/mmorph mmorph>
37+ , hoistNat
38+ , embedNat
39+ , squashNat
40+ , generalizeNat
41+
2242
2343 -- * Default error type
2444 , ServantErr(..)
@@ -58,12 +78,12 @@ module Servant.Server
5878
5979 ) where
6080
61−import Data.Proxy (Proxy)
62−import Network.Wai (Application)
63−import Servant.API (Canonicalize, canonicalize)
64−import Servant.Server.Internal
65−import Servant.Server.Internal.ServantErr
66−import Servant.Server.Internal.Enter
81+import Data.Proxy (Proxy)
82+import Network.Wai (Application)
83+import Servant.API (Canonicalize, canonicalize)
84+import Servant.Server.Internal
85+import Servant.Server.Internal.Enter
86+import Servant.Server.Internal.ServantErr
6787
6888
6989-- * Implementing Servers
@@ -88,5 +108,30 @@ import Servant.Server.Internal.Enter
88108-- >
89109-- > main :: IO ()
90110-- > main = Network.Wai.Handler.Warp.run 8080 app
111+--
91112serve :: HasServer (Canonicalize layout) => Proxy layout -> Server layout -> Application
92113serve p server = toApplication (route (canonicalize p) server)
114+
115+
116+-- Documentation
117+
118+-- $enterDoc
119+-- Sometimes our cherished `EitherT` monad isn't quite the type you'd like for
120+-- your handlers. Maybe you want to thread some configuration in a @Reader@
121+-- monad. Or have your types ensure that your handlers don't do any IO. Enter
122+-- `enter`.
123+--
124+-- With `enter`, you can provide a function, wrapped in the `(:~>)` / `Nat`
125+-- newtype, to convert any number of endpoints from one type constructor to
126+-- another. For example
127+--
128+-- >>> import Control.Monad.Reader
129+-- >>> import qualified Control.Category as C
130+-- >>> type ReaderAPI = "ep1" :> Get '[JSON] Int :<|> "ep2" :> Get '[JSON] String
131+-- >>> let readerServer = return 1797 :<|> ask :: ServerT ReaderAPI (Reader String)
132+-- >>> let mainServer = enter (generalizeNat C.. (runReaderTNat "hi")) readerServer :: Server ReaderAPI
133+--
134+
135+-- $setup
136+-- >>> import Servant.API
137+-- >>> import Servant.Server