-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy path02-Endpoints.hs
40 lines (30 loc) · 973 Bytes
/
02-Endpoints.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
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeOperators #-}
-- An API can have multiple endpoints combined with :<|>.
import Servant
import Network.Wai (Application)
import Network.Wai.Middleware.RequestLogger (logStdoutDev)
import Network.Wai.Handler.Warp (run)
import qualified Data.ByteString.Lazy.Char8 as BC
import GHC.Generics
instance MimeRender PlainText Int where
mimeRender _ = BC.pack . show
type API = "greeting" :> Get '[PlainText] String
:<|> "status" :> Get '[PlainText] Int
greetingServer = return "Hello World!\n"
statusServer = return 42
server :: Server API
server = greetingServer
:<|> statusServer
proxy :: Proxy API
proxy = Proxy
app :: Application
app = serve proxy server
main :: IO ()
main = do
putStrLn "Server endpoints:"
putStrLn " http://localhost:8000/greeting/"
putStrLn " http://localhost:8000/status/"
run 8000 $ logStdoutDev $ app