first getting-started example

This commit is contained in:
Alp Mestanogullari 2015-05-05 18:00:10 +02:00
parent 8b705f6d0e
commit 2b23253a8d
2 changed files with 58 additions and 0 deletions

View file

@ -0,0 +1,45 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE TypeOperators #-}
import Data.Aeson
import Data.Time.Calendar
import GHC.Generics
import Network.Wai.Handler.Warp (run)
import Servant
data User = User
{ name :: String
, age :: Int
, email :: String
, registration_date :: Day
} deriving (Eq, Show, Generic)
-- 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)
instance ToJSON User
type UserAPI = "users" :> Get '[JSON] [User]
users :: [User]
users =
[ 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
runServer :: Int -> IO ()
runServer port = run port (serve userAPI server)
main :: IO ()
main = runServer 8081

View file

@ -13,6 +13,19 @@ category: Web
build-type: Simple build-type: Simple
cabal-version: >=1.10 cabal-version: >=1.10
executable getting-started-1
main-is: GS1.hs
build-depends:
aeson >= 0.8
, base >= 4.7
, either
, servant
, servant-server
, time
, warp
hs-source-dirs: getting-started
default-language: Haskell2010
executable hackage executable hackage
main-is: hackage.hs main-is: hackage.hs
build-depends: build-depends: