Skip to content

Instantly share code, notes, and snippets.

@nguyentruongtho
Forked from thesamet/ReverseProxy.scala
Last active August 29, 2015 14:11
Show Gist options
  • Save nguyentruongtho/da697ba0ce594b2f05ad to your computer and use it in GitHub Desktop.
Save nguyentruongtho/da697ba0ce594b2f05ad to your computer and use it in GitHub Desktop.
def reverseProxy = Action.async(parse.raw) {
request: Request[RawBuffer] =>
// Create the request to the upstream server:
val proxyRequest =
WS.url("http://localhost:8887" + request.path)
.withFollowRedirects(false)
.withMethod(request.method)
.withVirtualHost("localhost:9000")
.withHeaders(flattenMultiMap(request.headers.toMap): _*)
.withQueryString(request.queryString.mapValues(_.head).toSeq: _*)
.withBody(request.body.asBytes().get)
// Stream the response to the client:
proxyRequest.stream.map {
case (headers: WSResponseHeaders, enum) => Result(
ResponseHeader(headers.status, headers.headers.mapValues(_.head)),
enum)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment