Skip to content

Commit 799537f

Browse files
Add serveWithContextT, ServerContext (#1441)
servant-server: add serveWithContexT and ServerContext
1 parent 47bd252 commit 799537f

File tree

5 files changed

+54
-13
lines changed

5 files changed

+54
-13
lines changed

.github/workflows/master.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ jobs:
9595
runs-on: ubuntu-latest
9696
strategy:
9797
matrix:
98-
stack: ["2.3.1"]
99-
ghc: ["8.8.4"]
98+
stack: ["2.7.3"]
99+
ghc: ["8.10.4"]
100100

101101
steps:
102102
- uses: actions/checkout@v2

servant-docs/servant-docs.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ test-suite spec
102102

103103
-- Additional dependencies
104104
build-depends:
105-
tasty >= 1.1.0.4 && < 1.3,
105+
tasty >= 1.1.0.4 && < 1.5,
106106
tasty-golden >= 2.3.2 && < 2.4,
107107
tasty-hunit >= 0.10.0.1 && < 0.11,
108108
transformers >= 0.5.2.0 && < 0.6

servant-server/src/Servant/Server.hs

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
{-# LANGUAGE ConstraintKinds #-}
2-
{-# LANGUAGE DataKinds #-}
3-
{-# LANGUAGE FlexibleContexts #-}
4-
{-# LANGUAGE RankNTypes #-}
5-
{-# LANGUAGE TypeFamilies #-}
6-
{-# LANGUAGE TypeOperators #-}
1+
{-# LANGUAGE ConstraintKinds #-}
2+
{-# LANGUAGE DataKinds #-}
3+
{-# LANGUAGE FlexibleContexts #-}
4+
{-# LANGUAGE RankNTypes #-}
5+
{-# LANGUAGE ScopedTypeVariables #-}
6+
{-# LANGUAGE TypeFamilies #-}
7+
{-# LANGUAGE TypeOperators #-}
78

89
-- | This module lets you implement 'Server's for defined APIs. You'll
910
-- most likely just need 'serve'.
1011
module Servant.Server
1112
( -- * Run a wai application from an API
1213
serve
1314
, serveWithContext
15+
, serveWithContextT
16+
, ServerContext
1417

1518
, -- * Construct a wai Application from an API
1619
toApplication
@@ -128,6 +131,15 @@ import Servant.Server.UVerb
128131

129132
-- * Implementing Servers
130133

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+
131143
-- | 'serve' allows you to implement an API and produce a wai 'Application'.
132144
--
133145
-- Example:
@@ -157,11 +169,21 @@ serve p = serveWithContext p EmptyContext
157169
-- 'defaultErrorFormatters' will always be appended to the end of the passed context,
158170
-- but if you pass your own formatter, it will override the default one.
159171
serveWithContext :: ( HasServer api context
160-
, HasContextEntry (context .++ DefaultErrorFormatters) ErrorFormatters )
172+
, ServerContext context
173+
)
161174
=> Proxy api -> Context context -> Server api -> Application
162-
serveWithContext p context server =
163-
toApplication (runRouter format404 (route p context (emptyDelayed (Route 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 =
184+
toApplication (runRouter format404 (route p context (emptyDelayed router)))
164185
where
186+
router = Route $ hoistServerWithContext p (Proxy :: Proxy context) toHandler server
165187
format404 = notFoundErrorFormatter . getContextEntry . mkContextWithErrorFormatter $ context
166188

167189
-- | Hoist server implementation.

stack.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
resolver: lts-16.24
1+
resolver: lts-18.5
22
packages:
33
- servant-client-core/
44
- servant-client/

stack.yaml.lock

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

0 commit comments

Comments
 (0)