2014-10-25 01:27:39 +02:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
|
|
|
{-# LANGUAGE PolyKinds #-}
|
|
|
|
{-# LANGUAGE TypeFamilies #-}
|
|
|
|
{-# LANGUAGE DeriveGeneric #-}
|
|
|
|
{-# LANGUAGE TypeOperators #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
|
|
|
import Data.Aeson
|
|
|
|
import Data.Monoid
|
|
|
|
import Data.Proxy
|
|
|
|
import Data.Text
|
|
|
|
import GHC.Generics
|
|
|
|
import Network.Wai
|
|
|
|
import Network.Wai.Handler.Warp
|
|
|
|
|
2014-11-27 18:28:01 +01:00
|
|
|
import Servant
|
2014-10-25 01:27:39 +02:00
|
|
|
|
|
|
|
-- * Example
|
|
|
|
|
2014-11-25 18:13:18 +01:00
|
|
|
-- | A greet message data type
|
|
|
|
newtype Greet = Greet { msg :: Text }
|
2014-10-25 01:27:39 +02:00
|
|
|
deriving (Generic, Show)
|
|
|
|
|
|
|
|
instance FromJSON Greet
|
|
|
|
instance ToJSON Greet
|
|
|
|
|
|
|
|
-- API specification
|
2014-10-28 15:22:28 +01:00
|
|
|
type TestApi =
|
2014-11-25 18:13:18 +01:00
|
|
|
-- GET /hello/:name?capital={true, false} returns a Greet as JSON
|
2014-10-28 15:06:47 +01:00
|
|
|
"hello" :> Capture "name" Text :> QueryParam "capital" Bool :> Get Greet
|
2014-11-25 18:13:18 +01:00
|
|
|
|
|
|
|
-- POST /greet with a Greet as JSON in the request body,
|
|
|
|
-- returns a Greet as JSON
|
2014-10-28 16:29:04 +01:00
|
|
|
:<|> "greet" :> ReqBody Greet :> Post Greet
|
2014-11-25 18:13:18 +01:00
|
|
|
|
|
|
|
-- DELETE /greet/:greetid
|
|
|
|
:<|> "greet" :> Capture "greetid" Text :> Delete
|
2014-10-25 01:27:39 +02:00
|
|
|
|
|
|
|
testApi :: Proxy TestApi
|
|
|
|
testApi = Proxy
|
|
|
|
|
2014-11-25 18:13:18 +01:00
|
|
|
-- Server-side handlers.
|
|
|
|
--
|
|
|
|
-- There's one handler per endpoint, which, just like in the type
|
|
|
|
-- that represents the API, are glued together using :<|>.
|
|
|
|
--
|
|
|
|
-- Each handler runs in the 'EitherT (Int, String) IO' monad.
|
2014-10-25 01:27:39 +02:00
|
|
|
server :: Server TestApi
|
2014-11-25 18:13:18 +01:00
|
|
|
server = helloH :<|> postGreetH :<|> deleteGreetH
|
2014-10-25 01:27:39 +02:00
|
|
|
|
2014-11-25 18:13:18 +01:00
|
|
|
where helloH name Nothing = helloH name (Just False)
|
|
|
|
helloH name (Just False) = return . Greet $ "Hello, " <> name
|
|
|
|
helloH name (Just True) = return . Greet . toUpper $ "Hello, " <> name
|
2014-10-25 01:27:39 +02:00
|
|
|
|
2014-11-25 18:13:18 +01:00
|
|
|
postGreetH greet = return greet
|
2014-10-25 01:27:39 +02:00
|
|
|
|
2014-11-25 18:13:18 +01:00
|
|
|
deleteGreetH _ = return ()
|
2014-10-28 09:14:10 +01:00
|
|
|
|
2014-11-25 18:13:18 +01:00
|
|
|
-- Turn the server into a WAI app. 'serve' is provided by servant,
|
|
|
|
-- more precisely by the Servant.Server module.
|
2014-10-25 01:27:39 +02:00
|
|
|
test :: Application
|
|
|
|
test = serve testApi server
|
|
|
|
|
2014-11-25 18:13:18 +01:00
|
|
|
-- Run the server.
|
|
|
|
--
|
|
|
|
-- 'run' comes from Network.Wai.Handler.Warp
|
2014-10-25 01:27:39 +02:00
|
|
|
runTestServer :: Port -> IO ()
|
|
|
|
runTestServer port = run port test
|
|
|
|
|
2014-11-25 18:13:18 +01:00
|
|
|
-- Put this all to work!
|
2014-10-25 01:27:39 +02:00
|
|
|
main :: IO ()
|
2014-11-27 18:28:01 +01:00
|
|
|
main = runTestServer 8001
|