Created
February 7, 2019 12:49
-
-
Save Yyukan/ba28fc37542a4426165148980a57702a to your computer and use it in GitHub Desktop.
Forward proxy on http4s
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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