Skip to content

Instantly share code, notes, and snippets.

@Yyukan
Created February 7, 2019 12:49
Show Gist options
  • Save Yyukan/ba28fc37542a4426165148980a57702a to your computer and use it in GitHub Desktop.
Save Yyukan/ba28fc37542a4426165148980a57702a to your computer and use it in GitHub Desktop.
Forward proxy on http4s
case class ForwardRoute[F[_]](components: Components[F])
(implicit E: Effect[F], L: Logging[F]) extends Routes[F] {
private val http = components.http
private val forward = components.config.forwardTo
def routes: HttpService[F] = HttpService[F] {
case request =>
for {
a <- E.pure(Authority(host = RegName(forward), port = Some(80)))
r <- E.delay(request
.withUri(request.uri.copy(authority = Some(a)))
.withHeaders(request.headers.put(Header("host", forward))))
response <- http.toHttpService.run(r).getOrElse(Response(NotFound))
_ <- L.info(s"Forwarded request $request to $r response $response")
} yield response
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment