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

Safe HaskellNone
LanguageHaskell2010

Servant.Server.Internal

Contents

Synopsis

Documentation

class HasServer layout where

Associated Types

type ServerT layout m :: *

Methods

route :: Proxy layout -> Delayed (Server layout) -> Router

Instances

HasServer * Raw

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"
(HasServer * a, HasServer * b) => HasServer * ((:<|>) a b)

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 = ...
(GetHeaders (Headers h v), AllCTRender ctypes v) => HasServer * (Get ctypes (Headers h v)) 
HasServer * (Get ctypes ()) 
AllCTRender ctypes a => HasServer * (Get ctypes a)

When implementing the handler for a Get endpoint, just like for Delete, Post and Put, the handler code runs in the ExceptT ServantErr IO monad, where the Int represents the status code and the String a message, returned in case of failure. You can quite handily use throwE to quickly fail if some conditions are not met.

If successfully returning a value, we use the type-level list, combined with the request's Accept header, to encode the value for you (returning a status code of 200). If there was no Accept header or it was */*, we return encode using the first Content-Type type on the list.

(GetHeaders (Headers h v), AllCTRender ctypes v) => HasServer * (Post ctypes (Headers h v)) 
HasServer * (Post ctypes ()) 
AllCTRender ctypes a => HasServer * (Post ctypes a)

When implementing the handler for a Post endpoint, just like for Delete, Get and Put, the handler code runs in the ExceptT ServantErr IO monad, where the Int represents the status code and the String a message, returned in case of failure. You can quite handily use throwE to quickly fail if some conditions are not met.

If successfully returning a value, we use the type-level list, combined with the request's Accept header, to encode the value for you (returning a status code of 201). If there was no Accept header or it was */*, we return encode using the first Content-Type type on the list.

(GetHeaders (Headers h v), AllCTRender ctypes v) => HasServer * (Delete ctypes (Headers h v)) 
HasServer * (Delete ctypes ()) 
AllCTRender ctypes a => HasServer * (Delete ctypes a)

If you have a Delete endpoint in your API, the handler for this endpoint is meant to delete a resource.

The code of the handler will, just like for Get, Post and Put, run in ExceptT ServantErr IO (). The Int represents the status code and the String a message to be returned. You can use throwE to painlessly error out if the conditions for a successful deletion are not met.

(GetHeaders (Headers h v), AllCTRender ctypes v) => HasServer * (Put ctypes (Headers h v)) 
HasServer * (Put ctypes ()) 
AllCTRender ctypes a => HasServer * (Put ctypes a)

When implementing the handler for a Put endpoint, just like for Delete, Get and Post, the handler code runs in the ExceptT ServantErr IO monad, where the Int represents the status code and the String a message, returned in case of failure. You can quite handily use throwE to quickly fail if some conditions are not met.

If successfully returning a value, we use the type-level list, combined with the request's Accept header, to encode the value for you (returning a status code of 200). If there was no Accept header or it was */*, we return encode using the first Content-Type type on the list.

(GetHeaders (Headers h v), AllCTRender ctypes v) => HasServer * (Patch ctypes (Headers h v)) 
HasServer * (Patch ctypes ()) 
AllCTRender ctypes a => HasServer * (Patch ctypes a)

When implementing the handler for a Patch endpoint, just like for Delete, Get and Put, the handler code runs in the ExceptT ServantErr IO monad, where the Int represents the status code and the String a message, returned in case of failure. You can quite handily use throwE to quickly fail if some conditions are not met.

If successfully returning a value, we just require that its type has a ToJSON instance and servant takes care of encoding it for you, yielding status code 200 along the way.

HasServer k api => HasServer * ((:>) * k HttpVersion api) 
HasServer k api => HasServer * ((:>) * k Vault api) 
HasServer k api => HasServer * ((:>) * k IsSecure api) 
HasServer k api => HasServer * ((:>) * k RemoteHost api) 
(AllCTUnrender list a, HasServer k sublayout) => HasServer * ((:>) * k (ReqBody * list a) sublayout)

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 -> ExceptT ServantErr IO Book
        postBook book = ...insert into your db...
(KnownSymbol sym, HasServer k sublayout) => HasServer * ((:>) * k (QueryFlag sym) sublayout)

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 -> ExceptT ServantErr IO [Book]
        getBooks onlyPublished = ...return all books, or only the ones that are already published, depending on the argument...
(KnownSymbol sym, FromHttpApiData a, HasServer k sublayout) => HasServer * ((:>) * k (QueryParams * sym a) sublayout)

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] -> ExceptT ServantErr IO [Book]
        getBooksBy authors = ...return all books by these authors...
(KnownSymbol sym, FromHttpApiData a, HasServer k sublayout) => HasServer * ((:>) * k (QueryParam * sym a) sublayout)

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 -> ExceptT ServantErr IO [Book]
        getBooksBy Nothing       = ...return all books...
        getBooksBy (Just author) = ...return books by the given author...
(KnownSymbol sym, FromHttpApiData a, HasServer k sublayout) => HasServer * ((:>) * k (Header sym a) sublayout)

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, ToText)

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

server :: Server MyApi
server = viewReferer
  where viewReferer :: Referer -> ExceptT ServantErr IO referer
        viewReferer referer = return referer
(KnownSymbol capture, FromHttpApiData a, HasServer k sublayout) => HasServer * ((:>) * k (Capture * capture a) sublayout)

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 -> ExceptT ServantErr IO Book
        getBook isbn = ...
(KnownSymbol path, HasServer k sublayout) => HasServer * ((:>) Symbol k path sublayout)

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

type Server layout = ServerT layout (ExceptT ServantErr IO)

Instances

captured :: FromHttpApiData a => proxy (Capture sym a) -> Text -> Maybe a

allowedMethodHead :: Method -> Request -> Bool

allowedMethod :: Method -> Request -> Bool

processMethodRouter :: forall a. ConvertibleStrings a ByteString => Maybe (a, ByteString) -> Status -> Method -> Maybe [(HeaderName, ByteString)] -> Request -> RouteResult Response

methodCheck :: Method -> Request -> IO (RouteResult ())

acceptCheck :: AllMime list => Proxy list -> ByteString -> IO (RouteResult ())

methodRouter :: AllCTRender ctypes a => Method -> Proxy ctypes -> Status -> Delayed (ExceptT ServantErr IO a) -> Router

methodRouterHeaders :: (GetHeaders (Headers h v), AllCTRender ctypes v) => Method -> Proxy ctypes -> Status -> Delayed (ExceptT ServantErr IO (Headers h v)) -> Router

pathIsEmpty :: Request -> Bool