on master help sign in
46 lines 1.0 kB history raw
1{-# LANGUAGE DataKinds #-}
2{-# LANGUAGE DeriveGeneric #-}
3{-# LANGUAGE DerivingStrategies #-}
4{-# LANGUAGE GeneralizedNewtypeDeriving #-}
5{-# LANGUAGE OverloadedStrings #-}
6{-# LANGUAGE TypeOperators #-}
8module Foo where
10import Data.Aeson
11import Data.Hashable
12import Data.Typeable (Typeable)
13import GHC.Generics (Generic)
14import Servant
16newtype Foo = Foo Int
17 deriving (Generic, Eq, Show, Typeable)
18 deriving newtype (FromHttpApiData, ToHttpApiData)
20instance Hashable Foo
22instance ToJSON Foo
24instance FromJSON Foo
26type Api =
27 "item" :> Get '[JSON] Foo
28 :<|> "itemAdd" :> Capture "one" Foo :> Capture "two" Foo :> Get '[JSON] Foo
29 :<|> "item" :> Capture "itemId" Foo :> Get '[JSON] ()
31intro :: Handler Foo
32intro = pure (Foo 1)
34combine :: Foo -> Foo -> Handler Foo
35combine (Foo a) (Foo b) = pure (Foo (a + b))
37eliminate :: Foo -> Handler ()
38eliminate (Foo a)
39 | a > 10 = throwError $ err500 {errBody = "eliminate blew up, oh no!"}
40 | otherwise = pure ()
42server :: Server Api
43server =
44 intro
45 :<|> combine
46 :<|> eliminate