@@ -0,0 +1,80 @@
1+{-# LANGUAGE DataKinds #-}
2+{-# LANGUAGE TypeFamilies #-}
3+{-# LANGUAGE DeriveGeneric #-}
4+{-# LANGUAGE TypeOperators #-}
7+import Control.Monad.Trans.Either
14+data Position = Position
19+instance ToJSON Position
21+newtype HelloMessage = HelloMessage { msg :: String }
24+instance ToJSON HelloMessage
26+data ClientInfo = ClientInfo
30+ , interested_in :: [String]
33+instance FromJSON ClientInfo
44+emailForClient :: ClientInfo -> Email
45+emailForClient c = Email from' to' subject' body'
47+ where from' = "great@company.com"
49+ subject' = "Hey " ++ name c ++ ", we miss you!"
50+ body' = "Hi " ++ name c ++ ",\n\n"
51+ ++ "Since you've recently turned " ++ show (age c)
52+ ++ ", have you checked out our latest "
53+ ++ intercalate ", " (interested_in c)
54+ ++ " ? Give us a visit!"
56+type API = "position" :> Capture "x" Int :> Capture "y" Int :> Get '[JSON] Position
57+ :<|> "hello" :> QueryParam "name" String :> Get '[JSON] HelloMessage
58+ :<|> "marketing" :> ReqBody '[JSON] ClientInfo :> Post '[JSON] Email
68+ where position :: Int -> Int -> EitherT Int IO Position
69+ position x y = return (Position x y)
71+ hello :: Maybe String -> EitherT Int IO HelloMessage
72+ hello mname = return . HelloMessage $ case mname of
73+ Nothing -> "Hello, anonymous coward"
74+ Just n -> "Hello, " ++ n
76+ marketing :: ClientInfo -> EitherT Int IO Email
77+ marketing clientinfo = return (emailForClient clientinfo)
80+app = serve api server