-
Notifications
You must be signed in to change notification settings - Fork 38
/
03-Json.hs
50 lines (38 loc) · 1.11 KB
/
03-Json.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeOperators #-}
-- Servant makes it easy to deliver JSON responses.
import Servant
import Network.Wai (Application)
import Network.Wai.Middleware.RequestLogger (logStdoutDev)
import Network.Wai.Handler.Warp (run)
import GHC.Generics
import Data.Aeson
type API = "hello" :> Get '[JSON] String
:<|> "numbers" :> Get '[JSON] [Int]
:<|> "pairs" :> Get '[JSON] [Pair]
helloServer = return "Hello World!"
numberServer = return [1..17]
data Pair = Pair
{ x :: Int
, y :: Int
} deriving (Eq, Show, Generic)
instance ToJSON Pair
pairServer = return [Pair 10 20, Pair 1 2]
server :: Server API
server = helloServer
:<|> numberServer
:<|> pairServer
proxy :: Proxy API
proxy = Proxy
app :: Application
app = serve proxy server
main :: IO ()
main = do
putStrLn "Server endpoints:"
putStrLn " http://localhost:8000/hello/"
putStrLn " http://localhost:8000/numbers/"
putStrLn " http://localhost:8000/pairs/"
run 8000 $ logStdoutDev $ app