Skip to content

Instantly share code, notes, and snippets.

@bthuillier
Last active August 29, 2015 13:57
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 bthuillier/9675750 to your computer and use it in GitHub Desktop.
Save bthuillier/9675750 to your computer and use it in GitHub Desktop.
proxy
package spray.routing
package directives
import spray.can.Http
import akka.io.IO
import akka.actor.ActorSystem
import spray.http.{ HttpRequest, Uri }
trait ProxyDirectives {
private def sending(f: RequestContext ⇒ HttpRequest)(implicit system: ActorSystem): Route = {
val transport = IO(Http)(system)
// transforming function of the response, updating all uri
mapHttpResponseEntity(transformingEntityFunction) {
ctx ⇒ transport.tell(f(ctx), ctx.responder)
}
}
/**
* proxy the request to the specified uri
*
*/
def proxyTo(uri: Uri)(implicit system: ActorSystem): Route = {
sending(_.request.copy(uri = uri))
}
/**
* proxy the request to the specified uri with the unmatched path
*
*/
def proxyToUnmatchedPath(uri: Uri)(implicit system: ActorSystem): Route = {
sending(ctx ⇒ ctx.request.copy(uri = uri.withPath(uri.path.++(ctx.unmatchedPath))))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment