aboutsummaryrefslogtreecommitdiff
path: root/core-rest/src/main/scala/xyz/driver/core/rest/ProxyRoute.scala
diff options
context:
space:
mode:
authorJakob Odersky <jakob@driver.xyz>2018-09-12 16:40:57 -0700
committerJakob Odersky <jakob@odersky.com>2018-10-09 16:19:39 -0700
commit4d1197099ce4e721c18bf4cacbb2e1980e4210b5 (patch)
tree1d3c2e482d5acf8ca613ba65fb5401c41ad686b5 /core-rest/src/main/scala/xyz/driver/core/rest/ProxyRoute.scala
parent7c755c77afbd67ae2ded9d8b004736d4e27e208f (diff)
downloaddriver-core-4d1197099ce4e721c18bf4cacbb2e1980e4210b5.tar.gz
driver-core-4d1197099ce4e721c18bf4cacbb2e1980e4210b5.tar.bz2
driver-core-4d1197099ce4e721c18bf4cacbb2e1980e4210b5.zip
Move REST functionality to separate project
Diffstat (limited to 'core-rest/src/main/scala/xyz/driver/core/rest/ProxyRoute.scala')
-rw-r--r--core-rest/src/main/scala/xyz/driver/core/rest/ProxyRoute.scala26
1 files changed, 26 insertions, 0 deletions
diff --git a/core-rest/src/main/scala/xyz/driver/core/rest/ProxyRoute.scala b/core-rest/src/main/scala/xyz/driver/core/rest/ProxyRoute.scala
new file mode 100644
index 0000000..c0e9f99
--- /dev/null
+++ b/core-rest/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)
+ }
+}