Skip to content

Instantly share code, notes, and snippets.

@ponkotuy
Created May 24, 2014 06:54
Show Gist options
  • Save ponkotuy/26f2b84b0e3c6ff28c33 to your computer and use it in GitHub Desktop.
Save ponkotuy/26f2b84b0e3c6ff28c33 to your computer and use it in GitHub Desktop.
FinagleProxySample
package com.ponkotuy.proxy
import com.twitter.finagle.builder.ClientBuilder
import com.twitter.finagle.{Http, Service, http}
import org.jboss.netty.handler.codec.http.{HttpMethod, HttpResponse, HttpRequest}
import com.twitter.util.{Await, Future}
import com.github.theon.uri.Uri
class FinagleProxySample(hosts: String, port: Int, inter: Intercepter) {
val client = ClientBuilder().codec(http.Http()).hosts(hosts).build()
val service = new Service[HttpRequest, HttpResponse] {
def apply(req: HttpRequest): Future[HttpResponse] = {
val res = if (req.getMethod == HttpMethod.POST) {
val uri = Uri.parseUri(req.getUri)
req.setUri(uri.pathRaw)
client.apply(req)
} else {
client.apply(req)
}
res.foreach(rs => inter.input(req, rs))
res
}
}
val server = Http.serve(s":$port", service)
def start(): Unit = {
Await.ready(server)
}
}
trait Intercepter {
def input(req: HttpRequest, res: HttpResponse): Unit
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment