servant/servant-examples/tutorial/T7.hs

34 lines
837 B
Haskell
Raw Normal View History

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeFamilies #-}
2015-05-06 19:01:32 +02:00
{-# LANGUAGE TypeOperators #-}
2015-05-10 13:39:18 +02:00
module T7 where
2015-05-06 19:01:32 +02:00
2015-09-12 14:11:24 +02:00
import Control.Monad.Trans.Except
import Control.Monad.Trans.Reader
import Network.Wai
import Servant
2015-05-06 19:01:32 +02:00
type ReaderAPI = "a" :> Get '[JSON] Int
:<|> "b" :> Get '[JSON] String
readerAPI :: Proxy ReaderAPI
readerAPI = Proxy
readerServerT :: ServerT ReaderAPI (Reader String)
readerServerT = a :<|> b
where a :: Reader String Int
a = return 1797
b :: Reader String String
b = ask
readerServer :: Server ReaderAPI
readerServer = enter readerToEither readerServerT
2015-09-12 14:11:24 +02:00
where readerToEither :: Reader String :~> ExceptT ServantErr IO
2015-05-06 19:01:32 +02:00
readerToEither = Nat $ \r -> return (runReader r "hi")
app :: Application
app = serve readerAPI readerServer