servant-server-0.14: A family of combinators for defining webservices APIs and serving them

Safe HaskellNone
LanguageHaskell2010

Servant.Server.Internal

Contents

Synopsis

Documentation

class HasServer api context where Source #

Minimal complete definition

route, hoistServerWithContext

Associated Types

type ServerT api (m :: * -> *) :: * Source #

Methods

route :: Proxy api -> Context context -> Delayed env (Server api) -> Router env Source #

hoistServerWithContext :: Proxy api -> Proxy context -> (forall x. m x -> n x) -> ServerT api m -> ServerT api n Source #

Instances

HasServer * Raw context Source #

Just pass the request to the underlying application and serve its response.

Example:

type MyApi = "images" :> Raw

server :: Server MyApi
server = serveDirectory "/var/www/images"

Associated Types

type ServerT Raw (context :: Raw) (m :: * -> *) :: * Source #

Methods

route :: Proxy Raw context -> Context context -> Delayed env (Server Raw context) -> Router env Source #

hoistServerWithContext :: Proxy Raw context -> Proxy [*] context -> (forall x. m x -> n x) -> ServerT Raw context m -> ServerT Raw context n Source #

HasServer * EmptyAPI context Source #

The server for an EmptyAPI is emptyAPIServer.

type MyApi = "nothing" :> EmptyApi

server :: Server MyApi
server = emptyAPIServer

Associated Types

type ServerT EmptyAPI (context :: EmptyAPI) (m :: * -> *) :: * Source #

Methods

route :: Proxy EmptyAPI context -> Context context -> Delayed env (Server EmptyAPI context) -> Router env Source #

hoistServerWithContext :: Proxy EmptyAPI context -> Proxy [*] context -> (forall x. m x -> n x) -> ServerT EmptyAPI context m -> ServerT EmptyAPI context n Source #

TypeError Constraint (HasServerArrowTypeError * * a b) => HasServer * (a -> b) context Source #

This instance prevents from accidentally using '->' instead of :>

