2014-10-25 01:27:39 +02:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
|
|
|
{-# LANGUAGE PolyKinds #-}
|
|
|
|
{-# LANGUAGE TypeFamilies #-}
|
|
|
|
{-# LANGUAGE DeriveGeneric #-}
|
|
|
|
{-# LANGUAGE TypeOperators #-}
|
2014-10-28 09:04:27 +01:00
|
|
|
{-# LANGUAGE FlexibleInstances #-}
|
2014-10-25 01:27:39 +02:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2014-10-28 15:22:28 +01:00
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
2014-10-28 09:04:27 +01:00
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
2014-10-25 01:27:39 +02:00
|
|
|
|
|
|
|
import Control.Concurrent (forkIO, killThread)
|
|
|
|
import Control.Monad.Trans.Either
|
|
|
|
import Data.Aeson
|
|
|
|
import Data.Monoid
|
|
|
|
import Data.Proxy
|
|
|
|
import Data.Text
|
|
|
|
import GHC.Generics
|
|
|
|
import Network.Wai
|
|
|
|
import Network.Wai.Handler.Warp
|
|
|
|
|
|
|
|
import Servant.API
|
|
|
|
import Servant.Client
|
2014-10-28 09:04:27 +01:00
|
|
|
import Servant.Docs
|
2014-10-25 01:27:39 +02:00
|
|
|
import Servant.Server
|
|
|
|
|
|
|
|
-- * 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
|
|
|
|
|
2014-11-25 18:13:18 +01:00
|
|
|
-- We add some useful annotations to our captures,
|
|
|
|
-- query parameters and request body to make the docs
|
|
|
|
-- really helpful.
|
2014-10-28 09:04:27 +01:00
|
|
|
instance ToCapture (Capture "name" Text) where
|
|
|
|
toCapture _ = DocCapture "name" "name of the person to greet"
|
|
|
|
|
2014-10-28 09:14:10 +01:00
|
|
|
instance ToCapture (Capture "greetid" Text) where
|
|
|
|
toCapture _ = DocCapture "greetid" "identifier of the greet msg to remove"
|
|
|
|
|
2014-10-28 15:06:47 +01:00
|
|
|
instance ToParam (QueryParam "capital" Bool) where
|
2014-10-28 09:04:27 +01:00
|
|
|
toParam _ =
|
2014-10-28 15:06:47 +01:00
|
|
|
DocQueryParam "capital"
|
|
|
|
["true", "false"]
|
|
|
|
"Get the greeting message in uppercase (true) or not (false). Default is false."
|
2014-10-30 12:15:52 +01:00
|
|
|
Normal
|
2014-10-28 09:04:27 +01:00
|
|
|
|
|
|
|
instance ToSample Greet where
|
2014-11-13 08:19:14 +01:00
|
|
|
toSample = Just $ Greet "Hello, haskeller!"
|
2014-10-28 09:04:27 +01:00
|
|
|
|
2014-10-25 01:27:39 +02:00
|
|
|
-- 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
|
|
|
-- Client-side querying functions
|
|
|
|
--
|
|
|
|
-- They're all derived automatically from the type, and glued together
|
|
|
|
-- with :<|> just like in the type and for the server handlers, except
|
|
|
|
-- that we don't have to implement them!
|
2014-10-25 01:27:39 +02:00
|
|
|
clientApi :: Client TestApi
|
|
|
|
clientApi = client testApi
|
|
|
|
|
2014-11-05 13:03:57 +01:00
|
|
|
getGreet :: Text -> Maybe Bool -> BaseUrl -> EitherT String IO Greet
|
|
|
|
postGreet :: Greet -> BaseUrl -> EitherT String IO Greet
|
|
|
|
deleteGreet :: Text -> BaseUrl -> EitherT String IO ()
|
2014-10-28 09:14:10 +01:00
|
|
|
getGreet :<|> postGreet :<|> deleteGreet = clientApi
|
2014-10-25 01:27:39 +02: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
|
|
|
-- Generate the data that lets us have API docs. This
|
|
|
|
-- is derived from the type as well as from
|
|
|
|
-- the 'ToCapture', 'ToParam' and 'ToSample' instances from above.
|
2014-10-28 09:04:27 +01:00
|
|
|
docsGreet :: API
|
|
|
|
docsGreet = docs testApi
|
|
|
|
|
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 ()
|
|
|
|
main = do
|
2014-11-25 18:13:18 +01:00
|
|
|
-- we start the server, binding it to port 8001
|
2014-10-25 01:27:39 +02:00
|
|
|
tid <- forkIO $ runTestServer 8001
|
2014-11-25 18:13:18 +01:00
|
|
|
|
|
|
|
-- we tell the client where to find it
|
2014-11-05 13:03:57 +01:00
|
|
|
let uri = BaseUrl Http "localhost" 8001
|
2014-11-25 18:13:18 +01:00
|
|
|
|
|
|
|
-- we run a couple of requests against the server
|
2014-10-25 01:27:39 +02:00
|
|
|
print =<< runEitherT (getGreet "alp" (Just True) uri)
|
|
|
|
print =<< runEitherT (getGreet "alp" (Just False) uri)
|
|
|
|
let g = Greet "yo"
|
|
|
|
print =<< runEitherT (postGreet g uri)
|
2014-10-28 09:14:10 +01:00
|
|
|
print =<< runEitherT (deleteGreet "blah" uri)
|
2014-10-25 01:27:39 +02:00
|
|
|
killThread tid
|
2014-10-28 09:04:27 +01:00
|
|
|
putStrLn "\n---------\n"
|
2014-11-25 18:13:18 +01:00
|
|
|
|
|
|
|
-- we print the markdown docs
|
2014-11-14 12:54:38 +01:00
|
|
|
putStrLn $ markdown docsGreet
|