Server.hs 1.04 KB
Newer Older
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
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}

module Server
    (app)
where

import           Data.Proxy
import           Data.Semigroup ((<>))
import           Servant.API
import           Servant.Server (Server, Handler, Application, serve)
import qualified Data.Text as DT

type Name = DT.Text
type Greeting = DT.Text

type HelloWorldAPI =
    "hello" :> QueryParam "name" DT.Text :> Get '[JSON] Greeting
    :<|> "goodbye" :> QueryParam "name" DT.Text :> Get '[JSON] Greeting

helloWorldServer :: Server HelloWorldAPI
helloWorldServer = handleHello
                   :<|> handleGoodbye

handleHello :: Maybe Name -> Handler Greeting
handleHello Nothing     = return $ "hello world"
handleHello (Just name) = return $ "hello " <> name

handleGoodbye :: Maybe Name -> Handler Greeting
handleGoodbye Nothing     = return $ "goodbye world"
handleGoodbye (Just name) = return $ "goodbye " <> name

helloWorldAPI :: Proxy HelloWorldAPI
helloWorldAPI = Proxy

app :: Application
app = serve helloWorldAPI helloWorldServer