From 3edc067c66ef8b3973634b6a36e0db1a59bfa3d1 Mon Sep 17 00:00:00 2001 From: Alp Mestanogullari Date: Tue, 5 May 2015 22:54:55 +0200 Subject: [PATCH] servant-examples: add GS3 --- servant-examples/getting-started/GS3.hs | 80 +++++++++++++++++++ .../getting-started/getting-started.hs | 2 + 2 files changed, 82 insertions(+) create mode 100644 servant-examples/getting-started/GS3.hs diff --git a/servant-examples/getting-started/GS3.hs b/servant-examples/getting-started/GS3.hs new file mode 100644 index 00000000..5dec0123 --- /dev/null +++ b/servant-examples/getting-started/GS3.hs @@ -0,0 +1,80 @@ +{-# LANGUAGE DataKinds #-} +{-# LANGUAGE TypeFamilies #-} +{-# LANGUAGE DeriveGeneric #-} +{-# LANGUAGE TypeOperators #-} +module GS3 where + +import Control.Monad.Trans.Either +import Data.Aeson +import Data.List +import GHC.Generics +import Network.Wai +import Servant + +data Position = Position + { x :: Int + , y :: Int + } deriving Generic + +instance ToJSON Position + +newtype HelloMessage = HelloMessage { msg :: String } + deriving Generic + +instance ToJSON HelloMessage + +data ClientInfo = ClientInfo + { name :: String + , email :: String + , age :: Int + , interested_in :: [String] + } deriving Generic + +instance FromJSON ClientInfo + +data Email = Email + { from :: String + , to :: String + , subject :: String + , body :: String + } deriving Generic + +instance ToJSON Email + +emailForClient :: ClientInfo -> Email +emailForClient c = Email from' to' subject' body' + + where from' = "great@company.com" + to' = email c + subject' = "Hey " ++ name c ++ ", we miss you!" + body' = "Hi " ++ name c ++ ",\n\n" + ++ "Since you've recently turned " ++ show (age c) + ++ ", have you checked out our latest " + ++ intercalate ", " (interested_in c) + ++ " ? Give us a visit!" + +type API = "position" :> Capture "x" Int :> Capture "y" Int :> Get '[JSON] Position + :<|> "hello" :> QueryParam "name" String :> Get '[JSON] HelloMessage + :<|> "marketing" :> ReqBody '[JSON] ClientInfo :> Post '[JSON] Email + +api :: Proxy API +api = Proxy + +server :: Server API +server = position + :<|> hello + :<|> marketing + + where position :: Int -> Int -> EitherT Int IO Position + position x y = return (Position x y) + + hello :: Maybe String -> EitherT Int IO HelloMessage + hello mname = return . HelloMessage $ case mname of + Nothing -> "Hello, anonymous coward" + Just n -> "Hello, " ++ n + + marketing :: ClientInfo -> EitherT Int IO Email + marketing clientinfo = return (emailForClient clientinfo) + +app :: Application +app = serve api server diff --git a/servant-examples/getting-started/getting-started.hs b/servant-examples/getting-started/getting-started.hs index 1b757e8e..ec0e93df 100644 --- a/servant-examples/getting-started/getting-started.hs +++ b/servant-examples/getting-started/getting-started.hs @@ -4,11 +4,13 @@ import System.Environment import qualified GS1 import qualified GS2 +import qualified GS3 app :: String -> Maybe Application app n = case n of "1" -> Just GS1.app "2" -> Just GS2.app + "3" -> Just GS3.app _ -> Nothing main :: IO ()