{-# 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 :: Proxy api -> Server api -> Application
serve Proxy api
p = Proxy api -> Context '[] -> Server api -> Application
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 :: Proxy api -> Context context -> Server api -> Application
serveWithContext Proxy api
p Context context
context = Proxy api
-> Context context
-> (forall x. Handler x -> Handler x)
-> Server api
-> Application
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
forall x. Handler x -> Handler x
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 :: 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 (Proxy api
-> Context context -> Delayed () (Server api) -> Router ()
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 (RouteResult (Server api) -> Delayed () (Server api)
forall a env. RouteResult a -> Delayed env a
emptyDelayed RouteResult (Server api)
router)))
where
router :: RouteResult (Server api)
router = Server api -> RouteResult (Server api)
forall a. a -> RouteResult a
Route (Server api -> RouteResult (Server api))
-> Server api -> RouteResult (Server api)
forall a b. (a -> b) -> a -> b
$ Proxy api
-> Proxy context
-> (forall x. m x -> Handler x)
-> ServerT api m
-> Server api
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 (Proxy context
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 (ErrorFormatters -> NotFoundErrorFormatter)
-> (Context context -> ErrorFormatters)
-> Context context
-> NotFoundErrorFormatter
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Context (context .++ DefaultErrorFormatters) -> ErrorFormatters
forall (context :: [*]) val.
HasContextEntry context val =>
Context context -> val
getContextEntry (Context (context .++ DefaultErrorFormatters) -> ErrorFormatters)
-> (Context context
-> Context (context .++ DefaultErrorFormatters))
-> Context context
-> ErrorFormatters
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Context context -> Context (context .++ DefaultErrorFormatters)
forall (ctx :: [*]).
Context ctx -> Context (MkContextWithErrorFormatter ctx)
mkContextWithErrorFormatter (Context context -> NotFoundErrorFormatter)
-> Context context -> NotFoundErrorFormatter
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 :: Proxy api
-> (forall x. m x -> n x) -> ServerT api m -> ServerT api n
hoistServer Proxy api
p = Proxy api
-> Proxy '[]
-> (forall x. m x -> n x)
-> ServerT api m
-> ServerT api n
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 (Proxy '[]
forall k (t :: k). Proxy t
Proxy :: Proxy '[])
layout :: (HasServer api '[]) => Proxy api -> Text
layout :: Proxy api -> Text
layout Proxy api
p = Proxy api -> Context '[] -> Text
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 :: Proxy api -> Context context -> Text
layoutWithContext Proxy api
p Context context
context =
Router' Any RoutingApplication -> Text
forall env a. Router' env a -> Text
routerLayout (Proxy api
-> Context context
-> Delayed Any (Server api)
-> Router' Any RoutingApplication
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 (RouteResult (Server api) -> Delayed Any (Server api)
forall a env. RouteResult a -> Delayed env a
emptyDelayed (ServerError -> RouteResult (Server api)
forall a. ServerError -> RouteResult a
FailFatal ServerError
err501)))