Skip to content

Instantly share code, notes, and snippets.

@thesamet
Created November 5, 2014 23:36

Revisions

  1. thesamet created this gist Nov 5, 2014.
    19 changes: 19 additions & 0 deletions ReverseProxy.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    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)
    }
    }