@@ -0,0 +1,47 @@
1+{-# LANGUAGE DataKinds #-}
2+{-# LANGUAGE DeriveGeneric #-}
3+{-# LANGUAGE DerivingStrategies #-}
4+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
5+{-# LANGUAGE OverloadedStrings #-}
6+{-# LANGUAGE TypeOperators #-}
7+
8+module Headers where
9+
10+import Data.Aeson
11+import Data.Typeable (Typeable)
12+import GHC.Generics (Generic)
13+import Servant
14+
15+newtype Foo = Foo Int
16+ deriving (Generic, Eq, Show, Typeable)
17+ deriving newtype (FromHttpApiData, ToHttpApiData)
18+
19+instance ToJSON Foo
20+
21+instance FromJSON Foo
22+
23+type Api =
24+ "item" :> Get '[JSON] Foo
25+ :<|> "itemAdd" :> Header "one" Foo :> Header "two" Foo :> Get '[JSON] Foo
26+ :<|> "item" :> Capture "itemId" Foo :> Get '[JSON] ()
27+
28+intro :: Handler Foo
29+intro = pure (Foo 1)
30+
31+combine :: Maybe Foo -> Maybe Foo -> Handler Foo
32+combine (Just (Foo a)) (Just (Foo b)) = pure (Foo (a + b))
33+combine (Just a) Nothing = pure a
34+combine Nothing (Just a) = pure a
35+combine Nothing Nothing = pure (Foo 1)
36+
37+
38+eliminate :: Foo -> Handler ()
39+eliminate (Foo a)
40+ | a > 10 = throwError $ err500 {errBody = "eliminate blew up, oh no!"}
41+ | otherwise = pure ()
42+
43+server :: Server Api
44+server =
45+ intro
46+ :<|> combine
47+ :<|> eliminate