1{-# LANGUAGE DataKinds #-}
2{-# LANGUAGE PolyKinds #-}
3{-# LANGUAGE TypeFamilies #-}
4{-# LANGUAGE DeriveGeneric #-}
5{-# LANGUAGE TypeOperators #-}
6{-# LANGUAGE FlexibleInstances #-}
7{-# LANGUAGE OverloadedStrings #-}
8{-# OPTIONS_GHC -fno-warn-orphans #-}
10import Control.Concurrent (forkIO, killThread)
11import Control.Monad.Trans.Either
19import Network.Wai.Handler.Warp
28data Greet = Greet { _msg :: Text }
29 deriving (Generic, Show)
31instance FromJSON Greet
34instance ToCapture (Capture "name" Text) where
35 toCapture _ = DocCapture "name" "name of the person to greet"
37instance ToParam (GetParam "capital" Bool) where
41 "Get the greeting message in uppercase (true) or not (false). Default is false."
43instance ToSample Greet where
44 toSample Proxy = Just (encode g)
46 where g = Greet "Hello, haskeller!"
50 "hello" :> Capture "name" Text :> GetParam "capital" Bool :> Get Greet
51 :<|> "greet" :> RQBody Greet :> Post Greet
53testApi :: Proxy TestApi
56-- Server-side handlers
57server :: Server TestApi
58server = hello :<|> greet
60 where hello name Nothing = hello name (Just False)
61 hello name (Just False) = return . Greet $ "Hello, " <> name
62 hello name (Just True) = return . Greet . toUpper $ "Hello, " <> name
66-- Client-side query functions
67clientApi :: Client TestApi
68clientApi = client testApi
70getGreet :: Text -> Maybe Bool -> URI -> EitherT String IO Greet
71postGreet :: Greet -> URI -> EitherT String IO Greet
72getGreet :<|> postGreet = clientApi
74-- Turn the server into a WAI app
76test = serve testApi server
79docsGreet = docs testApi
82runTestServer :: Port -> IO ()
83runTestServer port = run port test
85-- Run some queries against the server
88 tid <- forkIO $ runTestServer 8001
89 let Just uri = parseURI "http://localhost:8001"
90 print =<< runEitherT (getGreet "alp" (Just True) uri)
91 print =<< runEitherT (getGreet "alp" (Just False) uri)
93 print =<< runEitherT (postGreet g uri)
95 putStrLn "\n---------\n"
96 printMarkdown 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