servant/servant-examples/tutorial/T10.hs

72 lines
2 KiB
Haskell
Raw Normal View History

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
2015-05-09 16:05:09 +02:00
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
2015-05-10 13:39:18 +02:00
module T10 where
2015-05-09 16:05:09 +02:00
import Data.ByteString.Lazy (ByteString)
import Data.Text.Lazy (pack)
import Data.Text.Lazy.Encoding (encodeUtf8)
import Network.HTTP.Types
import Network.Wai
import Servant
import Servant.Docs
2015-05-10 13:39:18 +02:00
import qualified T3
2015-05-09 16:05:09 +02:00
2015-07-30 18:44:13 +02:00
type DocsAPI = T3.API :<|> Raw IO Application
2015-05-09 16:05:09 +02:00
instance ToCapture (Capture "x" Int) where
toCapture _ = DocCapture "x" "(integer) position on the x axis"
instance ToCapture (Capture "y" Int) where
toCapture _ = DocCapture "y" "(integer) position on the y axis"
instance ToSample T3.Position where
toSamples _ = singleSample (T3.Position 3 14)
2015-05-09 16:05:09 +02:00
instance ToParam (QueryParam "name" String) where
toParam _ =
DocQueryParam "name"
["Alp", "John Doe", "..."]
"Name of the person to say hello to."
Normal
instance ToSample T3.HelloMessage where
2015-05-09 16:05:09 +02:00
toSamples _ =
2015-05-10 13:39:18 +02:00
[ ("When a value is provided for 'name'", T3.HelloMessage "Hello, Alp")
, ("When 'name' is not specified", T3.HelloMessage "Hello, anonymous coward")
2015-05-09 16:05:09 +02:00
]
2015-05-10 13:39:18 +02:00
ci :: T3.ClientInfo
ci = T3.ClientInfo "Alp" "alp@foo.com" 26 ["haskell", "mathematics"]
2015-05-09 16:05:09 +02:00
instance ToSample T3.ClientInfo where
toSamples _ = singleSample ci
2015-05-09 16:05:09 +02:00
instance ToSample T3.Email where
toSamples _ = singleSample (T3.emailForClient ci)
2015-05-09 16:05:09 +02:00
api :: Proxy DocsAPI
api = Proxy
docsBS :: ByteString
docsBS = encodeUtf8
. pack
. markdown
2015-05-10 13:39:18 +02:00
$ docsWithIntros [intro] T3.api
2015-05-09 16:05:09 +02:00
where intro = DocIntro "Welcome" ["This is our super webservice's API.", "Enjoy!"]
2015-05-09 16:05:09 +02:00
server :: Server DocsAPI
2015-05-10 13:39:18 +02:00
server = T3.server :<|> serveDocs
2015-05-09 16:05:09 +02:00
2015-07-30 18:44:13 +02:00
where serveDocs = Raw (\_ respond ->
respond $ responseLBS ok200 [plain] docsBS)
2015-05-09 16:05:09 +02:00
plain = ("Content-Type", "text/plain")
2015-05-09 16:05:09 +02:00
app :: Application
2016-01-14 23:43:48 +01:00
app = serve api EmptyConfig server