git.iko.soy/typeable/servant
on master help sign in
Add serveWithContextT, ServerContext (#1441)
799537fBrandon Chinn
.github/workflows/master.yml+2−2
@@ -95,8 +95,8 @@ jobs:
95 runs-on: ubuntu-latest
95 runs-on: ubuntu-latest
96 strategy:
96 strategy:
97 matrix:
97 matrix:
98− stack: ["2.3.1"]
98+ stack: ["2.7.3"]
99− ghc: ["8.8.4"]
99+ ghc: ["8.10.4"]
100
100
101 steps:
101 steps:
102 - uses: actions/checkout@v2
102 - uses: actions/checkout@v2
servant-docs/servant-docs.cabal+1−1
@@ -102,7 +102,7 @@ test-suite spec
102
102
103 -- Additional dependencies
103 -- Additional dependencies
104 build-depends:
104 build-depends:
105− tasty >= 1.1.0.4 && < 1.3,
105+ tasty >= 1.1.0.4 && < 1.5,
106 tasty-golden >= 2.3.2 && < 2.4,
106 tasty-golden >= 2.3.2 && < 2.4,
107 tasty-hunit >= 0.10.0.1 && < 0.11,
107 tasty-hunit >= 0.10.0.1 && < 0.11,
108 transformers >= 0.5.2.0 && < 0.6
108 transformers >= 0.5.2.0 && < 0.6
servant-server/src/Servant/Server.hs+31−9
@@ -1,9 +1,10 @@
1−{-# LANGUAGE ConstraintKinds #-}
1+{-# LANGUAGE ConstraintKinds #-}
2−{-# LANGUAGE DataKinds #-}
2+{-# LANGUAGE DataKinds #-}
3−{-# LANGUAGE FlexibleContexts #-}
3+{-# LANGUAGE FlexibleContexts #-}
4−{-# LANGUAGE RankNTypes #-}
4+{-# LANGUAGE RankNTypes #-}
5+{-# LANGUAGE ScopedTypeVariables #-}
5−{-# LANGUAGE TypeFamilies #-}
6+{-# LANGUAGE TypeFamilies #-}
6−{-# LANGUAGE TypeOperators #-}
7+{-# LANGUAGE TypeOperators #-}
7
8
8-- | This module lets you implement 'Server's for defined APIs. You'll
9-- | This module lets you implement 'Server's for defined APIs. You'll
9-- most likely just need 'serve'.
10-- most likely just need 'serve'.
@@ -11,6 +12,8 @@ module Servant.Server
11 ( -- * Run a wai application from an API
12 ( -- * Run a wai application from an API
12 serve
13 serve
13 , serveWithContext
14 , serveWithContext
15+ , serveWithContextT
16+ , ServerContext
14
17
15 , -- * Construct a wai Application from an API
18 , -- * Construct a wai Application from an API
16 toApplication
19 toApplication
@@ -128,6 +131,15 @@ import Servant.Server.UVerb
128
131
129-- * Implementing Servers
132-- * Implementing Servers
130
133
134+-- | Constraints that need to be satisfied on a context for it to be passed to 'serveWithContext'.
135+--
136+-- Typically, this will add default context entries to the context. You shouldn't typically
137+-- need to worry about these constraints, but if you write a helper function that wraps
138+-- 'serveWithContext', you might need to include this constraint.
139+type ServerContext context =
140+ ( HasContextEntry (context .++ DefaultErrorFormatters) ErrorFormatters
141+ )
142+
131-- | 'serve' allows you to implement an API and produce a wai 'Application'.
143-- | 'serve' allows you to implement an API and produce a wai 'Application'.
132--
144--
133-- Example:
145-- Example:
@@ -157,11 +169,21 @@ serve p = serveWithContext p EmptyContext
157-- 'defaultErrorFormatters' will always be appended to the end of the passed context,
169-- 'defaultErrorFormatters' will always be appended to the end of the passed context,
158-- but if you pass your own formatter, it will override the default one.
170-- but if you pass your own formatter, it will override the default one.
159serveWithContext :: ( HasServer api context
171serveWithContext :: ( HasServer api context
160− , HasContextEntry (context .++ DefaultErrorFormatters) ErrorFormatters )
172+ , ServerContext context
173+ )
161 => Proxy api -> Context context -> Server api -> Application
174 => Proxy api -> Context context -> Server api -> Application
162−serveWithContext p context server =
175+serveWithContext p context = serveWithContextT p context id
176+
177+-- | A general 'serve' function that allows you to pass a custom context and hoisting function to
178+-- apply on all routes.
179+serveWithContextT ::
180+ forall api context m.
181+ (HasServer api context, ServerContext context) =>
182+ Proxy api -> Context context -> (forall x. m x -> Handler x) -> ServerT api m -> Application
183+serveWithContextT p context toHandler server =
163− toApplication (runRouter format404 (route p context (emptyDelayed (Route server))))
184+ toApplication (runRouter format404 (route p context (emptyDelayed router)))
164 where
185 where
186+ router = Route $ hoistServerWithContext p (Proxy :: Proxy context) toHandler server
165 format404 = notFoundErrorFormatter . getContextEntry . mkContextWithErrorFormatter $ context
187 format404 = notFoundErrorFormatter . getContextEntry . mkContextWithErrorFormatter $ context
166
188
167-- | Hoist server implementation.
189-- | Hoist server implementation.
stack.yaml+1−1
@@ -1,4 +1,4 @@
1−resolver: lts-16.24
1+resolver: lts-18.5
2packages:
2packages:
3- servant-client-core/
3- servant-client-core/
4- servant-client/
4- servant-client/
stack.yaml.locknew file+19
@@ -0,0 +1,19 @@
1+# This file was autogenerated by Stack.
2+# You should not edit this file by hand.
3+# For more information, please see the documentation at:
4+# https://docs.haskellstack.org/en/stable/lock_files
5+
6+packages:
7+- completed:
8+ hackage: hspec-wai-0.10.1@sha256:56dd9ec1d56f47ef1946f71f7cbf070e4c285f718cac1b158400ae5e7172ef47,2290
9+ pantry-tree:
10+ size: 809
11+ sha256: 17af1c2e709cd84bfda066b9ebb04cdde7f92660c51a1f7401a1e9f766524e93
12+ original:
13+ hackage: hspec-wai-0.10.1
14+snapshots:
15+- completed:
16+ size: 585817
17+ url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/18/5.yaml
18+ sha256: 22d24d0dacad9c1450b9a174c28d203f9bb482a2a8da9710a2f2a9f4afee2887
19+ original: lts-18.5