>>> serve (Proxy :: Proxy (Capture "foo" Int -> Get '[JSON] Int)) (error "...")
...
...No instance HasServer (a -> b).
...Maybe you have used '->' instead of ':>' between
...Capture' '[] "foo" Int
...and
...Verb 'GET 200 '[JSON] Int
...
>>> undefined :: Server (Capture "foo" Int -> Get '[JSON] Int)
...
...No instance HasServer (a -> b).
...Maybe you have used '->' instead of ':>' between
...Capture' '[] "foo" Int
...and
...Verb 'GET 200 '[JSON] Int
...

Associated Types

type ServerT (a -> b) (context :: a -> b) (m :: * -> *) :: * Source #

Methods

route :: Proxy (a -> b) context -> Context context -> Delayed env (Server (a -> b) context) -> Router env Source #

hoistServerWithContext :: Proxy (a -> b) context -> Proxy [*] context -> (forall x. m x -> n x) -> ServerT (a -> b) context m -> ServerT (a -> b) context n Source #

(HasServer * a context, HasServer * b context) => HasServer * ((:<|>) a b) context Source #

A server for a :<|> b first tries to match the request against the route represented by a and if it fails tries b. You must provide a request handler for each route.

type MyApi = "books" :> Get '[JSON] [Book] -- GET /books
        :<|> "books" :> ReqBody Book :> Post '[JSON] Book -- POST /books

server :: Server MyApi
server = listAllBooks :<|> postBook
  where listAllBooks = ...
        postBook book = ...

Associated Types

type ServerT (a :<|> b) (context :: a :<|> b) (m :: * -> *) :: * Source #

Methods

route :: Proxy (a :<|> b) context -> Context context -> Delayed env (Server (a :<|> b) context) -> Router env Source #

hoistServerWithContext :: Proxy (a :<|> b) context -> Proxy [*] context -> (forall x. m x -> n x) -> ServerT (a :<|> b) context m -> ServerT (a :<|> b) context n Source #

(HasContextEntry context (NamedContext name subContext), HasServer * subApi subContext) => HasServer * (WithNamedContext name subContext subApi) context Source # 

Associated Types

type ServerT (WithNamedContext name subContext subApi) (context :: WithNamedContext name subContext subApi) (m :: * -> *) :: * Source #

Methods

route :: Proxy (WithNamedContext name subContext subApi) context -> Context context -> Delayed env (Server (WithNamedContext name subContext subApi) context) -> Router env Source #

hoistServerWithContext :: Proxy (WithNamedContext name subContext subApi) context -> Proxy [*] context -> (forall x. m x -> n x) -> ServerT (WithNamedContext name subContext subApi) context m -> ServerT (WithNamedContext name subContext subApi) context n Source #

TypeError Constraint (HasServerArrowKindError (k -> l) arr) => HasServer * ((:>) (k -> l) arr api) context Source #

This instance catches mistakes when there are non-saturated type applications on LHS of :>.

>>> serve (Proxy :: Proxy (Capture "foo" :> Get '[JSON] Int)) (error "...")
...
...Expected something of kind Symbol or *, got: k -> l on the LHS of ':>'.
...Maybe you haven't applied enough arguments to
...Capture' '[] "foo"
...
>>> undefined :: Server (Capture "foo" :> Get '[JSON] Int)
...
...Expected something of kind Symbol or *, got: k -> l on the LHS of ':>'.
...Maybe you haven't applied enough arguments to
...Capture' '[] "foo"
...

Associated Types

type ServerT (((k -> l) :> arr) api) (context :: ((k -> l) :> arr) api) (m :: * -> *) :: * Source #

Methods

route :: Proxy (((k -> l) :> arr) api) context -> Context context -> Delayed env (Server (((k -> l) :> arr) api) context) -> Router env Source #

hoistServerWithContext :: Proxy (((k -> l) :> arr) api) context -> Proxy [*] context -> (forall x. m x -> n x) -> ServerT (((k -> l) :> arr) api) context m -> ServerT (((k -> l) :> arr) api) context n Source #

HasServer * api context => HasServer * ((:>) * HttpVersion api) context Source # 

Associated Types

type ServerT ((* :> HttpVersion) api) (context :: (* :> HttpVersion) api) (m :: * -> *) :: * Source #

Methods

route :: Proxy ((* :> HttpVersion) api) context -> Context context -> Delayed env (Server ((* :> HttpVersion) api) context) -> Router env Source #

hoistServerWithContext :: Proxy ((* :> HttpVersion) api) context -> Proxy [*] context -> (forall x. m x -> n x) -> ServerT ((* :> HttpVersion) api) context m -> ServerT ((* :> HttpVersion) api) context n Source #

(AllCTUnrender list a, HasServer * api context, SBoolI (FoldLenient mods)) => HasServer * ((:>) * (ReqBody' mods list a) api) context Source #

If you use ReqBody in one of the endpoints for your API, this automatically requires your server-side handler to be a function that takes an argument of the type specified by ReqBody. The Content-Type header is inspected, and the list provided is used to attempt deserialization. If the request does not have a Content-Type header, it is treated as application/octet-stream (as specified in RFC7231. This lets servant worry about extracting it from the request and turning it into a value of the type you specify.

All it asks is for a FromJSON instance.

Example:

type MyApi = "books" :> ReqBody '[JSON] Book :> Post '[JSON] Book

server :: Server MyApi
server = postBook
  where postBook :: Book -> Handler Book
        postBook book = ...insert into your db...

Associated Types

type ServerT ((* :> ReqBody' mods list a) api) (context :: (* :> ReqBody' mods list a) api) (m :: * -> *) :: * Source #

Methods

route :: Proxy ((* :> ReqBody' mods list a) api) context -> Context context -> Delayed env (Server ((* :> ReqBody' mods list a) api) context) -> Router env Source #

hoistServerWithContext :: Proxy ((* :> ReqBody' mods list a) api) context -> Proxy [*] context -> (forall x. m x -> n x) -> ServerT ((* :> ReqBody' mods list a) api) context m -> ServerT ((* :> ReqBody' mods list a) api) context n Source #

HasServer * api context => HasServer * ((:>) * RemoteHost api) context Source # 

Associated Types

type ServerT ((* :> RemoteHost) api) (context :: (* :> RemoteHost) api) (m :: * -> *) :: * Source #

Methods

route :: Proxy ((* :> RemoteHost) api) context -> Context context -> Delayed env (Server ((* :> RemoteHost) api) context) -> Router env Source #

hoistServerWithContext :: Proxy ((* :> RemoteHost) api) context -> Proxy [*] context -> (forall x. m x -> n x) -> ServerT ((* :> RemoteHost) api) context m -> ServerT ((* :> RemoteHost) api) context n Source #

(KnownSymbol sym, FromHttpApiData a, HasServer * api context, SBoolI (FoldRequired mods), SBoolI (FoldLenient mods)) => HasServer * ((:>) * (QueryParam' mods sym a) api) context Source #

If you use QueryParam "author" Text in one of the endpoints for your API, this automatically requires your server-side handler to be a function that takes an argument of type Maybe Text.

This lets servant worry about looking it up in the query string and turning it into a value of the type you specify, enclosed in Maybe, because it may not be there and servant would then hand you Nothing.

You can control how it'll be converted from Text to your type by simply providing an instance of FromHttpApiData for your type.

Example:

type MyApi = "books" :> QueryParam "author" Text :> Get '[JSON] [Book]

server :: Server MyApi
server = getBooksBy
  where getBooksBy :: Maybe Text -> Handler [Book]
        getBooksBy Nothing       = ...return all books...
        getBooksBy (Just author) = ...return books by the given author...

Associated Types

type ServerT ((* :> QueryParam' mods sym a) api) (context :: (* :> QueryParam' mods sym a) api) (m :: * -> *) :: * Source #

Methods

route :: Proxy ((* :> QueryParam' mods sym a) api) context -> Context context -> Delayed env (Server ((* :> QueryParam' mods sym a) api) context) -> Router env Source #

hoistServerWithContext :: Proxy ((* :> QueryParam' mods sym a) api) context -> Proxy [*] context -> (forall x. m x -> n x) -> ServerT ((* :> QueryParam' mods sym a) api) context m -> ServerT ((* :> QueryParam' mods sym a) api) context n Source #

(KnownSymbol sym, FromHttpApiData a, HasServer * api context) => HasServer * ((:>) * (QueryParams sym a) api) context Source #

If you use QueryParams "authors" Text in one of the endpoints for your API, this automatically requires your server-side handler to be a function that takes an argument of type [Text].

This lets servant worry about looking up 0 or more values in the query string associated to authors and turning each of them into a value of the type you specify.

You can control how the individual values are converted from Text to your type by simply providing an instance of FromHttpApiData for your type.

Example:

type MyApi = "books" :> QueryParams "authors" Text :> Get '[JSON] [Book]

server :: Server MyApi
server = getBooksBy
  where getBooksBy :: [Text] -> Handler [Book]
        getBooksBy authors = ...return all books by these authors...

Associated Types

type ServerT ((* :> QueryParams sym a) api) (context :: (* :> QueryParams sym a) api) (m :: * -> *) :: * Source #

Methods

route :: Proxy ((* :> QueryParams sym a) api) context -> Context context -> Delayed env (Server ((* :> QueryParams sym a) api) context) -> Router env Source #

hoistServerWithContext :: Proxy ((* :> QueryParams sym a) api) context -> Proxy [*] context -> (forall x. m x -> n x) -> ServerT ((* :> QueryParams sym a) api) context m -> ServerT ((* :> QueryParams sym a) api) context n Source #

(KnownSymbol sym, HasServer * api context) => HasServer * ((:>) * (QueryFlag sym) api) context Source #

If you use QueryFlag "published" in one of the endpoints for your API, this automatically requires your server-side handler to be a function that takes an argument of type Bool.

Example:

type MyApi = "books" :> QueryFlag "published" :> Get '[JSON] [Book]

server :: Server MyApi
server = getBooks
  where getBooks :: Bool -> Handler [Book]
        getBooks onlyPublished = ...return all books, or only the ones that are already published, depending on the argument...

Associated Types

type ServerT ((* :> QueryFlag sym) api) (context :: (* :> QueryFlag sym) api) (m :: * -> *) :: * Source #

Methods

route :: Proxy ((* :> QueryFlag sym) api) context -> Context context -> Delayed env (Server ((* :> QueryFlag sym) api) context) -> Router env Source #

hoistServerWithContext :: Proxy ((* :> QueryFlag sym) api) context -> Proxy [*] context -> (forall x. m x -> n x) -> ServerT ((* :> QueryFlag sym) api) context m -> ServerT ((* :> QueryFlag sym) api) context n Source #

(KnownSymbol sym, FromHttpApiData a, HasServer * api context, SBoolI (FoldRequired mods), SBoolI (FoldLenient mods)) => HasServer * ((:>) * (Header' * mods sym a) api) context Source #

If you use Header in one of the endpoints for your API, this automatically requires your server-side handler to be a function that takes an argument of the type specified by Header. This lets servant worry about extracting it from the request and turning it into a value of the type you specify.

All it asks is for a FromHttpApiData instance.

Example:

newtype Referer = Referer Text
  deriving (Eq, Show, FromHttpApiData)

           -- GET /view-my-referer
type MyApi = "view-my-referer" :> Header "Referer" Referer :> Get '[JSON] Referer

server :: Server MyApi
server = viewReferer
  where viewReferer :: Referer -> Handler referer
        viewReferer referer = return referer

Associated Types

type ServerT ((* :> Header' * mods sym a) api) (context :: (* :> Header' * mods sym a) api) (m :: * -> *) :: * Source #

Methods

route :: Proxy ((* :> Header' * mods sym a) api) context -> Context context -> Delayed env (Server ((* :> Header' * mods sym a) api) context) -> Router env Source #

hoistServerWithContext :: Proxy ((* :> Header' * mods sym a) api) context -> Proxy [*] context -> (forall x. m x -> n x) -> ServerT ((* :> Header' * mods sym a) api) context m -> ServerT ((* :> Header' * mods sym a) api) context n Source #

HasServer * api context => HasServer * ((:>) * IsSecure api) context Source # 

Associated Types

type ServerT ((* :> IsSecure) api) (context :: (* :> IsSecure) api) (m :: * -> *) :: * Source #

Methods

route :: Proxy ((* :> IsSecure) api) context -> Context context -> Delayed env (Server ((* :> IsSecure) api) context) -> Router env Source #

hoistServerWithContext :: Proxy ((* :> IsSecure) api) context -> Proxy [*] context -> (forall x. m x -> n x) -> ServerT ((* :> IsSecure) api) context m -> ServerT ((* :> IsSecure) api) context n Source #

HasServer * api ctx => HasServer * ((:>) * (Summary desc) api) ctx Source #

Ignore Summary in server handlers.

Associated Types

type ServerT ((* :> Summary desc) api) (ctx :: (* :> Summary desc) api) (m :: * -> *) :: * Source #

Methods

route :: Proxy ((* :> Summary desc) api) ctx -> Context context -> Delayed env (Server ((* :> Summary desc) api) ctx) -> Router env Source #

hoistServerWithContext :: Proxy ((* :> Summary desc) api) ctx -> Proxy [*] context -> (forall x. m x -> n x) -> ServerT ((* :> Summary desc) api) ctx m -> ServerT ((* :> Summary desc) api) ctx n Source #

HasServer * api ctx => HasServer * ((:>) * (Description desc) api) ctx Source #

Ignore Description in server handlers.

Associated Types

type ServerT ((* :> Description desc) api) (ctx :: (* :> Description desc) api) (m :: * -> *) :: * Source #

Methods

route :: Proxy ((* :> Description desc) api) ctx -> Context context -> Delayed env (Server ((* :> Description desc) api) ctx) -> Router env Source #

hoistServerWithContext :: Proxy ((* :> Description desc) api) ctx -> Proxy [*] context -> (forall x. m x -> n x) -> ServerT ((* :> Description desc) api) ctx m -> ServerT ((* :> Description desc) api) ctx n Source #

(KnownSymbol capture, FromHttpApiData a, HasServer * api context) => HasServer * ((:>) * (Capture' mods capture a) api) context Source #

If you use Capture in one of the endpoints for your API, this automatically requires your server-side handler to be a function that takes an argument of the type specified by the Capture. This lets servant worry about getting it from the URL and turning it into a value of the type you specify.

You can control how it'll be converted from Text to your type by simply providing an instance of FromHttpApiData for your type.

Example:

type MyApi = "books" :> Capture "isbn" Text :> Get '[JSON] Book

server :: Server MyApi
server = getBook
  where getBook :: Text -> Handler Book
        getBook isbn = ...

Associated Types

type ServerT ((* :> Capture' mods capture a) api) (context :: (* :> Capture' mods capture a) api) (m :: * -> *) :: * Source #

Methods

route :: Proxy ((* :> Capture' mods capture a) api) context -> Context context -> Delayed env (Server ((* :> Capture' mods capture a) api) context) -> Router env Source #

hoistServerWithContext :: Proxy ((* :> Capture' mods capture a) api) context -> Proxy [*] context -> (forall x. m x -> n x) -> ServerT ((* :> Capture' mods capture a) api) context m -> ServerT ((* :> Capture' mods capture a) api) context n Source #

(KnownSymbol capture, FromHttpApiData a, HasServer * api context) => HasServer * ((:>) * (CaptureAll capture a) api) context Source #

If you use CaptureAll in one of the endpoints for your API, this automatically requires your server-side handler to be a function that takes an argument of a list of the type specified by the CaptureAll. This lets servant worry about getting values from the URL and turning them into values of the type you specify.

You can control how they'll be converted from Text to your type by simply providing an instance of FromHttpApiData for your type.

Example:

type MyApi = "src" :> CaptureAll "segments" Text :> Get '[JSON] SourceFile

server :: Server MyApi
server = getSourceFile
  where getSourceFile :: [Text] -> Handler Book
        getSourceFile pathSegments = ...

Associated Types

type ServerT ((* :> CaptureAll capture a) api) (context :: (* :> CaptureAll capture a) api) (m :: * -> *) :: * Source #

Methods

route :: Proxy ((* :> CaptureAll capture a) api) context -> Context context -> Delayed env (Server ((* :> CaptureAll capture a) api) context) -> Router env Source #

hoistServerWithContext :: Proxy ((* :> CaptureAll capture a) api) context -> Proxy [*] context -> (forall x. m x -> n x) -> ServerT ((* :> CaptureAll capture a) api) context m -> ServerT ((* :> CaptureAll capture a) api) context n Source #

(KnownSymbol realm, HasServer * api context, HasContextEntry context (BasicAuthCheck usr)) => HasServer * ((:>) * (BasicAuth realm usr) api) context Source #

Basic Authentication

Associated Types

type ServerT ((* :> BasicAuth realm usr) api) (context :: (* :> BasicAuth realm usr) api) (m :: * -> *) :: * Source #

Methods

route :: Proxy ((* :> BasicAuth realm usr) api) context -> Context context -> Delayed env (Server ((* :> BasicAuth realm usr) api) context) -> Router env Source #

hoistServerWithContext :: Proxy ((* :> BasicAuth realm usr) api) context -> Proxy [*] context -> (forall x. m x -> n x) -> ServerT ((* :> BasicAuth realm usr) api) context m -> ServerT ((* :> BasicAuth realm usr) api) context n Source #

HasServer * api context => HasServer * ((:>) * Vault api) context Source # 

Associated Types

type ServerT ((* :> Vault) api) (context :: (* :> Vault) api) (m :: * -> *) :: * Source #

Methods

route :: Proxy ((* :> Vault) api) context -> Context context -> Delayed env (Server ((* :> Vault) api) context) -> Router env Source #

hoistServerWithContext :: Proxy ((* :> Vault) api) context -> Proxy [*] context -> (forall x. m x -> n x) -> ServerT ((* :> Vault) api) context m -> ServerT ((* :> Vault) api) context n Source #

(KnownSymbol path, HasServer * api context) => HasServer * ((:>) Symbol path api) context Source #

Make sure the incoming request starts with "/path", strip it and pass the rest of the request path to api.

Associated Types

type ServerT ((Symbol :> path) api) (context :: (Symbol :> path) api) (m :: * -> *) :: * Source #

Methods

route :: Proxy ((Symbol :> path) api) context -> Context context -> Delayed env (Server ((Symbol :> path) api) context) -> Router env Source #

hoistServerWithContext :: Proxy ((Symbol :> path) api) context -> Proxy [*] context -> (forall x. m x -> n x) -> ServerT ((Symbol :> path) api) context m -> ServerT ((Symbol :> path) api) context n Source #

(AllCTRender ctypes a, ReflectMethod k1 method, KnownNat status, GetHeaders (Headers h a)) => HasServer * (Verb k1 method status ctypes (Headers h a)) context Source # 

Associated Types

type ServerT (Verb k1 method status ctypes (Headers h a)) (context :: Verb k1 method status ctypes (Headers h a)) (m :: * -> *) :: * Source #

Methods

route :: Proxy (Verb k1 method status ctypes (Headers h a)) context -> Context context -> Delayed env (Server (Verb k1 method status ctypes (Headers h a)) context) -> Router env Source #

hoistServerWithContext :: Proxy (Verb k1 method status ctypes (Headers h a)) context -> Proxy [*] context -> (forall x. m x -> n x) -> ServerT (Verb k1 method status ctypes (Headers h a)) context m -> ServerT (Verb k1 method status ctypes (Headers h a)) context n Source #

(AllCTRender ctypes a, ReflectMethod k1 method, KnownNat status) => HasServer * (Verb k1 method status ctypes a) context Source # 

Associated Types

type ServerT (Verb k1 method status ctypes a) (context :: Verb k1 method status ctypes a) (m :: * -> *) :: * Source #

Methods

route :: Proxy (Verb k1 method status ctypes a) context -> Context context -> Delayed env (Server (Verb k1 method status ctypes a) context) -> Router env Source #

hoistServerWithContext :: Proxy (Verb k1 method status ctypes a) context -> Proxy [*] context -> (forall x. m x -> n x) -> ServerT (Verb k1 method status ctypes a) context m -> ServerT (Verb k1 method status ctypes a) context n Source #

(MimeRender * ctype a, ReflectMethod k1 method, KnownNat status, FramingRender * * framing ctype, ToStreamGenerator b a, GetHeaders (Headers h b)) => HasServer * (Stream k1 method status framing ctype (Headers h b)) context Source # 

Associated Types

type ServerT (Stream k1 method status framing ctype (Headers h b)) (context :: Stream k1 method status framing ctype (Headers h b)) (m :: * -> *) :: * Source #

Methods

route :: Proxy (Stream k1 method status framing ctype (Headers h b)) context -> Context context -> Delayed env (Server (Stream k1 method status framing ctype (Headers h b)) context) -> Router env Source #

hoistServerWithContext :: Proxy (Stream k1 method status framing ctype (Headers h b)) context -> Proxy [*] context -> (forall x. m x -> n x) -> ServerT (Stream k1 method status framing ctype (Headers h b)) context m -> ServerT (Stream k1 method status framing ctype (Headers h b)) context n Source #

(MimeRender * ctype a, ReflectMethod k1 method, KnownNat status, FramingRender * * framing ctype, ToStreamGenerator b a) => HasServer * (Stream k1 method status framing ctype b) context Source # 

Associated Types

type ServerT (Stream k1 method status framing ctype b) (context :: Stream k1 method status framing ctype b) (m :: * -> *) :: * Source #

Methods

route :: Proxy (Stream k1 method status framing ctype b) context -> Context context -> Delayed env (Server (Stream k1 method status framing ctype b) context) -> Router env Source #

hoistServerWithContext :: Proxy (Stream k1 method status framing ctype b) context -> Proxy [*] context -> (forall x. m x -> n x) -> ServerT (Stream k1 method status framing ctype b) context m -> ServerT (Stream k1 method status framing ctype b) context n Source #

type Server api = ServerT api Handler Source #

Instances

methodRouter :: AllCTRender ctypes a => (b -> ([(HeaderName, ByteString)], a)) -> Method -> Proxy ctypes -> Status -> Delayed env (Handler b) -> Router env Source #

streamRouter :: (MimeRender ctype a, FramingRender framing ctype, ToStreamGenerator b a) => (c -> ([(HeaderName, ByteString)], b)) -> Method -> Status -> Proxy framing -> Proxy ctype -> Delayed env (Handler c) -> Router env Source #

helpers

General Authentication

contexts

type HasServerArrowKindError arr = (Text "Expected something of kind Symbol or *, got: k -> l on the LHS of ':>'." :$$: Text "Maybe you haven't applied enough arguments to") :$$: ShowType arr Source #

type HasServerArrowTypeError a b = (((Text "No instance HasServer (a -> b)." :$$: Text "Maybe you have used '->' instead of ':>' between ") :$$: ShowType a) :$$: Text "and") :$$: ShowType b Source #

>>> import Servant