parsonsmatt / incremental-servant

Replace your old API with a Servant one!
BSD 3-Clause "New" or "Revised" License
5 stars 1 forks source link

Type error on: server :<|> (waiProxyTo forwardRequest defaultOnExc manager) #1

Open worldsayshi opened 5 years ago

worldsayshi commented 5 years ago

For some reason server :<|> (waiProxyTo forwardRequest defaultOnExc manager) doesn't compile for me.

You explicitly mention that "[...] while waiProxyTo ... has the type Application. If we have Servant server types (like "cat" :> Get '[JSON] Cat), then we need to have a Server for them. For the Raw endpoint, we just need any WAI application.". But you don't point it out as a problem at the time. So I assume something has changed about :<|> in later versions of Servant to not make it possible to combine Server and Application like that.

The error looks like this:

    • Couldn't match type ‘Request
                           -> (Response -> IO ResponseReceived) -> IO ResponseReceived’
                     with ‘Tagged Handler Application’
      Expected type: Server (API :<|> Raw)
        Actual type: Handler Cat :<|> Application
    • In the second argument of ‘($)’, namely
        ‘server :<|> (waiProxyTo forwardRequest defaultOnExc manager)’
      In the expression:
        serve api
          $ server :<|> (waiProxyTo forwardRequest defaultOnExc manager)
      In an equation for ‘app’:
          app manager
            = serve api
                $ server :<|> (waiProxyTo forwardRequest defaultOnExc manager)
   |
39 |     serve api $ server :<|> (waiProxyTo forwardRequest defaultOnExc manager)
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I found a solution that looks like this:

forwardServer :: Manager -> ServerT Raw m
forwardServer manager = Tagged $ waiProxyTo forwardRequest defaultOnExc manager

forwardRequest :: Request -> IO WaiProxyResponse
forwardRequest _ =
    pure . WPRProxyDest . ProxyDest "127.0.0.1" $ 4567

app :: Manager -> Application
app manager =
    serve api $ server :<|> (forwardServer manager)

I mimicked how serveDirectoryWith works. But I don't really know why this works.

More info here: https://stackoverflow.com/a/57842558/439034

DeepakKapiswe commented 5 years ago

I also had same issue, the given example does not works but gives same type error, perhaps it will be better if @parsonsmatt could modify the example so that future readers could avoid wastage of precious time figuring out what went wrong :)