monad stack

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

monad stack

Corentin Dupont
Hi guys,
I would like to run my API in this stack:

MyApp a = ReaderT AppConfig (ExceptT ServantErr IO) a

How do I need to configure the rest? I tried:

type ServerW api = ServerT api HandlerW

newtype HandlerW a = HandlerW { runHandlerW :: MyApp a }
  deriving
    ( Functor, Applicative, Monad, MonadIO, Generic
    , MonadError ServantErr
    , MonadThrow, MonadCatch
    )

type MyAPI = "api" :> "v1" :> Get '[JSON] [Perm]

server :: ServerW MyAPI
server = undefined

Is this correct?
With my attempt so far, I cannot combine different "ServerW" with the operator :<|> :

server :: ServerW MyAPI
server = Server1 :<|> Server2

       • Couldn't match type ‘ReaderT
                                 AppConfig (ExceptT ServantErr IO) ()’
                         with ‘HandlerW Token’
          Expected type: ServerW ()
            Actual type: (MyApp ())
                         :<|> (MyApp ())



--
Reply | Threaded
Open this post in threaded view
|

Re: monad stack

Corentin Dupont

On Tue, Nov 13, 2018 at 3:19 PM Corentin Dupont <[hidden email]> wrote:
Hi guys,
I would like to run my API in this stack:

MyApp a = ReaderT AppConfig (ExceptT ServantErr IO) a

How do I need to configure the rest? I tried:

type ServerW api = ServerT api HandlerW

newtype HandlerW a = HandlerW { runHandlerW :: MyApp a }
  deriving
    ( Functor, Applicative, Monad, MonadIO, Generic
    , MonadError ServantErr
    , MonadThrow, MonadCatch
    )

type MyAPI = "api" :> "v1" :> Get '[JSON] [Perm]

server :: ServerW MyAPI
server = undefined

Is this correct?
With my attempt so far, I cannot combine different "ServerW" with the operator :<|> :

server :: ServerW MyAPI
server = Server1 :<|> Server2

       • Couldn't match type ‘ReaderT
                                 AppConfig (ExceptT ServantErr IO) ()’
                         with ‘HandlerW Token’
          Expected type: ServerW ()
            Actual type: (MyApp ())
                         :<|> (MyApp ())



--
Reply | Threaded
Open this post in threaded view
|

Re: monad stack

Alp Mestanogullari
Yes, this page along with the tutorial section about it (https://haskell-servant.readthedocs.io/en/stable/tutorial/Server.html#using-another-monad-for-your-handlers) give you examples of using `hoistServer`, which will let you convert all the handlers from your monad to Handler, which is what servant-server knows how to serve.

Let us know here if you have any question though.

On Tue, Nov 13, 2018 at 3:51 PM Corentin Dupont <[hidden email]> wrote:

On Tue, Nov 13, 2018 at 3:19 PM Corentin Dupont <[hidden email]> wrote:
Hi guys,
I would like to run my API in this stack:

MyApp a = ReaderT AppConfig (ExceptT ServantErr IO) a

How do I need to configure the rest? I tried:

type ServerW api = ServerT api HandlerW

newtype HandlerW a = HandlerW { runHandlerW :: MyApp a }
  deriving
    ( Functor, Applicative, Monad, MonadIO, Generic
    , MonadError ServantErr
    , MonadThrow, MonadCatch
    )

type MyAPI = "api" :> "v1" :> Get '[JSON] [Perm]

server :: ServerW MyAPI
server = undefined

Is this correct?
With my attempt so far, I cannot combine different "ServerW" with the operator :<|> :

server :: ServerW MyAPI
server = Server1 :<|> Server2

       • Couldn't match type ‘ReaderT
                                 AppConfig (ExceptT ServantErr IO) ()’
                         with ‘HandlerW Token’
          Expected type: ServerW ()
            Actual type: (MyApp ())
                         :<|> (MyApp ())



--


--
Alp Mestanogullari

--