Add content types to the example
This commit is contained in:
parent
3c14343b88
commit
6d85885b42
2 changed files with 23 additions and 9 deletions
|
@ -1,14 +1,19 @@
|
|||
{-# LANGUAGE DataKinds #-}
|
||||
{-# LANGUAGE DeriveGeneric #-}
|
||||
{-# LANGUAGE TypeOperators #-}
|
||||
{-# LANGUAGE FlexibleInstances #-}
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
{-# LANGUAGE DataKinds #-}
|
||||
{-# LANGUAGE DeriveGeneric #-}
|
||||
{-# LANGUAGE FlexibleInstances #-}
|
||||
{-# LANGUAGE MultiParamTypeClasses #-}
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
{-# LANGUAGE TypeOperators #-}
|
||||
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
||||
import Data.Aeson
|
||||
import Data.Aeson.Encode.Pretty (encodePretty)
|
||||
import Data.Proxy
|
||||
import Data.Text(Text)
|
||||
import Data.Text (Text)
|
||||
import qualified Data.Text.Lazy as T
|
||||
import qualified Data.Text.Lazy.Encoding as T
|
||||
import GHC.Generics
|
||||
import Servant.API
|
||||
import Servant.API.ContentTypes
|
||||
import Servant.Docs
|
||||
|
||||
-- * Example
|
||||
|
@ -20,6 +25,14 @@ newtype Greet = Greet Text
|
|||
instance FromJSON Greet
|
||||
instance ToJSON Greet
|
||||
|
||||
instance MimeRender JSON Greet where
|
||||
toByteString Proxy v = encodePretty v
|
||||
|
||||
instance MimeRender HTML Greet where
|
||||
toByteString Proxy (Greet s) = "<h1>" <> (c s) <> "</h1>"
|
||||
where
|
||||
c = T.encodeUtf8 . T.fromStrict
|
||||
|
||||
-- We add some useful annotations to our captures,
|
||||
-- query parameters and request body to make the docs
|
||||
-- really helpful.
|
||||
|
@ -66,11 +79,11 @@ intro2 = DocIntro "This title is below the last"
|
|||
-- API specification
|
||||
type TestApi =
|
||||
-- GET /hello/:name?capital={true, false} returns a Greet as JSON
|
||||
"hello" :> MatrixParam "lang" String :> Capture "name" Text :> QueryParam "capital" Bool :> Get '[JSON] Greet
|
||||
"hello" :> MatrixParam "lang" String :> Capture "name" Text :> QueryParam "capital" Bool :> Get '[JSON, HTML] Greet
|
||||
|
||||
-- POST /greet with a Greet as JSON in the request body,
|
||||
-- returns a Greet as JSON
|
||||
:<|> "greet" :> ReqBody '[JSON] Greet :> Post '[JSON] Greet
|
||||
:<|> "greet" :> ReqBody '[JSON,HTML] Greet :> Post '[JSON] Greet
|
||||
|
||||
-- DELETE /greet/:greetid
|
||||
:<|> "greet" :> Capture "greetid" Text :> Delete
|
||||
|
|
|
@ -32,6 +32,7 @@ library
|
|||
, aeson-pretty < 0.8
|
||||
, bytestring
|
||||
, hashable
|
||||
, http-media
|
||||
, lens
|
||||
, servant >= 0.2.1
|
||||
, string-conversions
|
||||
|
@ -46,5 +47,5 @@ executable greet-docs
|
|||
main-is: greet.hs
|
||||
hs-source-dirs: example
|
||||
ghc-options: -Wall
|
||||
build-depends: base, aeson, servant, servant-docs, text
|
||||
build-depends: base, aeson, aeson-pretty, servant, servant-docs, text
|
||||
default-language: Haskell2010
|
||||
|
|
Loading…
Reference in a new issue