on master help sign in
Add content types to the example
6d85885Thomas Sutton
example/greet.hs+21−8
@@ -1,14 +1,19 @@
1−{-# LANGUAGE DataKinds #-}
1+{-# LANGUAGE DataKinds #-}
2−{-# LANGUAGE DeriveGeneric #-}
2+{-# LANGUAGE DeriveGeneric #-}
3−{-# LANGUAGE TypeOperators #-}
4−{-# LANGUAGE FlexibleInstances #-}
3+{-# LANGUAGE FlexibleInstances #-}
4+{-# LANGUAGE MultiParamTypeClasses #-}
5−{-# LANGUAGE OverloadedStrings #-}
5+{-# LANGUAGE OverloadedStrings #-}
6+{-# LANGUAGE TypeOperators #-}
6{-# OPTIONS_GHC -fno-warn-orphans #-}
7{-# OPTIONS_GHC -fno-warn-orphans #-}
7import Data.Aeson
8import Data.Aeson
9+import Data.Aeson.Encode.Pretty (encodePretty)
8import Data.Proxy
10import Data.Proxy
9−import Data.Text(Text)
11+import Data.Text (Text)
12+import qualified Data.Text.Lazy as T
13+import qualified Data.Text.Lazy.Encoding as T
10import GHC.Generics
14import GHC.Generics
11import Servant.API
15import Servant.API
16+import Servant.API.ContentTypes
12import Servant.Docs
17import Servant.Docs
13
18
14-- * Example
19-- * Example
@@ -20,6 +25,14 @@ newtype Greet = Greet Text
20instance FromJSON Greet
25instance FromJSON Greet
21instance ToJSON Greet
26instance ToJSON Greet
22
27
28+instance MimeRender JSON Greet where
29+ toByteString Proxy v = encodePretty v
30+
31+instance MimeRender HTML Greet where
32+ toByteString Proxy (Greet s) = "<h1>" <> (c s) <> "</h1>"
33+ where
34+ c = T.encodeUtf8 . T.fromStrict
35+
23-- We add some useful annotations to our captures,
36-- We add some useful annotations to our captures,
24-- query parameters and request body to make the docs
37-- query parameters and request body to make the docs
25-- really helpful.
38-- really helpful.
@@ -66,11 +79,11 @@ intro2 = DocIntro "This title is below the last"
66-- API specification
79-- API specification
67type TestApi =
80type TestApi =
68 -- GET /hello/:name?capital={true, false} returns a Greet as JSON
81 -- GET /hello/:name?capital={true, false} returns a Greet as JSON
69− "hello" :> MatrixParam "lang" String :> Capture "name" Text :> QueryParam "capital" Bool :> Get '[JSON] Greet
82+ "hello" :> MatrixParam "lang" String :> Capture "name" Text :> QueryParam "capital" Bool :> Get '[JSON, HTML] Greet
70
83
71 -- POST /greet with a Greet as JSON in the request body,
84 -- POST /greet with a Greet as JSON in the request body,
72 -- returns a Greet as JSON
85 -- returns a Greet as JSON
73− :<|> "greet" :> ReqBody '[JSON] Greet :> Post '[JSON] Greet
86+ :<|> "greet" :> ReqBody '[JSON,HTML] Greet :> Post '[JSON] Greet
74
87
75 -- DELETE /greet/:greetid
88 -- DELETE /greet/:greetid
76 :<|> "greet" :> Capture "greetid" Text :> Delete
89 :<|> "greet" :> Capture "greetid" Text :> Delete
servant-docs.cabal+2−1
@@ -32,6 +32,7 @@ library
32 , aeson-pretty < 0.8
32 , aeson-pretty < 0.8
33 , bytestring
33 , bytestring
34 , hashable
34 , hashable
35+ , http-media
35 , lens
36 , lens
36 , servant >= 0.2.1
37 , servant >= 0.2.1
37 , string-conversions
38 , string-conversions
@@ -46,5 +47,5 @@ executable greet-docs
46 main-is: greet.hs
47 main-is: greet.hs
47 hs-source-dirs: example
48 hs-source-dirs: example
48 ghc-options: -Wall
49 ghc-options: -Wall
49− build-depends: base, aeson, servant, servant-docs, text
50+ build-depends: base, aeson, aeson-pretty, servant, servant-docs, text
50 default-language: Haskell2010
51 default-language: Haskell2010