Skip to content

Commit

Permalink
Allow configuring clients from application.conf (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
raboof authored Jun 29, 2018
1 parent 9a606d0 commit 2c7a89c
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 2 deletions.
8 changes: 8 additions & 0 deletions runtime/src/main/resources/reference.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
akka.grpc.client."*" {
host = ""
port = 0
deadline = infinite
override-authority = ""
trusted-ca-certificate = ""
user-agent = ""
}
46 changes: 44 additions & 2 deletions runtime/src/main/scala/akka/grpc/GrpcClientSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,58 @@

package akka.grpc

import scala.concurrent.duration.Duration
import akka.actor.ActorSystem
import akka.util.Helpers
import com.typesafe.config.Config

import scala.concurrent.duration.Duration
import io.grpc.CallCredentials

object GrpcClientSettings {
/** Scala API */
def apply(host: String, port: Int): GrpcClientSettings =
new GrpcClientSettings(host, port)

/** Scala API */
def apply(serviceName: String, sys: ActorSystem): GrpcClientSettings = {
val akkaGrpcClientConfig = sys.settings.config.getConfig("akka.grpc.client")

val serviceConfig =
if (akkaGrpcClientConfig.hasPath('"' + serviceName + '"'))
akkaGrpcClientConfig.getConfig('"' + serviceName + '"').withFallback(akkaGrpcClientConfig.getConfig("\"*\""))
else
akkaGrpcClientConfig.getConfig("\"*\"")

GrpcClientSettings(serviceConfig)
}

/** Scala API */
def apply(config: Config): GrpcClientSettings =
GrpcClientSettings(config getString "host", config getInt "port")
.copy(
overrideAuthority = getOptionalString(config, "override-authority"),
deadline = getPotentiallyInfiniteDuration(config, "deadline"),
trustedCaCertificate = getOptionalString(config, "trusted-ca-certificate"),
userAgent = getOptionalString(config, "user-agent"))

private def getOptionalString(config: Config, path: String): Option[String] = config.getString(path) match {
case "" => None
case other => Some(other)
}

private def getPotentiallyInfiniteDuration(underlying: Config, path: String): Duration = Helpers.toRootLowerCase(underlying.getString(path)) match {
case "infinite" Duration.Inf
case _ Duration.fromNanos(underlying.getDuration(path).toNanos)
}

/** Java API */
def create(serviceName: String, sys: ActorSystem): GrpcClientSettings =
GrpcClientSettings(serviceName, sys)

/** Java API */
def create(config: Config): GrpcClientSettings =
GrpcClientSettings(config)

/** Java API */
def create(host: String, port: Int): GrpcClientSettings = apply(host, port)
}
Expand Down Expand Up @@ -64,4 +107,3 @@ final class GrpcClientSettings private (
userAgent = userAgent)

}

46 changes: 46 additions & 0 deletions runtime/src/test/scala/akka/grpc/GrpcClientSettingsSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (C) 2018 Lightbend Inc. <https://www.lightbend.com>
*/

package akka.grpc

import com.typesafe.config.ConfigFactory

import scala.concurrent.duration._

import akka.actor.ActorSystem

import org.scalatest.{ Matchers, WordSpec }

class GrpcClientSettingsSpec extends WordSpec with Matchers {
"The gRPC client settings spec" should {
val sys = ActorSystem("test", ConfigFactory.parseString(
"""
|akka.grpc.client {
| "project.WithSpecificConfiguration" {
| host = "myhost"
| port = 42
| override-authority = "google.fr"
| trusted-ca-certificate = "ca.pem"
| deadline = 10m
| user-agent = "Akka-gRPC"
| }
|}
""".stripMargin).withFallback(ConfigFactory.load()))

"fall back to the default configuration if no specific configuration is found" in {
// Should not crash:
GrpcClientSettings("project.WithoutSpecificConfiguration", sys)
}

"parse configuration values" in {
val parsed = GrpcClientSettings("project.WithSpecificConfiguration", sys)
parsed.host should be("myhost")
parsed.port should be(42)
parsed.overrideAuthority should be(Some("google.fr"))
parsed.trustedCaCertificate should be(Some("ca.pem"))
parsed.deadline should be(10.minutes)
parsed.userAgent should be(Some("Akka-gRPC"))
}
}
}

0 comments on commit 2c7a89c

Please sign in to comment.