{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
module Servant.Server
(
serve
, serveWithContext
, serveWithContextT
, ServerContext
,
toApplication
,
HasServer(..)
, Server
, EmptyServer
, emptyServer
, Handler (..)
, runHandler
, layout
, layoutWithContext
, hoistServer
, tweakResponse
, Context(..)
, HasContextEntry(getContextEntry)
, type (.++)
, (.++)
, NamedContext(..)
, descendIntoNamedContext
, BasicAuthCheck(BasicAuthCheck, unBasicAuthCheck)
, BasicAuthResult(..)
, ServerError(..)
, err300
, err301
, err302
, err303
, err304
, err305
, err307
, err400
, err401
, err402
, err403
, err404
, err405
, err406
, err407
, err409
, err410
, err411
, err412
, err413
, err414
, err415
, err416
, err417
, err418
, err422
, err500
, err501
, err502
, err503
, err504
, err505
, ErrorFormatter
, NotFoundErrorFormatter
, ErrorFormatters
, bodyParserErrorFormatter
, urlParseErrorFormatter
, headerParseErrorFormatter
, notFoundErrorFormatter
, DefaultErrorFormatters
, defaultErrorFormatters
, getAcceptHeader
, Application
, Tagged (..)
, module Servant.Server.UVerb
) where
import Data.Proxy
(Proxy (..))
import Data.Tagged
(Tagged (..))
import Data.Text
(Text)
import Network.Wai
(Application)
import Servant.Server.Internal
import Servant.Server.UVerb
type ServerContext context =
( HasContextEntry (context .++ DefaultErrorFormatters) ErrorFormatters
)
serve :: (HasServer api '[]) => Proxy api -> Server api -> Application
serve :: forall api.
HasServer api '[] =>
Proxy api -> Server api -> Application
serve Proxy api
p = forall api (context :: [*]).
(HasServer api context, ServerContext context) =>
Proxy api -> Context context -> Server api -> Application
serveWithContext Proxy api
p Context '[]
EmptyContext
serveWithContext :: ( HasServer api context
, ServerContext context
)
=> Proxy api -> Context context -> Server api -> Application
serveWithContext :: forall api (context :: [*]).
(HasServer api context, ServerContext context) =>
Proxy api -> Context context -> Server api -> Application
serveWithContext Proxy api
p Context context
context = forall api (context :: [*]) (m :: * -> *).
(HasServer api context, ServerContext context) =>
Proxy api
-> Context context
-> (forall x. m x -> Handler x)
-> ServerT api m
-> Application
serveWithContextT Proxy api
p Context context
context forall a. a -> a
id
serveWithContextT ::
forall api context m.
(HasServer api context, ServerContext context) =>
Proxy api -> Context context -> (forall x. m x -> Handler x) -> ServerT api m -> Application
serveWithContextT :: forall api (context :: [*]) (m :: * -> *).
(HasServer api context, ServerContext context) =>
Proxy api
-> Context context
-> (forall x. m x -> Handler x)
-> ServerT api m
-> Application
serveWithContextT Proxy api
p Context context
context forall x. m x -> Handler x
toHandler ServerT api m
server =
RoutingApplication -> Application
toApplication (NotFoundErrorFormatter -> Router () -> RoutingApplication
runRouter NotFoundErrorFormatter
format404 (forall {k} (api :: k) (context :: [*]) env.
HasServer api context =>
Proxy api
-> Context context -> Delayed env (Server api) -> Router env
route Proxy api
p Context context
context (forall a env. RouteResult a -> Delayed env a
emptyDelayed RouteResult (ServerT api Handler)
router)))
where
router :: RouteResult (ServerT api Handler)
router = forall a. a -> RouteResult a
Route forall a b. (a -> b) -> a -> b
$ forall {k} (api :: k) (context :: [*]) (m :: * -> *) (n :: * -> *).
HasServer api context =>
Proxy api
-> Proxy context
-> (forall x. m x -> n x)
-> ServerT api m
-> ServerT api n
hoistServerWithContext Proxy api
p (forall {k} (t :: k). Proxy t
Proxy :: Proxy context) forall x. m x -> Handler x
toHandler ServerT api m
server
format404 :: NotFoundErrorFormatter
format404 = ErrorFormatters -> NotFoundErrorFormatter
notFoundErrorFormatter forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (context :: [*]) val.
HasContextEntry context val =>
Context context -> val
getContextEntry forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (ctx :: [*]).
Context ctx -> Context (MkContextWithErrorFormatter ctx)
mkContextWithErrorFormatter forall a b. (a -> b) -> a -> b
$ Context context
context
hoistServer :: (HasServer api '[]) => Proxy api
-> (forall x. m x -> n x) -> ServerT api m -> ServerT api n
hoistServer :: forall api (m :: * -> *) (n :: * -> *).
HasServer api '[] =>
Proxy api
-> (forall x. m x -> n x) -> ServerT api m -> ServerT api n
hoistServer Proxy api
p = forall {k} (api :: k) (context :: [*]) (m :: * -> *) (n :: * -> *).
HasServer api context =>
Proxy api
-> Proxy context
-> (forall x. m x -> n x)
-> ServerT api m
-> ServerT api n
hoistServerWithContext Proxy api
p (forall {k} (t :: k). Proxy t
Proxy :: Proxy '[])
layout :: (HasServer api '[]) => Proxy api -> Text
layout :: forall api. HasServer api '[] => Proxy api -> Text
layout Proxy api
p = forall api (context :: [*]).
HasServer api context =>
Proxy api -> Context context -> Text
layoutWithContext Proxy api
p Context '[]
EmptyContext
layoutWithContext :: (HasServer api context)
=> Proxy api -> Context context -> Text
layoutWithContext :: forall api (context :: [*]).
HasServer api context =>
Proxy api -> Context context -> Text
layoutWithContext Proxy api
p Context context
context =
forall env a. Router' env a -> Text
routerLayout (forall {k} (api :: k) (context :: [*]) env.
HasServer api context =>
Proxy api
-> Context context -> Delayed env (Server api) -> Router env
route Proxy api
p Context context
context (forall a env. RouteResult a -> Delayed env a
emptyDelayed (forall a. ServerError -> RouteResult a
FailFatal ServerError
err501)))