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 DataKinds #-}
|
||||||
{-# LANGUAGE DeriveGeneric #-}
|
{-# LANGUAGE DeriveGeneric #-}
|
||||||
{-# LANGUAGE TypeOperators #-}
|
{-# LANGUAGE FlexibleInstances #-}
|
||||||
{-# LANGUAGE FlexibleInstances #-}
|
{-# LANGUAGE MultiParamTypeClasses #-}
|
||||||
{-# LANGUAGE OverloadedStrings #-}
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
{-# LANGUAGE TypeOperators #-}
|
||||||
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
||||||
import Data.Aeson
|
import Data.Aeson
|
||||||
|
import Data.Aeson.Encode.Pretty (encodePretty)
|
||||||
import Data.Proxy
|
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 GHC.Generics
|
||||||
import Servant.API
|
import Servant.API
|
||||||
|
import Servant.API.ContentTypes
|
||||||
import Servant.Docs
|
import Servant.Docs
|
||||||
|
|
||||||
-- * Example
|
-- * Example
|
||||||
|
@ -20,6 +25,14 @@ newtype Greet = Greet Text
|
||||||
instance FromJSON Greet
|
instance FromJSON Greet
|
||||||
instance ToJSON 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,
|
-- We add some useful annotations to our captures,
|
||||||
-- query parameters and request body to make the docs
|
-- query parameters and request body to make the docs
|
||||||
-- really helpful.
|
-- really helpful.
|
||||||
|
@ -66,11 +79,11 @@ intro2 = DocIntro "This title is below the last"
|
||||||
-- API specification
|
-- API specification
|
||||||
type TestApi =
|
type TestApi =
|
||||||
-- GET /hello/:name?capital={true, false} returns a Greet as JSON
|
-- 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,
|
-- POST /greet with a Greet as JSON in the request body,
|
||||||
-- returns a Greet as JSON
|
-- returns a Greet as JSON
|
||||||
:<|> "greet" :> ReqBody '[JSON] Greet :> Post '[JSON] Greet
|
:<|> "greet" :> ReqBody '[JSON,HTML] Greet :> Post '[JSON] Greet
|
||||||
|
|
||||||
-- DELETE /greet/:greetid
|
-- DELETE /greet/:greetid
|
||||||
:<|> "greet" :> Capture "greetid" Text :> Delete
|
:<|> "greet" :> Capture "greetid" Text :> Delete
|
||||||
|
|
|
@ -32,6 +32,7 @@ library
|
||||||
, aeson-pretty < 0.8
|
, aeson-pretty < 0.8
|
||||||
, bytestring
|
, bytestring
|
||||||
, hashable
|
, hashable
|
||||||
|
, http-media
|
||||||
, lens
|
, lens
|
||||||
, servant >= 0.2.1
|
, servant >= 0.2.1
|
||||||
, string-conversions
|
, string-conversions
|
||||||
|
@ -46,5 +47,5 @@ executable greet-docs
|
||||||
main-is: greet.hs
|
main-is: greet.hs
|
||||||
hs-source-dirs: example
|
hs-source-dirs: example
|
||||||
ghc-options: -Wall
|
ghc-options: -Wall
|
||||||
build-depends: base, aeson, servant, servant-docs, text
|
build-depends: base, aeson, aeson-pretty, servant, servant-docs, text
|
||||||
default-language: Haskell2010
|
default-language: Haskell2010
|
||||||
|
|
Loading…
Add table
Reference in a new issue