2015-11-05 02:13:24 +01:00
|
|
|
{-# LANGUAGE CPP #-}
|
2015-08-17 23:56:29 +02:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
2015-05-05 18:00:10 +02:00
|
|
|
{-# LANGUAGE DeriveGeneric #-}
|
2015-08-17 23:56:29 +02:00
|
|
|
{-# LANGUAGE TypeFamilies #-}
|
2015-05-05 18:00:10 +02:00
|
|
|
{-# LANGUAGE TypeOperators #-}
|
2015-05-10 13:39:18 +02:00
|
|
|
module T1 where
|
2015-05-05 18:00:10 +02:00
|
|
|
|
2015-08-17 23:56:29 +02:00
|
|
|
import Data.Aeson
|
|
|
|
import Data.Time.Calendar
|
|
|
|
import GHC.Generics
|
|
|
|
import Network.Wai
|
|
|
|
import Servant
|
2015-05-05 18:00:10 +02:00
|
|
|
|
|
|
|
data User = User
|
2015-08-17 23:56:29 +02:00
|
|
|
{ name :: String
|
|
|
|
, age :: Int
|
|
|
|
, email :: String
|
2015-05-05 18:00:10 +02:00
|
|
|
, registration_date :: Day
|
|
|
|
} deriving (Eq, Show, Generic)
|
|
|
|
|
2015-11-05 02:13:24 +01:00
|
|
|
#if !MIN_VERSION_aeson(0,10,0)
|
2015-05-05 18:00:10 +02:00
|
|
|
-- orphan ToJSON instance for Day. necessary to derive one for User
|
|
|
|
instance ToJSON Day where
|
|
|
|
-- display a day in YYYY-mm-dd format
|
|
|
|
toJSON d = toJSON (showGregorian d)
|
2015-11-05 02:13:24 +01:00
|
|
|
#endif
|
2015-05-05 18:00:10 +02:00
|
|
|
|
|
|
|
instance ToJSON User
|
|
|
|
|
|
|
|
type UserAPI = "users" :> Get '[JSON] [User]
|
|
|
|
|
|
|
|
users :: [User]
|
2015-08-17 23:56:29 +02:00
|
|
|
users =
|
2015-05-05 18:00:10 +02:00
|
|
|
[ User "Isaac Newton" 372 "isaac@newton.co.uk" (fromGregorian 1683 3 1)
|
|
|
|
, User "Albert Einstein" 136 "ae@mc2.org" (fromGregorian 1905 12 1)
|
|
|
|
]
|
|
|
|
|
|
|
|
userAPI :: Proxy UserAPI
|
|
|
|
userAPI = Proxy
|
|
|
|
|
|
|
|
server :: Server UserAPI
|
|
|
|
server = return users
|
|
|
|
|
2015-05-05 21:50:24 +02:00
|
|
|
app :: Application
|
2016-02-18 16:36:24 +01:00
|
|
|
app = serve userAPI server
|