on master help sign in
Add content types to the example
commit6d85885b4219a80bcc21cc972dba05477350aafe authorThomas Sutton <me@thomas-sutton.id.au> dateThu 19 Feb 2015, 12:48 (12 years ago) parent3c14343
2 files changed+23−9
example/greet.hs+21−8 servant-docs.cabal+2−1
example/greet.hs+21−8
@@ -1,14 +1,19 @@
1−{-# LANGUAGE DataKinds #-}
2−{-# LANGUAGE DeriveGeneric #-}
3−{-# LANGUAGE TypeOperators #-}
4−{-# LANGUAGE FlexibleInstances #-}
5−{-# LANGUAGE OverloadedStrings #-}
1+{-# LANGUAGE DataKinds #-}
2+{-# LANGUAGE DeriveGeneric #-}
3+{-# LANGUAGE FlexibleInstances #-}
4+{-# LANGUAGE MultiParamTypeClasses #-}
5+{-# LANGUAGE OverloadedStrings #-}
6+{-# LANGUAGE TypeOperators #-}
67{-# OPTIONS_GHC -fno-warn-orphans #-}
78import Data.Aeson
9+import Data.Aeson.Encode.Pretty (encodePretty)
810import 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
1014import GHC.Generics
1115import Servant.API
16+import Servant.API.ContentTypes
1217import Servant.Docs
1318
1419-- * Example
@@ -20,6 +25,14 @@ newtype Greet = Greet Text
2025instance FromJSON Greet
2126instance ToJSON Greet
2227
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+
2336-- We add some useful annotations to our captures,
2437-- query parameters and request body to make the docs
2538-- really helpful.
@@ -66,11 +79,11 @@ intro2 = DocIntro "This title is below the last"
6679-- API specification
6780type TestApi =
6881 -- 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
7083
7184 -- POST /greet with a Greet as JSON in the request body,
7285 -- returns a Greet as JSON
73− :<|> "greet" :> ReqBody '[JSON] Greet :> Post '[JSON] Greet
86+ :<|> "greet" :> ReqBody '[JSON,HTML] Greet :> Post '[JSON] Greet
7487
7588 -- DELETE /greet/:greetid
7689 :<|> "greet" :> Capture "greetid" Text :> Delete
servant-docs.cabal+2−1
@@ -32,6 +32,7 @@ library
3232 , aeson-pretty < 0.8
3333 , bytestring
3434 , hashable
35+ , http-media
3536 , lens
3637 , servant >= 0.2.1
3738 , string-conversions
@@ -46,5 +47,5 @@ executable greet-docs
4647 main-is: greet.hs
4748 hs-source-dirs: example
4849 ghc-options: -Wall
49− build-depends: base, aeson, servant, servant-docs, text
50+ build-depends: base, aeson, aeson-pretty, servant, servant-docs, text
5051 default-language: Haskell2010