{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module Servant.Server.Generic (
AsServerT,
AsServer,
genericServe,
genericServeT,
genericServeTWithContext,
genericServer,
genericServerT,
) where
import Data.Proxy
(Proxy (..))
import Servant.API.Generic
import Servant.Server
data AsServerT (m :: * -> *)
instance GenericMode (AsServerT m) where
type AsServerT m :- api = ServerT api m
type AsServer = AsServerT Handler
genericServe
:: forall routes.
( HasServer (ToServantApi routes) '[]
, GenericServant routes AsServer
, Server (ToServantApi routes) ~ ToServant routes AsServer
)
=> routes AsServer -> Application
genericServe :: routes AsServer -> Application
genericServe = Proxy (GToServant (Rep (routes AsApi)))
-> Server (GToServant (Rep (routes AsApi))) -> Application
forall api.
HasServer api '[] =>
Proxy api -> Server api -> Application
serve (Proxy (GToServant (Rep (routes AsApi)))
forall k (t :: k). Proxy t
Proxy :: Proxy (ToServantApi routes)) (GToServant (Rep (routes AsServer)) -> Application)
-> (routes AsServer -> GToServant (Rep (routes AsServer)))
-> routes AsServer
-> Application
forall b c a. (b -> c) -> (a -> b) -> a -> c
. routes AsServer -> GToServant (Rep (routes AsServer))
forall (routes :: * -> *).
GenericServant routes AsServer =>
routes AsServer -> ToServant routes AsServer
genericServer
genericServeT
:: forall (routes :: * -> *) (m :: * -> *).
( GenericServant routes (AsServerT m)
, GenericServant routes AsApi
, HasServer (ToServantApi routes) '[]
, ServerT (ToServantApi routes) m ~ ToServant routes (AsServerT m)
)
=> (forall a. m a -> Handler a)
-> routes (AsServerT m)
-> Application
genericServeT :: (forall a. m a -> Handler a) -> routes (AsServerT m) -> Application
genericServeT forall a. m a -> Handler a
f routes (AsServerT m)
server = Proxy (GToServant (Rep (routes AsApi)))
-> Server (GToServant (Rep (routes AsApi))) -> Application
forall api.
HasServer api '[] =>
Proxy api -> Server api -> Application
serve Proxy (GToServant (Rep (routes AsApi)))
p (Server (GToServant (Rep (routes AsApi))) -> Application)
-> Server (GToServant (Rep (routes AsApi))) -> Application
forall a b. (a -> b) -> a -> b
$ Proxy (GToServant (Rep (routes AsApi)))
-> (forall a. m a -> Handler a)
-> ServerT (GToServant (Rep (routes AsApi))) m
-> Server (GToServant (Rep (routes AsApi)))
forall api (m :: * -> *) (n :: * -> *).
HasServer api '[] =>
Proxy api
-> (forall x. m x -> n x) -> ServerT api m -> ServerT api n
hoistServer Proxy (GToServant (Rep (routes AsApi)))
p forall a. m a -> Handler a
f (routes (AsServerT m) -> ToServant routes (AsServerT m)
forall (routes :: * -> *) (m :: * -> *).
GenericServant routes (AsServerT m) =>
routes (AsServerT m) -> ToServant routes (AsServerT m)
genericServerT routes (AsServerT m)
server)
where
p :: Proxy (GToServant (Rep (routes AsApi)))
p = Proxy routes -> Proxy (GToServant (Rep (routes AsApi)))
forall (routes :: * -> *).
GenericServant routes AsApi =>
Proxy routes -> Proxy (ToServantApi routes)
genericApi (Proxy routes
forall k (t :: k). Proxy t
Proxy :: Proxy routes)
genericServeTWithContext
:: forall (routes :: * -> *) (m :: * -> *) (ctx :: [*]).
( GenericServant routes (AsServerT m)
, GenericServant routes AsApi
, HasServer (ToServantApi routes) ctx
, HasContextEntry (ctx .++ DefaultErrorFormatters) ErrorFormatters
, ServerT (ToServantApi routes) m ~ ToServant routes (AsServerT m)
)
=> (forall a. m a -> Handler a)
-> routes (AsServerT m)
-> Context ctx
-> Application
genericServeTWithContext :: (forall a. m a -> Handler a)
-> routes (AsServerT m) -> Context ctx -> Application
genericServeTWithContext forall a. m a -> Handler a
f routes (AsServerT m)
server Context ctx
ctx =
Proxy (GToServant (Rep (routes AsApi)))
-> Context ctx
-> Server (GToServant (Rep (routes AsApi)))
-> Application
forall api (context :: [*]).
(HasServer api context,
HasContextEntry
(context .++ DefaultErrorFormatters) ErrorFormatters) =>
Proxy api -> Context context -> Server api -> Application
serveWithContext Proxy (GToServant (Rep (routes AsApi)))
p Context ctx
ctx (Server (GToServant (Rep (routes AsApi))) -> Application)
-> Server (GToServant (Rep (routes AsApi))) -> Application
forall a b. (a -> b) -> a -> b
$
Proxy (GToServant (Rep (routes AsApi)))
-> Proxy ctx
-> (forall a. m a -> Handler a)
-> ServerT (GToServant (Rep (routes AsApi))) m
-> Server (GToServant (Rep (routes AsApi)))
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 (GToServant (Rep (routes AsApi)))
p Proxy ctx
pctx forall a. m a -> Handler a
f (routes (AsServerT m) -> ToServant routes (AsServerT m)
forall (routes :: * -> *) (m :: * -> *).
GenericServant routes (AsServerT m) =>
routes (AsServerT m) -> ToServant routes (AsServerT m)
genericServerT routes (AsServerT m)
server)
where
p :: Proxy (GToServant (Rep (routes AsApi)))
p = Proxy routes -> Proxy (GToServant (Rep (routes AsApi)))
forall (routes :: * -> *).
GenericServant routes AsApi =>
Proxy routes -> Proxy (ToServantApi routes)
genericApi (Proxy routes
forall k (t :: k). Proxy t
Proxy :: Proxy routes)
pctx :: Proxy ctx
pctx = Proxy ctx
forall k (t :: k). Proxy t
Proxy :: Proxy ctx
genericServer
:: GenericServant routes AsServer
=> routes AsServer
-> ToServant routes AsServer
genericServer :: routes AsServer -> ToServant routes AsServer
genericServer = routes AsServer -> ToServant routes AsServer
forall (routes :: * -> *) mode.
GenericServant routes mode =>
routes mode -> ToServant routes mode
toServant
genericServerT
:: GenericServant routes (AsServerT m)
=> routes (AsServerT m)
-> ToServant routes (AsServerT m)
genericServerT :: routes (AsServerT m) -> ToServant routes (AsServerT m)
genericServerT = routes (AsServerT m) -> ToServant routes (AsServerT m)
forall (routes :: * -> *) mode.
GenericServant routes mode =>
routes mode -> ToServant routes mode
toServant