kotlinreverse-proxyhttp4k

How to use reverseProxy with Http4k?


Can we use Http4k as a reverse proxy server? I see that Http4k has reverseProxy function, but couldn't find any example usage. How do I forward requests? I tried following and it works, but is there any better way?

 reverseProxy("" to {  req->
    OkHttp()(req.uri(req.uri.host(config.hostname).port(config.port).scheme(config.scheme)))
})

Solution

  • Turns out, there is no alternative way to implement this behaviour. We need instance of Http client explicitly. Some minor refactoring using extend so that we don't have to copy all attributes.

    val client = OkHttp()
    ...
    reverseProxy("" to { req ->
          client(req.uri(config.upstreamUri().extend(req.uri)))
    })