diff options
author | Satya Boora <sboora@users.noreply.github.com> | 2018-06-21 11:16:25 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-06-21 11:16:25 -0700 |
commit | 545f3d6e4bfcf46d6bff591f5c41a932f4565cb8 (patch) | |
tree | adc38de881b5d593e58370838957ca9a6d23bfd5 /src/main/scala/xyz | |
parent | 0f0295040ceea5ffefbee9b12c0137ad237c8723 (diff) | |
download | driver-core-1.11.4.tar.gz driver-core-1.11.4.tar.bz2 driver-core-1.11.4.zip |
Added ProxyRoute to enable proxying calls to other services (#177)v1.11.4
Diffstat (limited to 'src/main/scala/xyz')
-rw-r--r-- | src/main/scala/xyz/driver/core/rest/ProxyRoute.scala | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/main/scala/xyz/driver/core/rest/ProxyRoute.scala b/src/main/scala/xyz/driver/core/rest/ProxyRoute.scala new file mode 100644 index 0000000..c0e9f99 --- /dev/null +++ b/src/main/scala/xyz/driver/core/rest/ProxyRoute.scala @@ -0,0 +1,26 @@ +package xyz.driver.core.rest + +import akka.http.scaladsl.server.{RequestContext, Route, RouteResult} +import com.typesafe.config.Config +import xyz.driver.core.Name + +import scala.concurrent.ExecutionContext + +trait ProxyRoute extends DriverRoute { + implicit val executionContext: ExecutionContext + val config: Config + val httpClient: HttpClient + + protected def proxyToService(serviceName: Name[Service]): Route = { ctx: RequestContext => + val httpScheme = config.getString(s"services.${serviceName.value}.httpScheme") + val baseUrl = config.getString(s"services.${serviceName.value}.baseUrl") + + val originalUri = ctx.request.uri + val originalRequest = ctx.request + + val newUri = originalUri.withScheme(httpScheme).withHost(baseUrl) + val newRequest = originalRequest.withUri(newUri) + + httpClient.makeRequest(newRequest).map(RouteResult.Complete) + } +} |