on master help sign in
Sample program more sensible and update README
commitbdf61e4df941363d0f85fb291c78e266bf7e83d5 authorThomas Sutton <me@thomas-sutton.id.au> dateSun 22 Feb 2015, 17:18 (12 years ago) parent02c4adf
2 files changed+14−4
README.md+10−2 example/greet.hs+4−2
README.md+10−2
@@ -31,6 +31,14 @@ data Greet = Greet { _msg :: Text }
3131instance FromJSON Greet
3232instance ToJSON Greet
3333
34+-- we can render a Greeting into JSON using this ToJSON instance
35+instance MimeRender JSON Greet where
36+ toByteString Proxy = encodePretty
37+
38+-- or we can render it to HTML
39+instance MimeRender HTML Greet where
40+ toByteString Proxy (Greet s) = "<h1>" <> cs s <> "</h1>"
41+
3442-- we provide a sample value for the 'Greet' type
3543instance ToSample Greet where
3644 toSample = Just g
@@ -51,8 +59,8 @@ instance ToCapture (Capture "greetid" Text) where
5159
5260-- API specification
5361type TestApi =
54− "hello" :> Capture "name" Text :> QueryParam "capital" Bool :> Get Greet
55− :<|> "greet" :> RQBody Greet :> Post Greet
62+ "hello" :> Capture "name" Text :> QueryParam "capital" Bool :> Get '[JSON,HTML] Greet
63+ :<|> "greet" :> RQBody '[JSON] Greet :> Post '[JSON] Greet
5664 :<|> "delete" :> Capture "greetid" Text :> Delete
5765
5866testApi :: Proxy TestApi
example/greet.hs+4−2
@@ -23,9 +23,11 @@ newtype Greet = Greet Text
2323instance FromJSON Greet
2424instance ToJSON Greet
2525
26+-- | A 'Greet' value can be rendered to 'JSON'.
2627instance MimeRender JSON Greet where
2728 toByteString Proxy = encodePretty
2829
30+-- | A 'Greet' value can be rendered to 'HTML'.
2931instance MimeRender HTML Greet where
3032 toByteString Proxy (Greet s) = "<h1>" <> cs s <> "</h1>"
3133
@@ -74,12 +76,12 @@ intro2 = DocIntro "This title is below the last"
7476
7577-- API specification
7678type TestApi =
77− -- GET /hello/:name?capital={true, false} returns a Greet as JSON
79+ -- GET /hello/:name?capital={true, false} returns a Greet as JSON or HTML
7880 "hello" :> MatrixParam "lang" String :> Capture "name" Text :> QueryParam "capital" Bool :> Get '[JSON, HTML] Greet
7981
8082 -- POST /greet with a Greet as JSON in the request body,
8183 -- returns a Greet as JSON
82− :<|> "greet" :> ReqBody '[JSON,HTML] Greet :> Post '[JSON] Greet
84+ :<|> "greet" :> ReqBody '[JSON] Greet :> Post '[JSON] Greet
8385
8486 -- DELETE /greet/:greetid
8587 :<|> "greet" :> Capture "greetid" Text :> Delete