Skip to content

Instantly share code, notes, and snippets.

@thesamet
Created November 5, 2014 23:36
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thesamet/20e1711b406ffab6495d to your computer and use it in GitHub Desktop.
Save thesamet/20e1711b406ffab6495d to your computer and use it in GitHub Desktop.
Reverse-proxy in Play!
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)
}
}
@fangyishu
Copy link

How to do that in Java? I get problem with the body part:

import akka.stream.javadsl.Source;
.setBody(Source.single(request.body().asBytes()))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment