@@ -4,6 +4,7 @@
44{-# LANGUAGE FlexibleContexts #-}
55{-# LANGUAGE FlexibleInstances #-}
66{-# LANGUAGE GeneralizedNewtypeDeriving #-}
7+{-# LANGUAGE MultiParamTypeClasses #-}
78{-# LANGUAGE OverloadedStrings #-}
89{-# LANGUAGE PolyKinds #-}
910{-# LANGUAGE ScopedTypeVariables #-}
@@ -68,11 +69,10 @@ import Servant.Server.Internal.RoutingApplication
6869import Servant.Server.Internal.ServantErr
6970
7071
71−class HasServer layout where
72+class HasServer layout config where
7273 type ServerT layout (m :: * -> *) :: *
73− type HasConfig layout (c :: [*]) :: Constraint
7474
75− route :: HasConfig layout config => Proxy layout -> Config config -> Delayed (Server layout) -> Router
75+ route :: Proxy layout -> Config config -> Delayed (Server layout) -> Router
7676
7777type Server layout = ServerT layout (ExceptT ServantErr IO)
7878
@@ -89,10 +89,9 @@ type Server layout = ServerT layout (ExceptT ServantErr IO)
8989-- > server = listAllBooks :<|> postBook
9090-- > where listAllBooks = ...
9191-- > postBook book = ...
92−instance (HasServer a, HasServer b) => HasServer (a :<|> b) where
92+instance (HasServer a config, HasServer b config) => HasServer (a :<|> b) config where
9393
9494 type ServerT (a :<|> b) m = ServerT a m :<|> ServerT b m
95− type HasConfig (a :<|> b) c = (HasConfig a c, HasConfig b c)
9695
9796 route Proxy config server = choice (route pa config ((\ (a :<|> _) -> a) <$> server))
9897 (route pb config ((\ (_ :<|> b) -> b) <$> server))
@@ -119,12 +118,11 @@ captured _ = parseUrlPieceMaybe
119118-- > server = getBook
120119-- > where getBook :: Text -> ExceptT ServantErr IO Book
121120-- > getBook isbn = ...
122−instance (KnownSymbol capture, FromHttpApiData a, HasServer sublayout)
123− => HasServer (Capture capture a :> sublayout) where
121+instance (KnownSymbol capture, FromHttpApiData a, HasServer sublayout config)
122+ => HasServer (Capture capture a :> sublayout) config where
124123
125124 type ServerT (Capture capture a :> sublayout) m =
126125 a -> ServerT sublayout m
127− type HasConfig (Capture capture a :> sublayout) c = (HasConfig sublayout c)
128126
129127 route Proxy config d =
130128 DynamicRouter $ \ first ->
@@ -198,10 +196,9 @@ methodRouterHeaders method proxy status action = LeafRouter route'
198196
199197instance OVERLAPPABLE_
200198 ( AllCTRender ctypes a, ReflectMethod method, KnownNat status
201− ) => HasServer (Verb method status ctypes a) where
199+ ) => HasServer (Verb method status ctypes a) config where
202200
203201 type ServerT (Verb method status ctypes a) m = m a
204− type HasConfig (Verb method status ctypes a) c = ()
205202
206203 route Proxy _ = methodRouter method (Proxy :: Proxy ctypes) status
207204 where method = reflectMethod (Proxy :: Proxy method)
@@ -210,10 +207,9 @@ instance OVERLAPPABLE_
210207instance OVERLAPPING_
211208 ( AllCTRender ctypes a, ReflectMethod method, KnownNat status
212209 , GetHeaders (Headers h a)
213− ) => HasServer (Verb method status ctypes (Headers h a)) where
210+ ) => HasServer (Verb method status ctypes (Headers h a)) config where
214211
215212 type ServerT (Verb method status ctypes (Headers h a)) m = m (Headers h a)
216− type HasConfig (Verb method status ctypes (Headers h a)) c = ()
217213
218214 route Proxy _ = methodRouterHeaders method (Proxy :: Proxy ctypes) status
219215 where method = reflectMethod (Proxy :: Proxy method)
@@ -239,12 +235,11 @@ instance OVERLAPPING_
239235-- > server = viewReferer
240236-- > where viewReferer :: Referer -> ExceptT ServantErr IO referer
241237-- > viewReferer referer = return referer
242−instance (KnownSymbol sym, FromHttpApiData a, HasServer sublayout)
243− => HasServer (Header sym a :> sublayout) where
238+instance (KnownSymbol sym, FromHttpApiData a, HasServer sublayout config)
239+ => HasServer (Header sym a :> sublayout) config where
244240
245241 type ServerT (Header sym a :> sublayout) m =
246242 Maybe a -> ServerT sublayout m
247− type HasConfig (Header sym a :> sublayout) c = HasConfig sublayout c
248243
249244 route Proxy config subserver = WithRequest $ \ request ->
250245 let mheader = parseHeaderMaybe =<< lookup str (requestHeaders request)
@@ -272,12 +267,11 @@ instance (KnownSymbol sym, FromHttpApiData a, HasServer sublayout)
272267-- > where getBooksBy :: Maybe Text -> ExceptT ServantErr IO [Book]
273268-- > getBooksBy Nothing = ...return all books...
274269-- > getBooksBy (Just author) = ...return books by the given author...
275−instance (KnownSymbol sym, FromHttpApiData a, HasServer sublayout)
276− => HasServer (QueryParam sym a :> sublayout) where
270+instance (KnownSymbol sym, FromHttpApiData a, HasServer sublayout config)
271+ => HasServer (QueryParam sym a :> sublayout) config where
277272
278273 type ServerT (QueryParam sym a :> sublayout) m =
279274 Maybe a -> ServerT sublayout m
280− type HasConfig (QueryParam sym a :> sublayout) c = HasConfig sublayout c
281275
282276 route Proxy config subserver = WithRequest $ \ request ->
283277 let querytext = parseQueryText $ rawQueryString request
@@ -309,12 +303,11 @@ instance (KnownSymbol sym, FromHttpApiData a, HasServer sublayout)
309303-- > server = getBooksBy
310304-- > where getBooksBy :: [Text] -> ExceptT ServantErr IO [Book]
311305-- > getBooksBy authors = ...return all books by these authors...
312−instance (KnownSymbol sym, FromHttpApiData a, HasServer sublayout)
313− => HasServer (QueryParams sym a :> sublayout) where
306+instance (KnownSymbol sym, FromHttpApiData a, HasServer sublayout config)
307+ => HasServer (QueryParams sym a :> sublayout) config where
314308
315309 type ServerT (QueryParams sym a :> sublayout) m =
316310 [a] -> ServerT sublayout m
317− type HasConfig (QueryParams sym a :> sublayout) c = HasConfig sublayout c
318311
319312 route Proxy config subserver = WithRequest $ \ request ->
320313 let querytext = parseQueryText $ rawQueryString request
@@ -341,12 +334,11 @@ instance (KnownSymbol sym, FromHttpApiData a, HasServer sublayout)
341334-- > server = getBooks
342335-- > where getBooks :: Bool -> ExceptT ServantErr IO [Book]
343336-- > getBooks onlyPublished = ...return all books, or only the ones that are already published, depending on the argument...
344−instance (KnownSymbol sym, HasServer sublayout)
345− => HasServer (QueryFlag sym :> sublayout) where
337+instance (KnownSymbol sym, HasServer sublayout config)
338+ => HasServer (QueryFlag sym :> sublayout) config where
346339
347340 type ServerT (QueryFlag sym :> sublayout) m =
348341 Bool -> ServerT sublayout m
349− type HasConfig (QueryFlag sym :> sublayout) c = HasConfig sublayout c
350342
351343 route Proxy config subserver = WithRequest $ \ request ->
352344 let querytext = parseQueryText $ rawQueryString request
@@ -367,10 +359,9 @@ instance (KnownSymbol sym, HasServer sublayout)
367359-- >
368360-- > server :: Server MyApi
369361-- > server = serveDirectory "/var/www/images"
370−instance HasServer Raw where
362+instance HasServer Raw config where
371363
372364 type ServerT Raw m = Application
373− type HasConfig Raw c = ()
374365
375366 route Proxy _ rawApplication = LeafRouter $ \ request respond -> do
376367 r <- runDelayed rawApplication
@@ -400,12 +391,11 @@ instance HasServer Raw where
400391-- > server = postBook
401392-- > where postBook :: Book -> ExceptT ServantErr IO Book
402393-- > postBook book = ...insert into your db...
403−instance ( AllCTUnrender list a, HasServer sublayout
404− ) => HasServer (ReqBody list a :> sublayout) where
394+instance ( AllCTUnrender list a, HasServer sublayout config
395+ ) => HasServer (ReqBody list a :> sublayout) config where
405396
406397 type ServerT (ReqBody list a :> sublayout) m =
407398 a -> ServerT sublayout m
408− type HasConfig (ReqBody list a :> sublayout) c = HasConfig sublayout c
409399
410400 route Proxy config subserver = WithRequest $ \ request ->
411401 route (Proxy :: Proxy sublayout) config (addBodyCheck subserver (bodyCheck request))
@@ -426,42 +416,37 @@ instance ( AllCTUnrender list a, HasServer sublayout
426416
427417-- | Make sure the incoming request starts with @"/path"@, strip it and
428418-- pass the rest of the request path to @sublayout@.
429−instance (KnownSymbol path, HasServer sublayout) => HasServer (path :> sublayout) where
419+instance (KnownSymbol path, HasServer sublayout config) => HasServer (path :> sublayout) config where
430420
431421 type ServerT (path :> sublayout) m = ServerT sublayout m
432− type HasConfig (path :> sublayout) c = HasConfig sublayout c
433422
434423 route Proxy config subserver = StaticRouter $
435424 M.singleton (cs (symbolVal proxyPath))
436425 (route (Proxy :: Proxy sublayout) config subserver)
437426 where proxyPath = Proxy :: Proxy path
438427
439−instance HasServer api => HasServer (RemoteHost :> api) where
428+instance HasServer api config => HasServer (RemoteHost :> api) config where
440429 type ServerT (RemoteHost :> api) m = SockAddr -> ServerT api m
441− type HasConfig (RemoteHost :> api) c = HasConfig api c
442430
443431 route Proxy config subserver = WithRequest $ \req ->
444432 route (Proxy :: Proxy api) config (passToServer subserver $ remoteHost req)
445433
446−instance HasServer api => HasServer (IsSecure :> api) where
434+instance HasServer api config => HasServer (IsSecure :> api) config where
447435 type ServerT (IsSecure :> api) m = IsSecure -> ServerT api m
448− type HasConfig (IsSecure :> api) c = HasConfig api c
449436
450437 route Proxy config subserver = WithRequest $ \req ->
451438 route (Proxy :: Proxy api) config (passToServer subserver $ secure req)
452439
453440 where secure req = if isSecure req then Secure else NotSecure
454441
455−instance HasServer api => HasServer (Vault :> api) where
442+instance HasServer api config => HasServer (Vault :> api) config where
456443 type ServerT (Vault :> api) m = Vault -> ServerT api m
457− type HasConfig (Vault :> api) c = HasConfig api c
458444
459445 route Proxy config subserver = WithRequest $ \req ->
460446 route (Proxy :: Proxy api) config (passToServer subserver $ vault req)
461447
462−instance HasServer api => HasServer (HttpVersion :> api) where
448+instance HasServer api config => HasServer (HttpVersion :> api) config where
463449 type ServerT (HttpVersion :> api) m = HttpVersion -> ServerT api m
464− type HasConfig (HttpVersion :> api) c = HasConfig api c
465450
466451 route Proxy config subserver = WithRequest $ \req ->
467452 route (Proxy :: Proxy api) config (passToServer subserver $ httpVersion req)
@@ -477,11 +462,11 @@ ct_wildcard = "*" <> "/" <> "*" -- Because CPP
477462
478463-- * configs
479464
480−instance HasServer subApi => HasServer (WithNamedConfig name subConfig subApi) where
465+instance (HasConfigEntry config (NamedConfig name subConfig), HasServer subApi subConfig)
466+ => HasServer (WithNamedConfig name subConfig subApi) config where
467+
481468 type ServerT (WithNamedConfig name subConfig subApi) m =
482469 ServerT subApi m
483− type HasConfig (WithNamedConfig name subConfig subApi) config =
484− (HasConfigEntry config (NamedConfig name subConfig), HasConfig subApi subConfig)
485470
486471 route Proxy config delayed =
487472 route subProxy subConfig delayed