{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE PatternSynonyms #-}
module Servant.Server
(
serve
, serveWithContext
, serveWithContextT
, ServerContext
,
toApplication
,
HasServer(..)
, Server
, EmptyServer
, emptyServer
, Handler (..)
, runHandler
, pattern MkHandler
, 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
, err429
, 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 {k} (api :: k).
HasServer api '[] =>
Proxy api -> Server api -> Application
serve Proxy api
p = Proxy api -> Context '[] -> ServerT api Handler -> Application
forall {k} (api :: k) (context :: [Type]).
(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 {k} (api :: k) (context :: [Type]).
(HasServer api context, ServerContext context) =>
Proxy api -> Context context -> Server api -> Application
serveWithContext Proxy api
p Context context
context = Proxy api
-> Context context
-> (forall x. Handler x -> Handler x)
-> ServerT api Handler
-> Application
forall {k} (api :: k) (context :: [Type]) (m :: Type -> Type).
(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 Handler x -> Handler x
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 :: forall {k} (api :: k) (context :: [Type]) (m :: Type -> Type).
(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 (Proxy api
-> Context context -> Delayed () (Server api) -> Router ()
forall env.
Proxy api
-> Context context -> Delayed env (Server api) -> Router env
forall {k} (api :: k) (context :: [Type]) 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 :: [Type]) (m :: Type -> Type)
(n :: Type -> Type).
HasServer api context =>
Proxy api
-> Proxy context
-> (forall x. m x -> n x)
-> ServerT api m
-> ServerT api n
forall (m :: Type -> Type) (n :: Type -> Type).
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) m x -> Handler x
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 .++ '[ErrorFormatters]) -> ErrorFormatters
forall (context :: [Type]) val.
HasContextEntry context val =>
Context context -> val
getContextEntry (Context (context .++ '[ErrorFormatters]) -> ErrorFormatters)
-> (Context context -> Context (context .++ '[ErrorFormatters]))
-> Context context
-> ErrorFormatters
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Context context -> Context (context .++ '[ErrorFormatters])
forall (ctx :: [Type]).
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 :: forall {k} (api :: k) (m :: Type -> Type) (n :: Type -> Type).
HasServer api '[] =>
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 :: [Type]) (m :: Type -> Type)
(n :: Type -> Type).
HasServer api context =>
Proxy api
-> Proxy context
-> (forall x. m x -> n x)
-> ServerT api m
-> ServerT api n
forall (m :: Type -> Type) (n :: Type -> Type).
Proxy api
-> Proxy '[]
-> (forall x. m x -> n x)
-> ServerT api m
-> ServerT api n
hoistServerWithContext Proxy api
p (Proxy '[]
forall {k}. Proxy '[]
forall {k} (t :: k). Proxy t
Proxy :: Proxy '[])
layout :: (HasServer api '[]) => Proxy api -> Text
layout :: forall {k} (api :: k). HasServer api '[] => Proxy api -> Text
layout Proxy api
p = Proxy api -> Context '[] -> Text
forall {k} (api :: k) (context :: [Type]).
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 {k} (api :: k) (context :: [Type]).
HasServer api context =>
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 env.
Proxy api
-> Context context -> Delayed env (Server api) -> Router env
forall {k} (api :: k) (context :: [Type]) 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)))