1{-# LANGUAGE DataKinds #-}
2{-# LANGUAGE PolyKinds #-}
3{-# LANGUAGE TypeFamilies #-}
4{-# LANGUAGE DeriveGeneric #-}
5{-# LANGUAGE TypeOperators #-}
6{-# LANGUAGE FlexibleInstances #-}
7{-# LANGUAGE OverloadedStrings #-}
8{-# LANGUAGE QuasiQuotes #-}
9{-# OPTIONS_GHC -fno-warn-orphans #-}
11import Control.Concurrent (forkIO, killThread)
12import Control.Monad.Trans.Either
19import Network.Wai.Handler.Warp
25import Servant.Common.BaseUrl
29data Greet = Greet { _msg :: Text }
30 deriving (Generic, Show)
32instance FromJSON Greet
35instance ToCapture (Capture "name" Text) where
36 toCapture _ = DocCapture "name" "name of the person to greet"
38instance ToCapture (Capture "greetid" Text) where
39 toCapture _ = DocCapture "greetid" "identifier of the greet msg to remove"
41instance ToParam (QueryParam "capital" Bool) where
43 DocQueryParam "capital"
45 "Get the greeting message in uppercase (true) or not (false). Default is false."
48instance ToSample Greet where
49 toSample = Just $ Greet "Hello, haskeller!"
53 "hello" :> Capture "name" Text :> QueryParam "capital" Bool :> Get Greet
54 :<|> "greet" :> ReqBody Greet :> Post Greet
55 :<|> "delete" :> Capture "greetid" Text :> Delete
57testApi :: Proxy TestApi
60-- Server-side handlers
61server :: Server TestApi
62server = hello :<|> greet :<|> delete
64 where hello name Nothing = hello name (Just False)
65 hello name (Just False) = return . Greet $ "Hello, " <> name
66 hello name (Just True) = return . Greet . toUpper $ "Hello, " <> name
72-- Client-side query functions
73clientApi :: Client TestApi
74clientApi = client testApi
76getGreet :: Text -> Maybe Bool -> BaseUrl -> EitherT String IO Greet
77postGreet :: Greet -> BaseUrl -> EitherT String IO Greet
78deleteGreet :: Text -> BaseUrl -> EitherT String IO ()
79getGreet :<|> postGreet :<|> deleteGreet = clientApi
81-- Turn the server into a WAI app
83test = serve testApi server
87docsGreet = docs testApi
90runTestServer :: Port -> IO ()
91runTestServer port = run port test
93-- Run some queries against the server
96 tid <- forkIO $ runTestServer 8001
97 let uri = BaseUrl Http "localhost" 8001
98 print =<< runEitherT (getGreet "alp" (Just True) uri)
99 print =<< runEitherT (getGreet "alp" (Just False) uri)
101 print =<< runEitherT (postGreet g uri)
102 print =<< runEitherT (deleteGreet "blah" uri)
104 putStrLn "\n---------\n"
105 putStrLn $ markdown docsGreet
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list