@@ -1,77 +0,0 @@
1−{-# LANGUAGE DataKinds #-}
2−{-# LANGUAGE DeriveGeneric #-}
3−{-# LANGUAGE OverloadedStrings #-}
4−{-# LANGUAGE PolyKinds #-}
5−{-# LANGUAGE TypeFamilies #-}
6−{-# LANGUAGE TypeOperators #-}
7−
8−import Data.Aeson
9−import Data.Monoid
10−import Data.Proxy
11−import Data.Text
12−import GHC.Generics
13−import Network.Wai
14−import Network.Wai.Handler.Warp
15−
16−import Servant
17−import API
18−
19−-- * Example
20−
21−-- | A greet message data type
22−newtype Greet = Greet { _msg :: Text }
23− deriving (Generic, Show)
24−
25− instance FromJSON Greet
26−instance ToJSON Greet
27−
28−-- API specification
29−type TestApi =
30− -- GET /hello/:name?capital={true, false} returns a Greet as JSON
31− "hello" :> Capture "name" Text :> QueryParam "capital" Bool :> Get '[JSON] Greet
32−
33− -- POST /greet with a Greet as JSON in the request body,
34− -- returns a Greet as JSON
35− :<|> "greet" :> ReqBody '[JSON] Greet :> Post '[JSON] Greet
36−
37− -- DELETE /greet/:greetid
38− :<|> "greet" :> Capture "greetid" Text :> Delete '[JSON] ()
39−
40− testApi :: Proxy TestApi
41− testApi = Proxy
42−
43−
44−server1 :: Server API
45−server1 = return $ Right ()
46−
47− -- Server-side handlers.
48−--
49−-- There's one handler per endpoint, which, just like in the type
50−-- that represents the API, are glued together using :<|>.
51−--
52−-- Each handler runs in the 'ExceptT ServantErr IO' monad.
53−server :: Server TestApi
54−server = helloH :<|> postGreetH :<|> deleteGreetH
55−
56− where helloH name Nothing = helloH name (Just False)
57− helloH name (Just False) = return . Greet $ "Hello, " <> name
58− helloH name (Just True) = return . Greet . toUpper $ "Hello, " <> name
59−
60− postGreetH greet = return greet
61−
62− deleteGreetH _ = return ()
63−
64− -- Turn the server into a WAI app. 'serve' is provided by servant,
65−-- more precisely by the Servant.Server module.
66−test :: Application
67−test = serve testApi EmptyConfig server
68−
69−-- Run the server.
70−--
71−-- 'run' comes from Network.Wai.Handler.Warp
72−runTestServer :: Port -> IO ()
73−runTestServer port = run port test
74−
75−-- Put this all to work!
76−main :: IO ()
77−main = runTestServer 8000