Last active
August 29, 2015 13:57
-
-
Save bthuillier/9675750 to your computer and use it in GitHub Desktop.
proxy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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