Skip to content

Instantly share code, notes, and snippets.

@kovacshuni
Created June 10, 2015 00:27
Show Gist options
  • Save kovacshuni/afb7d53f40f501d0ab82 to your computer and use it in GitHub Desktop.
Save kovacshuni/afb7d53f40f501d0ab82 to your computer and use it in GitHub Desktop.
akka-http-streams-proxy
package sample.stream
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.headers.RawHeader
import akka.http.scaladsl.server.Route
import akka.stream.ActorFlowMaterializer
import akka.stream.scaladsl.{Sink, Source}
object Proxy extends App {
implicit val system = ActorSystem("Proxy")
implicit val materializer = ActorFlowMaterializer()
implicit val ec = system.dispatcher
val proxy = Route { context =>
val request = context.request
println("Opening connection to " + request.uri.authority.host.address)
val flow = Http(system).outgoingConnection(request.uri.authority.host.address(), 8080)
val handler = Source.single(context.request)
.map(r => r.withHeaders(RawHeader("x-authenticated", "someone")))
.via(flow)
.runWith(Sink.head)
.flatMap(context.complete(_))
handler
}
val binding = Http(system).bindAndHandle(handler = proxy, interface = "localhost", port = 9000)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment