Sample program more sensible and update README

This commit is contained in:
Thomas Sutton 2015-02-22 17:18:07 +11:00
parent 02c4adfd18
commit bdf61e4df9
2 changed files with 14 additions and 4 deletions

View File

@ -31,6 +31,14 @@ data Greet = Greet { _msg :: Text }
instance FromJSON Greet
instance ToJSON Greet
-- we can render a Greeting into JSON using this ToJSON instance
instance MimeRender JSON Greet where
toByteString Proxy = encodePretty
-- or we can render it to HTML
instance MimeRender HTML Greet where
toByteString Proxy (Greet s) = "<h1>" <> cs s <> "</h1>"
-- we provide a sample value for the 'Greet' type
instance ToSample Greet where
toSample = Just g
@ -51,8 +59,8 @@ instance ToCapture (Capture "greetid" Text) where
-- API specification
type TestApi =
"hello" :> Capture "name" Text :> QueryParam "capital" Bool :> Get Greet
:<|> "greet" :> RQBody Greet :> Post Greet
"hello" :> Capture "name" Text :> QueryParam "capital" Bool :> Get '[JSON,HTML] Greet
:<|> "greet" :> RQBody '[JSON] Greet :> Post '[JSON] Greet
:<|> "delete" :> Capture "greetid" Text :> Delete
testApi :: Proxy TestApi

View File

@ -23,9 +23,11 @@ newtype Greet = Greet Text
instance FromJSON Greet
instance ToJSON Greet
-- | A 'Greet' value can be rendered to 'JSON'.
instance MimeRender JSON Greet where
toByteString Proxy = encodePretty
-- | A 'Greet' value can be rendered to 'HTML'.
instance MimeRender HTML Greet where
toByteString Proxy (Greet s) = "<h1>" <> cs s <> "</h1>"
@ -74,12 +76,12 @@ intro2 = DocIntro "This title is below the last"
-- API specification
type TestApi =
-- GET /hello/:name?capital={true, false} returns a Greet as JSON
-- GET /hello/:name?capital={true, false} returns a Greet as JSON or HTML
"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,HTML] Greet :> Post '[JSON] Greet
:<|> "greet" :> ReqBody '[JSON] Greet :> Post '[JSON] Greet
-- DELETE /greet/:greetid
:<|> "greet" :> Capture "greetid" Text :> Delete