on master help sign in
roboservant/test/Breakdown.hs
50 lines 1.1 kB history raw
1{-# LANGUAGE DataKinds #-}
2{-# LANGUAGE DeriveGeneric #-}
3{-# LANGUAGE DerivingStrategies #-}
5{-# LANGUAGE OverloadedStrings #-}
6{-# LANGUAGE TypeOperators #-}
8module Breakdown where
10import Data.Aeson
11import Data.Hashable
12import Data.Typeable (Typeable)
13import GHC.Generics (Generic)
14import Servant
16data Foo = Foo Int String
17 deriving (Generic, Eq, Show, Typeable)
19instance Hashable Foo
21instance ToJSON Foo
23instance FromJSON Foo
25data SomeSum = A Int | B String
26 deriving (Generic, Eq, Show, Typeable)
28instance Hashable SomeSum
30instance ToJSON SomeSum
32instance FromJSON SomeSum
34type ProductApi =
35 "item" :> ReqBody '[JSON] Int :> Post '[JSON] ()
36 :<|> "getFoo" :> Get '[JSON] Foo
38eliminate :: Int -> Handler ()
39eliminate _ = throwError $ err500 {errBody = "eliminate blew up, oh no!"}
41productServer :: Server ProductApi
42productServer = eliminate :<|> pure (Foo 12 "abc")
44type SumApi =
45 "item" :> ReqBody '[JSON] Int :> Post '[JSON] ()
46 :<|> "getFoo1" :> Get '[JSON] SomeSum
47 :<|> "getFoo2" :> Get '[JSON] SomeSum
49sumServer :: Server SumApi
50sumServer = eliminate :<|> pure (B "hi") :<|> pure (A 3)