| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Servant.Server
Description
Implementing an API
serve :: HasServer layout => Proxy layout -> Server layout -> Application Source #
serve allows you to implement an API and produce a wai Application.
Example:
type MyApi = "books" :> Get [Book] -- GET /books
:<|> "books" :> ReqBody Book :> Post Book -- POST /books
server :: Server MyApi
server = listAllBooks :<|> postBook
where listAllBooks = ...
postBook book = ...
myApi :: Proxy MyApi
myApi = Proxy
app :: Application
app = serve myApi server
main :: IO ()
main = Network.Wai.Handler.Warp.run 8080 appHandlers for all standard combinators
class HasServer layout where Source #
Minimal complete definition
Instances
| HasServer * Delete Source # | If you have a The code of the handler will, just like
for |
| HasServer * Raw 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" |
| ToJSON result => HasServer * (Get result) Source # | When implementing the handler for a If successfully returning a value, we just require that its type has
a |
| ToJSON a => HasServer * (Post a) Source # | When implementing the handler for a If successfully returning a value, we just require that its type has
a |
| ToJSON a => HasServer * (Put a) Source # | When implementing the handler for a If successfully returning a value, we just require that its type has
a |
| (HasServer * a, HasServer * b) => HasServer * ((:<|>) a b) Source # | A server for type MyApi = "books" :> Get [Book] -- GET /books
:<|> "books" :> ReqBody Book :> Post Book -- POST /books
server :: Server MyApi
server = listAllBooks :<|> postBook
where listAllBooks = ...
postBook book = ... |
| (KnownSymbol capture, FromText a, HasServer * sublayout) => HasServer * ((:>) * (Capture Symbol * capture a) sublayout) Source # | If you use You can control how it'll be converted from Example: type MyApi = "books" :> Capture "isbn" Text :> Get Book
server :: Server MyApi
server = getBook
where getBook :: Text -> EitherT (Int, String) IO Book
getBook isbn = ... |
| (KnownSymbol sym, FromText a, HasServer * sublayout) => HasServer * ((:>) * (Header Symbol * sym a) sublayout) Source # | If you use All it asks is for a Example: newtype Referer = Referer Text
deriving (Eq, Show, FromText, ToText)
-- GET /view-my-referer
type MyApi = "view-my-referer" :> Header "Referer" Referer :> Get Referer
server :: Server MyApi
server = viewReferer
where viewReferer :: Referer -> EitherT (Int, String) IO referer
viewReferer referer = return referer |
| (KnownSymbol sym, FromText a, HasServer * sublayout) => HasServer * ((:>) * (QueryParam Symbol * sym a) sublayout) Source # | If you use 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 You can control how it'll be converted from Example: type MyApi = "books" :> QueryParam "author" Text :> Get [Book]
server :: Server MyApi
server = getBooksBy
where getBooksBy :: Maybe Text -> EitherT (Int, String) IO [Book]
getBooksBy Nothing = ...return all books...
getBooksBy (Just author) = ...return books by the given author... |
| (KnownSymbol sym, FromText a, HasServer * sublayout) => HasServer * ((:>) * (QueryParams Symbol * sym a) sublayout) Source # | If you use This lets servant worry about looking up 0 or more values in the query string
associated to You can control how the individual values are converted from Example: type MyApi = "books" :> QueryParams "authors" Text :> Get [Book]
server :: Server MyApi
server = getBooksBy
where getBooksBy :: [Text] -> EitherT (Int, String) IO [Book]
getBooksBy authors = ...return all books by these authors... |
| (KnownSymbol sym, HasServer * sublayout) => HasServer * ((:>) * (QueryFlag Symbol sym) sublayout) Source # | If you use Example: type MyApi = "books" :> QueryFlag "published" :> Get [Book]
server :: Server MyApi
server = getBooks
where getBooks :: Bool -> EitherT (Int, String) IO [Book]
getBooks onlyPublished = ...return all books, or only the ones that are already published, depending on the argument... |
| (FromJSON a, HasServer * sublayout) => HasServer * ((:>) * (ReqBody * a) sublayout) Source # | If you use All it asks is for a Example: type MyApi = "books" :> ReqBody Book :> Post Book
server :: Server MyApi
server = postBook
where postBook :: Book -> EitherT (Int, String) IO Book
postBook book = ...insert into your db... |
| (KnownSymbol sym, FromText a, HasServer * sublayout) => HasServer * ((:>) * (MatrixParam Symbol * sym a) sublayout) Source # | If you use 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 You can control how it'll be converted from Example: type MyApi = "books" :> MatrixParam "author" Text :> Get [Book]
server :: Server MyApi
server = getBooksBy
where getBooksBy :: Maybe Text -> EitherT (Int, String) IO [Book]
getBooksBy Nothing = ...return all books...
getBooksBy (Just author) = ...return books by the given author... |
| (KnownSymbol sym, FromText a, HasServer * sublayout) => HasServer * ((:>) * (MatrixParams Symbol * sym a) sublayout) Source # | If you use This lets servant worry about looking up 0 or more values in the query string
associated to You can control how the individual values are converted from Example: type MyApi = "books" :> MatrixParams "authors" Text :> Get [Book]
server :: Server MyApi
server = getBooksBy
where getBooksBy :: [Text] -> EitherT (Int, String) IO [Book]
getBooksBy authors = ...return all books by these authors... |
| (KnownSymbol sym, HasServer * sublayout) => HasServer * ((:>) * (MatrixFlag Symbol sym) sublayout) Source # | If you use Example: type MyApi = "books" :> MatrixFlag "published" :> Get [Book]
server :: Server MyApi
server = getBooks
where getBooks :: Bool -> EitherT (Int, String) IO [Book]
getBooks onlyPublished = ...return all books, or only the ones that are already published, depending on the argument... |
| (KnownSymbol path, HasServer * sublayout) => HasServer * ((:>) Symbol path sublayout) Source # | Make sure the incoming request starts with |