Skip to content

Instantly share code, notes, and snippets.

@esmevane
Forked from rklaehn/Proxy.scala
Created December 7, 2017 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esmevane/c44ff523aa491d78fbae3a94c32a96f4 to your computer and use it in GitHub Desktop.
Save esmevane/c44ff523aa491d78fbae3a94c32a96f4 to your computer and use it in GitHub Desktop.
Minimal akka http proxy
package akkahttptest
import akka.actor.ActorSystem
import akka.http.Http
import akka.stream.FlowMaterializer
import akka.http.server._
import akka.http.marshalling.PredefinedToResponseMarshallers._
import akka.stream.scaladsl.{HeadSink, Source}
object Proxy extends App {
implicit val system = ActorSystem("Proxy")
implicit val materializer = FlowMaterializer()
implicit val ec = system.dispatcher
val proxy: Route = 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, 80).flow
Source.single(context.request)
.via(flow)
.runWith(HeadSink())
.flatMap(r => context.complete(r))
}
val binding = Http(system).bind(interface = "localhost", port = 1080)
binding.startHandlingWith(proxy)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment