From 4bfd34742c8745910a01744a7536777523dff1c1 Mon Sep 17 00:00:00 2001 From: Alex GB Date: Wed, 4 Apr 2018 12:41:29 +0200 Subject: [PATCH 01/33] Update scala versions Replace spray json with the one from akka Preparing for v0.4.0 --- .travis.yml | 4 ++-- build.sbt | 9 +++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index 63037fd..7c33386 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ language: scala scala: - - 2.11.8 - - 2.12.2 + - 2.11.11 + - 2.12.4 jdk: - oraclejdk8 diff --git a/build.sbt b/build.sbt index 4d9a956..c084935 100644 --- a/build.sbt +++ b/build.sbt @@ -7,10 +7,10 @@ import scalariform.formatter.preferences._ // Common variables lazy val commonSettings = Seq( - scalaVersion := "2.11.8", - crossScalaVersions := Seq("2.11.8", "2.12.2"), + scalaVersion := "2.11.11", + crossScalaVersions := Seq("2.11.11", "2.12.4"), organization := "nl.stormlantern", - version := "0.4.0-SNAPSHOT", + version := "0.4.0", resolvers ++= Dependencies.resolutionRepos, scalacOptions ++= Seq("-unchecked", "-deprecation", "-feature") ) @@ -53,8 +53,9 @@ lazy val client = (project in file("client")) fork := true, libraryDependencies ++= Seq( akkaHttp, - sprayJson, + akkaHttpSprayJson, akkaActor, + akkaStream, slf4j, akkaSlf4j, scalaTest % "it,test", From 3cafc21757846496d953769cc34195ad1688a0fe Mon Sep 17 00:00:00 2001 From: Alex GB Date: Wed, 4 Apr 2018 12:42:35 +0200 Subject: [PATCH 02/33] Update sbt version Update plugins Update akka and akka-http Replace spray with akk --- project/Dependencies.scala | 30 +++++++++++++++--------------- project/build.properties | 2 +- project/plugins.sbt | 10 +++++----- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/project/Dependencies.scala b/project/Dependencies.scala index b6c244f..b3da23e 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -1,26 +1,26 @@ import sbt._ object Dependencies { + val resolutionRepos = Seq( "spray repo" at "http://repo.spray.io", "scalaz-bintray" at "http://dl.bintray.com/scalaz/releases", "softprops-maven" at "http://dl.bintray.com/content/softprops/maven" ) - val sprayVersion = "1.3.4" - val akkaVersion = "2.4.14" + val akkaVersion = "2.5.11" + val akkaHttpVersion = "10.1.1" - val sprayClient = "io.spray" %% "spray-client" % sprayVersion - val akkaHttp = "com.typesafe.akka" %% "akka-http-core" % "10.0.3" - val sprayRouting = "io.spray" %% "spray-routing" % sprayVersion - val sprayJson = "io.spray" %% "spray-json" % "1.3.2" - val akkaActor = "com.typesafe.akka" %% "akka-actor" % akkaVersion - val akkaSlf4j = "com.typesafe.akka" %% "akka-slf4j" % akkaVersion - val slf4j = "org.slf4j" % "slf4j-api" % "1.7.21" - val logback = "ch.qos.logback" % "logback-classic" % "1.1.7" - val spotifyDocker = "com.spotify" % "docker-client" % "3.6.8" - val spotifyDns = "com.spotify" % "dns" % "3.1.4" - val scalaTest = "org.scalatest" %% "scalatest" % "3.0.1" - val scalaMock = "org.scalamock" %% "scalamock-scalatest-support" % "3.6.0" - val akkaTestKit = "com.typesafe.akka" %% "akka-testkit" % akkaVersion + val akkaHttp = "com.typesafe.akka" %% "akka-http-core" % akkaHttpVersion + val akkaHttpSprayJson = "com.typesafe.akka" %% "akka-http-spray-json" % akkaHttpVersion + val akkaActor = "com.typesafe.akka" %% "akka-actor" % akkaVersion + val akkaStream = "com.typesafe.akka" %% "akka-stream" % akkaVersion + val akkaSlf4j = "com.typesafe.akka" %% "akka-slf4j" % akkaVersion + val slf4j = "org.slf4j" % "slf4j-api" % "1.7.21" + val logback = "ch.qos.logback" % "logback-classic" % "1.1.7" + val spotifyDocker = "com.spotify" % "docker-client" % "3.6.8" + val spotifyDns = "com.spotify" % "dns" % "3.1.4" + val scalaTest = "org.scalatest" %% "scalatest" % "3.0.1" + val scalaMock = "org.scalamock" %% "scalamock-scalatest-support" % "3.6.0" + val akkaTestKit = "com.typesafe.akka" %% "akka-testkit" % akkaVersion } diff --git a/project/build.properties b/project/build.properties index 64317fd..0531343 100644 --- a/project/build.properties +++ b/project/build.properties @@ -1 +1 @@ -sbt.version=0.13.15 +sbt.version=1.1.2 diff --git a/project/plugins.sbt b/project/plugins.sbt index 38dc4d5..9426ecf 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -1,5 +1,5 @@ -addSbtPlugin("io.spray" % "sbt-revolver" % "0.7.2") -addSbtPlugin("org.scalariform" % "sbt-scalariform" % "1.6.0") -addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.0.0-RC2") -addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.0.0") -addSbtPlugin("com.timushev.sbt" % "sbt-updates" % "0.3.0") +addSbtPlugin("io.spray" % "sbt-revolver" % "0.9.1") +addSbtPlugin("org.scalariform" % "sbt-scalariform" % "1.8.2") +addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.3.3") +addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.1.1") +addSbtPlugin("com.timushev.sbt" % "sbt-updates" % "0.3.4") From d0e041dc113bbfb0d3553ae5b75e934afac9ebfa Mon Sep 17 00:00:00 2001 From: Nicu Reut Date: Tue, 4 Sep 2018 14:31:54 +0200 Subject: [PATCH 03/33] Added ability to use only the healthy services for load balancing (#1) --- build.sbt | 4 +- .../client/ServiceBrokerIntegrationTest.scala | 2 +- .../AkkaHttpConsulClientIntegrationTest.scala | 10 +++ .../consul/client/ServiceBroker.scala | 4 +- .../consul/client/ServiceBrokerActor.scala | 6 +- .../consul/client/dao/ConsulHttpClient.scala | 10 ++- .../client/dao/ConsulHttpProtocol.scala | 7 +- .../client/dao/IndexedServiceInstances.scala | 76 ++++++++++++++++++- .../dao/akka/AkkaHttpConsulClient.scala | 26 ++++++- .../client/discovery/ConnectionStrategy.scala | 13 ++-- .../discovery/ServiceAvailabilityActor.scala | 13 +++- .../client/ServiceBrokerActorSpec.scala | 28 +++---- .../ServiceAvailabilityActorSpec.scala | 12 +-- .../stormlantern/consul/client/Dns.scala | 4 +- .../dockertestkit/DockerClientProvider.scala | 4 +- .../dockertestkit/client/Container.scala | 4 +- project/build.properties | 2 +- project/plugins.sbt | 4 +- 18 files changed, 176 insertions(+), 53 deletions(-) diff --git a/build.sbt b/build.sbt index c084935..2784971 100644 --- a/build.sbt +++ b/build.sbt @@ -7,10 +7,10 @@ import scalariform.formatter.preferences._ // Common variables lazy val commonSettings = Seq( - scalaVersion := "2.11.11", + scalaVersion := "2.12.4", crossScalaVersions := Seq("2.11.11", "2.12.4"), organization := "nl.stormlantern", - version := "0.4.0", + version := "0.4.1-SNAPSHOT", resolvers ++= Dependencies.resolutionRepos, scalacOptions ++= Seq("-unchecked", "-deprecation", "-feature") ) diff --git a/client/src/it/scala/stormlantern/consul/client/ServiceBrokerIntegrationTest.scala b/client/src/it/scala/stormlantern/consul/client/ServiceBrokerIntegrationTest.scala index bff0bae..8f406b4 100644 --- a/client/src/it/scala/stormlantern/consul/client/ServiceBrokerIntegrationTest.scala +++ b/client/src/it/scala/stormlantern/consul/client/ServiceBrokerIntegrationTest.scala @@ -29,7 +29,7 @@ class ServiceBrokerIntegrationTest extends FlatSpec with Matchers with ScalaFutu override def getConnection: Future[Any] = Future.successful(httpClient) } } - val connectionStrategy = ConnectionStrategy(ServiceDefinition("consul-http"), connectionProviderFactory, new RoundRobinLoadBalancer) + val connectionStrategy = ConnectionStrategy(ServiceDefinition("consul-http"), connectionProviderFactory, new RoundRobinLoadBalancer, onlyHealthyServices = true) val sut = ServiceBroker(actorSystem, akkaHttpClient, Set(connectionStrategy)) eventually { sut.withService("consul-http") { connection: ConsulHttpClient ⇒ diff --git a/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIntegrationTest.scala b/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIntegrationTest.scala index ccc77a9..e0e651e 100644 --- a/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIntegrationTest.scala +++ b/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIntegrationTest.scala @@ -28,6 +28,16 @@ class AkkaHttpConsulClientIntegrationTest extends FlatSpec with Matchers with Sc } } + it should "retrieve a single health aware Consul service from a freshly started Consul instance" in withConsulHttpClient { + subject ⇒ + eventually { + subject.getServiceHealthAware("consul").map { result ⇒ + result.resource should have size 1 + result.resource.head.serviceName shouldEqual "consul" + }.futureValue + } + } + it should "retrieve no unknown service from a freshly started Consul instance" in withConsulHttpClient { subject ⇒ eventually { subject.getService("bogus").map { result ⇒ diff --git a/client/src/main/scala/stormlantern/consul/client/ServiceBroker.scala b/client/src/main/scala/stormlantern/consul/client/ServiceBroker.scala index 33b323f..ac82baf 100644 --- a/client/src/main/scala/stormlantern/consul/client/ServiceBroker.scala +++ b/client/src/main/scala/stormlantern/consul/client/ServiceBroker.scala @@ -55,8 +55,8 @@ object ServiceBroker { def apply(rootActor: ActorSystem, httpClient: ConsulHttpClient, services: Set[ConnectionStrategy]): ServiceBroker = { implicit val ec = ExecutionContext.Implicits.global - val serviceAvailabilityActorFactory = (factory: ActorRefFactory, service: ServiceDefinition, listener: ActorRef) ⇒ - factory.actorOf(ServiceAvailabilityActor.props(httpClient, service, listener)) + val serviceAvailabilityActorFactory = (factory: ActorRefFactory, service: ServiceDefinition, listener: ActorRef, onlyHealthyServices: Boolean) ⇒ + factory.actorOf(ServiceAvailabilityActor.props(httpClient, service, listener, onlyHealthyServices)) val actorRef = rootActor.actorOf(ServiceBrokerActor.props(services, serviceAvailabilityActorFactory), "ServiceBroker") new ServiceBroker(actorRef, httpClient) } diff --git a/client/src/main/scala/stormlantern/consul/client/ServiceBrokerActor.scala b/client/src/main/scala/stormlantern/consul/client/ServiceBrokerActor.scala index a4cd663..75b8f87 100644 --- a/client/src/main/scala/stormlantern/consul/client/ServiceBrokerActor.scala +++ b/client/src/main/scala/stormlantern/consul/client/ServiceBrokerActor.scala @@ -18,7 +18,7 @@ import scala.concurrent.duration._ class ServiceBrokerActor( services: Set[ConnectionStrategy], - serviceAvailabilityActorFactory: (ActorRefFactory, ServiceDefinition, ActorRef) ⇒ ActorRef)(implicit ec: ExecutionContext) + serviceAvailabilityActorFactory: (ActorRefFactory, ServiceDefinition, ActorRef, Boolean) ⇒ ActorRef)(implicit ec: ExecutionContext) extends Actor with ActorLogging with Stash { // Actor state @@ -33,7 +33,7 @@ class ServiceBrokerActor( case (key, strategy) ⇒ loadbalancers.put(key, strategy.loadBalancerFactory(context)) log.info(s"Starting service availability Actor for $key") - val serviceAvailabilityActorRef = serviceAvailabilityActorFactory(context, strategy.serviceDefinition, self) + val serviceAvailabilityActorRef = serviceAvailabilityActorFactory(context, strategy.serviceDefinition, self, strategy.onlyHealthyServices) serviceAvailabilityActorRef ! Start serviceAvailability += serviceAvailabilityActorRef } @@ -103,7 +103,7 @@ object ServiceBrokerActor { // Constructors def props( services: Set[ConnectionStrategy], - serviceAvailabilityActorFactory: (ActorRefFactory, ServiceDefinition, ActorRef) ⇒ ActorRef)(implicit ec: ExecutionContext): Props = + serviceAvailabilityActorFactory: (ActorRefFactory, ServiceDefinition, ActorRef, Boolean) ⇒ ActorRef)(implicit ec: ExecutionContext): Props = Props(new ServiceBrokerActor(services, serviceAvailabilityActorFactory)) case class GetServiceConnection(key: String) case object Stop diff --git a/client/src/main/scala/stormlantern/consul/client/dao/ConsulHttpClient.scala b/client/src/main/scala/stormlantern/consul/client/dao/ConsulHttpClient.scala index 9a97758..2c8a8c0 100644 --- a/client/src/main/scala/stormlantern/consul/client/dao/ConsulHttpClient.scala +++ b/client/src/main/scala/stormlantern/consul/client/dao/ConsulHttpClient.scala @@ -12,6 +12,13 @@ trait ConsulHttpClient { wait: Option[String] = None, dataCenter: Option[String] = None ): Future[IndexedServiceInstances] + def getServiceHealthAware( + service: String, + tag: Option[String] = None, + index: Option[Long] = None, + wait: Option[String] = None, + dataCenter: Option[String] = None + ): Future[IndexedServiceInstances] def putService(registration: ServiceRegistration): Future[String] def deleteService(serviceId: String): Future[Unit] def putSession( @@ -27,5 +34,4 @@ trait ConsulHttpClient { recurse: Boolean = false, keysOnly: Boolean = false ): Future[Seq[KeyData]] -} - +} \ No newline at end of file diff --git a/client/src/main/scala/stormlantern/consul/client/dao/ConsulHttpProtocol.scala b/client/src/main/scala/stormlantern/consul/client/dao/ConsulHttpProtocol.scala index 4ce4ebc..62673af 100644 --- a/client/src/main/scala/stormlantern/consul/client/dao/ConsulHttpProtocol.scala +++ b/client/src/main/scala/stormlantern/consul/client/dao/ConsulHttpProtocol.scala @@ -35,11 +35,16 @@ trait ConsulHttpProtocol extends DefaultJsonProtocol { override def write(obj: BinaryData): JsValue = JsString(Base64.getMimeEncoder.encodeToString(obj.data)) } - implicit val serviceFormat = jsonFormat( + implicit val serviceFormat: RootJsonFormat[ServiceInstance] = jsonFormat( (node: String, address: String, serviceId: String, serviceName: String, serviceTags: Option[Set[String]], serviceAddress: String, servicePort: Int) ⇒ ServiceInstance(node, address, serviceId, serviceName, serviceTags.getOrElse(Set.empty), serviceAddress, servicePort), "Node", "Address", "ServiceID", "ServiceName", "ServiceTags", "ServiceAddress", "ServicePort" ) + + implicit val nodeFormat = jsonFormat(Node, "Node", "Address") + implicit val healthServiceFormat = jsonFormat(Service, "ID", "Service", "Tags", "Address", "Port") + implicit val healthServiceInstanceFormat = jsonFormat(HealthServiceInstance, "Node", "Service") + implicit val httpCheckFormat = jsonFormat(HttpHealthCheck, "HTTP", "Interval") implicit val scriptCheckFormat = jsonFormat(ScriptHealthCheck, "Script", "Interval") implicit val ttlCheckFormat = jsonFormat(TTLHealthCheck, "TTL") diff --git a/client/src/main/scala/stormlantern/consul/client/dao/IndexedServiceInstances.scala b/client/src/main/scala/stormlantern/consul/client/dao/IndexedServiceInstances.scala index 79e8056..8c55595 100644 --- a/client/src/main/scala/stormlantern/consul/client/dao/IndexedServiceInstances.scala +++ b/client/src/main/scala/stormlantern/consul/client/dao/IndexedServiceInstances.scala @@ -22,7 +22,81 @@ case class ServiceInstance( servicePort: Int ) -case class IndexedServiceInstances(index: Long, resource: Set[ServiceInstance]) extends Indexed[Set[ServiceInstance]] { +/* + * { + "Node": { + "ID": "40e4a748-2192-161a-0510-9bf59fe950b5", + "Node": "foobar", + "Address": "10.1.10.12", + "Datacenter": "dc1", + "TaggedAddresses": { + "lan": "10.1.10.12", + "wan": "10.1.10.12" + }, + "Meta": { + "instance_type": "t2.medium" + } + }, + "Service": { + "ID": "redis", + "Service": "redis", + "Tags": ["primary"], + "Address": "10.1.10.12", + "Meta": { + "redis_version": "4.0" + }, + "Port": 8000 + }, + "Checks": [ + { + "Node": "foobar", + "CheckID": "service:redis", + "Name": "Service 'redis' check", + "Status": "passing", + "Notes": "", + "Output": "", + "ServiceID": "redis", + "ServiceName": "redis", + "ServiceTags": ["primary"] + }, + { + "Node": "foobar", + "CheckID": "serfHealth", + "Name": "Serf Health Status", + "Status": "passing", + "Notes": "", + "Output": "", + "ServiceID": "", + "ServiceName": "", + "ServiceTags": [] + } + ] + } + * + * */ +case class Node(node: String, address: String) +case class Service( + id: String, + service: String, + tags: Set[String], + address: String, + port: Int) +case class HealthServiceInstance(node: Node, service: Service) { + def asServiceInstance: ServiceInstance = { + ServiceInstance( + node.node, + node.address, + service.id, + service.service, + service.tags, + service.address, + service.port + ) + } +} + +case class IndexedServiceInstances(index: Long, resource: Set[ServiceInstance]) + extends Indexed[Set[ServiceInstance]] { def filterForTags(tags: Set[String]): IndexedServiceInstances = { this.copy(resource = resource.filter { s ⇒ tags.forall(s.serviceTags.contains) diff --git a/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala b/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala index 6da45d2..9e860d6 100644 --- a/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala +++ b/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala @@ -46,6 +46,26 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) extends } } + def getServiceHealthAware(service: String, tag: Option[String] = None, index: Option[Long] = None, wait: Option[String] = None, dataCenter: Option[String] = None): Future[IndexedServiceInstances] = { + val dcParameter = dataCenter.map(dc ⇒ s"dc=$dc") + val waitParameter = wait.map(w ⇒ s"wait=$w") + val indexParameter = index.map(i ⇒ s"index=$i") + val tagParameter = tag.map(t ⇒ s"tag=$t") + val passingParameter = Some(s"passing=true") + val parameters = Seq(dcParameter, tagParameter, waitParameter, indexParameter, passingParameter).flatten.mkString("&") + val request: HttpRequest = HttpRequest(HttpMethods.GET).withUri(s"$host/v1/health/service/$service?$parameters") + + retry[IndexedServiceInstances]() { + getResponse(request, JsonMediaType).flatMap { response ⇒ + validIndex(response).map { idx ⇒ + println(response.body) + val services = response.body.parseJson.convertTo[Option[Set[HealthServiceInstance]]] + IndexedServiceInstances(idx, services.getOrElse(Set.empty[HealthServiceInstance]).map(_.asServiceInstance)) + } + } + } + } + def putService(registration: ServiceRegistration): Future[String] = { val request = HttpRequest(HttpMethods.PUT).withUri(s"$host/v1/agent/service/register") .withEntity(registration.toJson.asJsObject().toString.getBytes) @@ -95,7 +115,7 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) extends } // - // Key Values + // Key Values // ///////////////// def putKeyValuePair(key: String, value: Array[Byte], sessionOp: Option[SessionOp]): Future[Boolean] = { import StatusCodes._ @@ -157,7 +177,7 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) extends val expected = resp.status match { case st if st.isSuccess() ⇒ expectedMediaType case st if st.isFailure() ⇒ TextMediaType - case st if st.isRedirection() ⇒ TextMediaType // this is a guess + case st if st.isRedirection() ⇒ TextMediaType // this is a guess } if (resp.entity.contentType.mediaType == expected) { @@ -193,7 +213,7 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) extends } // -// Internal Objects +// Internal Objects // ////////////////////////// case class ConsulResponse(status: StatusCode, headers: Seq[HttpHeader], body: String) diff --git a/client/src/main/scala/stormlantern/consul/client/discovery/ConnectionStrategy.scala b/client/src/main/scala/stormlantern/consul/client/discovery/ConnectionStrategy.scala index a4f9ebe..4931559 100644 --- a/client/src/main/scala/stormlantern/consul/client/discovery/ConnectionStrategy.scala +++ b/client/src/main/scala/stormlantern/consul/client/discovery/ConnectionStrategy.scala @@ -19,23 +19,24 @@ object ServiceDefinition { case class ConnectionStrategy( serviceDefinition: ServiceDefinition, connectionProviderFactory: ConnectionProviderFactory, - loadBalancerFactory: ActorRefFactory ⇒ ActorRef + loadBalancerFactory: ActorRefFactory ⇒ ActorRef, + onlyHealthyServices: Boolean ) object ConnectionStrategy { - def apply(serviceDefinition: ServiceDefinition, connectionProviderFactory: ConnectionProviderFactory, loadBalancer: LoadBalancer): ConnectionStrategy = - ConnectionStrategy(serviceDefinition, connectionProviderFactory, ctx ⇒ ctx.actorOf(LoadBalancerActor.props(loadBalancer, serviceDefinition.key))) + def apply(serviceDefinition: ServiceDefinition, connectionProviderFactory: ConnectionProviderFactory, loadBalancer: LoadBalancer, onlyHealthyServices: Boolean): ConnectionStrategy = + ConnectionStrategy(serviceDefinition, connectionProviderFactory, ctx ⇒ ctx.actorOf(LoadBalancerActor.props(loadBalancer, serviceDefinition.key)), onlyHealthyServices) - def apply(serviceDefinition: ServiceDefinition, connectionProviderFactory: (String, Int) ⇒ ConnectionProvider, loadBalancer: LoadBalancer): ConnectionStrategy = { + def apply(serviceDefinition: ServiceDefinition, connectionProviderFactory: (String, Int) ⇒ ConnectionProvider, loadBalancer: LoadBalancer, onlyHealthyServices: Boolean): ConnectionStrategy = { val cpf = new ConnectionProviderFactory { override def create(host: String, port: Int): ConnectionProvider = connectionProviderFactory(host, port) } - ConnectionStrategy(serviceDefinition, cpf, ctx ⇒ ctx.actorOf(LoadBalancerActor.props(loadBalancer, serviceDefinition.key))) + ConnectionStrategy(serviceDefinition, cpf, ctx ⇒ ctx.actorOf(LoadBalancerActor.props(loadBalancer, serviceDefinition.key)), onlyHealthyServices) } def apply(serviceName: String, connectionProviderFactory: (String, Int) ⇒ ConnectionProvider, loadBalancer: LoadBalancer): ConnectionStrategy = { - ConnectionStrategy(ServiceDefinition(serviceName), connectionProviderFactory, loadBalancer) + ConnectionStrategy(ServiceDefinition(serviceName), connectionProviderFactory, loadBalancer, onlyHealthyServices = false) } def apply(serviceName: String, connectionProviderFactory: (String, Int) ⇒ ConnectionProvider): ConnectionStrategy = { diff --git a/client/src/main/scala/stormlantern/consul/client/discovery/ServiceAvailabilityActor.scala b/client/src/main/scala/stormlantern/consul/client/discovery/ServiceAvailabilityActor.scala index d567b20..27ce6a5 100644 --- a/client/src/main/scala/stormlantern/consul/client/discovery/ServiceAvailabilityActor.scala +++ b/client/src/main/scala/stormlantern/consul/client/discovery/ServiceAvailabilityActor.scala @@ -7,7 +7,7 @@ import akka.pattern.pipe import dao._ import ServiceAvailabilityActor._ -class ServiceAvailabilityActor(httpClient: ConsulHttpClient, serviceDefinition: ServiceDefinition, listener: ActorRef) extends Actor { +class ServiceAvailabilityActor(httpClient: ConsulHttpClient, serviceDefinition: ServiceDefinition, listener: ActorRef, onlyHealthServices: Boolean) extends Actor { implicit val ec: ExecutionContext = context.dispatcher @@ -36,7 +36,14 @@ class ServiceAvailabilityActor(httpClient: ConsulHttpClient, serviceDefinition: } else { None } - (update, httpClient.getService( + (update, if (onlyHealthServices) + httpClient.getServiceHealthAware( + serviceDefinition.serviceName, + serviceDefinition.serviceTags.headOption, + Some(services.index), + Some("1s") + ) + else httpClient.getService( serviceDefinition.serviceName, serviceDefinition.serviceTags.headOption, Some(services.index), @@ -54,7 +61,7 @@ class ServiceAvailabilityActor(httpClient: ConsulHttpClient, serviceDefinition: object ServiceAvailabilityActor { - def props(httpClient: ConsulHttpClient, serviceDefinition: ServiceDefinition, listener: ActorRef): Props = Props(new ServiceAvailabilityActor(httpClient, serviceDefinition, listener)) + def props(httpClient: ConsulHttpClient, serviceDefinition: ServiceDefinition, listener: ActorRef, onlyHealthyServices: Boolean): Props = Props(new ServiceAvailabilityActor(httpClient, serviceDefinition, listener, onlyHealthyServices)) // Messages case object Start diff --git a/client/src/test/scala/stormlantern/consul/client/ServiceBrokerActorSpec.scala b/client/src/test/scala/stormlantern/consul/client/ServiceBrokerActorSpec.scala index 0cf08d1..1b49450 100644 --- a/client/src/test/scala/stormlantern/consul/client/ServiceBrokerActorSpec.scala +++ b/client/src/test/scala/stormlantern/consul/client/ServiceBrokerActorSpec.scala @@ -24,8 +24,8 @@ class ServiceBrokerActorSpec(_system: ActorSystem) extends TestKit(_system) with trait TestScope { val httpClient: ConsulHttpClient = mock[ConsulHttpClient] - val serviceAvailabilityActorFactory: (ActorRefFactory, ServiceDefinition, ActorRef) ⇒ ActorRef = - mock[(ActorRefFactory, ServiceDefinition, ActorRef) ⇒ ActorRef] + val serviceAvailabilityActorFactory: (ActorRefFactory, ServiceDefinition, ActorRef, Boolean) ⇒ ActorRef = + mock[(ActorRefFactory, ServiceDefinition, ActorRef, Boolean) ⇒ ActorRef] val connectionProviderFactory: ConnectionProviderFactory = mock[ConnectionProviderFactory] val connectionProvider: ConnectionProvider = mock[ConnectionProvider] val connectionHolder: ConnectionHolder = mock[ConnectionHolder] @@ -33,13 +33,13 @@ class ServiceBrokerActorSpec(_system: ActorSystem) extends TestKit(_system) with val service2 = ServiceDefinition("service2Key", "service2") val loadBalancerProbeForService1 = TestProbe("LoadBalancerActorForService1") val loadBalancerProbeForService2 = TestProbe("LoadBalancerActorForService2") - val connectionStrategyForService1 = ConnectionStrategy(service1, connectionProviderFactory, ctx ⇒ loadBalancerProbeForService1.ref) - val connectionStrategyForService2 = ConnectionStrategy(service2, connectionProviderFactory, ctx ⇒ loadBalancerProbeForService2.ref) + val connectionStrategyForService1 = ConnectionStrategy(service1, connectionProviderFactory, ctx ⇒ loadBalancerProbeForService1.ref, onlyHealthyServices = true) + val connectionStrategyForService2 = ConnectionStrategy(service2, connectionProviderFactory, ctx ⇒ loadBalancerProbeForService2.ref, onlyHealthyServices = false) } "The ServiceBrokerActor" should "create a child actor per service" in new TestScope { val serviceAvailabilityProbe = TestProbe("ServiceAvailabilityActor") - (serviceAvailabilityActorFactory.apply _).expects(*, service1, *).returns(serviceAvailabilityProbe.ref) + (serviceAvailabilityActorFactory.apply _).expects(*, service1, *, *).returns(serviceAvailabilityProbe.ref) val sut: TestActorRef[ServiceBrokerActor] = TestActorRef[ServiceBrokerActor](ServiceBrokerActor.props( Set(connectionStrategyForService1), serviceAvailabilityActorFactory)) serviceAvailabilityProbe.expectMsg(Start) @@ -49,7 +49,7 @@ class ServiceBrokerActorSpec(_system: ActorSystem) extends TestKit(_system) with it should "create a load balancer for each new service" in new TestScope { val serviceAvailabilityProbe = TestProbe("ServiceAvailabilityActor") - (serviceAvailabilityActorFactory.apply _).expects(*, service1, *).returns(serviceAvailabilityProbe.ref) + (serviceAvailabilityActorFactory.apply _).expects(*, service1, *, *).returns(serviceAvailabilityProbe.ref) val sut: TestActorRef[ServiceBrokerActor] = TestActorRef[ServiceBrokerActor](ServiceBrokerActor.props( Set(connectionStrategyForService1), serviceAvailabilityActorFactory)) serviceAvailabilityProbe.expectMsg(Start) @@ -63,7 +63,7 @@ class ServiceBrokerActorSpec(_system: ActorSystem) extends TestKit(_system) with it should "remove the load balancer for each old service" in new TestScope { val serviceAvailabilityProbe = TestProbe("ServiceAvailabilityActor") - (serviceAvailabilityActorFactory.apply _).expects(*, service1, *).returns(serviceAvailabilityProbe.ref) + (serviceAvailabilityActorFactory.apply _).expects(*, service1, *, *).returns(serviceAvailabilityProbe.ref) val sut: TestActorRef[ServiceBrokerActor] = TestActorRef[ServiceBrokerActor](ServiceBrokerActor.props( Set(connectionStrategyForService1), serviceAvailabilityActorFactory)) serviceAvailabilityProbe.expectMsg(Start) @@ -76,7 +76,7 @@ class ServiceBrokerActorSpec(_system: ActorSystem) extends TestKit(_system) with it should "initialize after all services have been seen" in new TestScope { val serviceAvailabilityProbe = TestProbe("ServiceAvailabilityActor") - (serviceAvailabilityActorFactory.apply _).expects(*, service1, *).returns(serviceAvailabilityProbe.ref) + (serviceAvailabilityActorFactory.apply _).expects(*, service1, *, *).returns(serviceAvailabilityProbe.ref) val sut: TestActorRef[ServiceBrokerActor] = TestActorRef[ServiceBrokerActor](ServiceBrokerActor.props( Set(connectionStrategyForService1), serviceAvailabilityActorFactory)) serviceAvailabilityProbe.expectMsg(Start) @@ -85,7 +85,7 @@ class ServiceBrokerActorSpec(_system: ActorSystem) extends TestKit(_system) with it should "request a connection from a loadbalancer" in new TestScope { val serviceAvailabilityProbe = TestProbe("ServiceAvailabilityActor") - (serviceAvailabilityActorFactory.apply _).expects(*, service1, *).returns(serviceAvailabilityProbe.ref) + (serviceAvailabilityActorFactory.apply _).expects(*, service1, *, *).returns(serviceAvailabilityProbe.ref) val sut: TestActorRef[ServiceBrokerActor] = TestActorRef[ServiceBrokerActor](ServiceBrokerActor.props( Set(connectionStrategyForService1), serviceAvailabilityActorFactory)) serviceAvailabilityProbe.expectMsg(Start) @@ -107,7 +107,7 @@ class ServiceBrokerActorSpec(_system: ActorSystem) extends TestKit(_system) with it should "forward a query for connection provider availability" in new TestScope { val serviceAvailabilityProbe = TestProbe("ServiceAvailabilityActor") - (serviceAvailabilityActorFactory.apply _).expects(*, service1, *).returns(serviceAvailabilityProbe.ref) + (serviceAvailabilityActorFactory.apply _).expects(*, service1, *, *).returns(serviceAvailabilityProbe.ref) val sut: TestActorRef[ServiceBrokerActor] = TestActorRef[ServiceBrokerActor](ServiceBrokerActor.props( Set(connectionStrategyForService1), serviceAvailabilityActorFactory)) serviceAvailabilityProbe.expectMsg(Start) @@ -122,8 +122,8 @@ class ServiceBrokerActorSpec(_system: ActorSystem) extends TestKit(_system) with it should "return false when every service doesn't have at least one connection provider available" in new TestScope { val service1AvailabilityProbe = TestProbe("Service1AvailabilityActor") val service2AvailabilityProbe = TestProbe("Service2AvailabilityActor") - (serviceAvailabilityActorFactory.apply _).expects(*, service1, *).returns(service1AvailabilityProbe.ref) - (serviceAvailabilityActorFactory.apply _).expects(*, service2, *).returns(service2AvailabilityProbe.ref) + (serviceAvailabilityActorFactory.apply _).expects(*, service1, *, *).returns(service1AvailabilityProbe.ref) + (serviceAvailabilityActorFactory.apply _).expects(*, service2, *, *).returns(service2AvailabilityProbe.ref) val sut: TestActorRef[ServiceBrokerActor] = TestActorRef[ServiceBrokerActor](ServiceBrokerActor.props( Set(connectionStrategyForService1, connectionStrategyForService2), serviceAvailabilityActorFactory)) service1AvailabilityProbe.expectMsg(Start) @@ -142,8 +142,8 @@ class ServiceBrokerActorSpec(_system: ActorSystem) extends TestKit(_system) with it should "return true when every service has at least one connection provider avaiable" in new TestScope { val service1AvailabilityProbe = TestProbe("Service1AvailabilityActor") val service2AvailabilityProbe = TestProbe("Service2AvailabilityActor") - (serviceAvailabilityActorFactory.apply _).expects(*, service1, *).returns(service1AvailabilityProbe.ref) - (serviceAvailabilityActorFactory.apply _).expects(*, service2, *).returns(service2AvailabilityProbe.ref) + (serviceAvailabilityActorFactory.apply _).expects(*, service1, *, *).returns(service1AvailabilityProbe.ref) + (serviceAvailabilityActorFactory.apply _).expects(*, service2, *, *).returns(service2AvailabilityProbe.ref) val sut: TestActorRef[ServiceBrokerActor] = TestActorRef[ServiceBrokerActor](ServiceBrokerActor.props( Set(connectionStrategyForService1, connectionStrategyForService2), serviceAvailabilityActorFactory)) service1AvailabilityProbe.expectMsg(Start) diff --git a/client/src/test/scala/stormlantern/consul/client/discovery/ServiceAvailabilityActorSpec.scala b/client/src/test/scala/stormlantern/consul/client/discovery/ServiceAvailabilityActorSpec.scala index 91d0419..296709d 100644 --- a/client/src/test/scala/stormlantern/consul/client/discovery/ServiceAvailabilityActorSpec.scala +++ b/client/src/test/scala/stormlantern/consul/client/discovery/ServiceAvailabilityActorSpec.scala @@ -23,7 +23,7 @@ class ServiceAvailabilityActorSpec(_system: ActorSystem) extends TestKit(_system "The ServiceAvailabilityActor" should "receive one service update when there are no changes" in { val httpClient: ConsulHttpClient = mock[ConsulHttpClient] - val sut = TestActorRef(ServiceAvailabilityActor.props(httpClient, ServiceDefinition("bogus123", "bogus"), self)) + val sut = TestActorRef(ServiceAvailabilityActor.props(httpClient, ServiceDefinition("bogus123", "bogus"), self, onlyHealthyServices = false)) (httpClient.getService _).expects("bogus", None, Some(0L), Some("1s"), None).returns(Future.successful(IndexedServiceInstances(1, Set.empty))) (httpClient.getService _).expects("bogus", None, Some(1L), Some("1s"), None).onCall { p ⇒ sut.stop() @@ -32,12 +32,12 @@ class ServiceAvailabilityActorSpec(_system: ActorSystem) extends TestKit(_system sut ! Start expectMsg(1.second, ServiceAvailabilityActor.ServiceAvailabilityUpdate("bogus123")) expectMsg(1.second, ServiceAvailabilityActor.Started) - expectNoMsg(1.second) + expectNoMessage(1.second) } it should "receive two service updates when there is a change" in { val httpClient: ConsulHttpClient = mock[ConsulHttpClient] - lazy val sut = TestActorRef(ServiceAvailabilityActor.props(httpClient, ServiceDefinition("bogus123", "bogus"), self)) + lazy val sut = TestActorRef(ServiceAvailabilityActor.props(httpClient, ServiceDefinition("bogus123", "bogus"), self, onlyHealthyServices = false)) val service = ModelHelpers.createService("bogus123", "bogus") (httpClient.getService _).expects("bogus", None, Some(0L), Some("1s"), None).returns(Future.successful(IndexedServiceInstances(1, Set.empty))) (httpClient.getService _).expects("bogus", None, Some(1L), Some("1s"), None).returns(Future.successful(IndexedServiceInstances(2, Set(service)))) @@ -49,12 +49,12 @@ class ServiceAvailabilityActorSpec(_system: ActorSystem) extends TestKit(_system expectMsg(1.second, ServiceAvailabilityActor.ServiceAvailabilityUpdate("bogus123")) expectMsg(1.second, ServiceAvailabilityActor.Started) expectMsg(1.second, ServiceAvailabilityActor.ServiceAvailabilityUpdate("bogus123", Set(service), Set.empty)) - expectNoMsg(1.second) + expectNoMessage(1.second) } it should "receive one service update when there are two with different tags" in { val httpClient: ConsulHttpClient = mock[ConsulHttpClient] - lazy val sut = TestActorRef(ServiceAvailabilityActor.props(httpClient, ServiceDefinition("bogus123", "bogus", Set("one", "two")), self)) + lazy val sut = TestActorRef(ServiceAvailabilityActor.props(httpClient, ServiceDefinition("bogus123", "bogus", Set("one", "two")), self, onlyHealthyServices = false)) val nonMatchingservice = ModelHelpers.createService("bogus123", "bogus", tags = Set("one")) val matchingService = nonMatchingservice.copy(serviceTags = Set("one", "two")) (httpClient.getService _).expects("bogus", Some("one"), Some(0L), Some("1s"), None).returns(Future.successful(IndexedServiceInstances(1, Set.empty))) @@ -67,6 +67,6 @@ class ServiceAvailabilityActorSpec(_system: ActorSystem) extends TestKit(_system expectMsg(1.second, ServiceAvailabilityActor.ServiceAvailabilityUpdate("bogus123")) expectMsg(1.second, ServiceAvailabilityActor.Started) expectMsg(1.second, ServiceAvailabilityActor.ServiceAvailabilityUpdate("bogus123", Set(matchingService), Set.empty)) - expectNoMsg(1.second) + expectNoMessage(1.second) } } diff --git a/dns-helper/src/main/scala/stormlantern/consul/client/Dns.scala b/dns-helper/src/main/scala/stormlantern/consul/client/Dns.scala index 78412be..10da1c5 100644 --- a/dns-helper/src/main/scala/stormlantern/consul/client/Dns.scala +++ b/dns-helper/src/main/scala/stormlantern/consul/client/Dns.scala @@ -2,12 +2,12 @@ package stormlantern.consul.client import java.net.URL import com.spotify.dns.DnsSrvResolvers -import collection.JavaConversions._ +import collection.JavaConverters._ object DNS { def lookup(consulAddress: String): URL = { val resolver = DnsSrvResolvers.newBuilder().build() - val lookupResult = resolver.resolve(consulAddress).headOption.getOrElse(throw new RuntimeException(s"No record found for ${consulAddress}")) + val lookupResult = resolver.resolve(consulAddress).asScala.headOption.getOrElse(throw new RuntimeException(s"No record found for ${consulAddress}")) new URL(s"http://${lookupResult.host()}:${lookupResult.port()}") } } \ No newline at end of file diff --git a/docker-testkit/src/main/scala/stormlantern/dockertestkit/DockerClientProvider.scala b/docker-testkit/src/main/scala/stormlantern/dockertestkit/DockerClientProvider.scala index 32cbff6..c66ee72 100644 --- a/docker-testkit/src/main/scala/stormlantern/dockertestkit/DockerClientProvider.scala +++ b/docker-testkit/src/main/scala/stormlantern/dockertestkit/DockerClientProvider.scala @@ -5,7 +5,7 @@ import java.net.URI import com.spotify.docker.client.DockerClient.ListContainersParam import com.spotify.docker.client.{ DefaultDockerClient, DockerClient } -import scala.collection.JavaConversions._ +import scala.collection.JavaConverters._ object DockerClientProvider { @@ -20,6 +20,6 @@ object DockerClientProvider { } def cleanUp(): Unit = { - client.listContainers(ListContainersParam.allContainers()).foreach(c => client.removeContainer(c.id())) + client.listContainers(ListContainersParam.allContainers()).asScala.foreach(c => client.removeContainer(c.id())) } } diff --git a/docker-testkit/src/main/scala/stormlantern/dockertestkit/client/Container.scala b/docker-testkit/src/main/scala/stormlantern/dockertestkit/client/Container.scala index 3a91ad8..b480d4e 100644 --- a/docker-testkit/src/main/scala/stormlantern/dockertestkit/client/Container.scala +++ b/docker-testkit/src/main/scala/stormlantern/dockertestkit/client/Container.scala @@ -5,7 +5,7 @@ import java.util import com.spotify.docker.client.messages._ import stormlantern.dockertestkit.DockerClientProvider -import scala.collection.JavaConversions._ +import scala.collection.JavaConverters._ class Container(config: ContainerConfig) { @@ -33,7 +33,7 @@ class Container(config: ContainerConfig) { def mappedPort(port: String): Seq[PortBinding] = { val ports: util.Map[String, util.List[PortBinding]] = Option(docker.inspectContainer(id).networkSettings().ports()) .getOrElse(throw new IllegalStateException(s"No ports found for on container with id $id")) - Option(ports.get(port)).getOrElse(throw new IllegalStateException(s"Port $port not found on caintainer with id $id")) + Option(ports.get(port)).getOrElse(throw new IllegalStateException(s"Port $port not found on caintainer with id $id")).asScala } } diff --git a/project/build.properties b/project/build.properties index 0531343..5620cc5 100644 --- a/project/build.properties +++ b/project/build.properties @@ -1 +1 @@ -sbt.version=1.1.2 +sbt.version=1.2.1 diff --git a/project/plugins.sbt b/project/plugins.sbt index 9426ecf..67db58c 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -1,5 +1,5 @@ addSbtPlugin("io.spray" % "sbt-revolver" % "0.9.1") addSbtPlugin("org.scalariform" % "sbt-scalariform" % "1.8.2") -addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.3.3") -addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.1.1") +addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.3.6") +addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.1.2-1") addSbtPlugin("com.timushev.sbt" % "sbt-updates" % "0.3.4") From 43e59fad87a13d8e0df2759816872f2d4c2ec3a4 Mon Sep 17 00:00:00 2001 From: Sjoerd Mulder Date: Mon, 3 Dec 2018 12:18:55 +0100 Subject: [PATCH 04/33] Updating deps and adding release possibilities --- build.sbt | 5 ++++- project/Dependencies.scala | 1 - project/build.properties | 2 +- project/plugins.sbt | 2 ++ 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/build.sbt b/build.sbt index 2784971..3e8cd3f 100644 --- a/build.sbt +++ b/build.sbt @@ -5,11 +5,14 @@ import com.typesafe.sbt.SbtScalariform.ScalariformKeys import scalariform.formatter.preferences._ +import xerial.sbt.Sonatype._ +sonatypeProfileName := "com.crobox" + // Common variables lazy val commonSettings = Seq( scalaVersion := "2.12.4", crossScalaVersions := Seq("2.11.11", "2.12.4"), - organization := "nl.stormlantern", + organization := "com.crobox", version := "0.4.1-SNAPSHOT", resolvers ++= Dependencies.resolutionRepos, scalacOptions ++= Seq("-unchecked", "-deprecation", "-feature") diff --git a/project/Dependencies.scala b/project/Dependencies.scala index b3da23e..59bc40f 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -4,7 +4,6 @@ object Dependencies { val resolutionRepos = Seq( "spray repo" at "http://repo.spray.io", - "scalaz-bintray" at "http://dl.bintray.com/scalaz/releases", "softprops-maven" at "http://dl.bintray.com/content/softprops/maven" ) diff --git a/project/build.properties b/project/build.properties index 5620cc5..7c58a83 100644 --- a/project/build.properties +++ b/project/build.properties @@ -1 +1 @@ -sbt.version=1.2.1 +sbt.version=1.2.6 diff --git a/project/plugins.sbt b/project/plugins.sbt index 67db58c..f790a81 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -3,3 +3,5 @@ addSbtPlugin("org.scalariform" % "sbt-scalariform" % "1.8.2") addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.3.6") addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.1.2-1") addSbtPlugin("com.timushev.sbt" % "sbt-updates" % "0.3.4") +addSbtPlugin("com.github.gseitz" % "sbt-release" % "1.0.9") +addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "2.3") \ No newline at end of file From 99d7241a3b81b99e1e1ed95575696e1b5598e782 Mon Sep 17 00:00:00 2001 From: Sjoerd Mulder Date: Mon, 3 Dec 2018 12:21:50 +0100 Subject: [PATCH 05/33] Adding credentials --- .gitignore | 1 + build.sbt | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index d174770..d3ae43f 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ lib_managed/ src_managed/ project/boot/ project/plugins/project/ +credentials.sbt # Scala-IDE specific .scala_dependencies diff --git a/build.sbt b/build.sbt index 3e8cd3f..1e0b782 100644 --- a/build.sbt +++ b/build.sbt @@ -130,7 +130,6 @@ lazy val publishSettings = Seq( publishArtifact := false, publishMavenStyle := true, pomIncludeRepository := { _ => false }, - credentials += Credentials(Path.userHome / ".ivy2" / ".credentials"), publishTo := { val nexus = "https://oss.sonatype.org/" if (isSnapshot.value) From d808543c802e2bf73be59f701b8bde2d38e6c8e2 Mon Sep 17 00:00:00 2001 From: Sjoerd Mulder Date: Mon, 3 Dec 2018 12:25:08 +0100 Subject: [PATCH 06/33] Setting version to 0.4.1 --- version.sbt | 1 + 1 file changed, 1 insertion(+) create mode 100644 version.sbt diff --git a/version.sbt b/version.sbt new file mode 100644 index 0000000..9e6291c --- /dev/null +++ b/version.sbt @@ -0,0 +1 @@ +version in ThisBuild := "0.4.1" From 6f34399ef8e2082627d5a5c3cc3a4a1e45fd811e Mon Sep 17 00:00:00 2001 From: Sjoerd Mulder Date: Mon, 3 Dec 2018 12:25:39 +0100 Subject: [PATCH 07/33] Setting version to 0.4.2-SNAPSHOT --- version.sbt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version.sbt b/version.sbt index 9e6291c..11c590d 100644 --- a/version.sbt +++ b/version.sbt @@ -1 +1 @@ -version in ThisBuild := "0.4.1" +version in ThisBuild := "0.4.2-SNAPSHOT" From d622eb28e838aaa78cc644f76e214097548716e7 Mon Sep 17 00:00:00 2001 From: Sjoerd Mulder Date: Mon, 3 Dec 2018 12:26:29 +0100 Subject: [PATCH 08/33] Version --- build.sbt | 1 - 1 file changed, 1 deletion(-) diff --git a/build.sbt b/build.sbt index 1e0b782..b512a80 100644 --- a/build.sbt +++ b/build.sbt @@ -13,7 +13,6 @@ lazy val commonSettings = Seq( scalaVersion := "2.12.4", crossScalaVersions := Seq("2.11.11", "2.12.4"), organization := "com.crobox", - version := "0.4.1-SNAPSHOT", resolvers ++= Dependencies.resolutionRepos, scalacOptions ++= Seq("-unchecked", "-deprecation", "-feature") ) From 2a262b63f1439e553c6d5e65b8311dc643d1e0de Mon Sep 17 00:00:00 2001 From: Sjoerd Mulder Date: Mon, 3 Dec 2018 12:31:24 +0100 Subject: [PATCH 09/33] x --- build.sbt | 5 ++--- version.sbt | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/build.sbt b/build.sbt index b512a80..734f549 100644 --- a/build.sbt +++ b/build.sbt @@ -2,10 +2,8 @@ import Dependencies._ import sbt.Keys._ import com.typesafe.sbt.SbtScalariform import com.typesafe.sbt.SbtScalariform.ScalariformKeys - +import com.typesafe.sbt.pgp.PgpKeys import scalariform.formatter.preferences._ - -import xerial.sbt.Sonatype._ sonatypeProfileName := "com.crobox" // Common variables @@ -129,6 +127,7 @@ lazy val publishSettings = Seq( publishArtifact := false, publishMavenStyle := true, pomIncludeRepository := { _ => false }, + sbtrelease.ReleasePlugin.autoImport.releasePublishArtifactsAction := PgpKeys.publishSigned.value, publishTo := { val nexus = "https://oss.sonatype.org/" if (isSnapshot.value) diff --git a/version.sbt b/version.sbt index 11c590d..7338ce7 100644 --- a/version.sbt +++ b/version.sbt @@ -1 +1 @@ -version in ThisBuild := "0.4.2-SNAPSHOT" +version in ThisBuild := "0.5.1-SNAPSHOT" From f27922a3dd3c6714b34a02120487c9b94686643d Mon Sep 17 00:00:00 2001 From: Sjoerd Mulder Date: Mon, 3 Dec 2018 12:32:10 +0100 Subject: [PATCH 10/33] Setting version to 0.5.0 --- version.sbt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version.sbt b/version.sbt index 7338ce7..0d6d27c 100644 --- a/version.sbt +++ b/version.sbt @@ -1 +1 @@ -version in ThisBuild := "0.5.1-SNAPSHOT" +version in ThisBuild := "0.5.0" From d7e7e238839daab9e22887fa57bbd2e65c11d342 Mon Sep 17 00:00:00 2001 From: Sjoerd Mulder Date: Mon, 3 Dec 2018 12:33:23 +0100 Subject: [PATCH 11/33] Setting version to 0.5.1-SNAPSHOT --- version.sbt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version.sbt b/version.sbt index 0d6d27c..7338ce7 100644 --- a/version.sbt +++ b/version.sbt @@ -1 +1 @@ -version in ThisBuild := "0.5.0" +version in ThisBuild := "0.5.1-SNAPSHOT" From 896b0099235f107ccce0c2457760047425e0ab45 Mon Sep 17 00:00:00 2001 From: Sjoerd Mulder Date: Wed, 2 Jan 2019 13:13:34 +0100 Subject: [PATCH 12/33] Removing println --- .../consul/client/dao/akka/AkkaHttpConsulClient.scala | 1 - 1 file changed, 1 deletion(-) diff --git a/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala b/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala index 9e860d6..e34874d 100644 --- a/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala +++ b/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala @@ -58,7 +58,6 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) extends retry[IndexedServiceInstances]() { getResponse(request, JsonMediaType).flatMap { response ⇒ validIndex(response).map { idx ⇒ - println(response.body) val services = response.body.parseJson.convertTo[Option[Set[HealthServiceInstance]]] IndexedServiceInstances(idx, services.getOrElse(Set.empty[HealthServiceInstance]).map(_.asServiceInstance)) } From 20b0369e6ffad26ef4acab0d9d6721457c3c7f89 Mon Sep 17 00:00:00 2001 From: Sjoerd Mulder Date: Wed, 2 Jan 2019 13:15:15 +0100 Subject: [PATCH 13/33] Setting version to 0.5.1 --- version.sbt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version.sbt b/version.sbt index 7338ce7..c3fdbde 100644 --- a/version.sbt +++ b/version.sbt @@ -1 +1 @@ -version in ThisBuild := "0.5.1-SNAPSHOT" +version in ThisBuild := "0.5.1" From 7b0fee0ecbbe9f1a93069f87d3d1f3b72cf45e1e Mon Sep 17 00:00:00 2001 From: Sjoerd Mulder Date: Wed, 2 Jan 2019 13:16:04 +0100 Subject: [PATCH 14/33] Setting version to 0.5.2-SNAPSHOT --- version.sbt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version.sbt b/version.sbt index c3fdbde..f943617 100644 --- a/version.sbt +++ b/version.sbt @@ -1 +1 @@ -version in ThisBuild := "0.5.1" +version in ThisBuild := "0.5.2-SNAPSHOT" From eec86322db23d0fb9e8d917dba2fc47098b8ec97 Mon Sep 17 00:00:00 2001 From: Sjoerd Mulder Date: Thu, 26 Mar 2020 18:49:56 +0100 Subject: [PATCH 15/33] Update to scala 2.13 --- build.sbt | 8 ++++---- .../dockertestkit/client/Container.scala | 2 +- project/Dependencies.scala | 16 ++++++++-------- project/build.properties | 2 +- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/build.sbt b/build.sbt index 734f549..f0200e8 100644 --- a/build.sbt +++ b/build.sbt @@ -8,8 +8,8 @@ sonatypeProfileName := "com.crobox" // Common variables lazy val commonSettings = Seq( - scalaVersion := "2.12.4", - crossScalaVersions := Seq("2.11.11", "2.12.4"), + scalaVersion := "2.13.1", + crossScalaVersions := Seq("2.12.11", "2.13.1"), organization := "com.crobox", resolvers ++= Dependencies.resolutionRepos, scalacOptions ++= Seq("-unchecked", "-deprecation", "-feature") @@ -19,8 +19,8 @@ lazy val reactiveConsul = (project in file(".")) .settings( commonSettings: _* ) .settings( publishSettings: _* ) .aggregate(client, dnsHelper, dockerTestkit/*, example*/) - - + + lazy val dnsHelper = (project in file("dns-helper")) .settings( commonSettings: _* ) .settings( publishSettings: _* ) diff --git a/docker-testkit/src/main/scala/stormlantern/dockertestkit/client/Container.scala b/docker-testkit/src/main/scala/stormlantern/dockertestkit/client/Container.scala index b480d4e..93852fd 100644 --- a/docker-testkit/src/main/scala/stormlantern/dockertestkit/client/Container.scala +++ b/docker-testkit/src/main/scala/stormlantern/dockertestkit/client/Container.scala @@ -33,7 +33,7 @@ class Container(config: ContainerConfig) { def mappedPort(port: String): Seq[PortBinding] = { val ports: util.Map[String, util.List[PortBinding]] = Option(docker.inspectContainer(id).networkSettings().ports()) .getOrElse(throw new IllegalStateException(s"No ports found for on container with id $id")) - Option(ports.get(port)).getOrElse(throw new IllegalStateException(s"Port $port not found on caintainer with id $id")).asScala + Option(ports.get(port)).getOrElse(throw new IllegalStateException(s"Port $port not found on caintainer with id $id")).asScala.toSeq } } diff --git a/project/Dependencies.scala b/project/Dependencies.scala index 59bc40f..483c1d3 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -3,23 +3,23 @@ import sbt._ object Dependencies { val resolutionRepos = Seq( - "spray repo" at "http://repo.spray.io", - "softprops-maven" at "http://dl.bintray.com/content/softprops/maven" + "spray repo" at "https://repo.spray.io", + "softprops-maven" at "https://dl.bintray.com/content/softprops/maven" ) - val akkaVersion = "2.5.11" - val akkaHttpVersion = "10.1.1" + val akkaVersion = "2.5.30" + val akkaHttpVersion = "10.1.11" val akkaHttp = "com.typesafe.akka" %% "akka-http-core" % akkaHttpVersion val akkaHttpSprayJson = "com.typesafe.akka" %% "akka-http-spray-json" % akkaHttpVersion val akkaActor = "com.typesafe.akka" %% "akka-actor" % akkaVersion val akkaStream = "com.typesafe.akka" %% "akka-stream" % akkaVersion val akkaSlf4j = "com.typesafe.akka" %% "akka-slf4j" % akkaVersion - val slf4j = "org.slf4j" % "slf4j-api" % "1.7.21" + val slf4j = "org.slf4j" % "slf4j-api" % "1.7.30" val logback = "ch.qos.logback" % "logback-classic" % "1.1.7" val spotifyDocker = "com.spotify" % "docker-client" % "3.6.8" - val spotifyDns = "com.spotify" % "dns" % "3.1.4" - val scalaTest = "org.scalatest" %% "scalatest" % "3.0.1" - val scalaMock = "org.scalamock" %% "scalamock-scalatest-support" % "3.6.0" + val spotifyDns = "com.spotify" % "dns" % "3.2.2" + val scalaTest = "org.scalatest" %% "scalatest" % "3.1.1" + val scalaMock = "org.scalamock" %% "scalamock" % "4.4.0" val akkaTestKit = "com.typesafe.akka" %% "akka-testkit" % akkaVersion } diff --git a/project/build.properties b/project/build.properties index 7c58a83..a919a9b 100644 --- a/project/build.properties +++ b/project/build.properties @@ -1 +1 @@ -sbt.version=1.2.6 +sbt.version=1.3.8 From abc8f7a4c82905698d7b87745e32cd91332abcf3 Mon Sep 17 00:00:00 2001 From: Sjoerd Mulder Date: Thu, 26 Mar 2020 18:50:35 +0100 Subject: [PATCH 16/33] Setting version to 0.5.2 --- version.sbt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version.sbt b/version.sbt index f943617..d30395f 100644 --- a/version.sbt +++ b/version.sbt @@ -1 +1 @@ -version in ThisBuild := "0.5.2-SNAPSHOT" +version in ThisBuild := "0.5.2" From 5d489f7a9b141fb5a54a2ae5f2960540e23a4857 Mon Sep 17 00:00:00 2001 From: Sjoerd Mulder Date: Thu, 26 Mar 2020 18:51:32 +0100 Subject: [PATCH 17/33] Setting version to 0.5.3-SNAPSHOT --- version.sbt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version.sbt b/version.sbt index d30395f..7193baf 100644 --- a/version.sbt +++ b/version.sbt @@ -1 +1 @@ -version in ThisBuild := "0.5.2" +version in ThisBuild := "0.5.3-SNAPSHOT" From 58c334465204b087100176690a6449a2fff6cbcb Mon Sep 17 00:00:00 2001 From: Sjoerd Mulder Date: Thu, 26 Mar 2020 21:10:38 +0100 Subject: [PATCH 18/33] Updating build --- build.sbt | 26 ++++------ .../client/ServiceBrokerIntegrationTest.scala | 6 +-- .../AkkaHttpConsulClientIntegrationTest.scala | 48 +++++++++---------- .../client/util/ConsulDockerContainer.scala | 4 +- .../ConsulRegistratorDockerContainer.scala | 4 +- .../consul/client/util/TestActorSystem.scala | 2 +- .../loadbalancers/LoadBalancerActor.scala | 2 +- .../loadbalancers/LoadBalancerActorSpec.scala | 4 +- .../dockertestkit/DockerClientProvider.scala | 4 +- .../stormlantern/consul/example/Boot.scala | 6 +-- 10 files changed, 50 insertions(+), 56 deletions(-) diff --git a/build.sbt b/build.sbt index f0200e8..acec118 100644 --- a/build.sbt +++ b/build.sbt @@ -1,7 +1,6 @@ import Dependencies._ import sbt.Keys._ import com.typesafe.sbt.SbtScalariform -import com.typesafe.sbt.SbtScalariform.ScalariformKeys import com.typesafe.sbt.pgp.PgpKeys import scalariform.formatter.preferences._ sonatypeProfileName := "com.crobox" @@ -33,12 +32,7 @@ lazy val dnsHelper = (project in file("dns-helper")) fork := true, libraryDependencies ++= Seq( spotifyDns - ), - ScalariformKeys.preferences := ScalariformKeys.preferences.value - .setPreference(AlignSingleLineCaseStatements, true) - .setPreference(DoubleIndentClassDeclaration, true) - .setPreference(DanglingCloseParenthesis, Preserve) - .setPreference(RewriteArrowSymbols, true) + ) ) lazy val client = (project in file("client")) @@ -63,12 +57,7 @@ lazy val client = (project in file("client")) logback % "it,test", akkaTestKit % "it,test", spotifyDocker % "it,test" - ), - ScalariformKeys.preferences := ScalariformKeys.preferences.value - .setPreference(AlignSingleLineCaseStatements, true) - .setPreference(DoubleIndentClassDeclaration, true) - .setPreference(DanglingCloseParenthesis, Preserve) - .setPreference(RewriteArrowSymbols, true) + ) ) .configs( IntegrationTest ) .settings( Defaults.itSettings : _* ) @@ -136,7 +125,7 @@ lazy val publishSettings = Seq( Some("releases" at nexus + "service/local/staging/deploy/maven2") }, pomExtra := ( - http://github.com/dlouwers/reactive-consul + http://github.com/crobox/reactive-consul MIT @@ -145,10 +134,15 @@ lazy val publishSettings = Seq( - git@github.com:dlouwers/reactive-consul.git - scm:git@github.com:dlouwers/reactive-consul.git + git@github.com:crobox/reactive-consul.git + scm:git@github.com:crobox/reactive-consul.git + + sjoerdmulder + Sjoerd Mulder + http://github.com/sjoerdmulder + dlouwers Dirk Louwers diff --git a/client/src/it/scala/stormlantern/consul/client/ServiceBrokerIntegrationTest.scala b/client/src/it/scala/stormlantern/consul/client/ServiceBrokerIntegrationTest.scala index 8f406b4..ef1e283 100644 --- a/client/src/it/scala/stormlantern/consul/client/ServiceBrokerIntegrationTest.scala +++ b/client/src/it/scala/stormlantern/consul/client/ServiceBrokerIntegrationTest.scala @@ -16,8 +16,8 @@ class ServiceBrokerIntegrationTest extends FlatSpec with Matchers with ScalaFutu import scala.concurrent.ExecutionContext.Implicits.global - "The ServiceBroker" should "provide a usable connection to consul" in withConsulHost { (host, port) ⇒ - withActorSystem { implicit actorSystem ⇒ + "The ServiceBroker" should "provide a usable connection to consul" in withConsulHost { (host, port) => + withActorSystem { implicit actorSystem => val akkaHttpClient = new AkkaHttpConsulClient(new URL(s"http://$host:$port")) // Register the HTTP interface akkaHttpClient.putService(ServiceRegistration("consul-http", Some("consul-http-1"), address = Some(host), port = Some(port))) @@ -32,7 +32,7 @@ class ServiceBrokerIntegrationTest extends FlatSpec with Matchers with ScalaFutu val connectionStrategy = ConnectionStrategy(ServiceDefinition("consul-http"), connectionProviderFactory, new RoundRobinLoadBalancer, onlyHealthyServices = true) val sut = ServiceBroker(actorSystem, akkaHttpClient, Set(connectionStrategy)) eventually { - sut.withService("consul-http") { connection: ConsulHttpClient ⇒ + sut.withService("consul-http") { connection: ConsulHttpClient => connection.getService("bogus").map(_.resource should have size 0) } sut diff --git a/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIntegrationTest.scala b/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIntegrationTest.scala index e0e651e..f57dfb7 100644 --- a/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIntegrationTest.scala +++ b/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIntegrationTest.scala @@ -12,16 +12,16 @@ class AkkaHttpConsulClientIntegrationTest extends FlatSpec with Matchers with Sc import scala.concurrent.ExecutionContext.Implicits.global - def withConsulHttpClient[T](f: ConsulHttpClient ⇒ T): T = withConsulHost { (host, port) ⇒ - withActorSystem { implicit actorSystem ⇒ + def withConsulHttpClient[T](f: ConsulHttpClient => T): T = withConsulHost { (host, port) => + withActorSystem { implicit actorSystem => val subject: ConsulHttpClient = new AkkaHttpConsulClient(new URL(s"http://$host:$port")) f(subject) } } - "The AkkaHttpConsulClient" should "retrieve a single Consul service from a freshly started Consul instance" in withConsulHttpClient { subject ⇒ + "The AkkaHttpConsulClient" should "retrieve a single Consul service from a freshly started Consul instance" in withConsulHttpClient { subject => eventually { - subject.getService("consul").map { result ⇒ + subject.getService("consul").map { result => result.resource should have size 1 result.resource.head.serviceName shouldEqual "consul" }.futureValue @@ -29,30 +29,30 @@ class AkkaHttpConsulClientIntegrationTest extends FlatSpec with Matchers with Sc } it should "retrieve a single health aware Consul service from a freshly started Consul instance" in withConsulHttpClient { - subject ⇒ + subject => eventually { - subject.getServiceHealthAware("consul").map { result ⇒ + subject.getServiceHealthAware("consul").map { result => result.resource should have size 1 result.resource.head.serviceName shouldEqual "consul" }.futureValue } } - it should "retrieve no unknown service from a freshly started Consul instance" in withConsulHttpClient { subject ⇒ + it should "retrieve no unknown service from a freshly started Consul instance" in withConsulHttpClient { subject => eventually { - subject.getService("bogus").map { result ⇒ + subject.getService("bogus").map { result => logger.info(s"Index is ${result.index}") result.resource should have size 0 }.futureValue } } - it should "retrieve a single Consul service from a freshly started Consul instance and timeout after the second request if nothing changes" in withConsulHttpClient { subject ⇒ + it should "retrieve a single Consul service from a freshly started Consul instance and timeout after the second request if nothing changes" in withConsulHttpClient { subject => eventually { - subject.getService("consul").flatMap { result ⇒ + subject.getService("consul").flatMap { result => result.resource should have size 1 result.resource.head.serviceName shouldEqual "consul" - subject.getService("consul", None, Some(result.index), Some("500ms")).map { secondResult ⇒ + subject.getService("consul", None, Some(result.index), Some("500ms")).map { secondResult => secondResult.resource should have size 1 secondResult.index shouldEqual result.index } @@ -60,41 +60,41 @@ class AkkaHttpConsulClientIntegrationTest extends FlatSpec with Matchers with Sc } } - it should "register and deregister a new service with Consul" in withConsulHttpClient { subject ⇒ + it should "register and deregister a new service with Consul" in withConsulHttpClient { subject => subject.putService(ServiceRegistration("newservice", Some("newservice-1"))) .futureValue should equal("newservice-1") subject.putService(ServiceRegistration("newservice", Some("newservice-2"), check = Some(TTLHealthCheck("2s")))) .futureValue should equal("newservice-2") eventually { - subject.getService("newservice").map { result ⇒ + subject.getService("newservice").map { result => result.resource should have size 2 result.resource.head.serviceName shouldEqual "newservice" }.futureValue } subject.deleteService("newservice-1").futureValue should equal(()) eventually { - subject.getService("newservice").map { result ⇒ + subject.getService("newservice").map { result => result.resource should have size 1 result.resource.head.serviceName shouldEqual "newservice" } } } - it should "retrieve a service matching tags and leave out others" in withConsulHttpClient { subject ⇒ + it should "retrieve a service matching tags and leave out others" in withConsulHttpClient { subject => subject.putService(ServiceRegistration("newservice", Some("newservice-1"), Set("tag1", "tag2"))) .futureValue should equal("newservice-1") subject.putService(ServiceRegistration("newservice", Some("newservice-2"), Set("tag2", "tag3"))) .futureValue should equal("newservice-2") eventually { - subject.getService("newservice").map { result ⇒ + subject.getService("newservice").map { result => result.resource should have size 2 result.resource.head.serviceName shouldEqual "newservice" }.futureValue - subject.getService("newservice", Some("tag2")).map { result ⇒ + subject.getService("newservice", Some("tag2")).map { result => result.resource should have size 2 result.resource.head.serviceName shouldEqual "newservice" }.futureValue - subject.getService("newservice", Some("tag3")).map { result ⇒ + subject.getService("newservice", Some("tag3")).map { result => result.resource should have size 1 result.resource.head.serviceName shouldEqual "newservice" result.resource.head.serviceId shouldEqual "newservice-2" @@ -102,15 +102,15 @@ class AkkaHttpConsulClientIntegrationTest extends FlatSpec with Matchers with Sc } } - it should "register a session and get it's ID then read it back" in withConsulHttpClient { subject ⇒ + it should "register a session and get it's ID then read it back" in withConsulHttpClient { subject => val id: UUID = subject.putSession(Some(SessionCreation(name = Some("MySession")))).futureValue - subject.getSessionInfo(id).map { sessionInfo ⇒ + subject.getSessionInfo(id).map { sessionInfo => sessionInfo should be('defined) sessionInfo.get.id shouldEqual id }.futureValue } - it should "get a session lock on a key/value pair and fail to get a second lock" in withConsulHttpClient { subject ⇒ + it should "get a session lock on a key/value pair and fail to get a second lock" in withConsulHttpClient { subject => val id: UUID = subject.putSession(Some(SessionCreation(name = Some("MySession")))).futureValue val payload = """ { "name" : "test" } """.getBytes("UTF-8") subject.putKeyValuePair("my/key", payload, Some(AcquireSession(id))).futureValue should be(true) @@ -118,7 +118,7 @@ class AkkaHttpConsulClientIntegrationTest extends FlatSpec with Matchers with Sc subject.putKeyValuePair("my/key", payload, Some(ReleaseSession(id))).futureValue should be(true) } - it should "get a session lock on a key/value pair and get a second lock after release" in withConsulHttpClient { subject ⇒ + it should "get a session lock on a key/value pair and get a second lock after release" in withConsulHttpClient { subject => val id: UUID = subject.putSession(Some(SessionCreation(name = Some("MySession")))).futureValue val payload = """ { "name" : "test" } """.getBytes("UTF-8") subject.putKeyValuePair("my/key", payload, Some(AcquireSession(id))).futureValue should be(true) @@ -127,14 +127,14 @@ class AkkaHttpConsulClientIntegrationTest extends FlatSpec with Matchers with Sc subject.putKeyValuePair("my/key", payload, Some(ReleaseSession(id))).futureValue should be(true) } - it should "write a key/value pair and read it back" in withConsulHttpClient { subject ⇒ + it should "write a key/value pair and read it back" in withConsulHttpClient { subject => val payload = """ { "name" : "test" } """.getBytes("UTF-8") subject.putKeyValuePair("my/key", payload).futureValue should be(true) val keyDataSeq = subject.getKeyValuePair("my/key").futureValue keyDataSeq.head.value should equal(BinaryData(payload)) } - it should "fail when aquiring a lock on a key with a non-existent session" in withConsulHttpClient { subject ⇒ + it should "fail when aquiring a lock on a key with a non-existent session" in withConsulHttpClient { subject => val payload = """ { "name" : "test" } """.getBytes("UTF-8") val nonExistentSessionId = UUID.fromString("9A3BB9C-E2E7-43DF-BFD5-845417146552") val result = subject.putKeyValuePair("my/key", payload, Some(AcquireSession(nonExistentSessionId))).futureValue should be(false) diff --git a/client/src/it/scala/stormlantern/consul/client/util/ConsulDockerContainer.scala b/client/src/it/scala/stormlantern/consul/client/util/ConsulDockerContainer.scala index c20c1b0..9bf9c42 100644 --- a/client/src/it/scala/stormlantern/consul/client/util/ConsulDockerContainer.scala +++ b/client/src/it/scala/stormlantern/consul/client/util/ConsulDockerContainer.scala @@ -7,11 +7,11 @@ import stormlantern.dockertestkit.{ DockerClientProvider, DockerContainer } import scala.collection.JavaConversions._ -trait ConsulDockerContainer extends DockerContainer { this: Suite ⇒ +trait ConsulDockerContainer extends DockerContainer { this: Suite => def image: String = "progrium/consul" def command: Seq[String] = Seq("-server", "-bootstrap", DockerClientProvider.hostname) override def containerConfig = ContainerConfig.builder().image(image).hostConfig(hostConfig).cmd(command).build() - def withConsulHost[T](f: (String, Int) ⇒ T): T = super.withDockerHost("8500/tcp")(f) + def withConsulHost[T](f: (String, Int) => T): T = super.withDockerHost("8500/tcp")(f) } diff --git a/client/src/it/scala/stormlantern/consul/client/util/ConsulRegistratorDockerContainer.scala b/client/src/it/scala/stormlantern/consul/client/util/ConsulRegistratorDockerContainer.scala index 16fabc8..95221ca 100644 --- a/client/src/it/scala/stormlantern/consul/client/util/ConsulRegistratorDockerContainer.scala +++ b/client/src/it/scala/stormlantern/consul/client/util/ConsulRegistratorDockerContainer.scala @@ -6,7 +6,7 @@ import stormlantern.dockertestkit.{ DockerClientProvider, DockerContainers } import scala.collection.JavaConversions._ -trait ConsulRegistratorDockerContainer extends DockerContainers { this: Suite ⇒ +trait ConsulRegistratorDockerContainer extends DockerContainers { this: Suite => def consulContainerConfig = { val image: String = "progrium/consul" @@ -24,7 +24,7 @@ trait ConsulRegistratorDockerContainer extends DockerContainers { this: Suite override def containerConfigs = Set(consulContainerConfig, registratorContainerConfig) - def withConsulHost[T](f: (String, Int) ⇒ T): T = super.withDockerHosts(Set("8500/tcp")) { pb ⇒ + def withConsulHost[T](f: (String, Int) => T): T = super.withDockerHosts(Set("8500/tcp")) { pb => val (h, p) = pb("8500/tcp") f(h, p) } diff --git a/client/src/it/scala/stormlantern/consul/client/util/TestActorSystem.scala b/client/src/it/scala/stormlantern/consul/client/util/TestActorSystem.scala index 1562afd..24c6afa 100644 --- a/client/src/it/scala/stormlantern/consul/client/util/TestActorSystem.scala +++ b/client/src/it/scala/stormlantern/consul/client/util/TestActorSystem.scala @@ -3,7 +3,7 @@ package stormlantern.consul.client.util import akka.actor.ActorSystem trait TestActorSystem { - def withActorSystem[T](f: ActorSystem ⇒ T): T = { + def withActorSystem[T](f: ActorSystem => T): T = { val actorSystem = ActorSystem("test") try { f(actorSystem) diff --git a/client/src/main/scala/stormlantern/consul/client/loadbalancers/LoadBalancerActor.scala b/client/src/main/scala/stormlantern/consul/client/loadbalancers/LoadBalancerActor.scala index 4f54f5c..bc0d1d0 100644 --- a/client/src/main/scala/stormlantern/consul/client/loadbalancers/LoadBalancerActor.scala +++ b/client/src/main/scala/stormlantern/consul/client/loadbalancers/LoadBalancerActor.scala @@ -34,7 +34,7 @@ class LoadBalancerActor(loadBalancer: LoadBalancer, key: String) extends Actor w } def selectConnection: Option[(String, ConnectionProvider)] = - loadBalancer.selectConnection.flatMap(id ⇒ connectionProviders.get(id).map(id → _)) + loadBalancer.selectConnection.flatMap(id ⇒ connectionProviders.get(id).map(id -> _)) def returnConnection(connection: ConnectionHolder): Unit = { connectionProviders.get(connection.id).foreach(_.returnConnection(connection)) diff --git a/client/src/test/scala/stormlantern/consul/client/loadbalancers/LoadBalancerActorSpec.scala b/client/src/test/scala/stormlantern/consul/client/loadbalancers/LoadBalancerActorSpec.scala index 005b773..ef6671c 100644 --- a/client/src/test/scala/stormlantern/consul/client/loadbalancers/LoadBalancerActorSpec.scala +++ b/client/src/test/scala/stormlantern/consul/client/loadbalancers/LoadBalancerActorSpec.scala @@ -70,7 +70,7 @@ class LoadBalancerActorSpec(_system: ActorSystem) extends TestKit(_system) with (connectionProvider.destroy _).expects() val sut = TestActorRef(new LoadBalancerActor(loadBalancer, "service1")) sut ! LoadBalancerActor.AddConnectionProvider(instanceKey, connectionProvider) - sut.underlyingActor.connectionProviders should contain(instanceKey → connectionProvider) + sut.underlyingActor.connectionProviders should contain(instanceKey -> connectionProvider) sut.stop() } @@ -81,7 +81,7 @@ class LoadBalancerActorSpec(_system: ActorSystem) extends TestKit(_system) with val sut = TestActorRef(new LoadBalancerActor(loadBalancer, "service1")) sut.underlyingActor.connectionProviders.put(instanceKey, connectionProvider) sut ! LoadBalancerActor.RemoveConnectionProvider(instanceKey) - sut.underlyingActor.connectionProviders should not contain (instanceKey → connectionProvider) + sut.underlyingActor.connectionProviders should not contain (instanceKey -> connectionProvider) } it should "return true when it has at least one available connection provider for the service" in new TestScope { diff --git a/docker-testkit/src/main/scala/stormlantern/dockertestkit/DockerClientProvider.scala b/docker-testkit/src/main/scala/stormlantern/dockertestkit/DockerClientProvider.scala index c66ee72..0837790 100644 --- a/docker-testkit/src/main/scala/stormlantern/dockertestkit/DockerClientProvider.scala +++ b/docker-testkit/src/main/scala/stormlantern/dockertestkit/DockerClientProvider.scala @@ -14,8 +14,8 @@ object DockerClientProvider { lazy val hostname: String = { val uri = new URI(sys.env.getOrElse("DOCKER_HOST", "unix:///var/run/docker.sock")) uri.getScheme match { - case "tcp" ⇒ uri.getHost - case "unix" ⇒ "localhost" + case "tcp" => uri.getHost + case "unix" => "localhost" } } diff --git a/example/src/main/scala/stormlantern/consul/example/Boot.scala b/example/src/main/scala/stormlantern/consul/example/Boot.scala index 950844d..a11b2dc 100644 --- a/example/src/main/scala/stormlantern/consul/example/Boot.scala +++ b/example/src/main/scala/stormlantern/consul/example/Boot.scala @@ -26,7 +26,7 @@ object Boot extends App { IO(Http) ? Http.Bind(service, interface = "0.0.0.0", port = 8080) - def connectionProviderFactory = (host: String, port: Int) ⇒ new ConnectionProvider { + def connectionProviderFactory = (host: String, port: Int) => new ConnectionProvider { val client = new SprayExampleServiceClient(new URL(s"http://$host:$port")) override def getConnection: Future[Any] = Future.successful(client) } @@ -37,10 +37,10 @@ object Boot extends App { val serviceBroker = ServiceBroker(DNS.lookup("consul-8500.service.consul"), services) system.scheduler.schedule(5.seconds, 5.seconds) { - serviceBroker.withService("example-service-1") { client: SprayExampleServiceClient ⇒ + serviceBroker.withService("example-service-1") { client: SprayExampleServiceClient => client.identify }.foreach(println) - serviceBroker.withService("example-service-2") { client: SprayExampleServiceClient ⇒ + serviceBroker.withService("example-service-2") { client: SprayExampleServiceClient => client.identify }.foreach(println) } From 3f46be94655b6f053e3662341808138320c3bc1b Mon Sep 17 00:00:00 2001 From: Leonard Wolters Date: Thu, 2 Nov 2023 16:24:17 +0100 Subject: [PATCH 19/33] Cleaning up project, upgrading project to latest standards --- .scalafmt.conf | 28 +++ .travis.yml | 7 - build.sbt | 210 ++++++------------ client/src/it/resources/application.conf | 4 +- .../client/ServiceBrokerIntegrationTest.scala | 2 +- .../AkkaHttpConsulClientIntegrationTest.scala | 2 +- .../consul/client/util/TestActorSystem.scala | 2 +- client/src/main/resources/reference.conf | 4 +- .../consul/client/ServiceBroker.scala | 8 +- .../consul/client/ServiceBrokerActor.scala | 10 +- .../dao/akka/AkkaHttpConsulClient.scala | 10 +- .../client/discovery/ConnectionHolder.scala | 2 +- .../client/discovery/ConnectionProvider.scala | 2 +- .../client/discovery/ConnectionStrategy.scala | 2 +- .../discovery/ServiceAvailabilityActor.scala | 4 +- .../client/election/LeaderFollowerActor.scala | 2 +- .../loadbalancers/LoadBalancerActor.scala | 6 +- .../consul/client/session/SessionActor.scala | 2 +- .../consul/client/util/RetryPolicy.scala | 4 +- client/src/test/resources/application.conf | 4 +- .../client/ServiceBrokerActorSpec.scala | 6 +- .../consul/client/ServiceBrokerSpec.scala | 6 +- .../ServiceAvailabilityActorSpec.scala | 4 +- .../election/LeaderFollowerActorSpec.scala | 4 +- .../loadbalancers/LoadBalancerActorSpec.scala | 6 +- example/README.md | 10 - example/docker-compose.yml | 51 ----- example/src/main/resources/application.conf | 10 - example/src/main/resources/assets/app.js | 41 ---- example/src/main/resources/assets/index.html | 28 --- .../main/resources/assets/paper-full.min.js | 38 ---- example/src/main/resources/logback.xml | 14 -- .../stormlantern/consul/example/Boot.scala | 47 ---- .../ReactiveConsulHttpServiceActor.scala | 35 --- .../example/SprayExampleServiceClient.scala | 22 -- project/Config.scala | 25 +++ project/Dependencies.scala | 22 +- project/build.properties | 2 +- project/plugins.sbt | 13 +- 39 files changed, 176 insertions(+), 523 deletions(-) create mode 100644 .scalafmt.conf delete mode 100644 .travis.yml delete mode 100644 example/README.md delete mode 100644 example/docker-compose.yml delete mode 100644 example/src/main/resources/application.conf delete mode 100644 example/src/main/resources/assets/app.js delete mode 100644 example/src/main/resources/assets/index.html delete mode 100644 example/src/main/resources/assets/paper-full.min.js delete mode 100644 example/src/main/resources/logback.xml delete mode 100644 example/src/main/scala/stormlantern/consul/example/Boot.scala delete mode 100644 example/src/main/scala/stormlantern/consul/example/ReactiveConsulHttpServiceActor.scala delete mode 100644 example/src/main/scala/stormlantern/consul/example/SprayExampleServiceClient.scala create mode 100644 project/Config.scala diff --git a/.scalafmt.conf b/.scalafmt.conf new file mode 100644 index 0000000..46d822d --- /dev/null +++ b/.scalafmt.conf @@ -0,0 +1,28 @@ +style = defaultWithAlign +//@formatter on +align { + tokens = [ + {code = "=>", owner = "Case"} + {code = "extends", owner = "Defn.(Class|Trait|Object)"} + {code = "//", owner = ".*"} + {code = "{", owner = "Template"} + {code = "}", owner = "Template"} + {code = "%", owner = "Term.ApplyInfix"} + {code = "%%", owner = "Term.ApplyInfix"} + {code = "%%%", owner = "Term.ApplyInfix"} + {code = "⇒", owner = "Case"} + {code = "<-", owner = "Enumerator.Generator"} + {code = "←", owner = "Enumerator.Generator"} + {code = "->", owner = "Term.ApplyInfix"} + {code = "→", owner = "Term.ApplyInfix"} + {code = "=", owner = "(Enumerator.Val|Defn.(Va(l|r)|Def|Type))"} + ] +} +danglingParentheses = true +docstrings = JavaDoc +indentOperator = spray +maxColumn = 120 +rewrite.rules = [RedundantBraces, RedundantParens, SortImports] +unindentTopLevelOperators = true +project.git = true +newlines.alwaysBeforeTopLevelStatements = true \ No newline at end of file diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 7c33386..0000000 --- a/.travis.yml +++ /dev/null @@ -1,7 +0,0 @@ -language: scala -scala: - - 2.11.11 - - 2.12.4 -jdk: - - oraclejdk8 - diff --git a/build.sbt b/build.sbt index acec118..f08476b 100644 --- a/build.sbt +++ b/build.sbt @@ -1,155 +1,75 @@ import Dependencies._ -import sbt.Keys._ -import com.typesafe.sbt.SbtScalariform -import com.typesafe.sbt.pgp.PgpKeys -import scalariform.formatter.preferences._ -sonatypeProfileName := "com.crobox" -// Common variables -lazy val commonSettings = Seq( - scalaVersion := "2.13.1", - crossScalaVersions := Seq("2.12.11", "2.13.1"), - organization := "com.crobox", - resolvers ++= Dependencies.resolutionRepos, - scalacOptions ++= Seq("-unchecked", "-deprecation", "-feature") - ) +// Scala Formatting +ThisBuild / scalafmtVersion := "1.5.1" +ThisBuild / scalafmtOnCompile := false // all projects +ThisBuild / scalafmtTestOnCompile := false // all projects -lazy val reactiveConsul = (project in file(".")) - .settings( commonSettings: _* ) - .settings( publishSettings: _* ) - .aggregate(client, dnsHelper, dockerTestkit/*, example*/) +releaseCrossBuild := true +sonatypeProfileName := "com.crobox" -lazy val dnsHelper = (project in file("dns-helper")) - .settings( commonSettings: _* ) - .settings( publishSettings: _* ) +lazy val root = (project in file(".")) .settings( - name := "reactive-consul-dns", - publishArtifact in Compile := true, - publishArtifact in makePom := true, - publishArtifact in Test := false, - publishArtifact in IntegrationTest := false, - fork := true, - libraryDependencies ++= Seq( - spotifyDns - ) + publish := {}, + publishArtifact := false, + inThisBuild( + List( + organization := "com.crobox", + scalaVersion := "2.13.8", + crossScalaVersions := List("2.13.8"), + javacOptions ++= Seq("-g", "-Xlint:unchecked", "-Xlint:deprecation", "-source", "11", "-target", "11"), + scalacOptions ++= Seq("-unchecked", "-deprecation", "-feature", "-language:_", "-encoding", "UTF-8"), + publishTo := { + val nexus = "https://oss.sonatype.org/" + if (version.value.trim.endsWith("SNAPSHOT")) + Some("snapshots" at nexus + "content/repositories/snapshots") + else + Some("releases" at nexus + "service/local/staging/deploy/maven2") + }, + pomExtra := { + https://github.com/crobox/reactive-consul + + + MIT + https://opensource.org/licenses/MIT + repo + + + + git@github.com:crobox/reactive-consul.git + scm:git@github.com:crobox/reactive-consul.git + + + + crobox + crobox + https://github.com/crobox + + + dlouwers + Dirk Louwers + http://github.com/dlouwers + + + } + ) + ), + name := "reactive-consul" ) + .aggregate(client) -lazy val client = (project in file("client")) - .settings( commonSettings: _* ) - .settings( publishSettings: _* ) +lazy val client: Project = (project in file("client")) + .configs(Config.CustomIntegrationTest) + .settings(Config.testSettings: _*) .settings( - name := "reactive-consul", - publishArtifact in Compile := true, - publishArtifact in makePom := true, - publishArtifact in Test := false, - publishArtifact in IntegrationTest := false, - fork := true, + name := "client", + sbtrelease.ReleasePlugin.autoImport.releasePublishArtifactsAction := PgpKeys.publishSigned.value, libraryDependencies ++= Seq( - akkaHttp, - akkaHttpSprayJson, - akkaActor, - akkaStream, - slf4j, - akkaSlf4j, - scalaTest % "it,test", - scalaMock % "test", - logback % "it,test", - akkaTestKit % "it,test", - spotifyDocker % "it,test" - ) - ) - .configs( IntegrationTest ) - .settings( Defaults.itSettings : _* ) - .settings( SbtScalariform.scalariformSettingsWithIt : _* ) - .dependsOn(dockerTestkit % "it-internal") - -lazy val dockerTestkit = (project in file("docker-testkit")) - .settings( commonSettings: _* ) - .settings( - libraryDependencies ++= Seq( - slf4j, - scalaTest, - spotifyDocker - ) - ) - .configs( IntegrationTest ) - .settings( Defaults.itSettings : _* ) - .settings( SbtScalariform.scalariformSettingsWithIt : _* ) - .settings( publishSettings: _* ) - - -//lazy val example = (project in file("example")) -// .aggregate(client) -// .dependsOn(client, dnsHelper) -// .settings( commonSettings: _* ) -// .settings( -// crossScalaVersions := Seq() -// ) -// .settings( -// libraryDependencies ++= Seq( -// sprayClient, -// sprayRouting, -// sprayJson, -// slf4j, -// logback -// ) -// ) -// .settings( -// fork := true, -// libraryDependencies ++= Seq( -// akkaActor, -// sprayClient, -// sprayJson -// ) -// ) -// .settings( publishSettings: _* ) -// .enablePlugins(JavaAppPackaging) -// .settings( -// packageName in Docker := "reactive-consul-example", -// maintainer in Docker := "Dirk Louwers ", -// dockerExposedPorts in Docker := Seq(8080), -// dockerExposedVolumes in Docker := Seq("/opt/docker/logs") -// ) - -lazy val publishSettings = Seq( - publishArtifact := false, - publishMavenStyle := true, - pomIncludeRepository := { _ => false }, - sbtrelease.ReleasePlugin.autoImport.releasePublishArtifactsAction := PgpKeys.publishSigned.value, - publishTo := { - val nexus = "https://oss.sonatype.org/" - if (isSnapshot.value) - Some("snapshots" at nexus + "content/repositories/snapshots") - else - Some("releases" at nexus + "service/local/staging/deploy/maven2") - }, - pomExtra := ( - http://github.com/crobox/reactive-consul - - - MIT - https://opensource.org/licenses/MIT - repo - - - - git@github.com:crobox/reactive-consul.git - scm:git@github.com:crobox/reactive-consul.git - - - - sjoerdmulder - Sjoerd Mulder - http://github.com/sjoerdmulder - - - dlouwers - Dirk Louwers - http://github.com/dlouwers - - - ) -) - -Revolver.settings.settings + "org.apache.pekko" %% "pekko-actor" % Dependencies.PekkoVersion, + "org.apache.pekko" %% "pekko-stream" % Dependencies.PekkoVersion, + "org.apache.pekko" %% "pekko-http" % Dependencies.PekkoHttpVersion, + "com.typesafe.scala-logging" %% "scala-logging" % "3.9.5", + "joda-time" % "joda-time" % "2.12.5" + ) ++ Seq("org.apache.pekko" %% "pekko-testkit" % Dependencies.PekkoVersion % Test) ++ Dependencies.testDependencies.map(_ % Test) + ) \ No newline at end of file diff --git a/client/src/it/resources/application.conf b/client/src/it/resources/application.conf index b546707..bd255d6 100644 --- a/client/src/it/resources/application.conf +++ b/client/src/it/resources/application.conf @@ -1,6 +1,6 @@ akka { - loggers = ["akka.event.slf4j.Slf4jLogger"] + loggers = ["org.apache.pekko.event.slf4j.Slf4jLogger"] loglevel = "INFO" - logging-filter = "akka.event.slf4j.Slf4jLoggingFilter" + logging-filter = "org.apache.pekko.event.slf4j.Slf4jLoggingFilter" log-dead-letters-during-shutdown = off } \ No newline at end of file diff --git a/client/src/it/scala/stormlantern/consul/client/ServiceBrokerIntegrationTest.scala b/client/src/it/scala/stormlantern/consul/client/ServiceBrokerIntegrationTest.scala index ef1e283..1d624bc 100644 --- a/client/src/it/scala/stormlantern/consul/client/ServiceBrokerIntegrationTest.scala +++ b/client/src/it/scala/stormlantern/consul/client/ServiceBrokerIntegrationTest.scala @@ -4,7 +4,7 @@ import java.net.URL import org.scalatest._ import org.scalatest.concurrent.{ Eventually, IntegrationPatience, ScalaFutures } -import stormlantern.consul.client.dao.akka.AkkaHttpConsulClient +import stormlantern.consul.client.dao.org.apache.pekko.AkkaHttpConsulClient import stormlantern.consul.client.dao.{ ConsulHttpClient, ServiceRegistration } import stormlantern.consul.client.discovery.{ ConnectionProvider, ConnectionProviderFactory, ConnectionStrategy, ServiceDefinition } import stormlantern.consul.client.loadbalancers.RoundRobinLoadBalancer diff --git a/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIntegrationTest.scala b/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIntegrationTest.scala index f57dfb7..c33c84a 100644 --- a/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIntegrationTest.scala +++ b/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIntegrationTest.scala @@ -5,7 +5,7 @@ import java.util.UUID import org.scalatest._ import org.scalatest.concurrent.{ Eventually, IntegrationPatience, ScalaFutures } -import stormlantern.consul.client.dao.akka.AkkaHttpConsulClient +import stormlantern.consul.client.dao.org.apache.pekko.AkkaHttpConsulClient import stormlantern.consul.client.util.{ ConsulDockerContainer, Logging, RetryPolicy, TestActorSystem } class AkkaHttpConsulClientIntegrationTest extends FlatSpec with Matchers with ScalaFutures with Eventually with IntegrationPatience with ConsulDockerContainer with TestActorSystem with RetryPolicy with Logging { diff --git a/client/src/it/scala/stormlantern/consul/client/util/TestActorSystem.scala b/client/src/it/scala/stormlantern/consul/client/util/TestActorSystem.scala index 24c6afa..239af0f 100644 --- a/client/src/it/scala/stormlantern/consul/client/util/TestActorSystem.scala +++ b/client/src/it/scala/stormlantern/consul/client/util/TestActorSystem.scala @@ -1,6 +1,6 @@ package stormlantern.consul.client.util -import akka.actor.ActorSystem +import org.apache.pekko.actor.ActorSystem trait TestActorSystem { def withActorSystem[T](f: ActorSystem => T): T = { diff --git a/client/src/main/resources/reference.conf b/client/src/main/resources/reference.conf index 23e7636..28dcbae 100644 --- a/client/src/main/resources/reference.conf +++ b/client/src/main/resources/reference.conf @@ -1,5 +1,5 @@ akka { - loggers = ["akka.event.slf4j.Slf4jLogger"] + loggers = ["org.apache.pekko.event.slf4j.Slf4jLogger"] loglevel = "DEBUG" - logging-filter = "akka.event.slf4j.Slf4jLoggingFilter" + logging-filter = "org.apache.pekko.event.slf4j.Slf4jLoggingFilter" } \ No newline at end of file diff --git a/client/src/main/scala/stormlantern/consul/client/ServiceBroker.scala b/client/src/main/scala/stormlantern/consul/client/ServiceBroker.scala index ac82baf..dbd20ff 100644 --- a/client/src/main/scala/stormlantern/consul/client/ServiceBroker.scala +++ b/client/src/main/scala/stormlantern/consul/client/ServiceBroker.scala @@ -5,12 +5,12 @@ import java.net.URL import scala.concurrent.duration._ import scala.concurrent._ -import akka.actor._ -import akka.util.Timeout -import akka.pattern.ask +import org.apache.pekko.actor._ +import org.apache.pekko.util.Timeout +import org.apache.pekko.pattern.ask import stormlantern.consul.client.dao._ -import stormlantern.consul.client.dao.akka.AkkaHttpConsulClient +import stormlantern.consul.client.dao.org.apache.pekko.AkkaHttpConsulClient import stormlantern.consul.client.discovery._ import stormlantern.consul.client.election.LeaderInfo import stormlantern.consul.client.loadbalancers.LoadBalancerActor diff --git a/client/src/main/scala/stormlantern/consul/client/ServiceBrokerActor.scala b/client/src/main/scala/stormlantern/consul/client/ServiceBrokerActor.scala index 75b8f87..a9bcf93 100644 --- a/client/src/main/scala/stormlantern/consul/client/ServiceBrokerActor.scala +++ b/client/src/main/scala/stormlantern/consul/client/ServiceBrokerActor.scala @@ -2,9 +2,9 @@ package stormlantern.consul.client import java.util.UUID -import akka.actor.Status.Failure -import akka.actor._ -import akka.util.Timeout +import org.apache.pekko.actor.Status.Failure +import org.apache.pekko.actor._ +import org.apache.pekko.util.Timeout import stormlantern.consul.client.dao.ServiceInstance import stormlantern.consul.client.discovery.{ ConnectionStrategy, ServiceAvailabilityActor, ServiceDefinition } import stormlantern.consul.client.loadbalancers.LoadBalancerActor @@ -71,7 +71,7 @@ class ServiceBrokerActor( sender ! false } case AllConnectionProvidersAvailable ⇒ - import akka.pattern.pipe + import org.apache.pekko.pattern.pipe queryConnectionProviderAvailability pipeTo sender case JoinElection(key) ⇒ } @@ -93,7 +93,7 @@ class ServiceBrokerActor( def queryConnectionProviderAvailability: Future[Boolean] = { implicit val timeout: Timeout = 1.second - import akka.pattern.ask + import org.apache.pekko.pattern.ask Future.sequence(loadbalancers.values.map(_.ask(LoadBalancerActor.HasAvailableConnectionProvider).mapTo[Boolean])) .map(_.forall(p ⇒ p)) } diff --git a/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala b/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala index e34874d..031b261 100644 --- a/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala +++ b/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala @@ -3,11 +3,11 @@ package stormlantern.consul.client.dao.akka import java.net.URL import java.util.UUID -import akka.actor.{ ActorSystem, Scheduler } -import akka.http.scaladsl.Http -import akka.http.scaladsl.model.{ HttpHeader, StatusCode, _ } -import akka.stream.{ ActorMaterializer, Materializer } -import akka.util.ByteString +import org.apache.pekko.actor.{ ActorSystem, Scheduler } +import org.apache.pekko.http.scaladsl.Http +import org.apache.pekko.http.scaladsl.model.{ HttpHeader, StatusCode, _ } +import org.apache.pekko.stream.{ ActorMaterializer, Materializer } +import org.apache.pekko.util.ByteString import spray.json._ import stormlantern.consul.client.dao._ import stormlantern.consul.client.util.{ Logging, RetryPolicy } diff --git a/client/src/main/scala/stormlantern/consul/client/discovery/ConnectionHolder.scala b/client/src/main/scala/stormlantern/consul/client/discovery/ConnectionHolder.scala index 95ef8ed..af0678e 100644 --- a/client/src/main/scala/stormlantern/consul/client/discovery/ConnectionHolder.scala +++ b/client/src/main/scala/stormlantern/consul/client/discovery/ConnectionHolder.scala @@ -1,6 +1,6 @@ package stormlantern.consul.client.discovery -import akka.actor.ActorRef +import org.apache.pekko.actor.ActorRef import scala.concurrent.Future diff --git a/client/src/main/scala/stormlantern/consul/client/discovery/ConnectionProvider.scala b/client/src/main/scala/stormlantern/consul/client/discovery/ConnectionProvider.scala index 617ab7f..64d8730 100644 --- a/client/src/main/scala/stormlantern/consul/client/discovery/ConnectionProvider.scala +++ b/client/src/main/scala/stormlantern/consul/client/discovery/ConnectionProvider.scala @@ -1,6 +1,6 @@ package stormlantern.consul.client.discovery -import akka.actor.ActorRef +import org.apache.pekko.actor.ActorRef import scala.concurrent.Future import scala.concurrent.ExecutionContext.Implicits.global diff --git a/client/src/main/scala/stormlantern/consul/client/discovery/ConnectionStrategy.scala b/client/src/main/scala/stormlantern/consul/client/discovery/ConnectionStrategy.scala index 4931559..626afcc 100644 --- a/client/src/main/scala/stormlantern/consul/client/discovery/ConnectionStrategy.scala +++ b/client/src/main/scala/stormlantern/consul/client/discovery/ConnectionStrategy.scala @@ -1,6 +1,6 @@ package stormlantern.consul.client.discovery -import akka.actor.{ ActorRef, ActorRefFactory } +import org.apache.pekko.actor.{ ActorRef, ActorRefFactory } import stormlantern.consul.client.loadbalancers.{ LoadBalancer, LoadBalancerActor, RoundRobinLoadBalancer } case class ServiceDefinition(key: String, serviceName: String, serviceTags: Set[String] = Set.empty, dataCenter: Option[String] = None) diff --git a/client/src/main/scala/stormlantern/consul/client/discovery/ServiceAvailabilityActor.scala b/client/src/main/scala/stormlantern/consul/client/discovery/ServiceAvailabilityActor.scala index 27ce6a5..c0d74ac 100644 --- a/client/src/main/scala/stormlantern/consul/client/discovery/ServiceAvailabilityActor.scala +++ b/client/src/main/scala/stormlantern/consul/client/discovery/ServiceAvailabilityActor.scala @@ -2,8 +2,8 @@ package stormlantern.consul.client package discovery import scala.concurrent.{ ExecutionContext, Future } -import akka.actor._ -import akka.pattern.pipe +import org.apache.pekko.actor._ +import org.apache.pekko.pattern.pipe import dao._ import ServiceAvailabilityActor._ diff --git a/client/src/main/scala/stormlantern/consul/client/election/LeaderFollowerActor.scala b/client/src/main/scala/stormlantern/consul/client/election/LeaderFollowerActor.scala index ed1ab0d..ff43016 100644 --- a/client/src/main/scala/stormlantern/consul/client/election/LeaderFollowerActor.scala +++ b/client/src/main/scala/stormlantern/consul/client/election/LeaderFollowerActor.scala @@ -2,7 +2,7 @@ package stormlantern.consul.client.election import java.util.UUID -import akka.actor.{ Actor, Props } +import org.apache.pekko.actor.{ Actor, Props } import spray.json._ import stormlantern.consul.client.dao.{ AcquireSession, BinaryData, ConsulHttpClient, KeyData } import stormlantern.consul.client.election.LeaderFollowerActor._ diff --git a/client/src/main/scala/stormlantern/consul/client/loadbalancers/LoadBalancerActor.scala b/client/src/main/scala/stormlantern/consul/client/loadbalancers/LoadBalancerActor.scala index bc0d1d0..a79555b 100644 --- a/client/src/main/scala/stormlantern/consul/client/loadbalancers/LoadBalancerActor.scala +++ b/client/src/main/scala/stormlantern/consul/client/loadbalancers/LoadBalancerActor.scala @@ -1,7 +1,7 @@ package stormlantern.consul.client.loadbalancers -import akka.actor.Status.Failure -import akka.actor.{ Props, Actor, ActorLogging } +import org.apache.pekko.actor.Status.Failure +import org.apache.pekko.actor.{ Props, Actor, ActorLogging } import LoadBalancerActor._ import stormlantern.consul.client.discovery.{ ConnectionProvider, ConnectionHolder } import stormlantern.consul.client.ServiceUnavailableException @@ -10,7 +10,7 @@ import scala.collection.mutable class LoadBalancerActor(loadBalancer: LoadBalancer, key: String) extends Actor with ActorLogging { - import akka.pattern.pipe + import org.apache.pekko.pattern.pipe // Actor state val connectionProviders = mutable.Map.empty[String, ConnectionProvider] diff --git a/client/src/main/scala/stormlantern/consul/client/session/SessionActor.scala b/client/src/main/scala/stormlantern/consul/client/session/SessionActor.scala index c952ad9..13d15b9 100644 --- a/client/src/main/scala/stormlantern/consul/client/session/SessionActor.scala +++ b/client/src/main/scala/stormlantern/consul/client/session/SessionActor.scala @@ -2,7 +2,7 @@ package stormlantern.consul.client.session import java.util.UUID -import akka.actor.{ ActorRef, Props, Actor } +import org.apache.pekko.actor.{ ActorRef, Props, Actor } import stormlantern.consul.client.dao.ConsulHttpClient import stormlantern.consul.client.session.SessionActor.{ MonitorSession, SessionAcquired, StartSession } diff --git a/client/src/main/scala/stormlantern/consul/client/util/RetryPolicy.scala b/client/src/main/scala/stormlantern/consul/client/util/RetryPolicy.scala index 71e8b6f..edf19b6 100644 --- a/client/src/main/scala/stormlantern/consul/client/util/RetryPolicy.scala +++ b/client/src/main/scala/stormlantern/consul/client/util/RetryPolicy.scala @@ -4,8 +4,8 @@ package util import scala.concurrent._ import scala.concurrent.duration._ -import akka.actor.Scheduler -import akka.pattern.after +import org.apache.pekko.actor.Scheduler +import org.apache.pekko.pattern.after trait RetryPolicy { def maxRetries = 4 diff --git a/client/src/test/resources/application.conf b/client/src/test/resources/application.conf index 293ad13..2726de1 100644 --- a/client/src/test/resources/application.conf +++ b/client/src/test/resources/application.conf @@ -1,8 +1,8 @@ akka { - loggers = ["akka.event.slf4j.Slf4jLogger"] + loggers = ["org.apache.pekko.event.slf4j.Slf4jLogger"] loglevel = "INFO" logger-startup-timeout = 30s - logging-filter = "akka.event.slf4j.Slf4jLoggingFilter" + logging-filter = "org.apache.pekko.event.slf4j.Slf4jLoggingFilter" log-dead-letters-during-shutdown = off log-dead-letters = off } diff --git a/client/src/test/scala/stormlantern/consul/client/ServiceBrokerActorSpec.scala b/client/src/test/scala/stormlantern/consul/client/ServiceBrokerActorSpec.scala index 1b49450..08e7840 100644 --- a/client/src/test/scala/stormlantern/consul/client/ServiceBrokerActorSpec.scala +++ b/client/src/test/scala/stormlantern/consul/client/ServiceBrokerActorSpec.scala @@ -1,8 +1,8 @@ package stormlantern.consul.client -import akka.actor.Status.Failure -import akka.actor._ -import akka.testkit.{ ImplicitSender, TestActorRef, TestKit, TestProbe } +import org.apache.pekko.actor.Status.Failure +import org.apache.pekko.actor._ +import org.apache.pekko.testkit.{ ImplicitSender, TestActorRef, TestKit, TestProbe } import org.scalamock.scalatest.MockFactory import org.scalatest.{ BeforeAndAfterAll, FlatSpecLike, Matchers } import stormlantern.consul.client.dao.{ ConsulHttpClient, ServiceInstance } diff --git a/client/src/test/scala/stormlantern/consul/client/ServiceBrokerSpec.scala b/client/src/test/scala/stormlantern/consul/client/ServiceBrokerSpec.scala index ed94276..fd816f8 100644 --- a/client/src/test/scala/stormlantern/consul/client/ServiceBrokerSpec.scala +++ b/client/src/test/scala/stormlantern/consul/client/ServiceBrokerSpec.scala @@ -1,8 +1,8 @@ package stormlantern.consul.client -import akka.actor.{ ActorRef, ActorSystem } -import akka.actor.Status.Failure -import akka.testkit.{ ImplicitSender, TestKit } +import org.apache.pekko.actor.{ ActorRef, ActorSystem } +import org.apache.pekko.actor.Status.Failure +import org.apache.pekko.testkit.{ ImplicitSender, TestKit } import org.scalamock.scalatest.MockFactory import org.scalatest.concurrent.ScalaFutures import org.scalatest.{ BeforeAndAfterAll, FlatSpecLike, Matchers } diff --git a/client/src/test/scala/stormlantern/consul/client/discovery/ServiceAvailabilityActorSpec.scala b/client/src/test/scala/stormlantern/consul/client/discovery/ServiceAvailabilityActorSpec.scala index 296709d..bf56281 100644 --- a/client/src/test/scala/stormlantern/consul/client/discovery/ServiceAvailabilityActorSpec.scala +++ b/client/src/test/scala/stormlantern/consul/client/discovery/ServiceAvailabilityActorSpec.scala @@ -1,7 +1,7 @@ package stormlantern.consul.client.discovery -import akka.actor.ActorSystem -import akka.testkit.{ ImplicitSender, TestActorRef, TestKit } +import org.apache.pekko.actor.ActorSystem +import org.apache.pekko.testkit.{ ImplicitSender, TestActorRef, TestKit } import org.scalamock.scalatest.MockFactory import org.scalatest.{ BeforeAndAfterAll, FlatSpecLike, Matchers } import stormlantern.consul.client.dao.{ ConsulHttpClient, IndexedServiceInstances } diff --git a/client/src/test/scala/stormlantern/consul/client/election/LeaderFollowerActorSpec.scala b/client/src/test/scala/stormlantern/consul/client/election/LeaderFollowerActorSpec.scala index 76e55c5..038b5f1 100644 --- a/client/src/test/scala/stormlantern/consul/client/election/LeaderFollowerActorSpec.scala +++ b/client/src/test/scala/stormlantern/consul/client/election/LeaderFollowerActorSpec.scala @@ -3,8 +3,8 @@ package stormlantern.consul.client.election import java.util import java.util.UUID -import akka.actor.ActorSystem -import akka.testkit.{ TestActorRef, ImplicitSender, TestKit } +import org.apache.pekko.actor.ActorSystem +import org.apache.pekko.testkit.{ TestActorRef, ImplicitSender, TestKit } import org.scalamock.scalatest.MockFactory import org.scalatest.{ BeforeAndAfterAll, Matchers, FlatSpecLike } import stormlantern.consul.client.dao.{ BinaryData, KeyData, AcquireSession, ConsulHttpClient } diff --git a/client/src/test/scala/stormlantern/consul/client/loadbalancers/LoadBalancerActorSpec.scala b/client/src/test/scala/stormlantern/consul/client/loadbalancers/LoadBalancerActorSpec.scala index ef6671c..2d66d7f 100644 --- a/client/src/test/scala/stormlantern/consul/client/loadbalancers/LoadBalancerActorSpec.scala +++ b/client/src/test/scala/stormlantern/consul/client/loadbalancers/LoadBalancerActorSpec.scala @@ -1,8 +1,8 @@ package stormlantern.consul.client.loadbalancers -import akka.actor.ActorSystem -import akka.actor.Status.Failure -import akka.testkit.{ ImplicitSender, TestActorRef, TestKit } +import org.apache.pekko.actor.ActorSystem +import org.apache.pekko.actor.Status.Failure +import org.apache.pekko.testkit.{ ImplicitSender, TestActorRef, TestKit } import org.scalamock.scalatest.MockFactory import org.scalatest.{ BeforeAndAfterAll, FlatSpecLike, Matchers } import stormlantern.consul.client.ServiceUnavailableException diff --git a/example/README.md b/example/README.md deleted file mode 100644 index 2026a7b..0000000 --- a/example/README.md +++ /dev/null @@ -1,10 +0,0 @@ -# Running -Build and publish to local Docker repository: - - sbt compile - sbt stage - sbt docker:publishLocal - -Run the examle by running `docker-compose up` in the example directory. Add more server instances by running: - - docker run --rm -e SERVICE_NAME= -p 8080 --dns 172.17.42.1 -e INSTANCE_NAME= reactive-consul-example:0.1-SNAPSHOT diff --git a/example/docker-compose.yml b/example/docker-compose.yml deleted file mode 100644 index 38a451a..0000000 --- a/example/docker-compose.yml +++ /dev/null @@ -1,51 +0,0 @@ -# Make sure you have built the example application before running -# May have to run 'docker-compose rm' before 'docker-compose up' to make sure clean containers are used -consulserver: - image: "progrium/consul" - ports: - - "8400:8400" - - "8500:8500" - - "53:53/udp" - command: -server -bootstrap -ui-dir /ui -advertise 192.168.59.103 -registrator: - image: "progrium/registrator" - volumes: - - "/var/run/docker.sock:/tmp/docker.sock" - hostname: 192.168.59.103 - command: consul://192.168.59.103:8500 -example1i1: - image: "reactive-consul-example:0.1-SNAPSHOT" - ports: - - "8080" - environment: - - SERVICE_NAME=example-service-1 - - INSTANCE_NAME=Alvin - dns: - - 172.17.42.1 -example1i2: - image: "reactive-consul-example:0.1-SNAPSHOT" - ports: - - "8080" - environment: - - SERVICE_NAME=example-service-1 - - INSTANCE_NAME=Simon - dns: - - 172.17.42.1 -example2i1: - image: "reactive-consul-example:0.1-SNAPSHOT" - ports: - - "8080" - environment: - - SERVICE_NAME=example-service-2 - - INSTANCE_NAME=Theodore - dns: - - 172.17.42.1 -example2i2: - image: "reactive-consul-example:0.1-SNAPSHOT" - ports: - - "8080" - environment: - - SERVICE_NAME=example-service-2 - - INSTANCE_NAME=Chip - dns: - - 172.17.42.1 \ No newline at end of file diff --git a/example/src/main/resources/application.conf b/example/src/main/resources/application.conf deleted file mode 100644 index 5d8af36..0000000 --- a/example/src/main/resources/application.conf +++ /dev/null @@ -1,10 +0,0 @@ -akka { - loggers = ["akka.event.slf4j.Slf4jLogger"] - loglevel = "INFO" - logging-filter = "akka.event.slf4j.Slf4jLoggingFilter" - log-dead-letters-during-shutdown = off -} - -spray.can.server { - request-timeout = 1s -} \ No newline at end of file diff --git a/example/src/main/resources/assets/app.js b/example/src/main/resources/assets/app.js deleted file mode 100644 index 97d96b3..0000000 --- a/example/src/main/resources/assets/app.js +++ /dev/null @@ -1,41 +0,0 @@ - -var QuarterCircle = function(paper) { - var start = new paper.Point(0, 0); - var through = new paper.Point(Math.cos(Math.PI * 0.25), 1 - Math.sin(Math.PI * 0.25)); - var end = new paper.Point(1, 1); - var arc = new paper.Path.Arc(start, through, end); - arc.strokeColor = 'black'; - return arc; -}; - -var Box = function(paper) { - var topLeft = new paper.Point(0, 0); - var bottomRight = new paper.Point(1, 1); - var rect = new paper.Path.Rectangle(topLeft, bottomRight); - rect.strokeColor = 'black'; - rect.dashArray = [10, 4]; - return rect; -} - -var Scene = function(paper) { - var circle = QuarterCircle(paper); - var box = Box(paper); - return circle.join(box); -} - -var scene = Scene(paper); -var height = view.size.height - (view.size.height * 0.20); -scene.scale(height); -scene.position = view.center; -// Draw the view now: -var text = new PointText({ - point: [50, 50], - content: 'The contents of the point text', - fillColor: 'black', - fontFamily: 'Courier New', - fontWeight: 'bold', - fontSize: 25 -}); -text.content = 'And now something different'; -view.draw(); - diff --git a/example/src/main/resources/assets/index.html b/example/src/main/resources/assets/index.html deleted file mode 100644 index 68bea9a..0000000 --- a/example/src/main/resources/assets/index.html +++ /dev/null @@ -1,28 +0,0 @@ - - - - - Reactive Consul Demo - - - - - - - - - \ No newline at end of file diff --git a/example/src/main/resources/assets/paper-full.min.js b/example/src/main/resources/assets/paper-full.min.js deleted file mode 100644 index 11220a1..0000000 --- a/example/src/main/resources/assets/paper-full.min.js +++ /dev/null @@ -1,38 +0,0 @@ -/*! - * Paper.js v0.9.23 - The Swiss Army Knife of Vector Graphics Scripting. - * http://paperjs.org/ - * - * Copyright (c) 2011 - 2014, Juerg Lehni & Jonathan Puckey - * http://scratchdisk.com/ & http://jonathanpuckey.com/ - * - * Distributed under the MIT license. See LICENSE file for details. - * - * All rights reserved. - * - * Date: Tue Jul 7 11:47:32 2015 +0200 - * - *** - * - * Straps.js - Class inheritance library with support for bean-style accessors - * - * Copyright (c) 2006 - 2013 Juerg Lehni - * http://scratchdisk.com/ - * - * Distributed under the MIT license. - * - *** - * - * Acorn.js - * http://marijnhaverbeke.nl/acorn/ - * - * Acorn is a tiny, fast JavaScript parser written in JavaScript, - * created by Marijn Haverbeke and released under an MIT license. - * - */ -var paper=new function(t){var e=new function(){function n(t,n,i,r,a){function o(s,o){o=o||(o=u(n,s))&&(o.get?o:o.value),"string"==typeof o&&"#"===o[0]&&(o=t[o.substring(1)]||o);var l,d="function"==typeof o,f=o,_=a||d?o&&o.get?s in t:t[s]:null;a&&_||(d&&_&&(o.base=_),d&&r!==!1&&(l=s.match(/^([gs]et|is)(([A-Z])(.*))$/))&&(h[l[3].toLowerCase()+l[4]]=l[2]),f&&!d&&f.get&&"function"==typeof f.get&&e.isPlainObject(f)||(f={value:f,writable:!0}),(u(t,s)||{configurable:!0}).configurable&&(f.configurable=!0,f.enumerable=i),c(t,s,f))}var h={};if(n){for(var l in n)n.hasOwnProperty(l)&&!s.test(l)&&o(l);for(var l in h){var d=h[l],f=t["set"+d],_=t["get"+d]||f&&t["is"+d];!_||r!==!0&&0!==_.length||o(l,{get:_,set:f})}}return t}function i(t,e,n){return t&&("length"in t&&!t.getLength&&"number"==typeof t.length?a:o).call(t,e,n=n||t),n}function r(t,e,n){for(var i in e)!e.hasOwnProperty(i)||n&&n[i]||(t[i]=e[i]);return t}var s=/^(statics|enumerable|beans|preserve)$/,a=[].forEach||function(t,e){for(var n=0,i=this.length;i>n;n++)t.call(e,this[n],n,this)},o=function(t,e){for(var n in this)this.hasOwnProperty(n)&&t.call(e,this[n],n,this)},h=Object.create||function(t){return{__proto__:t}},u=Object.getOwnPropertyDescriptor||function(t,e){var n=t.__lookupGetter__&&t.__lookupGetter__(e);return n?{get:n,set:t.__lookupSetter__(e),enumerable:!0,configurable:!0}:t.hasOwnProperty(e)?{value:t[e],enumerable:!0,configurable:!0,writable:!0}:null},l=Object.defineProperty||function(t,e,n){return(n.get||n.set)&&t.__defineGetter__?(n.get&&t.__defineGetter__(e,n.get),n.set&&t.__defineSetter__(e,n.set)):t[e]=n.value,t},c=function(t,e,n){return delete t[e],l(t,e,n)};return n(function(){for(var t=0,e=arguments.length;e>t;t++)r(this,arguments[t])},{inject:function(t){if(t){var e=t.statics===!0?t:t.statics,i=t.beans,r=t.preserve;e!==t&&n(this.prototype,t,t.enumerable,i,r),n(this,e,!0,i,r)}for(var s=1,a=arguments.length;a>s;s++)this.inject(arguments[s]);return this},extend:function(){for(var t,e,i=this,r=0,s=arguments.length;s>r&&!(t=arguments[r].initialize);r++);return t=t||function(){i.apply(this,arguments)},e=t.prototype=h(this.prototype),c(e,"constructor",{value:t,writable:!0,configurable:!0}),n(t,this,!0),arguments.length&&this.inject.apply(t,arguments),t.base=i,t}},!0).inject({inject:function(){for(var t=0,e=arguments.length;e>t;t++){var i=arguments[t];i&&n(this,i,i.enumerable,i.beans,i.preserve)}return this},extend:function(){var t=h(this);return t.inject.apply(t,arguments)},each:function(t,e){return i(this,t,e)},set:function(t){return r(this,t)},clone:function(){return new this.constructor(this)},statics:{each:i,create:h,define:c,describe:u,set:r,clone:function(t){return r(new t.constructor,t)},isPlainObject:function(t){var n=null!=t&&t.constructor;return n&&(n===Object||n===e||"Object"===n.name)},pick:function(e,n){return e!==t?e:n}}})};"undefined"!=typeof module&&(module.exports=e),e.inject({toString:function(){return null!=this._id?(this._class||"Object")+(this._name?" '"+this._name+"'":" @"+this._id):"{ "+e.each(this,function(t,e){if(!/^_/.test(e)){var n=typeof t;this.push(e+": "+("number"===n?a.instance.number(t):"string"===n?"'"+t+"'":t))}},[]).join(", ")+" }"},getClassName:function(){return this._class||""},exportJSON:function(t){return e.exportJSON(this,t)},toJSON:function(){return e.serialize(this)},_set:function(n,i,r){if(n&&(r||e.isPlainObject(n))){var s=n._filtering||n;for(var a in s)if(s.hasOwnProperty(a)&&(!i||!i[a])){var o=n[a];o!==t&&(this[a]=o)}return!0}},statics:{exports:{enumerable:!0},extend:function st(){var t=st.base.apply(this,arguments),n=t.prototype._class;return n&&!e.exports[n]&&(e.exports[n]=t),t},equals:function(t,n){function i(t,e){for(var n in t)if(t.hasOwnProperty(n)&&!e.hasOwnProperty(n))return!1;return!0}if(t===n)return!0;if(t&&t.equals)return t.equals(n);if(n&&n.equals)return n.equals(t);if(Array.isArray(t)&&Array.isArray(n)){if(t.length!==n.length)return!1;for(var r=0,s=t.length;s>r;r++)if(!e.equals(t[r],n[r]))return!1;return!0}if(t&&"object"==typeof t&&n&&"object"==typeof n){if(!i(t,n)||!i(n,t))return!1;for(var r in t)if(t.hasOwnProperty(r)&&!e.equals(t[r],n[r]))return!1;return!0}return!1},read:function(n,i,r,s){if(this===e){var a=this.peek(n,i);return n.__index++,a}var o=this.prototype,h=o._readIndex,u=i||h&&n.__index||0;s||(s=n.length-u);var l=n[u];return l instanceof this||r&&r.readNull&&null==l&&1>=s?(h&&(n.__index=u+1),l&&r&&r.clone?l.clone():l):(l=e.create(this.prototype),h&&(l.__read=!0),l=l.initialize.apply(l,u>0||ss;s++)r.push(Array.isArray(i=t[s])?this.read(i,0,n):this.read(t,s,n,1));return r},readNamed:function(n,i,r,s,a){var o=this.getNamed(n,i),h=o!==t;if(h){var u=n._filtered;u||(u=n._filtered=e.create(n[0]),u._filtering=n[0]),u[i]=t}return this.read(h?[o]:n,r,s,a)},getNamed:function(n,i){var r=n[0];return n._hasObject===t&&(n._hasObject=1===n.length&&e.isPlainObject(r)),n._hasObject?i?r[i]:n._filtered||r:t},hasNamed:function(t,e){return!!this.getNamed(t,e)},isPlainValue:function(t,e){return this.isPlainObject(t)||Array.isArray(t)||e&&"string"==typeof t},serialize:function(t,n,i,r){n=n||{};var s,o=!r;if(o&&(n.formatter=new a(n.precision),r={length:0,definitions:{},references:{},add:function(t,e){var n="#"+t._id,i=this.references[n];if(!i){this.length++;var r=e.call(t),s=t._class;s&&r[0]!==s&&r.unshift(s),this.definitions[n]=r,i=this.references[n]=[n]}return i}}),t&&t._serialize){s=t._serialize(n,r);var h=t._class;!h||i||s._compact||s[0]===h||s.unshift(h)}else if(Array.isArray(t)){s=[];for(var u=0,l=t.length;l>u;u++)s[u]=e.serialize(t[u],n,i,r);i&&(s._compact=!0)}else if(e.isPlainObject(t)){s={};for(var u in t)t.hasOwnProperty(u)&&(s[u]=e.serialize(t[u],n,i,r))}else s="number"==typeof t?n.formatter.number(t,n.precision):t;return o&&r.length>0?[["dictionary",r.definitions],s]:s},deserialize:function(t,n,i,r){var s=t,a=!i;if(i=i||{},Array.isArray(t)){var o=t[0],h="dictionary"===o;if(1==t.length&&/^#/.test(o))return i.dictionary[o];o=e.exports[o],s=[],r&&(i.dictionary=s);for(var u=o?1:0,l=t.length;l>u;u++)s.push(e.deserialize(t[u],n,i,h));if(o){var c=s;n?s=n(o,c):(s=e.create(o.prototype),o.apply(s,c))}}else if(e.isPlainObject(t)){s={},r&&(i.dictionary=s);for(var d in t)s[d]=e.deserialize(t[d],n,i)}return a&&t&&t.length&&"dictionary"===t[0][0]?s[1]:s},exportJSON:function(t,n){var i=e.serialize(t,n);return n&&n.asString===!1?i:JSON.stringify(i)},importJSON:function(t,n){return e.deserialize("string"==typeof t?JSON.parse(t):t,function(t,i){var r=n&&n.constructor===t?n:e.create(t.prototype),s=r===n;if(1===i.length&&r instanceof x&&(s||!(r instanceof C))){var a=i[0];e.isPlainObject(a)&&(a.insert=!1)}return t.apply(r,i),s&&(n=null),r})},splice:function(e,n,i,r){var s=n&&n.length,a=i===t;i=a?e.length:i,i>e.length&&(i=e.length);for(var o=0;s>o;o++)n[o]._index=i+o;if(a)return e.push.apply(e,n),[];var h=[i,r];n&&h.push.apply(h,n);for(var u=e.splice.apply(e,h),o=0,l=u.length;l>o;o++)u[o]._index=t;for(var o=i+s,l=e.length;l>o;o++)e[o]._index=o;return u},capitalize:function(t){return t.replace(/\b[a-z]/g,function(t){return t.toUpperCase()})},camelize:function(t){return t.replace(/-(.)/g,function(t,e){return e.toUpperCase()})},hyphenate:function(t){return t.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase()}}});var n={on:function(t,n){if("string"!=typeof t)e.each(t,function(t,e){this.on(e,t)},this);else{var i=this._eventTypes,r=i&&i[t],s=this._callbacks=this._callbacks||{};s=s[t]=s[t]||[],-1===s.indexOf(n)&&(s.push(n),r&&r.install&&1==s.length&&r.install.call(this,t))}return this},off:function(n,i){if("string"!=typeof n)return e.each(n,function(t,e){this.off(e,t)},this),t;var r,s=this._eventTypes,a=s&&s[n],o=this._callbacks&&this._callbacks[n];return o&&(!i||-1!==(r=o.indexOf(i))&&1===o.length?(a&&a.uninstall&&a.uninstall.call(this,n),delete this._callbacks[n]):-1!==r&&o.splice(r,1)),this},once:function(t,e){return this.on(t,function(){e.apply(this,arguments),this.off(t,e)})},emit:function(t,e){var n=this._callbacks&&this._callbacks[t];if(!n)return!1;var i=[].slice.call(arguments,1);n=n.slice();for(var r=0,s=n.length;s>r;r++)if(n[r].apply(this,i)===!1){e&&e.stop&&e.stop();break}return!0},responds:function(t){return!(!this._callbacks||!this._callbacks[t])},attach:"#on",detach:"#off",fire:"#emit",_installEvents:function(t){var e=this._callbacks,n=t?"install":"uninstall";for(var i in e)if(e[i].length>0){var r=this._eventTypes,s=r&&r[i],a=s&&s[n];a&&a.call(this,i)}},statics:{inject:function at(t){var n=t._events;if(n){var i={};e.each(n,function(n,r){var s="string"==typeof n,a=s?n:r,o=e.capitalize(a),h=a.substring(2).toLowerCase();i[h]=s?{}:n,a="_"+a,t["get"+o]=function(){return this[a]},t["set"+o]=function(t){var e=this[a];e&&this.off(h,e),t&&this.on(h,t),this[a]=t}}),t._eventTypes=i}return at.base.apply(this,arguments)}}},r=e.extend({_class:"PaperScope",initialize:function ot(){paper=this,this.settings=new e({applyMatrix:!0,handleSize:4,hitTolerance:0}),this.project=null,this.projects=[],this.tools=[],this.palettes=[],this._id=ot._id++,ot._scopes[this._id]=this;var t=ot.prototype;if(!this.support){var n=et.getContext(1,1);t.support={nativeDash:"setLineDash"in n||"mozDash"in n,nativeBlendModes:nt.nativeModes},et.release(n)}if(!this.browser){var i=t.browser={};navigator.userAgent.toLowerCase().replace(/(opera|chrome|safari|webkit|firefox|msie|trident|atom)\/?\s*([.\d]+)(?:.*version\/([.\d]+))?(?:.*rv\:([.\d]+))?/g,function(t,e,n,r,s){if(!i.chrome){var a="opera"===e?r:n;"trident"===e&&(a=s,e="msie"),i.version=a,i.versionNumber=parseFloat(a),i.name=e,i[e]=!0}}),i.chrome&&delete i.webkit,i.atom&&delete i.chrome}},version:"0.9.23",getView:function(){return this.project&&this.project.getView()},getPaper:function(){return this},execute:function(t,e,n){paper.PaperScript.execute(t,this,e,n),W.updateFocus()},install:function(t){var n=this;e.each(["project","view","tool"],function(i){e.define(t,i,{configurable:!0,get:function(){return n[i]}})});for(var i in this)!/^_/.test(i)&&this[i]&&(t[i]=this[i])},setup:function(t){return paper=this,this.project=new y(t),this},activate:function(){paper=this},clear:function(){for(var t=this.projects.length-1;t>=0;t--)this.projects[t].remove();for(var t=this.tools.length-1;t>=0;t--)this.tools[t].remove();for(var t=this.palettes.length-1;t>=0;t--)this.palettes[t].remove()},remove:function(){this.clear(),delete r._scopes[this._id]},statics:new function(){function t(t){return t+="Attribute",function(e,n){return e[t](n)||e[t]("data-paper-"+n)}}return{_scopes:{},_id:0,get:function(t){return this._scopes[t]||null},getAttribute:t("get"),hasAttribute:t("has")}}}),s=e.extend(n,{initialize:function(t){this._scope=paper,this._index=this._scope[this._list].push(this)-1,(t||!this._scope[this._reference])&&this.activate()},activate:function(){if(!this._scope)return!1;var t=this._scope[this._reference];return t&&t!==this&&t.emit("deactivate"),this._scope[this._reference]=this,this.emit("activate",t),!0},isActive:function(){return this._scope[this._reference]===this},remove:function(){return null==this._index?!1:(e.splice(this._scope[this._list],null,this._index,1),this._scope[this._reference]==this&&(this._scope[this._reference]=null),this._scope=null,!0)}}),a=e.extend({initialize:function(t){this.precision=t||5,this.multiplier=Math.pow(10,this.precision)},number:function(t){return Math.round(t*this.multiplier)/this.multiplier},pair:function(t,e,n){return this.number(t)+(n||",")+this.number(e)},point:function(t,e){return this.number(t.x)+(e||",")+this.number(t.y)},size:function(t,e){return this.number(t.width)+(e||",")+this.number(t.height)},rectangle:function(t,e){return this.point(t,e)+(e||",")+this.size(t,e)}});a.instance=new a;var o=new function(){var t=[[.5773502691896257],[0,.7745966692414834],[.33998104358485626,.8611363115940526],[0,.5384693101056831,.906179845938664],[.2386191860831969,.6612093864662645,.932469514203152],[0,.4058451513773972,.7415311855993945,.9491079123427585],[.1834346424956498,.525532409916329,.7966664774136267,.9602898564975363],[0,.3242534234038089,.6133714327005904,.8360311073266358,.9681602395076261],[.14887433898163122,.4333953941292472,.6794095682990244,.8650633666889845,.9739065285171717],[0,.26954315595234496,.5190961292068118,.7301520055740494,.8870625997680953,.978228658146057],[.1252334085114689,.3678314989981802,.5873179542866175,.7699026741943047,.9041172563704749,.9815606342467192],[0,.2304583159551348,.44849275103644687,.6423493394403402,.8015780907333099,.9175983992229779,.9841830547185881],[.10805494870734367,.31911236892788974,.5152486363581541,.6872929048116855,.827201315069765,.9284348836635735,.9862838086968123],[0,.20119409399743451,.3941513470775634,.5709721726085388,.7244177313601701,.8482065834104272,.937273392400706,.9879925180204854],[.09501250983763744,.2816035507792589,.45801677765722737,.6178762444026438,.755404408355003,.8656312023878318,.9445750230732326,.9894009349916499]],e=[[1],[.8888888888888888,.5555555555555556],[.6521451548625461,.34785484513745385],[.5688888888888889,.47862867049936647,.23692688505618908],[.46791393457269104,.3607615730481386,.17132449237917036],[.4179591836734694,.3818300505051189,.27970539148927664,.1294849661688697],[.362683783378362,.31370664587788727,.22238103445337448,.10122853629037626],[.3302393550012598,.31234707704000286,.26061069640293544,.1806481606948574,.08127438836157441],[.29552422471475287,.26926671930999635,.21908636251598204,.1494513491505806,.06667134430868814],[.2729250867779006,.26280454451024665,.23319376459199048,.18629021092773426,.1255803694649046,.05566856711617366],[.24914704581340277,.2334925365383548,.20316742672306592,.16007832854334622,.10693932599531843,.04717533638651183],[.2325515532308739,.22628318026289723,.2078160475368885,.17814598076194574,.13887351021978725,.09212149983772845,.04048400476531588],[.2152638534631578,.2051984637212956,.18553839747793782,.15720316715819355,.12151857068790319,.08015808715976021,.03511946033175186],[.2025782419255613,.19843148532711158,.1861610000155622,.16626920581699392,.13957067792615432,.10715922046717194,.07036604748810812,.03075324199611727],[.1894506104550685,.18260341504492358,.16915651939500254,.14959598881657674,.12462897125553388,.09515851168249279,.062253523938647894,.027152459411754096]],n=Math.abs,i=Math.sqrt,r=Math.pow,s=1e-6,a=1e-12,h=1.12e-16;return{TOLERANCE:s,EPSILON:a,MACHINE_EPSILON:h,KAPPA:4*(i(2)-1)/3,isZero:function(t){return n(t)<=a},integrate:function(n,i,r,s){for(var a=t[s-2],o=e[s-2],h=.5*(r-i),u=h+i,l=0,c=s+1>>1,d=1&s?o[l++]*n(u):0;c>l;){var f=h*a[l];d+=o[l++]*(n(u+f)+n(u-f))}return h*d},findRoot:function(t,e,i,r,s,a,o){for(var h=0;a>h;h++){var u=t(i),l=u/e(i),c=i-l;if(n(l)0?(s=i,i=r>=c?.5*(r+s):c):(r=i,i=c>=s?.5*(r+s):c)}return i},solveQuadratic:function(t,e,r,s,a,o){var u,l,c=0,d=1/0,f=e;if(e/=2,l=e*e-t*r,0!==l&&n(l)g){var p=_(10,n(Math.floor(Math.log(g)*Math.LOG10E)));isFinite(p)||(p=0),t*=p,e*=p,r*=p,l=e*e-t*r}}if(n(t)=-h){l=0>l?0:l;var v=i(l);if(e>=h&&h>=e)u=n(t)>=n(r)?v/t:-r/v,d=-u;else{var m=-(e+(0>e?-1:1)*v);u=m/t,d=r/m}}return isFinite(u)&&(null==a||u>=a&&o>=u)&&(s[c++]=u),d!==u&&isFinite(d)&&(null==a||d>=a&&o>=d)&&(s[c++]=d),c},solveCubic:function(t,e,s,a,u,l,c){var d,f,_,g=0;if(0===t)t=e,f=s,_=a,d=1/0;else if(0===a)f=e,_=s,d=0;else{var p,v,m,y,w,x,b,C=1+h;if(d=-(e/t)/3,b=t*d,f=b+e,_=f*d+s,m=(b+f)*d+_,v=_*d+a,y=v/t,w=r(n(y),1/3),x=0>y?-1:1,y=-m/t,w=y>0?1.3247179572*Math.max(w,i(y)):w,p=d-x*w,p!==d){do if(d=p,b=t*d,f=b+e,_=f*d+s,m=(b+f)*d+_,v=_*d+a,p=0===m?d:d-v/m/C,p===d){d=p;break}while(x*p>x*d);n(t)*d*d>n(a/d)&&(_=-a/d,f=(_-s)/d)}}var g=o.solveQuadratic(t,f,_,u,l,c);return isFinite(d)&&(0===g||d!==u[g-1])&&(null==l||d>=l&&c>=d)&&(u[g++]=d),g}}},h={_id:1,_pools:{},get:function(t){if(t){var e=t._class,n=this._pools[e];return n||(n=this._pools[e]={_id:1}),n._id++}return this._id++}},u=e.extend({_class:"Point",_readIndex:!0,initialize:function(t,e){var n=typeof t;if("number"===n){var i="number"==typeof e;this.x=t,this.y=i?e:t,this.__read&&(this.__read=i?2:1)}else"undefined"===n||null===t?(this.x=this.y=0,this.__read&&(this.__read=null===t?1:0)):(Array.isArray(t)?(this.x=t[0],this.y=t.length>1?t[1]:t[0]):null!=t.x?(this.x=t.x,this.y=t.y):null!=t.width?(this.x=t.width,this.y=t.height):null!=t.angle?(this.x=t.length,this.y=0,this.setAngle(t.angle)):(this.x=this.y=0,this.__read&&(this.__read=0)),this.__read&&(this.__read=1))},set:function(t,e){return this.x=t,this.y=e,this},equals:function(t){return this===t||t&&(this.x===t.x&&this.y===t.y||Array.isArray(t)&&this.x===t[0]&&this.y===t[1])||!1},clone:function(){return new u(this.x,this.y)},toString:function(){var t=a.instance;return"{ x: "+t.number(this.x)+", y: "+t.number(this.y)+" }"},_serialize:function(t){var e=t.formatter;return[e.number(this.x),e.number(this.y)]},getLength:function(){return Math.sqrt(this.x*this.x+this.y*this.y)},setLength:function(t){if(this.isZero()){var e=this._angle||0;this.set(Math.cos(e)*t,Math.sin(e)*t)}else{var n=t/this.getLength();o.isZero(n)&&this.getAngle(),this.set(this.x*n,this.y*n)}},getAngle:function(){return 180*this.getAngleInRadians.apply(this,arguments)/Math.PI},setAngle:function(t){this.setAngleInRadians.call(this,t*Math.PI/180)},getAngleInDegrees:"#getAngle",setAngleInDegrees:"#setAngle",getAngleInRadians:function(){if(arguments.length){var t=u.read(arguments),e=this.getLength()*t.getLength();if(o.isZero(e))return NaN;var n=this.dot(t)/e;return Math.acos(-1>n?-1:n>1?1:n)}return this.isZero()?this._angle||0:this._angle=Math.atan2(this.y,this.x)},setAngleInRadians:function(t){if(this._angle=t,!this.isZero()){var e=this.getLength();this.set(Math.cos(t)*e,Math.sin(t)*e)}},getQuadrant:function(){return this.x>=0?this.y>=0?1:4:this.y>=0?2:3}},{beans:!1,getDirectedAngle:function(){var t=u.read(arguments);return 180*Math.atan2(this.cross(t),this.dot(t))/Math.PI},getDistance:function(){var t=u.read(arguments),n=t.x-this.x,i=t.y-this.y,r=n*n+i*i,s=e.read(arguments);return s?r:Math.sqrt(r)},normalize:function(e){e===t&&(e=1);var n=this.getLength(),i=0!==n?e/n:0,r=new u(this.x*i,this.y*i);return i>=0&&(r._angle=this._angle),r},rotate:function(t,e){if(0===t)return this.clone();t=t*Math.PI/180;var n=e?this.subtract(e):this,i=Math.sin(t),r=Math.cos(t);return n=new u(n.x*r-n.y*i,n.x*i+n.y*r),e?n.add(e):n},transform:function(t){return t?t._transformPoint(this):this},add:function(){var t=u.read(arguments);return new u(this.x+t.x,this.y+t.y)},subtract:function(){var t=u.read(arguments);return new u(this.x-t.x,this.y-t.y)},multiply:function(){var t=u.read(arguments);return new u(this.x*t.x,this.y*t.y)},divide:function(){var t=u.read(arguments);return new u(this.x/t.x,this.y/t.y)},modulo:function(){var t=u.read(arguments);return new u(this.x%t.x,this.y%t.y)},negate:function(){return new u(-this.x,-this.y)},isInside:function(){return _.read(arguments).contains(this)},isClose:function(t,e){return this.getDistance(t)1?t[1]:t[0]):null!=t.width?(this.width=t.width,this.height=t.height):null!=t.x?(this.width=t.x,this.height=t.y):(this.width=this.height=0,this.__read&&(this.__read=0)),this.__read&&(this.__read=1))},set:function(t,e){return this.width=t,this.height=e,this},equals:function(t){return t===this||t&&(this.width===t.width&&this.height===t.height||Array.isArray(t)&&this.width===t[0]&&this.height===t[1])||!1},clone:function(){return new d(this.width,this.height)},toString:function(){var t=a.instance;return"{ width: "+t.number(this.width)+", height: "+t.number(this.height)+" }"},_serialize:function(t){var e=t.formatter;return[e.number(this.width),e.number(this.height)]},add:function(){var t=d.read(arguments);return new d(this.width+t.width,this.height+t.height)},subtract:function(){var t=d.read(arguments);return new d(this.width-t.width,this.height-t.height)},multiply:function(){var t=d.read(arguments);return new d(this.width*t.width,this.height*t.height)},divide:function(){var t=d.read(arguments);return new d(this.width/t.width,this.height/t.height)},modulo:function(){var t=d.read(arguments);return new d(this.width%t.width,this.height%t.height)},negate:function(){return new d(-this.width,-this.height)},isZero:function(){return o.isZero(this.width)&&o.isZero(this.height)},isNaN:function(){return isNaN(this.width)||isNaN(this.height)},statics:{min:function(t,e){return new d(Math.min(t.width,e.width),Math.min(t.height,e.height))},max:function(t,e){return new d(Math.max(t.width,e.width),Math.max(t.height,e.height))},random:function(){return new d(Math.random(),Math.random())}}},e.each(["round","ceil","floor","abs"],function(t){var e=Math[t];this[t]=function(){return new d(e(this.width),e(this.height))}},{})),f=d.extend({initialize:function(t,e,n,i){this._width=t,this._height=e,this._owner=n,this._setter=i},set:function(t,e,n){return this._width=t,this._height=e,n||this._owner[this._setter](this),this},getWidth:function(){return this._width},setWidth:function(t){this._width=t,this._owner[this._setter](this)},getHeight:function(){return this._height},setHeight:function(t){this._height=t,this._owner[this._setter](this)}}),_=e.extend({_class:"Rectangle",_readIndex:!0,beans:!0,initialize:function(n,i,r,s){var a=typeof n,o=0;if("number"===a?(this.x=n,this.y=i,this.width=r,this.height=s,o=4):"undefined"===a||null===n?(this.x=this.y=this.width=this.height=0,o=null===n?1:0):1===arguments.length&&(Array.isArray(n)?(this.x=n[0],this.y=n[1],this.width=n[2],this.height=n[3],o=1):n.x!==t||n.width!==t?(this.x=n.x||0,this.y=n.y||0,this.width=n.width||0,this.height=n.height||0,o=1):n.from===t&&n.to===t&&(this.x=this.y=this.width=this.height=0,this._set(n),o=1)),!o){var h=u.readNamed(arguments,"from"),l=e.peek(arguments);if(this.x=h.x,this.y=h.y,l&&l.x!==t||e.hasNamed(arguments,"to")){var c=u.readNamed(arguments,"to");this.width=c.x-h.x,this.height=c.y-h.y,this.width<0&&(this.x=c.x,this.width=-this.width),this.height<0&&(this.y=c.y,this.height=-this.height)}else{var f=d.read(arguments);this.width=f.width,this.height=f.height}o=arguments.__index}this.__read&&(this.__read=o)},set:function(t,e,n,i){return this.x=t,this.y=e,this.width=n,this.height=i,this},clone:function(){return new _(this.x,this.y,this.width,this.height)},equals:function(t){var n=e.isPlainValue(t)?_.read(arguments):t;return n===this||n&&this.x===n.x&&this.y===n.y&&this.width===n.width&&this.height===n.height||!1},toString:function(){var t=a.instance;return"{ x: "+t.number(this.x)+", y: "+t.number(this.y)+", width: "+t.number(this.width)+", height: "+t.number(this.height)+" }"},_serialize:function(t){var e=t.formatter;return[e.number(this.x),e.number(this.y),e.number(this.width),e.number(this.height)]},getPoint:function(t){var e=t?u:c;return new e(this.x,this.y,this,"setPoint")},setPoint:function(){var t=u.read(arguments);this.x=t.x,this.y=t.y},getSize:function(t){var e=t?d:f;return new e(this.width,this.height,this,"setSize")},setSize:function(){var t=d.read(arguments);this._fixX&&(this.x+=(this.width-t.width)*this._fixX),this._fixY&&(this.y+=(this.height-t.height)*this._fixY),this.width=t.width,this.height=t.height,this._fixW=1,this._fixH=1},getLeft:function(){return this.x},setLeft:function(t){this._fixW||(this.width-=t-this.x),this.x=t,this._fixX=0},getTop:function(){return this.y},setTop:function(t){this._fixH||(this.height-=t-this.y),this.y=t,this._fixY=0},getRight:function(){return this.x+this.width},setRight:function(e){this._fixX!==t&&1!==this._fixX&&(this._fixW=0),this._fixW?this.x=e-this.width:this.width=e-this.x,this._fixX=1},getBottom:function(){return this.y+this.height},setBottom:function(e){this._fixY!==t&&1!==this._fixY&&(this._fixH=0),this._fixH?this.y=e-this.height:this.height=e-this.y,this._fixY=1},getCenterX:function(){return this.x+.5*this.width},setCenterX:function(t){this.x=t-.5*this.width,this._fixX=.5},getCenterY:function(){return this.y+.5*this.height},setCenterY:function(t){this.y=t-.5*this.height,this._fixY=.5},getCenter:function(t){var e=t?u:c;return new e(this.getCenterX(),this.getCenterY(),this,"setCenter")},setCenter:function(){var t=u.read(arguments);return this.setCenterX(t.x),this.setCenterY(t.y),this},getArea:function(){return this.width*this.height},isEmpty:function(){return 0===this.width||0===this.height},contains:function(e){return e&&e.width!==t||4==(Array.isArray(e)?e:arguments).length?this._containsRectangle(_.read(arguments)):this._containsPoint(u.read(arguments))},_containsPoint:function(t){var e=t.x,n=t.y;return e>=this.x&&n>=this.y&&e<=this.x+this.width&&n<=this.y+this.height},_containsRectangle:function(t){var e=t.x,n=t.y;return e>=this.x&&n>=this.y&&e+t.width<=this.x+this.width&&n+t.height<=this.y+this.height},intersects:function(){var t=_.read(arguments);return t.x+t.width>this.x&&t.y+t.height>this.y&&t.x=this.x&&t.y+t.height>=this.y&&t.x<=this.x+this.width&&t.y<=this.y+this.height},intersect:function(){var t=_.read(arguments),e=Math.max(this.x,t.x),n=Math.max(this.y,t.y),i=Math.min(this.x+this.width,t.x+t.width),r=Math.min(this.y+this.height,t.y+t.height);return new _(e,n,i-e,r-n)},unite:function(){var t=_.read(arguments),e=Math.min(this.x,t.x),n=Math.min(this.y,t.y),i=Math.max(this.x+this.width,t.x+t.width),r=Math.max(this.y+this.height,t.y+t.height);return new _(e,n,i-e,r-n)},include:function(){var t=u.read(arguments),e=Math.min(this.x,t.x),n=Math.min(this.y,t.y),i=Math.max(this.x+this.width,t.x),r=Math.max(this.y+this.height,t.y);return new _(e,n,i-e,r-n)},expand:function(){var t=d.read(arguments),e=t.width,n=t.height;return new _(this.x-e/2,this.y-n/2,this.width+e,this.height+n)},scale:function(e,n){return this.expand(this.width*e-this.width,this.height*(n===t?e:n)-this.height)}},e.each([["Top","Left"],["Top","Right"],["Bottom","Left"],["Bottom","Right"],["Left","Center"],["Top","Center"],["Right","Center"],["Bottom","Center"]],function(t,e){var n=t.join(""),i=/^[RL]/.test(n);e>=4&&(t[1]+=i?"Y":"X");var r=t[i?0:1],s=t[i?1:0],a="get"+r,o="get"+s,h="set"+r,l="set"+s,d="get"+n,f="set"+n;this[d]=function(t){var e=t?u:c;return new e(this[a](),this[o](),this,f)},this[f]=function(){var t=u.read(arguments);this[h](t.x),this[l](t.y)}},{beans:!0})),g=_.extend({initialize:function(t,e,n,i,r,s){this.set(t,e,n,i,!0),this._owner=r,this._setter=s},set:function(t,e,n,i,r){return this._x=t,this._y=e,this._width=n,this._height=i,r||this._owner[this._setter](this),this}},new function(){var t=_.prototype;return e.each(["x","y","width","height"],function(t){var n=e.capitalize(t),i="_"+t;this["get"+n]=function(){return this[i]},this["set"+n]=function(t){this[i]=t,this._dontNotify||this._owner[this._setter](this)}},e.each(["Point","Size","Center","Left","Top","Right","Bottom","CenterX","CenterY","TopLeft","TopRight","BottomLeft","BottomRight","LeftCenter","TopCenter","RightCenter","BottomCenter"],function(e){var n="set"+e;this[n]=function(){this._dontNotify=!0,t[n].apply(this,arguments),this._dontNotify=!1,this._owner[this._setter](this)}},{isSelected:function(){return this._owner._boundsSelected},setSelected:function(t){var e=this._owner;e.setSelected&&(e._boundsSelected=t,e.setSelected(t||e._selectedSegmentState>0))}}))}),p=e.extend({_class:"Matrix",initialize:function ht(t){var e=arguments.length,n=!0;if(6===e?this.set.apply(this,arguments):1===e?t instanceof ht?this.set(t._a,t._c,t._b,t._d,t._tx,t._ty):Array.isArray(t)?this.set.apply(this,t):n=!1:0===e?this.reset():n=!1,!n)throw Error("Unsupported matrix parameters")},set:function(t,e,n,i,r,s,a){return this._a=t,this._c=e,this._b=n,this._d=i,this._tx=r,this._ty=s,a||this._changed(),this},_serialize:function(t){return e.serialize(this.getValues(),t)},_changed:function(){var t=this._owner;t&&(t._applyMatrix?t.transform(null,!0):t._changed(9))},clone:function(){return new p(this._a,this._c,this._b,this._d,this._tx,this._ty)},equals:function(t){return t===this||t&&this._a===t._a&&this._b===t._b&&this._c===t._c&&this._d===t._d&&this._tx===t._tx&&this._ty===t._ty||!1},toString:function(){var t=a.instance;return"[["+[t.number(this._a),t.number(this._b),t.number(this._tx)].join(", ")+"], ["+[t.number(this._c),t.number(this._d),t.number(this._ty)].join(", ")+"]]"},reset:function(t){return this._a=this._d=1,this._c=this._b=this._tx=this._ty=0,t||this._changed(),this},apply:function(t,n){var i=this._owner;return i?(i.transform(null,!0,e.pick(t,!0),n),this.isIdentity()):!1},translate:function(){var t=u.read(arguments),e=t.x,n=t.y;return this._tx+=e*this._a+n*this._b,this._ty+=e*this._c+n*this._d,this._changed(),this},scale:function(){var t=u.read(arguments),e=u.read(arguments,0,{readNull:!0});return e&&this.translate(e),this._a*=t.x,this._c*=t.x,this._b*=t.y,this._d*=t.y,e&&this.translate(e.negate()),this._changed(),this},rotate:function(t){t*=Math.PI/180;var e=u.read(arguments,1),n=e.x,i=e.y,r=Math.cos(t),s=Math.sin(t),a=n-n*r+i*s,o=i-n*s-i*r,h=this._a,l=this._b,c=this._c,d=this._d;return this._a=r*h+s*l,this._b=-s*h+r*l,this._c=r*c+s*d,this._d=-s*c+r*d,this._tx+=a*h+o*l,this._ty+=a*c+o*d,this._changed(),this},shear:function(){var t=u.read(arguments),e=u.read(arguments,0,{readNull:!0});e&&this.translate(e);var n=this._a,i=this._c;return this._a+=t.y*this._b,this._c+=t.y*this._d,this._b+=t.x*n,this._d+=t.x*i,e&&this.translate(e.negate()),this._changed(),this},skew:function(){var t=u.read(arguments),e=u.read(arguments,0,{readNull:!0}),n=Math.PI/180,i=new u(Math.tan(t.x*n),Math.tan(t.y*n));return this.shear(i,e)},concatenate:function(t){var e=this._a,n=this._b,i=this._c,r=this._d,s=t._a,a=t._b,o=t._c,h=t._d,u=t._tx,l=t._ty;return this._a=s*e+o*n,this._b=a*e+h*n,this._c=s*i+o*r,this._d=a*i+h*r,this._tx+=u*e+l*n,this._ty+=u*i+l*r,this._changed(),this},preConcatenate:function(t){var e=this._a,n=this._b,i=this._c,r=this._d,s=this._tx,a=this._ty,o=t._a,h=t._b,u=t._c,l=t._d,c=t._tx,d=t._ty;return this._a=o*e+h*i,this._b=o*n+h*r,this._c=u*e+l*i,this._d=u*n+l*r,this._tx=o*s+h*a+c,this._ty=u*s+l*a+d,this._changed(),this},chain:function(t){var e=this._a,n=this._b,i=this._c,r=this._d,s=this._tx,a=this._ty,o=t._a,h=t._b,u=t._c,l=t._d,c=t._tx,d=t._ty; -return new p(o*e+u*n,o*i+u*r,h*e+l*n,h*i+l*r,s+c*e+d*n,a+c*i+d*r)},isIdentity:function(){return 1===this._a&&0===this._c&&0===this._b&&1===this._d&&0===this._tx&&0===this._ty},orNullIfIdentity:function(){return this.isIdentity()?null:this},isInvertible:function(){return!!this._getDeterminant()},isSingular:function(){return!this._getDeterminant()},transform:function(t,e,n){return arguments.length<3?this._transformPoint(u.read(arguments)):this._transformCoordinates(t,e,n)},_transformPoint:function(t,e,n){var i=t.x,r=t.y;return e||(e=new u),e.set(i*this._a+r*this._b+this._tx,i*this._c+r*this._d+this._ty,n)},_transformCoordinates:function(t,e,n){for(var i=0,r=0,s=2*n;s>i;){var a=t[i++],o=t[i++];e[r++]=a*this._a+o*this._b+this._tx,e[r++]=a*this._c+o*this._d+this._ty}return e},_transformCorners:function(t){var e=t.x,n=t.y,i=e+t.width,r=n+t.height,s=[e,n,i,n,i,r,e,r];return this._transformCoordinates(s,s,4)},_transformBounds:function(t,e,n){for(var i=this._transformCorners(t),r=i.slice(0,2),s=i.slice(),a=2;8>a;a++){var o=i[a],h=1&a;os[h]&&(s[h]=o)}return e||(e=new _),e.set(r[0],r[1],s[0]-r[0],s[1]-r[1],n)},inverseTransform:function(){return this._inverseTransform(u.read(arguments))},_getDeterminant:function(){var t=this._a*this._d-this._b*this._c;return isFinite(t)&&!o.isZero(t)&&isFinite(this._tx)&&isFinite(this._ty)?t:null},_inverseTransform:function(t,e,n){var i=this._getDeterminant();if(!i)return null;var r=t.x-this._tx,s=t.y-this._ty;return e||(e=new u),e.set((r*this._d-s*this._b)/i,(s*this._a-r*this._c)/i,n)},decompose:function(){var t=this._a,e=this._b,n=this._c,i=this._d;if(o.isZero(t*i-e*n))return null;var r=Math.sqrt(t*t+e*e);t/=r,e/=r;var s=t*n+e*i;n-=t*s,i-=e*s;var a=Math.sqrt(n*n+i*i);return n/=a,i/=a,s/=a,e*n>t*i&&(t=-t,e=-e,s=-s,r=-r),{scaling:new u(r,a),rotation:180*-Math.atan2(e,t)/Math.PI,shearing:s}},getValues:function(){return[this._a,this._c,this._b,this._d,this._tx,this._ty]},getTranslation:function(){return new u(this._tx,this._ty)},getScaling:function(){return(this.decompose()||{}).scaling},getRotation:function(){return(this.decompose()||{}).rotation},inverted:function(){var t=this._getDeterminant();return t&&new p(this._d/t,-this._c/t,-this._b/t,this._a/t,(this._b*this._ty-this._d*this._tx)/t,(this._c*this._tx-this._a*this._ty)/t)},shiftless:function(){return new p(this._a,this._c,this._b,this._d,0,0)},applyToContext:function(t){t.transform(this._a,this._c,this._b,this._d,this._tx,this._ty)}},e.each(["a","c","b","d","tx","ty"],function(t){var n=e.capitalize(t),i="_"+t;this["get"+n]=function(){return this[i]},this["set"+n]=function(t){this[i]=t,this._changed()}},{})),v=e.extend({_class:"Line",initialize:function(t,e,n,i,r){var s=!1;arguments.length>=4?(this._px=t,this._py=e,this._vx=n,this._vy=i,s=r):(this._px=t.x,this._py=t.y,this._vx=e.x,this._vy=e.y,s=n),s||(this._vx-=this._px,this._vy-=this._py)},getPoint:function(){return new u(this._px,this._py)},getVector:function(){return new u(this._vx,this._vy)},getLength:function(){return this.getVector().getLength()},intersect:function(t,e){return v.intersect(this._px,this._py,this._vx,this._vy,t._px,t._py,t._vx,t._vy,!0,e)},getSide:function(t){return v.getSide(this._px,this._py,this._vx,this._vy,t.x,t.y,!0)},getDistance:function(t){return Math.abs(v.getSignedDistance(this._px,this._py,this._vx,this._vy,t.x,t.y,!0))},statics:{intersect:function(t,e,n,i,r,s,a,h,l,c){l||(n-=t,i-=e,a-=r,h-=s);var d=n*h-i*a;if(!o.isZero(d)){var f=t-r,_=e-s,g=(a*_-h*f)/d,p=(n*_-i*f)/d;if(c||g>=0&&1>=g&&p>=0&&1>=p)return new u(t+g*n,e+g*i)}},getSide:function(t,e,n,i,r,s,a){a||(n-=t,i-=e);var o=r-t,h=s-e,u=o*i-h*n;return 0===u&&(u=o*n+h*i,u>0&&(o-=n,h-=i,u=o*n+h*i,0>u&&(u=0))),0>u?-1:u>0?1:0},getSignedDistance:function(t,e,n,i,r,s,a){return a||(n-=t,i-=e),o.isZero(n)?i>=0?t-r:r-t:o.isZero(i)?n>=0?s-e:e-s:(n*(s-e)-i*(r-t))/Math.sqrt(n*n+i*i)}}}),y=s.extend({_class:"Project",_list:"projects",_reference:"project",initialize:function(t){s.call(this,!0),this.layers=[],this._activeLayer=null,this.symbols=[],this._currentStyle=new Z(null,null,this),this._view=W.create(this,t||et.getCanvas(1,1)),this._selectedItems={},this._selectedItemCount=0,this._updateVersion=0},_serialize:function(t,n){return e.serialize(this.layers,t,!0,n)},clear:function(){for(var t=this.layers.length-1;t>=0;t--)this.layers[t].remove();this.symbols=[]},isEmpty:function(){return 0===this.layers.length},remove:function ut(){return ut.base.call(this)?(this._view&&this._view.remove(),!0):!1},getView:function(){return this._view},getCurrentStyle:function(){return this._currentStyle},setCurrentStyle:function(t){this._currentStyle.initialize(t)},getIndex:function(){return this._index},getOptions:function(){return this._scope.settings},getActiveLayer:function(){return this._activeLayer||new C({project:this})},getSelectedItems:function(){var t=[];for(var e in this._selectedItems){var n=this._selectedItems[e];n.isInserted()&&t.push(n)}return t},insertChild:function(t,n,i){return n instanceof C?(n._remove(!1,!0),e.splice(this.layers,[n],t,0),n._setProject(this,!0),this._changes&&n._changed(5),this._activeLayer||(this._activeLayer=n)):n instanceof x?(this._activeLayer||this.insertChild(t,new C(x.NO_INSERT))).insertChild(t,n,i):n=null,n},addChild:function(e,n){return this.insertChild(t,e,n)},_updateSelection:function(t){var e=t._id,n=this._selectedItems;t._selected?n[e]!==t&&(this._selectedItemCount++,n[e]=t):n[e]===t&&(this._selectedItemCount--,delete n[e])},selectAll:function(){for(var t=this.layers,e=0,n=t.length;n>e;e++)t[e].setFullySelected(!0)},deselectAll:function(){var t=this._selectedItems;for(var e in t)t[e].setFullySelected(!1)},hitTest:function(){for(var t=u.read(arguments),n=I.getOptions(e.read(arguments)),i=this.layers.length-1;i>=0;i--){var r=this.layers[i]._hitTest(t,n);if(r)return r}return null},getItems:function(t){return x._getItems(this.layers,t)},getItem:function(t){return x._getItems(this.layers,t,null,null,!0)[0]||null},importJSON:function(t){this.activate();var n=this._activeLayer;return e.importJSON(t,n&&n.isEmpty()&&n)},draw:function(t,n,i){this._updateVersion++,t.save(),n.applyToContext(t);for(var r=new e({offset:new u(0,0),pixelRatio:i,viewMatrix:n.isIdentity()?null:n,matrices:[new p],updateMatrix:!0}),s=0,a=this.layers,o=a.length;o>s;s++)a[s].draw(t,r);if(t.restore(),this._selectedItemCount>0){t.save(),t.strokeWidth=1;var h=this._selectedItems,l=this._scope.settings.handleSize,c=this._updateVersion;for(var d in h)h[d]._drawSelection(t,n,l,h,c);t.restore()}}}),w=e.extend({_class:"Symbol",initialize:function(t,e){this._id=h.get(),this.project=paper.project,this.project.symbols.push(this),t&&this.setDefinition(t,e)},_serialize:function(t,n){return n.add(this,function(){return e.serialize([this._class,this._definition],t,!1,n)})},_changed:function(t){8&t&&x._clearBoundsCache(this),1&t&&(this.project._needsUpdate=!0)},getDefinition:function(){return this._definition},setDefinition:function(t,e){t._parentSymbol&&(t=t.clone()),this._definition&&(this._definition._parentSymbol=null),this._definition=t,t.remove(),t.setSelected(!1),e||t.setPosition(new u),t._parentSymbol=this,this._changed(9)},place:function(t){return new M(this,t)},clone:function(){return new w(this._definition.clone(!1))},equals:function(t){return t===this||t&&this.definition.equals(t.definition)||!1}}),x=e.extend(n,{statics:{extend:function lt(t){return t._serializeFields&&(t._serializeFields=new e(this.prototype._serializeFields,t._serializeFields)),lt.base.apply(this,arguments)},NO_INSERT:{insert:!1}},_class:"Item",_applyMatrix:!0,_canApplyMatrix:!0,_boundsSelected:!1,_selectChildren:!1,_serializeFields:{name:null,applyMatrix:null,matrix:new p,pivot:null,locked:!1,visible:!0,blendMode:"normal",opacity:1,guide:!1,selected:!1,clipMask:!1,data:{}},initialize:function(){},_initialize:function(t,n){var i=t&&e.isPlainObject(t),r=i&&t.internal===!0,s=this._matrix=new p,a=i&&t.project||paper.project;return r||(this._id=h.get()),this._applyMatrix=this._canApplyMatrix&&paper.settings.applyMatrix,n&&s.translate(n),s._owner=this,this._style=new Z(a._currentStyle,this,a),this._project||(r||i&&t.insert===!1?this._setProject(a):i&&t.parent?this.setParent(t.parent):(a._activeLayer||new C).addChild(this)),i&&t!==x.NO_INSERT&&this._set(t,{insert:!0,parent:!0},!0),i},_events:new function(){var t={mousedown:{mousedown:1,mousedrag:1,click:1,doubleclick:1},mouseup:{mouseup:1,mousedrag:1,click:1,doubleclick:1},mousemove:{mousedrag:1,mousemove:1,mouseenter:1,mouseleave:1}},n={install:function(e){var n=this.getView()._eventCounters;if(n)for(var i in t)n[i]=(n[i]||0)+(t[i][e]||0)},uninstall:function(e){var n=this.getView()._eventCounters;if(n)for(var i in t)n[i]-=t[i][e]||0}};return e.each(["onMouseDown","onMouseUp","onMouseDrag","onClick","onDoubleClick","onMouseMove","onMouseEnter","onMouseLeave"],function(t){this[t]=n},{onFrame:{install:function(){this._animateItem(!0)},uninstall:function(){this._animateItem(!1)}},onLoad:{}})},_animateItem:function(t){this.getView()._animateItem(this,t)},_serialize:function(t,n){function i(i){for(var a in i){var o=s[a];e.equals(o,"leading"===a?1.2*i.fontSize:i[a])||(r[a]=e.serialize(o,t,"data"!==a,n))}}var r={},s=this;return i(this._serializeFields),this instanceof b||i(this._style._defaults),[this._class,r]},_changed:function(e){var n=this._parentSymbol,i=this._parent||n,r=this._project;if(8&e&&(this._bounds=this._position=this._decomposed=this._globalMatrix=this._currentPath=t),i&&40&e&&x._clearBoundsCache(i),2&e&&x._clearBoundsCache(this),r&&(1&e&&(r._needsUpdate=!0),r._changes)){var s=r._changesById[this._id];s?s.flags|=e:(s={item:this,flags:e},r._changesById[this._id]=s,r._changes.push(s))}n&&n._changed(e)},set:function(t){return t&&this._set(t),this},getId:function(){return this._id},getName:function(){return this._name},setName:function(e,n){if(this._name&&this._removeNamed(),e===+e+"")throw Error("Names consisting only of numbers are not supported.");var i=this._parent;if(e&&i){for(var r=i._children,s=i._namedChildren,a=e,o=1;n&&r[e];)e=a+" "+o++;(s[e]=s[e]||[]).push(this),r[e]=this}this._name=e||t,this._changed(128)},getStyle:function(){return this._style},setStyle:function(t){this.getStyle().set(t)}},e.each(["locked","visible","blendMode","opacity","guide"],function(t){var n=e.capitalize(t),t="_"+t;this["get"+n]=function(){return this[t]},this["set"+n]=function(e){e!=this[t]&&(this[t]=e,this._changed("_locked"===t?128:129))}},{}),{beans:!0,_locked:!1,_visible:!0,_blendMode:"normal",_opacity:1,_guide:!1,isSelected:function(){if(this._selectChildren)for(var t=this._children,e=0,n=t.length;n>e;e++)if(t[e].isSelected())return!0;return this._selected},setSelected:function(t,e){if(!e&&this._selectChildren)for(var n=this._children,i=0,r=n.length;r>i;i++)n[i].setSelected(t);(t=!!t)^this._selected&&(this._selected=t,this._project._updateSelection(this),this._changed(129))},_selected:!1,isFullySelected:function(){var t=this._children;if(t&&this._selected){for(var e=0,n=t.length;n>e;e++)if(!t[e].isFullySelected())return!1;return!0}return this._selected},setFullySelected:function(t){var e=this._children;if(e)for(var n=0,i=e.length;i>n;n++)e[n].setFullySelected(t);this.setSelected(t,!0)},isClipMask:function(){return this._clipMask},setClipMask:function(t){this._clipMask!=(t=!!t)&&(this._clipMask=t,t&&(this.setFillColor(null),this.setStrokeColor(null)),this._changed(129),this._parent&&this._parent._changed(1024))},_clipMask:!1,getData:function(){return this._data||(this._data={}),this._data},setData:function(t){this._data=t},getPosition:function(t){var e=this._position,n=t?u:c;if(!e){var i=this._pivot;e=this._position=i?this._matrix._transformPoint(i):this.getBounds().getCenter(!0)}return new n(e.x,e.y,this,"setPosition")},setPosition:function(){this.translate(u.read(arguments).subtract(this.getPosition(!0)))},getPivot:function(t){var e=this._pivot;if(e){var n=t?u:c;e=new n(e.x,e.y,this,"setPivot")}return e},setPivot:function(){this._pivot=u.read(arguments),this._position=t},_pivot:null,getRegistration:"#getPivot",setRegistration:"#setPivot"},e.each(["bounds","strokeBounds","handleBounds","roughBounds","internalBounds","internalRoughBounds"],function(t){var n="get"+e.capitalize(t),i=t.match(/^internal(.*)$/),r=i?"get"+i[1]:null;this[n]=function(e){var i=this._boundsGetter,s=!r&&("string"==typeof i?i:i&&i[n])||n,a=this._getCachedBounds(s,e,this,r);return"bounds"===t?new g(a.x,a.y,a.width,a.height,this,"setBounds"):a}},{beans:!0,_getBounds:function(t,e,n){var i=this._children;if(!i||0==i.length)return new _;x._updateBoundsCache(this,n);for(var r=1/0,s=-r,a=r,o=s,h=0,u=i.length;u>h;h++){var l=i[h];if(l._visible&&!l.isEmpty()){var c=l._getCachedBounds(t,e&&e.chain(l._matrix),n);r=Math.min(c.x,r),a=Math.min(c.y,a),s=Math.max(c.x+c.width,s),o=Math.max(c.y+c.height,o)}}return isFinite(r)?new _(r,a,s-r,o-a):new _},setBounds:function(){var t=_.read(arguments),e=this.getBounds(),n=new p,i=t.getCenter();n.translate(i),(t.width!=e.width||t.height!=e.height)&&n.scale(0!=e.width?t.width/e.width:1,0!=e.height?t.height/e.height:1),i=e.getCenter(),n.translate(-i.x,-i.y),this.transform(n)},_getCachedBounds:function(t,e,n,i){e=e&&e.orNullIfIdentity();var r=i?null:this._matrix.orNullIfIdentity(),s=(!e||e.equals(r))&&t;if(x._updateBoundsCache(this._parent||this._parentSymbol,n),s&&this._bounds&&this._bounds[s])return this._bounds[s].clone();var a=this._getBounds(i||t,e||r,n);if(s){this._bounds||(this._bounds={});var o=this._bounds[s]=a.clone();o._internal=!!i}return a},statics:{_updateBoundsCache:function(t,e){if(t){var n=e._id,i=t._boundsCache=t._boundsCache||{ids:{},list:[]};i.ids[n]||(i.list.push(e),i.ids[n]=e)}},_clearBoundsCache:function(e){var n=e._boundsCache;if(n){e._bounds=e._position=e._boundsCache=t;for(var i=0,r=n.list,s=r.length;s>i;i++){var a=r[i];a!==e&&(a._bounds=a._position=t,a._boundsCache&&x._clearBoundsCache(a))}}}}}),{beans:!0,_decompose:function(){return this._decomposed=this._matrix.decompose()},getRotation:function(){var t=this._decomposed||this._decompose();return t&&t.rotation},setRotation:function(t){var e=this.getRotation();if(null!=e&&null!=t){var n=this._decomposed;this.rotate(t-e),n.rotation=t,this._decomposed=n}},getScaling:function(t){var e=this._decomposed||this._decompose(),n=e&&e.scaling,i=t?u:c;return n&&new i(n.x,n.y,this,"setScaling")},setScaling:function(){var t=this.getScaling();if(t){var e=u.read(arguments,0,{clone:!0}),n=this._decomposed;this.scale(e.x/t.x,e.y/t.y),n.scaling=e,this._decomposed=n}},getMatrix:function(){return this._matrix},setMatrix:function(t){this._matrix.initialize(t),this._applyMatrix?this.transform(null,!0):this._changed(9)},getGlobalMatrix:function(t){var e=this._globalMatrix,n=this._project._updateVersion;if(e&&e._updateVersion!==n&&(e=null),!e){e=this._globalMatrix=this._matrix.clone();var i=this._parent;i&&e.preConcatenate(i.getGlobalMatrix(!0)),e._updateVersion=n}return t?e:e.clone()},getApplyMatrix:function(){return this._applyMatrix},setApplyMatrix:function(t){(this._applyMatrix=this._canApplyMatrix&&!!t)&&this.transform(null,!0)},getTransformContent:"#getApplyMatrix",setTransformContent:"#setApplyMatrix"},{getProject:function(){return this._project},_setProject:function(t,e){if(this._project!==t){this._project&&this._installEvents(!1),this._project=t;for(var n=this._children,i=0,r=n&&n.length;r>i;i++)n[i]._setProject(t);e=!0}e&&this._installEvents(!0)},getView:function(){return this._project.getView()},_installEvents:function ct(t){ct.base.call(this,t);for(var e=this._children,n=0,i=e&&e.length;i>n;n++)e[n]._installEvents(t)},getLayer:function(){for(var t=this;t=t._parent;)if(t instanceof C)return t;return null},getParent:function(){return this._parent},setParent:function(t){return t.addChild(this)},getChildren:function(){return this._children},setChildren:function(t){this.removeChildren(),this.addChildren(t)},getFirstChild:function(){return this._children&&this._children[0]||null},getLastChild:function(){return this._children&&this._children[this._children.length-1]||null},getNextSibling:function(){return this._parent&&this._parent._children[this._index+1]||null},getPreviousSibling:function(){return this._parent&&this._parent._children[this._index-1]||null},getIndex:function(){return this._index},equals:function(t){return t===this||t&&this._class===t._class&&this._style.equals(t._style)&&this._matrix.equals(t._matrix)&&this._locked===t._locked&&this._visible===t._visible&&this._blendMode===t._blendMode&&this._opacity===t._opacity&&this._clipMask===t._clipMask&&this._guide===t._guide&&this._equals(t)||!1},_equals:function(t){return e.equals(this._children,t._children)},clone:function(t){return this._clone(new this.constructor(x.NO_INSERT),t)},_clone:function(n,i,r){var s=["_locked","_visible","_blendMode","_opacity","_clipMask","_guide"],a=this._children;n.setStyle(this._style);for(var o=0,h=a&&a.length;h>o;o++)n.addChild(a[o].clone(!1),!0);for(var o=0,h=s.length;h>o;o++){var u=s[o];this.hasOwnProperty(u)&&(n[u]=this[u])}return r!==!1&&n._matrix.initialize(this._matrix),n.setApplyMatrix(this._applyMatrix),n.setSelected(this._selected),n._data=this._data?e.clone(this._data):null,(i||i===t)&&n.insertAbove(this),this._name&&n.setName(this._name,!0),n},copyTo:function(t){return t.addChild(this.clone(!1))},rasterize:function(t){var n=this.getStrokeBounds(),i=(t||this.getView().getResolution())/72,r=n.getTopLeft().floor(),s=n.getBottomRight().ceil(),a=new d(s.subtract(r)),o=et.getCanvas(a.multiply(i)),h=o.getContext("2d"),u=(new p).scale(i).translate(r.negate());h.save(),u.applyToContext(h),this.draw(h,new e({matrices:[u]})),h.restore();var l=new P(x.NO_INSERT);return l.setCanvas(o),l.transform((new p).translate(r.add(a.divide(2))).scale(1/i)),l.insertAbove(this),l},contains:function(){return!!this._contains(this._matrix._inverseTransform(u.read(arguments)))},_contains:function(t){if(this._children){for(var e=this._children.length-1;e>=0;e--)if(this._children[e].contains(t))return!0;return!1}return t.isInside(this.getInternalBounds())},isInside:function(){return _.read(arguments).contains(this.getBounds())},_asPathItem:function(){return new E.Rectangle({rectangle:this.getInternalBounds(),matrix:this._matrix,insert:!1})},intersects:function(t,e){return t instanceof x?this._asPathItem().getIntersections(t._asPathItem(),e||t._matrix).length>0:!1},hitTest:function(){return this._hitTest(u.read(arguments),I.getOptions(e.read(arguments)))},_hitTest:function(n,i){function r(i,r){var s=_["get"+r]();return n.subtract(s).divide(u).length<=1?new I(i,f,{name:e.hyphenate(r),point:s}):t}if(this._locked||!this._visible||this._guide&&!i.guides||this.isEmpty())return null;var s=this._matrix,a=i._totalMatrix,o=this.getView(),h=i._totalMatrix=a?a.chain(s):this.getGlobalMatrix().preConcatenate(o._matrix),u=i._tolerancePadding=new d(E._getPenPadding(1,h.inverted())).multiply(Math.max(i.tolerance,1e-6));if(n=s._inverseTransform(n),!this._children&&!this.getInternalRoughBounds().expand(u.multiply(2))._containsPoint(n))return null;var l,c=!(i.guides&&!this._guide||i.selected&&!this._selected||i.type&&i.type!==e.hyphenate(this._class)||i["class"]&&!(this instanceof i["class"])),f=this;if(c&&(i.center||i.bounds)&&this._parent){var _=this.getInternalBounds();if(i.center&&(l=r("center","Center")),!l&&i.bounds)for(var g=["TopLeft","TopRight","BottomLeft","BottomRight","LeftCenter","TopCenter","RightCenter","BottomCenter"],p=0;8>p&&!l;p++)l=r("bounds",g[p])}var v=!l&&this._children;if(v)for(var m=this._getChildHitTestOptions(i),p=v.length-1;p>=0&&!l;p--)l=v[p]._hitTest(n,m);return!l&&c&&(l=this._hitTestSelf(n,i)),l&&l.point&&(l.point=s.transform(l.point)),i._totalMatrix=a,l},_getChildHitTestOptions:function(t){return t},_hitTestSelf:function(e,n){return n.fill&&this.hasFill()&&this._contains(e)?new I("fill",this):t},matches:function(t,n){function i(t,n){for(var r in t)if(t.hasOwnProperty(r)){var s=t[r],a=n[r];if(e.isPlainObject(s)&&e.isPlainObject(a)){if(!i(s,a))return!1}else if(!e.equals(s,a))return!1}return!0}if("object"==typeof t){for(var r in t)if(t.hasOwnProperty(r)&&!this.matches(r,t[r]))return!1}else{var s=/^(empty|editable)$/.test(t)?this["is"+e.capitalize(t)]():"type"===t?e.hyphenate(this._class):this[t];if(/^(constructor|class)$/.test(t)){if(!(this instanceof n))return!1}else if(n instanceof RegExp){if(!n.test(s))return!1}else if("function"==typeof n){if(!n(s))return!1}else if(e.isPlainObject(n)){if(!i(n,s))return!1}else if(!e.equals(s,n))return!1}return!0},getItems:function(t){return x._getItems(this._children,t,this._matrix)},getItem:function(t){return x._getItems(this._children,t,this._matrix,null,!0)[0]||null},statics:{_getItems:function dt(t,n,i,r,s){if(!r){var a=n.overlapping,o=n.inside,h=a||o,u=h&&_.read([h]);r={items:[],inside:u,overlapping:a&&new E.Rectangle({rectangle:u,insert:!1})},h&&(n=e.set({},n,{inside:!0,overlapping:!0}))}var l=r.items,o=r.inside,a=r.overlapping;i=o&&(i||new p);for(var c=0,d=t&&t.length;d>c;c++){var f=t[c],g=i&&i.chain(f._matrix),v=!0;if(o){var h=f.getBounds(g);if(!o.intersects(h))continue;o&&o.contains(h)||a&&a.intersects(f,g)||(v=!1)}if(v&&f.matches(n)&&(l.push(f),s))break;if(dt(f._children,n,g,r,s),s&&l.length>0)break}return l}}},{importJSON:function(t){var n=e.importJSON(t,this);return n!==this?this.addChild(n):n},addChild:function(e,n){return this.insertChild(t,e,n)},insertChild:function(t,e,n){var i=e?this.insertChildren(t,[e],n):null;return i&&i[0]},addChildren:function(t,e){return this.insertChildren(this._children.length,t,e)},insertChildren:function(t,n,i,r){var s=this._children;if(s&&n&&n.length>0){n=Array.prototype.slice.apply(n);for(var a=n.length-1;a>=0;a--){var o=n[a];if(!r||o instanceof r){var h=o._parent===this&&o._indexa;a++){var o=n[a];o._parent=this,o._setProject(this._project,!0),o._name&&o.setName(o._name),l&&this._changed(5)}this._changed(11)}else n=null;return n},_insertSibling:function(t,e,n){return this._parent?this._parent.insertChild(t,e,n):null},insertAbove:function(t,e){return t._insertSibling(t._index+1,this,e)},insertBelow:function(t,e){return t._insertSibling(t._index,this,e)},sendToBack:function(){return(this._parent||this instanceof C&&this._project).insertChild(0,this)},bringToFront:function(){return(this._parent||this instanceof C&&this._project).addChild(this)},appendTop:"#addChild",appendBottom:function(t){return this.insertChild(0,t)},moveAbove:"#insertAbove",moveBelow:"#insertBelow",reduce:function(){if(this._children&&1===this._children.length){var t=this._children[0].reduce();return t.insertAbove(this),t.setStyle(this._style),this.remove(),t}return this},_removeNamed:function(){var t=this._parent;if(t){var e=t._children,n=t._namedChildren,i=this._name,r=n[i],s=r?r.indexOf(this):-1;-1!==s&&(e[i]==this&&delete e[i],r.splice(s,1),r.length?e[i]=r[r.length-1]:delete n[i])}},_remove:function(t,n){var i=this._parent;if(i){if(this._name&&this._removeNamed(),null!=this._index&&e.splice(i._children,null,this._index,1),this._installEvents(!1),t){var r=this._project;r&&r._changes&&this._changed(5)}return n&&i._changed(11),this._parent=null,!0}return!1},remove:function(){return this._remove(!0,!0)},replaceWith:function(t){var e=t&&t.insertBelow(this);return e&&this.remove(),e},removeChildren:function(t,n){if(!this._children)return null;t=t||0,n=e.pick(n,this._children.length);for(var i=e.splice(this._children,null,t,n-t),r=i.length-1;r>=0;r--)i[r]._remove(!0,!1);return i.length>0&&this._changed(11),i},clear:"#removeChildren",reverseChildren:function(){if(this._children){this._children.reverse();for(var t=0,e=this._children.length;e>t;t++)this._children[t]._index=t;this._changed(11)}},isEmpty:function(){return!this._children||0===this._children.length},isEditable:function(){for(var t=this;t;){if(!t._visible||t._locked)return!1;t=t._parent}return!0},hasFill:function(){return this.getStyle().hasFill()},hasStroke:function(){return this.getStyle().hasStroke()},hasShadow:function(){return this.getStyle().hasShadow()},_getOrder:function(t){function e(t){var e=[];do e.unshift(t);while(t=t._parent);return e}for(var n=e(this),i=e(t),r=0,s=Math.min(n.length,i.length);s>r;r++)if(n[r]!=i[r])return n[r]._index0},isInserted:function(){return this._parent?this._parent.isInserted():!1},isAbove:function(t){return-1===this._getOrder(t)},isBelow:function(t){return 1===this._getOrder(t)},isParent:function(t){return this._parent===t},isChild:function(t){return t&&t._parent===this},isDescendant:function(t){for(var e=this;e=e._parent;)if(e==t)return!0;return!1},isAncestor:function(t){return t?t.isDescendant(this):!1},isGroupedWith:function(t){for(var e=this._parent;e;){if(e._parent&&/^(Group|Layer|CompoundPath)$/.test(e._class)&&t.isDescendant(e))return!0;e=e._parent}return!1},translate:function(){var t=new p;return this.transform(t.translate.apply(t,arguments))},rotate:function(t){return this.transform((new p).rotate(t,u.read(arguments,1,{readNull:!0})||this.getPosition(!0)))}},e.each(["scale","shear","skew"],function(t){this[t]=function(){var e=u.read(arguments),n=u.read(arguments,0,{readNull:!0});return this.transform((new p)[t](e,n||this.getPosition(!0)))}},{}),{transform:function(t,e,n,i){t&&t.isIdentity()&&(t=null);var r=this._matrix,s=(e||this._applyMatrix)&&(!r.isIdentity()||t||e&&n&&this._children);if(!t&&!s)return this;if(t&&r.preConcatenate(t),s=s&&this._transformContent(r,n,i)){var a=this._pivot,o=this._style,h=o.getFillColor(!0),u=o.getStrokeColor(!0);a&&r._transformPoint(a,a,!0),h&&h.transform(r),u&&u.transform(r),r.reset(!0),i&&this._canApplyMatrix&&(this._applyMatrix=!0)}var l=this._bounds,c=this._position;this._changed(9);var d=l&&t&&t.decompose();if(d&&!d.shearing&&d.rotation%90===0){for(var f in l){var _=l[f];(s||!_._internal)&&t._transformBounds(_,_)}var g=this._boundsGetter,_=l[g&&g.getBounds||g||"getBounds"];_&&(this._position=_.getCenter(!0)),this._bounds=l}else t&&c&&(this._position=t._transformPoint(c,c));return this},_transformContent:function(t,e,n){var i=this._children;if(i){for(var r=0,s=i.length;s>r;r++)i[r].transform(t,!0,e,n);return!0}},globalToLocal:function(){return this.getGlobalMatrix(!0)._inverseTransform(u.read(arguments))},localToGlobal:function(){return this.getGlobalMatrix(!0)._transformPoint(u.read(arguments))},parentToLocal:function(){return this._matrix._inverseTransform(u.read(arguments))},localToParent:function(){return this._matrix._transformPoint(u.read(arguments))},fitBounds:function(t,e){t=_.read(arguments);var n=this.getBounds(),i=n.height/n.width,r=t.height/t.width,s=(e?i>r:r>i)?t.width/n.width:t.height/n.height,a=new _(new u,new d(n.width*s,n.height*s));a.setCenter(t.getCenter()),this.setBounds(a)},_setStyles:function(t){var e=this._style,n=e.getFillColor(),i=e.getStrokeColor(),r=e.getShadowColor();if(n&&(t.fillStyle=n.toCanvasStyle(t)),i){var s=e.getStrokeWidth();if(s>0){t.strokeStyle=i.toCanvasStyle(t),t.lineWidth=s;var a=e.getStrokeJoin(),o=e.getStrokeCap(),h=e.getMiterLimit();if(a&&(t.lineJoin=a),o&&(t.lineCap=o),h&&(t.miterLimit=h),paper.support.nativeDash){var u=e.getDashArray(),l=e.getDashOffset();u&&u.length&&("setLineDash"in t?(t.setLineDash(u),t.lineDashOffset=l):(t.mozDash=u,t.mozDashOffset=l))}}}if(r){var c=e.getShadowBlur();if(c>0){t.shadowColor=r.toCanvasStyle(t),t.shadowBlur=c;var d=this.getShadowOffset();t.shadowOffsetX=d.x,t.shadowOffsetY=d.y}}},draw:function(t,e,n){function i(t){return a?a.chain(t):t}var r=this._updateVersion=this._project._updateVersion;if(this._visible&&0!==this._opacity){var s=e.matrices,a=e.viewMatrix,o=this._matrix,h=s[s.length-1].chain(o);if(h.isInvertible()){s.push(h),e.updateMatrix&&(h._updateVersion=r,this._globalMatrix=h);var u,l,c,d=this._blendMode,f=this._opacity,_="normal"===d,g=nt.nativeModes[d],p=_&&1===f||e.dontStart||e.clip||(g||_&&1>f)&&this._canComposite(),v=e.pixelRatio;if(!p){var m=this.getStrokeBounds(i(h));if(!m.width||!m.height)return;c=e.offset,l=e.offset=m.getTopLeft().floor(),u=t,t=et.getContext(m.getSize().ceil().add(1).multiply(v)),1!==v&&t.scale(v,v)}t.save();var y=n?n.chain(o):!this.getStrokeScaling(!0)&&i(h),w=!p&&e.clipItem,x=!y||w;if(p?(t.globalAlpha=f,g&&(t.globalCompositeOperation=d)):x&&t.translate(-l.x,-l.y),x&&(p?o:i(h)).applyToContext(t),w&&e.clipItem.draw(t,e.extend({clip:!0})),y){t.setTransform(v,0,0,v,0,0);var b=e.offset;b&&t.translate(-b.x,-b.y)}this._draw(t,e,y),t.restore(),s.pop(),e.clip&&!e.dontFinish&&t.clip(),p||(nt.process(d,t,u,f,l.subtract(c).multiply(v)),et.release(t),e.offset=c)}}},_isUpdated:function(t){var e=this._parent;if(e instanceof N)return e._isUpdated(t);var n=this._updateVersion===t;return!n&&e&&e._visible&&e._isUpdated(t)&&(this._updateVersion=t,n=!0),n},_drawSelection:function(t,e,n,i,r){if((this._drawSelected||this._boundsSelected)&&this._isUpdated(r)){var s=this.getSelectedColor(!0)||this.getLayer().getSelectedColor(!0),a=e.chain(this.getGlobalMatrix(!0));if(t.strokeStyle=t.fillStyle=s?s.toCanvasStyle(t):"#009dec",this._drawSelected&&this._drawSelected(t,a,i),this._boundsSelected){var o=n/2;coords=a._transformCorners(this.getInternalBounds()),t.beginPath();for(var h=0;8>h;h++)t[0===h?"moveTo":"lineTo"](coords[h],coords[++h]);t.closePath(),t.stroke();for(var h=0;8>h;h++)t.fillRect(coords[h]-o,coords[++h]-o,n,n)}}},_canComposite:function(){return!1}},e.each(["down","drag","up","move"],function(t){this["removeOn"+e.capitalize(t)]=function(){var e={};return e[t]=!0,this.removeOn(e)}},{removeOn:function(t){for(var e in t)if(t[e]){var n="mouse"+e,i=this._project,r=i._removeSets=i._removeSets||{};r[n]=r[n]||{},r[n][this._id]=this}return this}})),b=x.extend({_class:"Group",_selectChildren:!0,_serializeFields:{children:[]},initialize:function(t){this._children=[],this._namedChildren={},this._initialize(t)||this.addChildren(Array.isArray(t)?t:arguments)},_changed:function ft(e){ft.base.call(this,e),1026&e&&(this._clipItem=t)},_getClipItem:function(){var e=this._clipItem;if(e===t){e=null;for(var n=0,i=this._children.length;i>n;n++){var r=this._children[n];if(r._clipMask){e=r;break}}this._clipItem=e}return e},isClipped:function(){return!!this._getClipItem()},setClipped:function(t){var e=this.getFirstChild();e&&e.setClipMask(t)},_draw:function(t,e){var n=e.clip,i=!n&&this._getClipItem(),r=!0;if(e=e.extend({clipItem:i,clip:!1}),n?this._currentPath?(t.currentPath=this._currentPath,r=!1):(t.beginPath(),e.dontStart=e.dontFinish=!0):i&&i.draw(t,e.extend({clip:!0})),r)for(var s=0,a=this._children.length;a>s;s++){var o=this._children[s];o!==i&&o.draw(t,e)}n&&(this._currentPath=t.currentPath)}}),C=b.extend({_class:"Layer",initialize:function(n){var i=e.isPlainObject(n)?new e(n):{children:Array.isArray(n)?n:arguments},r=i.insert;i.insert=!1,b.call(this,i),(r||r===t)&&(this._project.addChild(this),this.activate())},_remove:function _t(t,n){if(this._parent)return _t.base.call(this,t,n);if(null!=this._index){var i=this._project;return i._activeLayer===this&&(i._activeLayer=this.getNextSibling()||this.getPreviousSibling()),e.splice(i.layers,null,this._index,1),this._installEvents(!1),t&&i._changes&&this._changed(5),n&&(i._needsUpdate=!0),!0}return!1},getNextSibling:function gt(){return this._parent?gt.base.call(this):this._project.layers[this._index+1]||null},getPreviousSibling:function pt(){return this._parent?pt.base.call(this):this._project.layers[this._index-1]||null},isInserted:function vt(){return this._parent?vt.base.call(this):null!=this._index},activate:function(){this._project._activeLayer=this},_insertSibling:function mt(t,e,n){return this._parent?mt.base.call(this,t,e,n):this._project.insertChild(t,e,n)}}),S=x.extend({_class:"Shape",_applyMatrix:!1,_canApplyMatrix:!1,_boundsSelected:!0,_serializeFields:{type:null,size:null,radius:null},initialize:function(t){this._initialize(t)},_equals:function(t){return this._type===t._type&&this._size.equals(t._size)&&e.equals(this._radius,t._radius); -},clone:function(t){var e=new S(x.NO_INSERT);return e.setType(this._type),e.setSize(this._size),e.setRadius(this._radius),this._clone(e,t)},getType:function(){return this._type},setType:function(t){this._type=t},getShape:"#getType",setShape:"#setType",getSize:function(){var t=this._size;return new f(t.width,t.height,this,"setSize")},setSize:function(){var t=d.read(arguments);if(this._size){if(!this._size.equals(t)){var e=this._type,n=t.width,i=t.height;if("rectangle"===e){var r=d.min(this._radius,t.divide(2));this._radius.set(r.width,r.height)}else"circle"===e?(n=i=(n+i)/2,this._radius=n/2):"ellipse"===e&&this._radius.set(n/2,i/2);this._size.set(n,i),this._changed(9)}}else this._size=t.clone()},getRadius:function(){var t=this._radius;return"circle"===this._type?t:new f(t.width,t.height,this,"setRadius")},setRadius:function(t){var e=this._type;if("circle"===e){if(t===this._radius)return;var n=2*t;this._radius=t,this._size.set(n,n)}else if(t=d.read(arguments),this._radius){if(this._radius.equals(t))return;if(this._radius.set(t.width,t.height),"rectangle"===e){var n=d.max(this._size,t.multiply(2));this._size.set(n.width,n.height)}else"ellipse"===e&&this._size.set(2*t.width,2*t.height)}else this._radius=t.clone();this._changed(9)},isEmpty:function(){return!1},toPath:function(t){var n=this._clone(new(E[e.capitalize(this._type)])({center:new u,size:this._size,radius:this._radius,insert:!1}),t);return paper.settings.applyMatrix&&n.setApplyMatrix(!0),n},_draw:function(t,e,n){var i=this._style,r=i.hasFill(),s=i.hasStroke(),a=e.dontFinish||e.clip,o=!n;if(r||s||a){var h=this._type,u=this._radius,l="circle"===h;if(e.dontStart||t.beginPath(),o&&l)t.arc(0,0,u,0,2*Math.PI,!0);else{var c=l?u:u.width,d=l?u:u.height,f=this._size,_=f.width,g=f.height;if(o&&"rect"===h&&0===c&&0===d)t.rect(-_/2,-g/2,_,g);else{var p=_/2,v=g/2,m=.44771525016920644,y=c*m,w=d*m,x=[-p,-v+d,-p,-v+w,-p+y,-v,-p+c,-v,p-c,-v,p-y,-v,p,-v+w,p,-v+d,p,v-d,p,v-w,p-y,v,p-c,v,-p+c,v,-p+y,v,-p,v-w,-p,v-d];n&&n.transform(x,x,32),t.moveTo(x[0],x[1]),t.bezierCurveTo(x[2],x[3],x[4],x[5],x[6],x[7]),p!==c&&t.lineTo(x[8],x[9]),t.bezierCurveTo(x[10],x[11],x[12],x[13],x[14],x[15]),v!==d&&t.lineTo(x[16],x[17]),t.bezierCurveTo(x[18],x[19],x[20],x[21],x[22],x[23]),p!==c&&t.lineTo(x[24],x[25]),t.bezierCurveTo(x[26],x[27],x[28],x[29],x[30],x[31])}}t.closePath()}a||!r&&!s||(this._setStyles(t),r&&(t.fill(i.getWindingRule()),t.shadowColor="rgba(0,0,0,0)"),s&&t.stroke())},_canComposite:function(){return!(this.hasFill()&&this.hasStroke())},_getBounds:function(t,e){var n=new _(this._size).setCenter(0,0);return"getBounds"!==t&&this.hasStroke()&&(n=n.expand(this.getStrokeWidth())),e?e._transformBounds(n):n}},new function(){function t(t,e,n){var i=t._radius;if(!i.isZero())for(var r=t._size.divide(2),s=0;4>s;s++){var a=new u(1&s?1:-1,s>1?1:-1),o=a.multiply(r),h=o.subtract(a.multiply(i)),l=new _(o,h);if((n?l.expand(n):l).contains(e))return h}}function e(t,e){var n=t.getAngleInRadians(),i=2*e.width,r=2*e.height,s=i*Math.sin(n),a=r*Math.cos(n);return i*r/(2*Math.sqrt(s*s+a*a))}return{_contains:function n(e){if("rectangle"===this._type){var i=t(this,e);return i?e.subtract(i).divide(this._radius).getLength()<=1:n.base.call(this,e)}return e.divide(this.size).getLength()<=.5},_hitTestSelf:function i(n,r){var s=!1;if(this.hasStroke()){var a=this._type,o=this._radius,h=this.getStrokeWidth()+2*r.tolerance;if("rectangle"===a){var u=t(this,n,h);if(u){var l=n.subtract(u);s=2*Math.abs(l.getLength()-e(l,o))<=h}else{var c=new _(this._size).setCenter(0,0),d=c.expand(h),f=c.expand(-h);s=d._containsPoint(n)&&!f._containsPoint(n)}}else"ellipse"===a&&(o=e(n,o)),s=2*Math.abs(n.getLength()-o)<=h}return s?new I("stroke",this):i.base.apply(this,arguments)}}},{statics:new function(){function t(t,n,i,r,s){var a=new S(e.getNamed(s));return a._type=t,a._size=i,a._radius=r,a.translate(n)}return{Circle:function(){var n=u.readNamed(arguments,"center"),i=e.readNamed(arguments,"radius");return t("circle",n,new d(2*i),i,arguments)},Rectangle:function(){var e=_.readNamed(arguments,"rectangle"),n=d.min(d.readNamed(arguments,"radius"),e.getSize(!0).divide(2));return t("rectangle",e.getCenter(!0),e.getSize(!0),n,arguments)},Ellipse:function(){var e=S._readEllipse(arguments),n=e.radius;return t("ellipse",e.center,n.multiply(2),n,arguments)},_readEllipse:function(t){var n,i;if(e.hasNamed(t,"radius"))n=u.readNamed(t,"center"),i=d.readNamed(t,"radius");else{var r=_.readNamed(t,"rectangle");n=r.getCenter(!0),i=r.getSize(!0).divide(2)}return{center:n,radius:i}}}}}),P=x.extend({_class:"Raster",_applyMatrix:!1,_canApplyMatrix:!1,_boundsGetter:"getBounds",_boundsSelected:!0,_serializeFields:{source:null},initialize:function(e,n){this._initialize(e,n!==t&&u.read(arguments,1))||("string"==typeof e?this.setSource(e):this.setImage(e)),this._size||(this._size=new d,this._loaded=!1)},_equals:function(t){return this.getSource()===t.getSource()},clone:function(t){var e=new P(x.NO_INSERT),n=this._image,i=this._canvas;if(n)e.setImage(n);else if(i){var r=et.getCanvas(this._size);r.getContext("2d").drawImage(i,0,0),e.setImage(r)}return this._clone(e,t)},getSize:function(){var t=this._size;return new f(t?t.width:0,t?t.height:0,this,"setSize")},setSize:function(){var t=d.read(arguments);if(!t.equals(this._size))if(t.width>0&&t.height>0){var e=this.getElement();this.setImage(et.getCanvas(t)),e&&this.getContext(!0).drawImage(e,0,0,t.width,t.height)}else this._canvas&&et.release(this._canvas),this._size=t.clone()},getWidth:function(){return this._size?this._size.width:0},setWidth:function(t){this.setSize(t,this.getHeight())},getHeight:function(){return this._size?this._size.height:0},setHeight:function(t){this.setSize(this.getWidth(),t)},isEmpty:function(){var t=this._size;return!t||0===t.width&&0===t.height},getResolution:function(){var t=this._matrix,e=new u(0,0).transform(t),n=new u(1,0).transform(t).subtract(e),i=new u(0,1).transform(t).subtract(e);return new d(72/n.getLength(),72/i.getLength())},getPpi:"#getResolution",getImage:function(){return this._image},setImage:function(t){this._canvas&&et.release(this._canvas),t&&t.getContext?(this._image=null,this._canvas=t,this._loaded=!0):(this._image=t,this._canvas=null,this._loaded=t&&t.complete),this._size=new d(t?t.naturalWidth||t.width:0,t?t.naturalHeight||t.height:0),this._context=null,this._changed(521)},getCanvas:function(){if(!this._canvas){var t=et.getContext(this._size);try{this._image&&t.drawImage(this._image,0,0),this._canvas=t.canvas}catch(e){et.release(t)}}return this._canvas},setCanvas:"#setImage",getContext:function(t){return this._context||(this._context=this.getCanvas().getContext("2d")),t&&(this._image=null,this._changed(513)),this._context},setContext:function(t){this._context=t},getSource:function(){return this._image&&this._image.src||this.toDataURL()},setSource:function(t){function e(){var t=i.getView();t&&(paper=t._scope,i.setImage(n),i.emit("load"),t.update())}var n,i=this;n=document.getElementById(t)||new Image,n.naturalWidth&&n.naturalHeight?setTimeout(e,0):(H.add(n,{load:e}),n.src||(n.src=t)),this.setImage(n)},getElement:function(){return this._canvas||this._loaded&&this._image}},{beans:!1,getSubCanvas:function(){var t=_.read(arguments),e=et.getContext(t.getSize());return e.drawImage(this.getCanvas(),t.x,t.y,t.width,t.height,0,0,t.width,t.height),e.canvas},getSubRaster:function(){var t=_.read(arguments),e=new P(x.NO_INSERT);return e.setImage(this.getSubCanvas(t)),e.translate(t.getCenter().subtract(this.getSize().divide(2))),e._matrix.preConcatenate(this._matrix),e.insertAbove(this),e},toDataURL:function(){var t=this._image&&this._image.src;if(/^data:/.test(t))return t;var e=this.getCanvas();return e?e.toDataURL():null},drawImage:function(t){var e=u.read(arguments,1);this.getContext(!0).drawImage(t,e.x,e.y)},getAverageColor:function(t){var n,i;t?t instanceof L?(i=t,n=t.getBounds()):t.width?n=new _(t):t.x&&(n=new _(t.x-.5,t.y-.5,1,1)):n=this.getBounds();var r=32,s=Math.min(n.width,r),a=Math.min(n.height,r),o=P._sampleContext;o?o.clearRect(0,0,r+1,r+1):o=P._sampleContext=et.getContext(new d(r)),o.save();var h=(new p).scale(s/n.width,a/n.height).translate(-n.x,-n.y);h.applyToContext(o),i&&i.draw(o,new e({clip:!0,matrices:[h]})),this._matrix.applyToContext(o);var u=this.getElement(),l=this._size;u&&o.drawImage(u,-l.width/2,-l.height/2),o.restore();for(var c=o.getImageData(.5,.5,Math.ceil(s),Math.ceil(a)).data,f=[0,0,0],g=0,v=0,m=c.length;m>v;v+=4){var y=c[v+3];g+=y,y/=255,f[0]+=c[v]*y,f[1]+=c[v+1]*y,f[2]+=c[v+2]*y}for(var v=0;3>v;v++)f[v]/=g;return g?F.read(f):null},getPixel:function(){var t=u.read(arguments),e=this.getContext().getImageData(t.x,t.y,1,1).data;return new F("rgb",[e[0]/255,e[1]/255,e[2]/255],e[3]/255)},setPixel:function(){var t=u.read(arguments),e=F.read(arguments),n=e._convert("rgb"),i=e._alpha,r=this.getContext(!0),s=r.createImageData(1,1),a=s.data;a[0]=255*n[0],a[1]=255*n[1],a[2]=255*n[2],a[3]=null!=i?255*i:255,r.putImageData(s,t.x,t.y)},createImageData:function(){var t=d.read(arguments);return this.getContext().createImageData(t.width,t.height)},getImageData:function(){var t=_.read(arguments);return t.isEmpty()&&(t=new _(this._size)),this.getContext().getImageData(t.x,t.y,t.width,t.height)},setImageData:function(t){var e=u.read(arguments,1);this.getContext(!0).putImageData(t,e.x,e.y)},_getBounds:function(t,e){var n=new _(this._size).setCenter(0,0);return e?e._transformBounds(n):n},_hitTestSelf:function(t){if(this._contains(t)){var e=this;return new I("pixel",e,{offset:t.add(e._size.divide(2)).round(),color:{get:function(){return e.getPixel(this.offset)}}})}},_draw:function(t){var e=this.getElement();e&&(t.globalAlpha=this._opacity,t.drawImage(e,-this._size.width/2,-this._size.height/2))},_canComposite:function(){return!0}}),M=x.extend({_class:"PlacedSymbol",_applyMatrix:!1,_canApplyMatrix:!1,_boundsGetter:{getBounds:"getStrokeBounds"},_boundsSelected:!0,_serializeFields:{symbol:null},initialize:function(e,n){this._initialize(e,n!==t&&u.read(arguments,1))||this.setSymbol(e instanceof w?e:new w(e))},_equals:function(t){return this._symbol===t._symbol},getSymbol:function(){return this._symbol},setSymbol:function(t){this._symbol=t,this._changed(9)},clone:function(t){var e=new M(x.NO_INSERT);return e.setSymbol(this._symbol),this._clone(e,t)},isEmpty:function(){return this._symbol._definition.isEmpty()},_getBounds:function(t,e,n){var i=this.symbol._definition;return i._getCachedBounds(t,e&&e.chain(i._matrix),n)},_hitTestSelf:function(t,e){var n=this._symbol._definition._hitTest(t,e);return n&&(n.item=this),n},_draw:function(t,e){this.symbol._definition.draw(t,e)}}),I=e.extend({_class:"HitResult",initialize:function(t,e,n){this.type=t,this.item=e,n&&(n.enumerable=!0,this.inject(n))},statics:{getOptions:function(t){return new e({type:null,tolerance:paper.settings.hitTolerance,fill:!t,stroke:!t,segments:!t,handles:!1,ends:!1,center:!1,bounds:!1,guides:!1,selected:!1},t)}}}),z=e.extend({_class:"Segment",beans:!0,initialize:function(e,n,i,r,s,a){var o,h,u,l=arguments.length;0===l||(1===l?e.point?(o=e.point,h=e.handleIn,u=e.handleOut):o=e:2===l&&"number"==typeof e?o=arguments:3>=l?(o=e,h=n,u=i):(o=e!==t?[e,n]:null,h=i!==t?[i,r]:null,u=s!==t?[s,a]:null)),new A(o,this,"_point"),new A(h,this,"_handleIn"),new A(u,this,"_handleOut")},_serialize:function(t){return e.serialize(this.isLinear()?this._point:[this._point,this._handleIn,this._handleOut],t,!0)},_changed:function(t){var e=this._path;if(e){var n,i=e._curves,r=this._index;i&&(t&&t!==this._point&&t!==this._handleIn||!(n=r>0?i[r-1]:e._closed?i[i.length-1]:null)||n._changed(),t&&t!==this._point&&t!==this._handleOut||!(n=i[r])||n._changed()),e._changed(25)}},getPoint:function(){return this._point},setPoint:function(){var t=u.read(arguments);this._point.set(t.x,t.y)},getHandleIn:function(){return this._handleIn},setHandleIn:function(){var t=u.read(arguments);this._handleIn.set(t.x,t.y)},getHandleOut:function(){return this._handleOut},setHandleOut:function(){var t=u.read(arguments);this._handleOut.set(t.x,t.y)},isLinear:function(){return this._handleIn.isZero()&&this._handleOut.isZero()},setLinear:function(t){t&&(this._handleIn.set(0,0),this._handleOut.set(0,0))},isCollinear:function(t){var e=this.getNext(),n=t.getNext();return this._handleOut.isZero()&&e._handleIn.isZero()&&t._handleOut.isZero()&&n._handleIn.isZero()&&e._point.subtract(this._point).isCollinear(n._point.subtract(t._point))},isColinear:"#isCollinear",isOrthogonal:function(){var t=this.getPrevious(),e=this.getNext();return t._handleOut.isZero()&&this._handleIn.isZero()&&this._handleOut.isZero()&&e._handleIn.isZero()&&this._point.subtract(t._point).isOrthogonal(e._point.subtract(this._point))},isArc:function(){var t=this.getNext(),e=this._handleOut,n=t._handleIn,i=.5522847498307936;if(e.isOrthogonal(n)){var r=this._point,s=t._point,a=new v(r,e,!0).intersect(new v(s,n,!0),!0);return a&&o.isZero(e.getLength()/a.subtract(r).getLength()-i)&&o.isZero(n.getLength()/a.subtract(s).getLength()-i)}return!1},_selectionState:0,isSelected:function(t){var e=this._selectionState;return t?t===this._point?!!(4&e):t===this._handleIn?!!(1&e):t===this._handleOut?!!(2&e):!1:!!(7&e)},setSelected:function(t,e){var n=this._path,t=!!t,i=this._selectionState,r=i,s=e?e===this._point?4:e===this._handleIn?1:e===this._handleOut?2:0:7;t?i|=s:i&=~s,this._selectionState=i,n&&i!==r&&(n._updateSelection(this,r,i),n._changed(129))},getIndex:function(){return this._index!==t?this._index:null},getPath:function(){return this._path||null},getCurve:function(){var t=this._path,e=this._index;return t?(e>0&&!t._closed&&e===t._segments.length-1&&e--,t.getCurves()[e]||null):null},getLocation:function(){var t=this.getCurve();return t?new T(t,this===t._segment1?0:1):null},getNext:function(){var t=this._path&&this._path._segments;return t&&(t[this._index+1]||this._path._closed&&t[0])||null},getPrevious:function(){var t=this._path&&this._path._segments;return t&&(t[this._index-1]||this._path._closed&&t[t.length-1])||null},reverse:function(){return new z(this._point,this._handleOut,this._handleIn)},remove:function(){return this._path?!!this._path.removeSegment(this._index):!1},clone:function(){return new z(this._point,this._handleIn,this._handleOut)},equals:function(t){return t===this||t&&this._class===t._class&&this._point.equals(t._point)&&this._handleIn.equals(t._handleIn)&&this._handleOut.equals(t._handleOut)||!1},toString:function(){var t=["point: "+this._point];return this._handleIn.isZero()||t.push("handleIn: "+this._handleIn),this._handleOut.isZero()||t.push("handleOut: "+this._handleOut),"{ "+t.join(", ")+" }"},transform:function(t){this._transformCoordinates(t,Array(6),!0),this._changed()},_transformCoordinates:function(t,e,n){var i=this._point,r=n&&this._handleIn.isZero()?null:this._handleIn,s=n&&this._handleOut.isZero()?null:this._handleOut,a=i._x,o=i._y,h=2;return e[0]=a,e[1]=o,r&&(e[h++]=r._x+a,e[h++]=r._y+o),s&&(e[h++]=s._x+a,e[h++]=s._y+o),t&&(t._transformCoordinates(e,e,h/2),a=e[0],o=e[1],n?(i._x=a,i._y=o,h=2,r&&(r._x=e[h++]-a,r._y=e[h++]-o),s&&(s._x=e[h++]-a,s._y=e[h++]-o)):(r||(e[h++]=a,e[h++]=o),s||(e[h++]=a,e[h++]=o))),e}}),A=u.extend({initialize:function(e,n,i){var r,s,a;if(e)if((r=e[0])!==t)s=e[1];else{var o=e;(r=o.x)===t&&(o=u.read(arguments),r=o.x),s=o.y,a=o.selected}else r=s=0;this._x=r,this._y=s,this._owner=n,n[i]=this,a&&this.setSelected(!0)},set:function(t,e){return this._x=t,this._y=e,this._owner._changed(this),this},_serialize:function(t){var e=t.formatter,n=e.number(this._x),i=e.number(this._y);return this.isSelected()?{x:n,y:i,selected:!0}:[n,i]},getX:function(){return this._x},setX:function(t){this._x=t,this._owner._changed(this)},getY:function(){return this._y},setY:function(t){this._y=t,this._owner._changed(this)},isZero:function(){return o.isZero(this._x)&&o.isZero(this._y)},setSelected:function(t){this._owner.setSelected(t,this)},isSelected:function(){return this._owner.isSelected(this)}}),O=e.extend({_class:"Curve",initialize:function(t,e,n,i,r,s,a,o){var h=arguments.length;if(3===h)this._path=t,this._segment1=e,this._segment2=n;else if(0===h)this._segment1=new z,this._segment2=new z;else if(1===h)this._segment1=new z(t.segment1),this._segment2=new z(t.segment2);else if(2===h)this._segment1=new z(t),this._segment2=new z(e);else{var u,l,c,d;4===h?(u=t,l=e,c=n,d=i):8===h&&(u=[t,e],d=[a,o],l=[n-t,i-e],c=[r-a,s-o]),this._segment1=new z(u,null,l),this._segment2=new z(d,c,null)}},_changed:function(){this._length=this._bounds=t},getPoint1:function(){return this._segment1._point},setPoint1:function(){var t=u.read(arguments);this._segment1._point.set(t.x,t.y)},getPoint2:function(){return this._segment2._point},setPoint2:function(){var t=u.read(arguments);this._segment2._point.set(t.x,t.y)},getHandle1:function(){return this._segment1._handleOut},setHandle1:function(){var t=u.read(arguments);this._segment1._handleOut.set(t.x,t.y)},getHandle2:function(){return this._segment2._handleIn},setHandle2:function(){var t=u.read(arguments);this._segment2._handleIn.set(t.x,t.y)},getSegment1:function(){return this._segment1},getSegment2:function(){return this._segment2},getPath:function(){return this._path},getIndex:function(){return this._segment1._index},getNext:function(){var t=this._path&&this._path._curves;return t&&(t[this._segment1._index+1]||this._path._closed&&t[0])||null},getPrevious:function(){var t=this._path&&this._path._curves;return t&&(t[this._segment1._index-1]||this._path._closed&&t[t.length-1])||null},isSelected:function(){return this.getPoint1().isSelected()&&this.getHandle2().isSelected()&&this.getHandle2().isSelected()&&this.getPoint2().isSelected()},setSelected:function(t){this.getPoint1().setSelected(t),this.getHandle1().setSelected(t),this.getHandle2().setSelected(t),this.getPoint2().setSelected(t)},getValues:function(t){return O.getValues(this._segment1,this._segment2,t)},getPoints:function(){for(var t=this.getValues(),e=[],n=0;8>n;n+=2)e.push(new u(t[n],t[n+1]));return e},getLength:function(){return null==this._length&&(this._length=this.isLinear()?this._segment2._point.getDistance(this._segment1._point):O.getLength(this.getValues(),0,1)),this._length},getArea:function(){return O.getArea(this.getValues())},getPart:function(t,e){return new O(O.getPart(this.getValues(),t,e))},getPartLength:function(t,e){return O.getLength(this.getValues(),t,e)},isLinear:function(){return this._segment1._handleOut.isZero()&&this._segment2._handleIn.isZero()},getIntersections:function(t){return O.filterIntersections(O.getIntersections(this.getValues(),t.getValues(),this,t,[]))},_getParameter:function(e,n){return n?e:e&&e.curve===this?e.parameter:e===t&&n===t?.5:this.getParameterAt(e,0)},divide:function(t,e,n){var i=this._getParameter(t,e),r=1e-6,s=null;if(i>r&&1-r>i){var a=O.subdivide(this.getValues(),i),o=n?!1:this.isLinear(),h=a[0],l=a[1];o||(this._segment1._handleOut.set(h[2]-h[0],h[3]-h[1]),this._segment2._handleIn.set(l[4]-l[6],l[5]-l[7]));var c=h[6],d=h[7],f=new z(new u(c,d),!o&&new u(h[4]-c,h[5]-d),!o&&new u(l[2]-c,l[3]-d));if(this._path)this._segment1._index>0&&0===this._segment2._index?this._path.add(f):this._path.insert(this._segment2._index,f),s=this;else{var _=this._segment2;this._segment2=f,s=new O(f,_)}}return s},split:function(t,e){return this._path?this._path.split(this._segment1._index,this._getParameter(t,e)):null},reverse:function(){return new O(this._segment2.reverse(),this._segment1.reverse())},remove:function(){var t=!1;if(this._path){var e=this._segment2,n=e._handleOut;t=e.remove(),t&&this._segment1._handleOut.set(n.x,n.y)}return t},clone:function(){return new O(this._segment1,this._segment2)},toString:function(){var t=["point1: "+this._segment1._point];return this._segment1._handleOut.isZero()||t.push("handle1: "+this._segment1._handleOut),this._segment2._handleIn.isZero()||t.push("handle2: "+this._segment2._handleIn),t.push("point2: "+this._segment2._point),"{ "+t.join(", ")+" }"},statics:{getValues:function(t,e,n){var i=t._point,r=t._handleOut,s=e._handleIn,a=e._point,o=[i._x,i._y,i._x+r._x,i._y+r._y,a._x+s._x,a._y+s._y,a._x,a._y];return n&&n._transformCoordinates(o,o,4),o},evaluate:function(t,e,n){if(null==e||0>e||e>1)return null;var i,r,s=t[0],a=t[1],o=t[2],h=t[3],l=t[4],c=t[5],d=t[6],f=t[7],_=1e-6;if(0===n&&(_>e||e>1-_)){var g=_>e;i=g?s:d,r=g?a:f}else{var p=3*(o-s),v=3*(l-o)-p,m=d-s-p-v,y=3*(h-a),w=3*(c-h)-y,x=f-a-y-w;if(0===n)i=((m*e+v)*e+p)*e+s,r=((x*e+w)*e+y)*e+a;else if(_>e&&o===s&&h===a||e>1-_&&l===d&&c===f?(i=l-o,r=c-h):_>e?(i=p,r=y):e>1-_?(i=3*(d-l),r=3*(f-c)):(i=(3*m*e+2*v)*e+p,r=(3*x*e+2*w)*e+y),3===n){var b=6*m*e+2*v,C=6*x*e+2*w;return(i*C-r*b)/Math.pow(i*i+r*r,1.5)}}return 2===n?new u(r,-i):new u(i,r)},subdivide:function(e,n){var i=e[0],r=e[1],s=e[2],a=e[3],o=e[4],h=e[5],u=e[6],l=e[7];n===t&&(n=.5);var c=1-n,d=c*i+n*s,f=c*r+n*a,_=c*s+n*o,g=c*a+n*h,p=c*o+n*u,v=c*h+n*l,m=c*d+n*_,y=c*f+n*g,w=c*_+n*p,x=c*g+n*v,b=c*m+n*w,C=c*y+n*x;return[[i,r,d,f,m,y,b,C],[b,C,w,x,p,v,u,l]]},solveCubic:function(t,e,n,i,r,s){var a=t[e],h=t[e+2],u=t[e+4],l=t[e+6],c=3*(h-a),d=3*(u-h)-c,f=l-a-c-d,_=o.isZero;return _(f)&&_(d)&&(f=d=0),o.solveCubic(f,d,c,a-n,i,r,s)},getParameterOf:function(t,e,n){var i=1e-6;if(Math.abs(t[0]-e)l;)if(-1===h||(r=a[l++])>0&&1>r){for(var c=0;-1===u||u>c;)if((-1===u||(s=o[c++])>0&&1>s)&&(-1===h?r=s:-1===u&&(s=r),Math.abs(r-s)0&&(t=O.subdivide(t,e)[1]),1>n&&(t=O.subdivide(t,(n-e)/(1-e))[0]),t},isLinear:function(t){var e=o.isZero;return e(t[0]-t[2])&&e(t[1]-t[3])&&e(t[4]-t[6])&&e(t[5]-t[7])},isFlatEnough:function(t,e){var n=t[0],i=t[1],r=t[2],s=t[3],a=t[4],o=t[5],h=t[6],u=t[7],l=3*r-2*n-h,c=3*s-2*i-u,d=3*a-2*h-n,f=3*o-2*u-i;return Math.max(l*l,d*d)+Math.max(c*c,f*f)<10*e*e},getArea:function(t){var e=t[0],n=t[1],i=t[2],r=t[3],s=t[4],a=t[5],o=t[6],h=t[7];return(3*r*e-1.5*r*s-1.5*r*o-3*n*i-1.5*n*s-.5*n*o+1.5*a*e+1.5*a*i-3*a*o+.5*h*e+1.5*h*i+3*h*s)/10},getEdgeSum:function(t){return(t[0]-t[2])*(t[3]+t[1])+(t[2]-t[4])*(t[5]+t[3])+(t[4]-t[6])*(t[7]+t[5])},getBounds:function(t){for(var e=t.slice(0,2),n=e.slice(),i=[0,0],r=0;2>r;r++)O._addBounds(t[r],t[r+2],t[r+4],t[r+6],r,0,e,n,i);return new _(e[0],e[1],n[0]-e[0],n[1]-e[1])},_addBounds:function(t,e,n,i,r,s,a,h,u){function l(t,e){var n=t-e,i=t+e;nh[r]&&(h[r]=i)}var c=3*(e-n)-t+i,d=2*(t+n)-4*e,f=e-t,_=o.solveQuadratic(c,d,f,u),g=1e-6,p=1-g;l(i,0);for(var v=0;_>v;v++){var m=u[v],y=1-m;m>g&&p>m&&l(y*y*y*t+3*y*y*m*e+3*y*m*m*n+m*m*m*i,s)}}}},e.each(["getBounds","getStrokeBounds","getHandleBounds","getRoughBounds"],function(t){this[t]=function(){this._bounds||(this._bounds={});var e=this._bounds[t];return e||(e=this._bounds[t]=E[t]([this._segment1,this._segment2],!1,this._path.getStyle())),e.clone()}},{}),e.each(["getPoint","getTangent","getNormal","getCurvature"],function(t,e){this[t+"At"]=function(t,n){var i=this.getValues();return O.evaluate(i,n?t:O.getParameterAt(i,t,0),e)},this[t]=function(t){return O.evaluate(this.getValues(),t,e)}},{beans:!1,getParameterAt:function(t,e){return O.getParameterAt(this.getValues(),t,e)},getParameterOf:function(){var t=u.read(arguments);return O.getParameterOf(this.getValues(),t.x,t.y)},getLocationAt:function(t,e){var n=e?t:this.getParameterAt(t);return null!=n&&n>=0&&1>=n?new T(this,n):null},getLocationOf:function(){return this.getLocationAt(this.getParameterOf(u.read(arguments)),!0)},getOffsetOf:function(){var t=this.getLocationOf.apply(this,arguments);return t?t.getOffset():null},getNearestLocation:function(){function t(t){if(t>=0&&1>=t){var i=e.getDistance(O.evaluate(n,t,0),!0);if(r>i)return r=i,s=t,!0}}for(var e=u.read(arguments),n=this.getValues(),i=100,r=1/0,s=0,a=0;i>=a;a++)t(a/i);for(var o=1/(2*i);o>1e-6;)t(s-o)||t(s+o)||(o/=2);var h=O.evaluate(n,s,0);return new T(this,s,h,null,null,null,e.getDistance(h))},getNearestPoint:function(){return this.getNearestLocation.apply(this,arguments).getPoint()}}),new function(){function e(t){var e=t[0],n=t[1],i=t[2],r=t[3],s=t[4],a=t[5],o=t[6],h=t[7],u=9*(i-s)+3*(o-e),l=6*(e+s)-12*i,c=3*(i-e),d=9*(r-a)+3*(h-n),f=6*(n+a)-12*r,_=3*(r-n);return function(t){var e=(u*t+l)*t+c,n=(d*t+f)*t+_;return Math.sqrt(e*e+n*n)}}function n(t,e){return Math.max(2,Math.min(16,Math.ceil(32*Math.abs(e-t))))}return{statics:!0,getLength:function(i,r,s){r===t&&(r=0),s===t&&(s=1);var a=o.isZero;if(0===r&&1===s&&a(i[0]-i[2])&&a(i[1]-i[3])&&a(i[6]-i[4])&&a(i[7]-i[5])){var h=i[6]-i[0],u=i[7]-i[1];return Math.sqrt(h*h+u*u)}var l=e(i);return o.integrate(l,r,s,n(r,s))},getParameterAt:function(i,r,s){function a(t){return p+=o.integrate(f,s,t,n(s,t)),s=t,p-r}if(s===t&&(s=0>r?1:0),0===r)return s;var h=1e-6,u=Math.abs,l=r>0,c=l?s:0,d=l?1:s,f=e(i),_=o.integrate(f,c,d,n(c,d));if(u(r-_)_)return null;var g=r/_,p=0;return o.findRoot(a,f,s+g,c,d,16,h)}}},new function(){function t(t,e,n,i,r,s,a,o){var h=new T(n,i,r,s,a,o);(!e||e(h))&&t.push(h)}function e(r,s,a,o,h,u,l,c,d,f,_,g,p){if(!(p>32)){var m,y,w,x=s[0],b=s[1],C=s[6],S=s[7],P=1e-6,k=v.getSignedDistance,M=k(x,b,C,S,s[2],s[3])||0,I=k(x,b,C,S,s[4],s[5])||0,z=M*I>0?.75:4/9,A=z*Math.min(0,M,I),T=z*Math.max(0,M,I),L=k(x,b,C,S,r[0],r[1]),E=k(x,b,C,S,r[2],r[3]),N=k(x,b,C,S,r[4],r[5]),B=k(x,b,C,S,r[6],r[7]);if(x===C&&P>f-d&&p>3)y=m=(c+l)/2,w=0;else{var j,R,D=n(L,E,N,B),F=D[0],q=D[1];if(j=i(F,q,A,T),F.reverse(),q.reverse(),R=i(F,q,A,T),null==j||null==R)return;r=O.getPart(r,j,R),w=R-j,m=c*j+l*(1-j),y=c*R+l*(1-R)}if(_>.5&&w>.5)if(y-m>f-d){var V=O.subdivide(r,.5),Z=m+(y-m)/2;e(s,V[0],o,a,h,u,d,f,m,Z,w,!g,++p),e(s,V[1],o,a,h,u,d,f,Z,y,w,!g,p)}else{var V=O.subdivide(s,.5),Z=d+(f-d)/2;e(V[0],r,o,a,h,u,d,Z,m,y,w,!g,++p),e(V[1],r,o,a,h,u,Z,f,m,y,w,!g,p)}else if(Math.max(f-d,y-m)0&&e(s,r,o,a,h,u,d,f,m,y,w,!g,++p)}}function n(t,e,n,i){var r,s=[0,t],a=[1/3,e],o=[2/3,n],h=[1,i],u=v.getSignedDistance,l=u(0,t,1,i,1/3,e),c=u(0,t,1,i,2/3,n),d=!1;if(0>l*c)r=[[s,a,h],[s,o,h]],d=0>l;else{var f,_=0,g=0===l||0===c;Math.abs(l)>Math.abs(c)?(f=a,_=(i-n-(i-t)/3)*(2*(i-n)-i+e)/3):(f=o,_=(e-t+(t-i)/3)*(-2*(t-e)+t-n)/3),r=0>_||g?[[s,f,h],[s,h]]:[[s,a,o,h],[s,h]],d=l?0>l:0>c}return d?r.reverse():r}function i(t,e,n,i){return t[0][1]i?r(e,!1,i):t[0][0]}function r(t,e,n){for(var i=t[0][0],r=t[0][1],s=1,a=t.length;a>s;s++){var o=t[s][0],h=t[s][1];if(e?h>=n:n>=h)return i+(n-r)*(o-i)/(h-r);i=o,r=h}return null}function s(e,n,i,r,s,a){for(var o=O.isLinear(e),h=o?n:e,u=o?e:n,l=u[0],c=u[1],d=u[6],f=u[7],_=d-l,g=f-c,p=Math.atan2(-g,_),v=Math.sin(p),m=Math.cos(p),y=_*m-g*v,w=[0,0,0,0,y,0,y,0],x=[],b=0;8>b;b+=2){var C=h[b]-l,S=h[b+1]-c;x.push(C*m-S*v,S*m+C*v)}for(var P=[],k=O.solveCubic(x,1,0,P,0,1),b=0;k>b;b++){var M=P[b],C=O.evaluate(x,M,0).x;if(C>=0&&y>=C){var I=O.getParameterOf(w,C,0),z=o?I:M,A=o?M:I;t(s,a,i,z,O.evaluate(e,z,0),r,A,O.evaluate(n,A,0))}}}function a(e,n,i,r,s,a){var o=v.intersect(e[0],e[1],e[6],e[7],n[0],n[1],n[6],n[7]);if(o){var h=o.x,u=o.y;t(s,a,i,O.getParameterOf(e,h,u),o,r,O.getParameterOf(n,h,u),o)}}return{statics:{getIntersections:function(n,i,r,o,h,u){var l=O.isLinear(n),c=O.isLinear(i),d=r.getPoint1(),f=r.getPoint2(),_=o.getPoint1(),g=o.getPoint2(),p=1e-6;return d.isClose(_,p)&&t(h,u,r,0,d,o,0,d),d.isClose(g,p)&&t(h,u,r,0,d,o,1,d),(l&&c?a:l||c?s:e)(n,i,r,o,h,u,0,1,0,1,0,!1,0),f.isClose(_,p)&&t(h,u,r,1,f,o,0,f),f.isClose(g,p)&&t(h,u,r,1,f,o,1,f),h},filterIntersections:function(t,e){function n(t,e){var n=t.getPath(),i=e.getPath();return n===i?t.getIndex()+t.getParameter()-(e.getIndex()+e.getParameter()):n._id-i._id}for(var i=t.length-1,r=1-1e-6,s=i;s>=0;s--){var a=t[s],o=a._curve.getNext(),h=a._curve2.getNext();o&&a._parameter>=r&&(a._parameter=0,a._curve=o),h&&a._parameter2>=r&&(a._parameter2=0,a._curve2=h)}if(i>0){t.sort(n);for(var s=i;s>0;s--)t[s].equals(t[s-1])&&(t.splice(s,1),i--)}if(e){for(var s=i;s>=0;s--)t.push(t[s].getIntersection());t.sort(n)}return t}}}}),T=e.extend({_class:"CurveLocation",beans:!0,initialize:function yt(t,e,n,i,r,s,a){this._id=h.get(yt);var o=t._path;this._version=o?o._version:0,this._curve=t,this._parameter=e,this._point=n||t.getPointAt(e,!0),this._curve2=i,this._parameter2=r,this._point2=s,this._distance=a,this._segment1=t._segment1,this._segment2=t._segment2},getSegment:function(t){if(!this._segment){var e=this.getCurve(),n=this.getParameter();if(1===n)this._segment=e._segment2;else if(0===n||t)this._segment=e._segment1;else{if(null==n)return null;this._segment=e.getPartLength(0,n)_;_++)c[_]=a[_].getValues(h);for(var _=0;u>_;_++){var g=s[_],p=e?g.getValues(o):c[_];if(!e){var m=g.getSegment1(),y=g.getSegment2(),w=m._handleOut,x=y._handleIn;if(new v(m._point.subtract(w),w.multiply(2),!0).intersect(new v(y._point.subtract(x),x.multiply(2),!0),!1)){var b=O.subdivide(p);O.getIntersections(b[0],b[1],g,g,r,function(e){return e._parameter<=f?(e._parameter/=2,e._parameter2=.5+e._parameter2/2,!0):t})}}for(var C=e?0:_+1;l>C;C++)O.getIntersections(p,c[C],g,a[C],r,!e&&(C===_+1||C===l-1&&0===_)&&function(t){var e=t._parameter;return e>=d&&f>=e})}return O.filterIntersections(r,i)},_asPathItem:function(){return this},setPathData:function(t){function e(t,e){var n=+i[t];return o&&(n+=h[e]),n}function n(t){return new u(e(t,"x"),e(t+1,"y"))}var i,r,s,a=t.match(/[mlhvcsqtaz][^mlhvcsqtaz]*/gi),o=!1,h=new u,l=new u;this.clear();for(var c=0,f=a&&a.length;f>c;c++){var _=a[c],g=_[0],p=g.toLowerCase();i=_.match(/[+-]?(?:\d*\.\d+|\d+\.?)(?:[eE][+-]?\d+)?/g);var v=i&&i.length;switch(o=g===p,"z"!==r||/[mz]/.test(p)||this.moveTo(h=l), -p){case"m":case"l":for(var m="m"===p,y=0;v>y;y+=2)this[0===y&&m?"moveTo":"lineTo"](h=n(y));s=h,m&&(l=h);break;case"h":case"v":for(var w="h"===p?"x":"y",y=0;v>y;y++)h[w]=e(y,w),this.lineTo(h);s=h;break;case"c":for(var y=0;v>y;y+=6)this.cubicCurveTo(n(y),s=n(y+2),h=n(y+4));break;case"s":for(var y=0;v>y;y+=4)this.cubicCurveTo(/[cs]/.test(r)?h.multiply(2).subtract(s):h,s=n(y),h=n(y+2)),r=p;break;case"q":for(var y=0;v>y;y+=4)this.quadraticCurveTo(s=n(y),h=n(y+2));break;case"t":for(var y=0;v>y;y+=2)this.quadraticCurveTo(s=/[qt]/.test(r)?h.multiply(2).subtract(s):h,h=n(y)),r=p;break;case"a":for(var y=0;v>y;y+=7)this.arcTo(h=n(y+5),new d(+i[y],+i[y+1]),+i[y+2],+i[y+4],+i[y+3]);break;case"z":this.closePath(!0)}r=p}},_canComposite:function(){return!(this.hasFill()&&this.hasStroke())},_contains:function(t){var e=this._getWinding(t,!1,!0);return!!("evenodd"===this.getWindingRule()?1&e:e)}}),E=L.extend({_class:"Path",_serializeFields:{segments:[],closed:!1},initialize:function(e){this._closed=!1,this._segments=[],this._version=0;var n=Array.isArray(e)?"object"==typeof e[0]?e:arguments:!e||e.size!==t||e.x===t&&e.point===t?null:arguments;n&&n.length>0?this.setSegments(n):(this._curves=t,this._selectedSegmentState=0,n||"string"!=typeof e||(this.setPathData(e),e=null)),this._initialize(!n&&e)},_equals:function(t){return this._closed===t._closed&&e.equals(this._segments,t._segments)},clone:function(e){var n=new E(x.NO_INSERT);return n.setSegments(this._segments),n._closed=this._closed,this._clockwise!==t&&(n._clockwise=this._clockwise),this._clone(n,e)},_changed:function wt(e){if(wt.base.call(this,e),8&e){var n=this._parent;if(n&&(n._currentPath=t),this._length=this._clockwise=t,16&e)this._version++;else if(this._curves)for(var i=0,r=this._curves.length;r>i;i++)this._curves[i]._changed();this._monoCurves=t}else 32&e&&(this._bounds=t)},getStyle:function(){var t=this._parent;return(t instanceof N?t:this)._style},getSegments:function(){return this._segments},setSegments:function(e){var n=this.isFullySelected();this._segments.length=0,this._selectedSegmentState=0,this._curves=t,e&&e.length>0&&this._add(z.readAll(e)),n&&this.setFullySelected(!0)},getFirstSegment:function(){return this._segments[0]},getLastSegment:function(){return this._segments[this._segments.length-1]},getCurves:function(){var t=this._curves,e=this._segments;if(!t){var n=this._countCurves();t=this._curves=Array(n);for(var i=0;n>i;i++)t[i]=new O(this,e[i],e[i+1]||e[0])}return t},getFirstCurve:function(){return this.getCurves()[0]},getLastCurve:function(){var t=this.getCurves();return t[t.length-1]},isClosed:function(){return this._closed},setClosed:function(t){if(this._closed!=(t=!!t)){if(this._closed=t,this._curves){var e=this._curves.length=this._countCurves();t&&(this._curves[e-1]=new O(this,this._segments[e-1],this._segments[0]))}this._changed(25)}}},{beans:!0,getPathData:function(t,e){function n(e,n){e._transformCoordinates(t,g,!1),i=g[0],r=g[1],p?(v.push("M"+_.pair(i,r)),p=!1):(h=g[2],u=g[3],h===i&&u===r&&l===s&&c===o?n||v.push("l"+_.pair(i-s,r-o)):v.push("c"+_.pair(l-s,c-o)+" "+_.pair(h-s,u-o)+" "+_.pair(i-s,r-o))),s=i,o=r,l=g[4],c=g[5]}var i,r,s,o,h,u,l,c,d=this._segments,f=d.length,_=new a(e),g=Array(6),p=!0,v=[];if(0===f)return"";for(var m=0;f>m;m++)n(d[m]);return this._closed&&f>0&&(n(d[0],!0),v.push("z")),v.join("")}},{isEmpty:function(){return 0===this._segments.length},isPolygon:function(){for(var t=0,e=this._segments.length;e>t;t++)if(!this._segments[t].isLinear())return!1;return!0},_transformContent:function(t){for(var e=Array(6),n=0,i=this._segments.length;i>n;n++)this._segments[n]._transformCoordinates(t,e,!0);return!0},_add:function(t,e){for(var n=this._segments,i=this._curves,r=t.length,s=null==e,e=s?n.length:e,a=0;r>a;a++){var o=t[a];o._path&&(o=t[a]=o.clone()),o._path=this,o._index=e+a,o._selectionState&&this._updateSelection(o,0,o._selectionState)}if(s)n.push.apply(n,t);else{n.splice.apply(n,[e,0].concat(t));for(var a=e+r,h=n.length;h>a;a++)n[a]._index=a}if(i||t._curves){i||(i=this._curves=[]);var u=e>0?e-1:e,l=u,c=Math.min(u+r,this._countCurves());t._curves&&(i.splice.apply(i,[u,0].concat(t._curves)),l+=t._curves.length);for(var a=l;c>a;a++)i.splice(a,0,new O(this,null,null));this._adjustCurves(u,c)}return this._changed(25),t},_adjustCurves:function(t,e){for(var n,i=this._segments,r=this._curves,s=t;e>s;s++)n=r[s],n._path=this,n._segment1=i[s],n._segment2=i[s+1]||i[0],n._changed();(n=r[this._closed&&0===t?i.length-1:t-1])&&(n._segment2=i[t]||i[0],n._changed()),(n=r[e])&&(n._segment1=i[e],n._changed())},_countCurves:function(){var t=this._segments.length;return!this._closed&&t>0?t-1:t},add:function(t){return arguments.length>1&&"number"!=typeof t?this._add(z.readAll(arguments)):this._add([z.read(arguments)])[0]},insert:function(t,e){return arguments.length>2&&"number"!=typeof e?this._add(z.readAll(arguments,1),t):this._add([z.read(arguments,1)],t)[0]},addSegment:function(){return this._add([z.read(arguments)])[0]},insertSegment:function(t){return this._add([z.read(arguments,1)],t)[0]},addSegments:function(t){return this._add(z.readAll(t))},insertSegments:function(t,e){return this._add(z.readAll(e),t)},removeSegment:function(t){return this.removeSegments(t,t+1)[0]||null},removeSegments:function(t,n,i){t=t||0,n=e.pick(n,this._segments.length);var r=this._segments,s=this._curves,a=r.length,o=r.splice(t,n-t),h=o.length;if(!h)return o;for(var u=0;h>u;u++){var l=o[u];l._selectionState&&this._updateSelection(l,l._selectionState,0),l._index=l._path=null}for(var u=t,c=r.length;c>u;u++)r[u]._index=u;if(s){var d=t>0&&n===a+(this._closed?1:0)?t-1:t,s=s.splice(d,h);i&&(o._curves=s.slice(1)),this._adjustCurves(d,d)}return this._changed(25),o},clear:"#removeSegments",getLength:function(){if(null==this._length){var t=this.getCurves();this._length=0;for(var e=0,n=t.length;n>e;e++)this._length+=t[e].getLength()}return this._length},getArea:function(){for(var t=this.getCurves(),e=0,n=0,i=t.length;i>n;n++)e+=t[n].getArea();return e},isFullySelected:function(){var t=this._segments.length;return this._selected&&t>0&&this._selectedSegmentState===7*t},setFullySelected:function(t){t&&this._selectSegments(!0),this.setSelected(t)},setSelected:function xt(t){t||this._selectSegments(!1),xt.base.call(this,t)},_selectSegments:function(t){var e=this._segments.length;this._selectedSegmentState=t?7*e:0;for(var n=0;e>n;n++)this._segments[n]._selectionState=t?7:0},_updateSelection:function(t,e,n){t._selectionState=n;var i=this._selectedSegmentState+=n-e;i>0&&this.setSelected(!0)},flatten:function(t){for(var e=new B(this,64,.1),n=0,i=e.length/Math.ceil(e.length/t),r=e.length+(this._closed?-i:i)/2,s=[];r>=n;)s.push(new z(e.evaluate(n,0))),n+=i;this.setSegments(s)},reduce:function(){for(var t=this.getCurves(),e=t.length-1;e>=0;e--){var n=t[e];n.isLinear()&&0===n.getLength()&&n.remove()}return this},simplify:function(t){if(this._segments.length>2){var e=new j(this,t||2.5);this.setSegments(e.fit())}},split:function(t,e){if(null===e)return null;if(1===arguments.length){var n=t;if("number"==typeof n&&(n=this.getLocationAt(n)),!n)return null;t=n.index,e=n.parameter}var i=1e-6;e>=1-i&&(t++,e--);var r=this.getCurves();if(t>=0&&ti&&r[t++].divide(e,!0);var s,a=this.removeSegments(t,this._segments.length,!0);return this._closed?(this.setClosed(!1),s=this):s=this._clone((new E).insertAbove(this,!0)),s._add(a,0),this.addSegment(a[0]),s}return null},isClockwise:function(){return this._clockwise!==t?this._clockwise:E.isClockwise(this._segments)},setClockwise:function(t){this.isClockwise()!=(t=!!t)&&this.reverse(),this._clockwise=t},reverse:function(){this._segments.reverse();for(var e=0,n=this._segments.length;n>e;e++){var i=this._segments[e],r=i._handleIn;i._handleIn=i._handleOut,i._handleOut=r,i._index=e}this._curves=null,this._clockwise!==t&&(this._clockwise=!this._clockwise),this._changed(9)},join:function(t){if(t){var e=t._segments,n=this.getLastSegment(),i=t.getLastSegment();if(!i)return this;n&&n._point.equals(i._point)&&t.reverse();var r=t.getFirstSegment();if(n&&n._point.equals(r._point))n.setHandleOut(r._handleOut),this._add(e.slice(1));else{var s=this.getFirstSegment();s&&s._point.equals(r._point)&&t.reverse(),i=t.getLastSegment(),s&&s._point.equals(i._point)?(s.setHandleIn(i._handleIn),this._add(e.slice(0,e.length-1),0)):this._add(e.slice())}t.closed&&this._add([e[0]]),t.remove()}var a=this.getFirstSegment(),o=this.getLastSegment();return a!==o&&a._point.equals(o._point)&&(a.setHandleIn(o._handleIn),o.remove(),this.setClosed(!0)),this},toShape:function(t){function e(t,e){return l[t].isCollinear(l[e])}function n(t){return l[t].isOrthogonal()}function i(t){return l[t].isArc()}function r(t,e){return l[t]._point.getDistance(l[e]._point)}if(!this._closed)return null;var s,a,h,u,l=this._segments;if(this.isPolygon()&&4===l.length&&e(0,2)&&e(1,3)&&n(1)?(s=S.Rectangle,a=new d(r(0,3),r(0,1)),u=l[1]._point.add(l[2]._point).divide(2)):8===l.length&&i(0)&&i(2)&&i(4)&&i(6)&&e(1,5)&&e(3,7)?(s=S.Rectangle,a=new d(r(1,6),r(0,3)),h=a.subtract(new d(r(0,7),r(1,2))).divide(2),u=l[3]._point.add(l[4]._point).divide(2)):4===l.length&&i(0)&&i(1)&&i(2)&&i(3)&&(o.isZero(r(0,2)-r(1,3))?(s=S.Circle,h=r(0,2)/2):(s=S.Ellipse,h=new d(r(2,0)/2,r(3,1)/2)),u=l[1]._point),s){var c=this.getPosition(!0),f=this._clone(new s({center:c,size:a,radius:h,insert:!1}),t,!1);return f.rotate(u.subtract(c).getAngle()+90),f}return null},_hitTestSelf:function(t,e){function n(e,n){return t.subtract(e).divide(n).length<=1}function i(t,i,r){if(!e.selected||i.isSelected()){var s=t._point;if(i!==s&&(i=i.add(s)),n(i,w))return new I(r,_,{segment:t,point:i})}}function r(t,n){return(n||e.segments)&&i(t,t._point,"segment")||!n&&e.handles&&(i(t,t._handleIn,"handle-in")||i(t,t._handleOut,"handle-out"))}function s(t){c.add(t)}function a(e){if(("round"!==o||"round"!==h)&&(c=new E({internal:!0,closed:!0}),m||e._index>0&&e._index0||C?0:null;if(null!==S&&(S>0?(o=g.getStrokeJoin(),h=g.getStrokeCap(),l=S*g.getMiterLimit(),w=y.add(new u(S,S))):o=h="round"),!e.ends||e.segments||m){if(e.segments||e.handles)for(var P=0;v>P;P++)if(f=r(p[P]))return f}else if(f=r(p[0],!0)||r(p[v-1],!0))return f;if(null!==S){if(d=this.getNearestLocation(t)){var k=d.getParameter();0===k||1===k&&v>1?a(d.getSegment())||(d=null):n(d.getPoint(),w)||(d=null)}if(!d&&"miter"===o&&v>1)for(var P=0;v>P;P++){var M=p[P];if(t.getDistance(M._point)<=l&&a(M)){d=M.getLocation();break}}}return!d&&b&&this._contains(t)||d&&!x&&!C?new I("fill",this):d?new I(x?"stroke":"curve",this,{location:d,point:d.getPoint()}):null}},e.each(["getPoint","getTangent","getNormal","getCurvature"],function(t){this[t+"At"]=function(e,n){var i=this.getLocationAt(e,n);return i&&i[t]()}},{beans:!1,_getOffset:function(t){var e=t&&t.getIndex();if(null!=e){for(var n=this.getCurves(),i=0,r=0;e>r;r++)i+=n[r].getLength();var s=n[e],a=t.getParameter();return a>0&&(i+=s.getPartLength(0,a)),i}return null},getLocationOf:function(){for(var t=u.read(arguments),e=this.getCurves(),n=0,i=e.length;i>n;n++){var r=e[n].getLocationOf(t);if(r)return r}return null},getOffsetOf:function(){var t=this.getLocationOf.apply(this,arguments);return t?t.getOffset():null},getLocationAt:function(t,e){var n=this.getCurves(),i=0;if(e){var r=~~t;return n[r].getLocationAt(t-r,!0)}for(var s=0,a=n.length;a>s;s++){var o=i,h=n[s];if(i+=h.getLength(),i>t)return h.getLocationAt(t-o)}return t<=this.getLength()?new T(n[n.length-1],1):null},getNearestLocation:function(){for(var t=u.read(arguments),e=this.getCurves(),n=1/0,i=null,r=0,s=e.length;s>r;r++){var a=e[r].getNearestLocation(t);a._distanceo;o++){var u=e[o];u._transformCoordinates(n,a,!1);var l=u._selectionState,c=a[0],d=a[1];if(1&l&&r(2),2&l&&r(4),t.fillRect(c-s,d-s,i,i),!(4&l)){var f=t.fillStyle;t.fillStyle="#ffffff",t.fillRect(c-s+1,d-s+1,i-2,i-2),t.fillStyle=f}}}function e(t,e,n){function i(e){if(n)e._transformCoordinates(n,_,!1),r=_[0],s=_[1];else{var i=e._point;r=i._x,s=i._y}if(g)t.moveTo(r,s),g=!1;else{if(n)h=_[2],u=_[3];else{var d=e._handleIn;h=r+d._x,u=s+d._y}h===r&&u===s&&l===a&&c===o?t.lineTo(r,s):t.bezierCurveTo(l,c,h,u,r,s)}if(a=r,o=s,n)l=_[4],c=_[5];else{var d=e._handleOut;l=a+d._x,c=o+d._y}}for(var r,s,a,o,h,u,l,c,d=e._segments,f=d.length,_=Array(6),g=!0,p=0;f>p;p++)i(d[p]);e._closed&&f>0&&i(d[0])}return{_draw:function(t,n,i){function r(t){return l[(t%c+c)%c]}var s=n.dontStart,a=n.dontFinish||n.clip,o=this.getStyle(),h=o.hasFill(),u=o.hasStroke(),l=o.getDashArray(),c=!paper.support.nativeDash&&u&&l&&l.length;if(s||t.beginPath(),!s&&this._currentPath?t.currentPath=this._currentPath:(h||u&&!c||a)&&(e(t,this,i),this._closed&&t.closePath(),s||(this._currentPath=t.currentPath)),!a&&(h||u)&&(this._setStyles(t),h&&(t.fill(o.getWindingRule()),t.shadowColor="rgba(0,0,0,0)"),u)){if(c){s||t.beginPath();var d,f=new B(this,32,.25,i),_=f.length,g=-o.getDashOffset(),p=0;for(g%=_;g>0;)g-=r(p--)+r(p--);for(;_>g;)d=g+r(p++),(g>0||d>0)&&f.drawPart(t,Math.max(g,0),Math.max(d,0)),g=d+r(p++)}t.stroke()}},_drawSelected:function(n,i){n.beginPath(),e(n,this,i),n.stroke(),t(n,this._segments,i,paper.settings.handleSize)}}},new function(){function t(t){var e=t.length,n=[],i=[],r=2;n[0]=t[0]/r;for(var s=1;e>s;s++)i[s]=1/r,r=(e-1>s?4:2)-i[s],n[s]=(t[s]-n[s-1])/r;for(var s=1;e>s;s++)n[e-s-1]-=i[e-s]*n[e-s];return n}return{smooth:function(){var e=this._segments,n=e.length,i=this._closed,r=n,s=0;if(!(2>=n)){i&&(s=Math.min(n,4),r+=2*Math.min(n,s));for(var a=[],o=0;n>o;o++)a[o+s]=e[o]._point;if(i)for(var o=0;s>o;o++)a[o]=e[o+n-s]._point,a[o+n+s]=e[o]._point;else r--;for(var h=[],o=1;r-1>o;o++)h[o]=4*a[o]._x+2*a[o+1]._x;h[0]=a[0]._x+2*a[1]._x,h[r-1]=3*a[r-1]._x;for(var l=t(h),o=1;r-1>o;o++)h[o]=4*a[o]._y+2*a[o+1]._y;h[0]=a[0]._y+2*a[1]._y,h[r-1]=3*a[r-1]._y;var c=t(h);if(i){for(var o=0,d=n;s>o;o++,d++){var f=o/s,_=1-f,g=o+s,p=d+s;l[d]=l[o]*f+l[d]*_,c[d]=c[o]*f+c[d]*_,l[p]=l[g]*_+l[p]*f,c[p]=c[g]*_+c[p]*f}r--}for(var v=null,o=s;r-s>=o;o++){var m=e[o-s];v&&m.setHandleIn(v.subtract(m._point)),r>o&&(m.setHandleOut(new u(l[o],c[o]).subtract(m._point)),v=r-1>o?new u(2*a[o+1]._x-l[o+1],2*a[o+1]._y-c[o+1]):new u((a[r]._x+l[r-1])/2,(a[r]._y+c[r-1])/2))}if(i&&v){var m=this._segments[0];m.setHandleIn(v.subtract(m._point))}}}}},new function(){function t(t){var e=t._segments;if(0===e.length)throw Error("Use a moveTo() command first");return e[e.length-1]}return{moveTo:function(){var t=this._segments;1===t.length&&this.removeSegment(0),t.length||this._add([new z(u.read(arguments))])},moveBy:function(){throw Error("moveBy() is unsupported on Path items.")},lineTo:function(){this._add([new z(u.read(arguments))])},cubicCurveTo:function(){var e=u.read(arguments),n=u.read(arguments),i=u.read(arguments),r=t(this);r.setHandleOut(e.subtract(r._point)),this._add([new z(i,n.subtract(i))])},quadraticCurveTo:function(){var e=u.read(arguments),n=u.read(arguments),i=t(this)._point;this.cubicCurveTo(e.add(i.subtract(e).multiply(1/3)),e.add(n.subtract(e).multiply(1/3)),n)},curveTo:function(){var n=u.read(arguments),i=u.read(arguments),r=e.pick(e.read(arguments),.5),s=1-r,a=t(this)._point,o=n.subtract(a.multiply(s*s)).subtract(i.multiply(r*r)).divide(2*r*s);if(o.isNaN())throw Error("Cannot put a curve through points with parameter = "+r);this.quadraticCurveTo(o,i)},arcTo:function(){var n,i,r,s,a,o=t(this),h=o._point,l=u.read(arguments),c=e.peek(arguments),f=e.pick(c,!0);if("boolean"==typeof f)var _=h.add(l).divide(2),n=_.add(_.subtract(h).rotate(f?-90:90));else if(e.remain(arguments)<=2)n=l,l=u.read(arguments);else{var g=d.read(arguments);if(g.isZero())return this.lineTo(l);var m=e.read(arguments),f=!!e.read(arguments),y=!!e.read(arguments),_=h.add(l).divide(2),w=h.subtract(_).rotate(-m),x=w.x,b=w.y,C=Math.abs,S=1e-12,P=C(g.width),k=C(g.height),M=P*P,I=k*k,A=x*x,O=b*b,T=Math.sqrt(A/M+O/I);if(T>1&&(P*=T,k*=T,M=P*P,I=k*k),T=(M*I-M*O-I*A)/(M*O+I*A),C(T)T)throw Error("Cannot create an arc with the given arguments");i=new u(P*b/k,-k*x/P).multiply((y===f?-1:1)*Math.sqrt(T)).rotate(m).add(_),a=(new p).translate(i).rotate(m).scale(P,k),s=a._inverseTransform(h),r=s.getDirectedAngle(a._inverseTransform(l)),!f&&r>0?r-=360:f&&0>r&&(r+=360)}if(n){var L=new v(h.add(n).divide(2),n.subtract(h).rotate(90),!0),E=new v(n.add(l).divide(2),l.subtract(n).rotate(90),!0),N=new v(h,l),B=N.getSide(n);if(i=L.intersect(E,!0),!i){if(!B)return this.lineTo(l);throw Error("Cannot create an arc with the given arguments")}s=h.subtract(i),r=s.getDirectedAngle(l.subtract(i));var j=N.getSide(i);0===j?r=B*Math.abs(r):B===j&&(r+=0>r?360:-360)}for(var R=Math.abs(r),D=R>=360?4:Math.ceil(R/90),F=r/D,q=F*Math.PI/360,V=4/3*Math.sin(q)/(1+Math.cos(q)),Z=[],U=0;D>=U;U++){var w=l,H=null;if(D>U&&(H=s.rotate(90).multiply(V),a?(w=a._transformPoint(s),H=a._transformPoint(s.add(H)).subtract(w)):w=i.add(s)),0===U)o.setHandleOut(H);else{var W=s.rotate(-90).multiply(V);a&&(W=a._transformPoint(s.add(W)).subtract(w)),Z.push(new z(w,W,H))}s=s.rotate(F)}this._add(Z)},lineBy:function(){var e=u.read(arguments),n=t(this)._point;this.lineTo(n.add(e))},curveBy:function(){var n=u.read(arguments),i=u.read(arguments),r=e.read(arguments),s=t(this)._point;this.curveTo(s.add(n),s.add(i),r)},cubicCurveBy:function(){var e=u.read(arguments),n=u.read(arguments),i=u.read(arguments),r=t(this)._point;this.cubicCurveTo(r.add(e),r.add(n),r.add(i))},quadraticCurveBy:function(){var e=u.read(arguments),n=u.read(arguments),i=t(this)._point;this.quadraticCurveTo(i.add(e),i.add(n))},arcBy:function(){var n=t(this)._point,i=n.add(u.read(arguments)),r=e.pick(e.peek(arguments),!0);"boolean"==typeof r?this.arcTo(i,r):this.arcTo(i,n.add(u.read(arguments)))},closePath:function(t){this.setClosed(!0),t&&this.join()}}},{_getBounds:function(t,e){return E[t](this._segments,this._closed,this.getStyle(),e)},statics:{isClockwise:function(t){for(var e=0,n=0,i=t.length;i>n;n++)e+=O.getEdgeSum(O.getValues(t[n],t[i>n+1?n+1:0]));return e>0},getBounds:function(t,e,n,i,r){function s(t){t._transformCoordinates(i,o,!1);for(var e=0;2>e;e++)O._addBounds(h[e],h[e+4],o[e+2],o[e],e,r?r[e]:0,u,l,c);var n=h;h=o,o=n}var a=t[0];if(!a)return new _;for(var o=Array(6),h=a._transformCoordinates(i,Array(6),!1),u=h.slice(0,2),l=u.slice(),c=Array(2),d=1,f=t.length;f>d;d++)s(t[d]);return e&&s(a),new _(u[0],u[1],l[0]-u[0],l[1]-u[1])},getStrokeBounds:function(t,e,n,i){function r(t){c=c.include(i?i._transformPoint(t,t):t)}function s(t){c=c.unite(v.setCenter(i?i._transformPoint(t._point):t._point))}function a(t,e){var n=t._handleIn,i=t._handleOut;"round"===e||!n.isZero()&&!i.isZero()&&n.isCollinear(i)?s(t):E._addBevelJoin(t,e,u,p,r)}function o(t,e){"round"===e?s(t):E._addSquareCap(t,e,u,r)}if(!n.hasStroke())return E.getBounds(t,e,n,i);for(var h=t.length-(e?0:1),u=n.getStrokeWidth()/2,l=E._getPenPadding(u,i),c=E.getBounds(t,e,n,i,l),f=n.getStrokeJoin(),g=n.getStrokeCap(),p=u*n.getMiterLimit(),v=new _(new d(l).multiply(2)),m=1;h>m;m++)a(t[m],f);return e?a(t[0],f):h>0&&(o(t[0],g),o(t[t.length-1],g)),c},_getPenPadding:function(t,e){if(!e)return[t,t];var n=e.shiftless(),i=n.transform(new u(t,0)),r=n.transform(new u(0,t)),s=i.getAngleInRadians(),a=i.getLength(),o=r.getLength(),h=Math.sin(s),l=Math.cos(s),c=Math.tan(s),d=-Math.atan(o*c/a),f=Math.atan(o/(c*a));return[Math.abs(a*Math.cos(d)*l-o*Math.sin(d)*h),Math.abs(o*Math.sin(f)*l+a*Math.cos(f)*h)]},_addBevelJoin:function(t,e,n,i,r,s){var a=t.getCurve(),o=a.getPrevious(),h=a.getPointAt(0,!0),l=o.getNormalAt(1,!0),c=a.getNormalAt(0,!0),d=l.getDirectedAngle(c)<0?-n:n;if(l.setLength(d),c.setLength(d),s&&(r(h),r(h.add(l))),"miter"===e){var f=new v(h.add(l),new u(-l.y,l.x),!0).intersect(new v(h.add(c),new u(-c.y,c.x),!0),!0);if(f&&h.getDistance(f)<=i&&(r(f),!s))return}s||r(h.add(l)),r(h.add(c))},_addSquareCap:function(t,e,n,i,r){var s=t._point,a=t.getLocation(),o=a.getNormal().normalize(n);r&&(i(s.subtract(o)),i(s.add(o))),"square"===e&&(s=s.add(o.rotate(0===a.getParameter()?-90:90))),i(s.add(o)),i(s.subtract(o))},getHandleBounds:function(t,e,n,i,r,s){for(var a=Array(6),o=1/0,h=-o,u=o,l=h,c=0,d=t.length;d>c;c++){var f=t[c];f._transformCoordinates(i,a,!1);for(var g=0;6>g;g+=2){var p=0===g?s:r,v=p?p[0]:0,m=p?p[1]:0,y=a[g],w=a[g+1],x=y-v,b=y+v,C=w-m,S=w+m;o>x&&(o=x),b>h&&(h=b),u>C&&(u=C),S>l&&(l=S)}}return new _(o,u,h-o,l-u)},getRoughBounds:function(t,e,n,i){var r=n.hasStroke()?n.getStrokeWidth()/2:0,s=r;return r>0&&("miter"===n.getStrokeJoin()&&(s=r*n.getMiterLimit()),"square"===n.getStrokeCap()&&(s=Math.max(s,r*Math.sqrt(2)))),E.getHandleBounds(t,e,n,i,E._getPenPadding(r,i),E._getPenPadding(s,i))}}});E.inject({statics:new function(){function t(t,n,i){var r=e.getNamed(i),s=new E(r&&r.insert===!1&&x.NO_INSERT);return s._add(t),s._closed=n,s.set(r)}function n(e,n,i){for(var s=Array(4),a=0;4>a;a++){var o=r[a];s[a]=new z(o._point.multiply(n).add(e),o._handleIn.multiply(n),o._handleOut.multiply(n))}return t(s,!0,i)}var i=.5522847498307936,r=[new z([-1,0],[0,i],[0,-i]),new z([0,-1],[-i,0],[i,0]),new z([1,0],[0,-i],[0,i]),new z([0,1],[i,0],[-i,0])];return{Line:function(){return t([new z(u.readNamed(arguments,"from")),new z(u.readNamed(arguments,"to"))],!1,arguments)},Circle:function(){var t=u.readNamed(arguments,"center"),i=e.readNamed(arguments,"radius");return n(t,new d(i),arguments)},Rectangle:function(){var e,n=_.readNamed(arguments,"rectangle"),r=d.readNamed(arguments,"radius",0,{readNull:!0}),s=n.getBottomLeft(!0),a=n.getTopLeft(!0),o=n.getTopRight(!0),h=n.getBottomRight(!0);if(!r||r.isZero())e=[new z(s),new z(a),new z(o),new z(h)];else{r=d.min(r,n.getSize(!0).divide(2));var u=r.width,l=r.height,c=u*i,f=l*i;e=[new z(s.add(u,0),null,[-c,0]),new z(s.subtract(0,l),[0,f]),new z(a.add(0,l),null,[0,-f]),new z(a.add(u,0),[-c,0],null),new z(o.subtract(u,0),null,[c,0]),new z(o.add(0,l),[0,-f],null),new z(h.subtract(0,l),null,[0,f]),new z(h.subtract(u,0),[c,0])]}return t(e,!0,arguments)},RoundRectangle:"#Rectangle",Ellipse:function(){var t=S._readEllipse(arguments);return n(t.center,t.radius,arguments)},Oval:"#Ellipse",Arc:function(){var t=u.readNamed(arguments,"from"),n=u.readNamed(arguments,"through"),i=u.readNamed(arguments,"to"),r=e.getNamed(arguments),s=new E(r&&r.insert===!1&&x.NO_INSERT);return s.moveTo(t),s.arcTo(n,i),s.set(r)},RegularPolygon:function(){for(var n=u.readNamed(arguments,"center"),i=e.readNamed(arguments,"sides"),r=e.readNamed(arguments,"radius"),s=360/i,a=!(i%3),o=new u(0,a?-r:r),h=a?-1:.5,l=Array(i),c=0;i>c;c++)l[c]=new z(n.add(o.rotate((c+h)*s)));return t(l,!0,arguments)},Star:function(){for(var n=u.readNamed(arguments,"center"),i=2*e.readNamed(arguments,"points"),r=e.readNamed(arguments,"radius1"),s=e.readNamed(arguments,"radius2"),a=360/i,o=new u(0,-1),h=Array(i),l=0;i>l;l++)h[l]=new z(n.add(o.rotate(a*l).multiply(l%2?s:r)));return t(h,!0,arguments)}}}});var N=L.extend({_class:"CompoundPath",_serializeFields:{children:[]},initialize:function(t){this._children=[],this._namedChildren={},this._initialize(t)||("string"==typeof t?this.setPathData(t):this.addChildren(Array.isArray(t)?t:arguments))},insertChildren:function bt(e,n,i){n=bt.base.call(this,e,n,i,E);for(var r=0,s=!i&&n&&n.length;s>r;r++){var a=n[r];a._clockwise===t&&a.setClockwise(0===a._index)}return n},reverse:function(){for(var t=this._children,e=0,n=t.length;n>e;e++)t[e].reverse()},smooth:function(){for(var t=0,e=this._children.length;e>t;t++)this._children[t].smooth()},reduce:function Ct(){if(0===this._children.length){var t=new E(x.NO_INSERT);return t.insertAbove(this),t.setStyle(this._style),this.remove(),t}return Ct.base.call(this)},isClockwise:function(){var t=this.getFirstChild();return t&&t.isClockwise()},setClockwise:function(t){this.isClockwise()!==!!t&&this.reverse()},getFirstSegment:function(){var t=this.getFirstChild();return t&&t.getFirstSegment()},getLastSegment:function(){var t=this.getLastChild();return t&&t.getLastSegment()},getCurves:function(){for(var t=this._children,e=[],n=0,i=t.length;i>n;n++)e.push.apply(e,t[n].getCurves());return e},getFirstCurve:function(){var t=this.getFirstChild();return t&&t.getFirstCurve()},getLastCurve:function(){var t=this.getLastChild();return t&&t.getFirstCurve()},getArea:function(){for(var t=this._children,e=0,n=0,i=t.length;i>n;n++)e+=t[n].getArea();return e}},{beans:!0,getPathData:function(t,e){for(var n=this._children,i=[],r=0,s=n.length;s>r;r++){var a=n[r],o=a._matrix;i.push(a.getPathData(t&&!o.isIdentity()?t.chain(o):o,e))}return i.join(" ")}},{_getChildHitTestOptions:function(t){return t["class"]===E||"path"===t.type?t:new e(t,{fill:!1})},_draw:function(t,e,n){var i=this._children;if(0!==i.length){if(this._currentPath)t.currentPath=this._currentPath;else{e=e.extend({dontStart:!0,dontFinish:!0}),t.beginPath();for(var r=0,s=i.length;s>r;r++)i[r].draw(t,e,n);this._currentPath=t.currentPath}if(!e.clip){this._setStyles(t);var a=this._style;a.hasFill()&&(t.fill(a.getWindingRule()),t.shadowColor="rgba(0,0,0,0)"),a.hasStroke()&&t.stroke()}}},_drawSelected:function(t,e,n){for(var i=this._children,r=0,s=i.length;s>r;r++){var a=i[r],o=a._matrix;n[a._id]||a._drawSelected(t,o.isIdentity()?e:e.chain(o))}}},new function(){function t(t,e){var n=t._children;if(e&&0===n.length)throw Error("Use a moveTo() command first");return n[n.length-1]}var n={moveTo:function(){var e=t(this),n=e&&e.isEmpty()?e:new E;n!==e&&this.addChild(n),n.moveTo.apply(n,arguments)},moveBy:function(){var e=t(this,!0),n=e&&e.getLastSegment(),i=u.read(arguments);this.moveTo(n?i.add(n._point):i)},closePath:function(e){t(this,!0).closePath(e)}};return e.each(["lineTo","cubicCurveTo","quadraticCurveTo","curveTo","arcTo","lineBy","cubicCurveBy","quadraticCurveBy","curveBy","arcBy"],function(e){n[e]=function(){var n=t(this,!0);n[e].apply(n,arguments)}}),n});L.inject(new function(){function t(t,s,a){function o(t){return t.clone(!1).reduce().reorient().transform(null,!0,!0)}function h(t){for(var e=0,n=t.length;n>e;e++){var i=t[e];f.push.apply(f,i._segments),_.push.apply(_,i._getMonoCurves())}}var u=r[a],l=o(t),c=s&&t!==s&&o(s);c&&/^(subtract|exclude)$/.test(a)^c.isClockwise()!==l.isClockwise()&&c.reverse(),e(l.getIntersections(c,null,!0));var d=[],f=[],_=[],g=1e-6;h(l._children||[l]),c&&h(c._children||[c]),f.sort(function(t,e){var n=t._intersection,i=e._intersection;return!n&&!i||n&&i?0:n?-1:1});for(var p=0,v=f.length;v>p;p++){var y=f[p];if(null==y._winding){d.length=0;var w=y,b=0,C=0;do{var S=y.getCurve().getLength();d.push({segment:y,length:S}),b+=S,y=y.getNext()}while(y&&!y._intersection&&y!==w);for(var P=0;3>P;P++){var S=b*(P+1)/4;for(k=0,m=d.length;k=S){(g>S||g>I-S)&&(S=I/2);var z=M.segment.getCurve(),A=z.getPointAt(S),O=z.isLinear()&&Math.abs(z.getTangentAt(.5,!0).y)=0;P--)d[P].segment._winding=L}}var E=new N(x.NO_INSERT);return E.insertAbove(t),E.addChildren(i(f,u),!0),E=E.reduce(),E.setStyle(t._style),E}function e(t){function e(){for(var t=0,e=n.length;e>t;t++)n[t].set(0,0)}for(var n,i,r,s=1e-6,a=1-s,o=t.length-1;o>=0;o--){var h=t[o],u=h._parameter;r&&r._curve===h._curve&&r._parameter>0?u/=r._parameter:(i=h._curve,n&&e(),n=i.isLinear()?[i._segment1._handleOut,i._segment2._handleIn]:null);var l,c;(l=i.divide(u,!0,!0))?(c=l._segment1,i=l.getPrevious(),n&&n.push(c._handleOut,c._handleIn)):c=s>u?i._segment1:u>a?i._segment2:i.getPartLength(0,u)w;w++){var b=e[w].values;if(O.solveCubic(b,0,l,_,0,1)>0)for(var C=_.length-1;C>=0;C--){var S=O.evaluate(b,_[C],0).y;m>S&&S>p?p=S:S>y&&v>S&&(v=S)}}p=(p+c)/2,v=(v+c)/2,p>-(1/0)&&(d=n(new u(l,p),e)),1/0>v&&(f=n(new u(l,v),e))}else for(var P=l-s,k=l+s,w=0,x=e.length;x>w;w++){var M,I,z=e[w],b=z.values,A=z.winding;if(A&&(1===A&&c>=b[1]&&c<=b[7]||c>=b[7]&&c<=b[1])&&1===O.solveCubic(b,1,c,_,0,1)){var T=_[0],L=O.evaluate(b,T,0).x,E=O.evaluate(b,T,1).y;T>h&&(w===x-1||z.next!==e[w+1])&&g(O.evaluate(z.next.values,0,0).x-L)0&&z.previous===e[w-1]&&g(I-L)h&&a>T||(o.isZero(E)&&!O.isLinear(b)||a>T&&E*O.evaluate(z.previous.values,1,1).y<0||T>h&&E*O.evaluate(z.next.values,0,1).y<0?r&&L>=P&&k>=L&&(++d,++f):P>=L?d+=A:L>=k&&(f+=A)),M=T,I=L}}return Math.max(g(d),g(f))}function i(t,e,n){for(var i,r,s=[],a=1e-6,o=1-a,h=0,u=t.length;u>h;h++)if(i=r=t[h],!i._visited&&e(i._winding)){var l=new E(x.NO_INSERT),c=i._intersection,d=c&&c._segment,f=!1,_=1;do{var g,p=_>0?i._handleIn:i._handleOut,v=_>0?i._handleOut:i._handleIn;if(f&&(!e(i._winding)||n)&&(c=i._intersection)&&(g=c._segment)&&g!==r){if(n)i._visited=g._visited,i=g,_=1;else{var m=i.getCurve();_>0&&(m=m.getPrevious());var y=m.getTangentAt(1>_?a:o,!0),w=g.getCurve(),b=w.getPrevious(),C=b.getTangentAt(o,!0),S=w.getTangentAt(a,!0),P=y.cross(C),k=y.cross(S);if(P*k!==0){var M=k>P?b:w,I=e(M._segment1._winding)?M:k>P?w:b,A=I._segment1;_=I===b?-1:1,A._visited&&i._path!==A._path||!e(A._winding)?_=1:(i._visited=g._visited,i=g,A._visited&&(_=1))}else _=1}v=_>0?i._handleOut:i._handleIn}l.add(new z(i._point,f&&p,v)),f=!0,i._visited=!0,i=_>0?i.getNext():i.getPrevious()}while(i&&!i._visited&&i!==r&&i!==d&&(i._intersection||e(i._winding)));!i||i!==r&&i!==d?l.lastSegment._handleOut.set(0,0):(l.firstSegment.setHandleIn((i===d?d:i)._handleIn),l.setClosed(!0)),l._segments.length>(l._closed?l.isPolygon()?2:0:1)&&s.push(l)}return s}var r={unite:function(t){return 1===t||0===t},intersect:function(t){return 2===t},subtract:function(t){return 1===t},exclude:function(t){return 1===t}};return{_getWinding:function(t,e,i){return n(t,this._getMonoCurves(),e,i)},unite:function(e){return t(this,e,"unite")},intersect:function(e){return t(this,e,"intersect")},subtract:function(e){return t(this,e,"subtract")},exclude:function(e){return t(this,e,"exclude")},divide:function(t){return new b([this.subtract(t),this.intersect(t)])}}}),E.inject({_getMonoCurves:function(){function t(t){var e=t[1],r=t[7],s={values:t,winding:e===r?0:e>r?-1:1,previous:n,next:null};n&&(n.next=s),i.push(s),n=s}function e(e){if(0!==O.getLength(e)){var n=e[1],i=e[3],r=e[5],s=e[7];if(O.isLinear(e))t(e);else{var a=3*(i-r)-n+s,h=2*(n+r)-4*i,u=i-n,l=1e-6,c=[],d=o.solveQuadratic(a,h,u,c,l,1-l);if(0===d)t(e);else{c.sort();var f=c[0],_=O.subdivide(e,f);t(_[0]),d>1&&(f=(c[1]-f)/(1-f),_=O.subdivide(_[1],f),t(_[0])),t(_[1])}}}}var n,i=this._monoCurves;if(!i){i=this._monoCurves=[];for(var r=this.getCurves(),s=this._segments,a=0,h=r.length;h>a;a++)e(r[a].getValues());if(!this._closed&&s.length>1){var u=s[s.length-1]._point,l=s[0]._point,c=u._x,d=u._y,f=l._x,_=l._y;e([c,d,c,d,f,_,f,_])}if(i.length>0){var g=i[0],p=i[i.length-1];g.previous=p,p.next=g}}return i},getInteriorPoint:function(){var t=this.getBounds(),e=t.getCenter(!0);if(!this.contains(e)){for(var n=this._getMonoCurves(),i=[],r=e.y,s=[],a=0,o=n.length;o>a;a++){var h=n[a].values;if((1===n[a].winding&&r>=h[1]&&r<=h[7]||r>=h[7]&&r<=h[1])&&O.solveCubic(h,1,r,i,0,1)>0)for(var u=i.length-1;u>=0;u--)s.push(O.evaluate(h,i[u],0).x);if(s.length>1)break}e.x=(s[0]+s[1])/2}return e},reorient:function(){return this.setClockwise(!0), -this}}),N.inject({_getMonoCurves:function(){for(var t=this._children,e=[],n=0,i=t.length;i>n;n++)e.push.apply(e,t[n]._getMonoCurves());return e},reorient:function(){var t=this.removeChildren().sort(function(t,e){return e.getBounds().getArea()-t.getBounds().getArea()});if(t.length>0){this.addChildren(t);for(var e=t[0].isClockwise(),n=1,i=t.length;i>n;n++){for(var r=t[n].getInteriorPoint(),s=0,a=n-1;a>=0;a--)t[a].contains(r)&&s++;t[n].setClockwise(s%2===0&&e)}}return this}});var B=e.extend({_class:"PathIterator",initialize:function(t,e,n,i){function r(t,e){var n=O.getValues(t,e,i);o.push(n),s(n,t._index,0,1)}function s(t,e,i,r){if(r-i>l&&!O.isFlatEnough(t,n||.25)){var a=O.subdivide(t),o=(i+r)/2;s(a[0],e,i,o),s(a[1],e,o,r)}else{var c=t[6]-t[0],d=t[7]-t[1],f=Math.sqrt(c*c+d*d);f>1e-6&&(u+=f,h.push({offset:u,value:r,index:e}))}}for(var a,o=[],h=[],u=0,l=1/(e||32),c=t._segments,d=c[0],f=1,_=c.length;_>f;f++)a=c[f],r(d,a),d=a;t._closed&&r(a,c[0]),this.curves=o,this.parts=h,this.length=u,this.index=0},getParameterAt:function(t){for(var e,n=this.index;e=n,!(0==n||this.parts[--n].offsete;e++){var r=this.parts[e];if(r.offset>=t){this.index=e;var s=this.parts[e-1],a=s&&s.index==r.index?s.value:0,o=s?s.offset:0;return{value:a+(r.value-a)*(t-o)/(r.offset-o),index:r.index}}}var r=this.parts[this.parts.length-1];return{value:1,index:r.index}},evaluate:function(t,e){var n=this.getParameterAt(t);return O.evaluate(this.curves[n.index],n.value,e)},drawPart:function(t,e,n){e=this.getParameterAt(e),n=this.getParameterAt(n);for(var i=e.index;i<=n.index;i++){var r=O.getPart(this.curves[i],i==e.index?e.value:0,i==n.index?n.value:1);i==e.index&&t.moveTo(r[0],r[1]),t.bezierCurveTo.apply(t,r.slice(2))}}},e.each(["getPoint","getTangent","getNormal","getCurvature"],function(t,e){this[t+"At"]=function(t){return this.evaluate(t,e)}},{})),j=e.extend({initialize:function(t,e){for(var n,i=this.points=[],r=t._segments,s=0,a=r.length;a>s;s++){var o=r[s].point.clone();n&&n.equals(o)||(i.push(o),n=o)}t._closed&&(this.closed=!0,i.unshift(i[i.length-1]),i.push(i[1])),this.error=e},fit:function(){var t=this.points,e=t.length,n=this.segments=e>0?[new z(t[0])]:[];return e>1&&this.fitCubic(0,e-1,t[1].subtract(t[0]).normalize(),t[e-2].subtract(t[e-1]).normalize()),this.closed&&(n.shift(),n.pop()),n},fitCubic:function(e,n,i,r){if(n-e==1){var s=this.points[e],a=this.points[n],o=s.getDistance(a)/3;return this.addCurve([s,s.add(i.normalize(o)),a.add(r.normalize(o)),a]),t}for(var h,u=this.chordLengthParameterize(e,n),l=Math.max(this.error,this.error*this.error),c=!0,d=0;4>=d;d++){var f=this.generateBezier(e,n,u,i,r),_=this.findMaxError(e,n,f,u);if(_.error=l)break;c=this.reparameterize(e,n,u,f),l=_.error}var g=this.points[h-1].subtract(this.points[h]),p=this.points[h].subtract(this.points[h+1]),v=g.add(p).divide(2).normalize();this.fitCubic(e,h,i,v),this.fitCubic(h,n,v.negate(),r)},addCurve:function(t){var e=this.segments[this.segments.length-1];e.setHandleOut(t[1].subtract(t[0])),this.segments.push(new z(t[3],t[2].subtract(t[3])))},generateBezier:function(t,e,n,i,r){for(var s=1e-12,a=this.points[t],o=this.points[e],h=[[0,0],[0,0]],u=[0,0],l=0,c=e-t+1;c>l;l++){var d=n[l],f=1-d,_=3*d*f,g=f*f*f,p=_*f,v=_*d,m=d*d*d,y=i.normalize(p),w=r.normalize(v),x=this.points[t+l].subtract(a.multiply(g+p)).subtract(o.multiply(v+m));h[0][0]+=y.dot(y),h[0][1]+=y.dot(w),h[1][0]=h[0][1],h[1][1]+=w.dot(w),u[0]+=y.dot(x),u[1]+=w.dot(x)}var b,C,S=h[0][0]*h[1][1]-h[1][0]*h[0][1];if(Math.abs(S)>s){var P=h[0][0]*u[1]-h[1][0]*u[0],k=u[0]*h[1][1]-u[1]*h[0][1];b=k/S,C=P/S}else{var M=h[0][0]+h[0][1],I=h[1][0]+h[1][1];b=C=Math.abs(M)>s?u[0]/M:Math.abs(I)>s?u[1]/I:0}var z,A,O=o.getDistance(a),T=s*O;if(T>b||T>C)b=C=O/3;else{var L=o.subtract(a);z=i.normalize(b),A=r.normalize(C),z.dot(L)-A.dot(L)>O*O&&(b=C=O/3,z=A=null)}return[a,a.add(z||i.normalize(b)),o.add(A||r.normalize(C)),o]},reparameterize:function(t,e,n,i){for(var r=t;e>=r;r++)n[r-t]=this.findRoot(i,this.points[r],n[r-t]);for(var r=1,s=n.length;s>r;r++)if(n[r]<=n[r-1])return!1;return!0},findRoot:function(t,e,n){for(var i=[],r=[],s=0;2>=s;s++)i[s]=t[s+1].subtract(t[s]).multiply(3);for(var s=0;1>=s;s++)r[s]=i[s+1].subtract(i[s]).multiply(2);var a=this.evaluate(3,t,n),o=this.evaluate(2,i,n),h=this.evaluate(1,r,n),u=a.subtract(e),l=o.dot(o)+u.dot(h);return Math.abs(l)<1e-6?n:n-u.dot(o)/l},evaluate:function(t,e,n){for(var i=e.slice(),r=1;t>=r;r++)for(var s=0;t-r>=s;s++)i[s]=i[s].multiply(1-n).add(i[s+1].multiply(n));return i[0]},chordLengthParameterize:function(t,e){for(var n=[0],i=t+1;e>=i;i++)n[i-t]=n[i-t-1]+this.points[i].getDistance(this.points[i-1]);for(var i=1,r=e-t;r>=i;i++)n[i]/=n[r];return n},findMaxError:function(t,e,n,i){for(var r=Math.floor((e-t+1)/2),s=0,a=t+1;e>a;a++){var o=this.evaluate(3,n,i[a-t]),h=o.subtract(this.points[a]),u=h.x*h.x+h.y*h.y;u>=s&&(s=u,r=a)}return{error:s,index:r}}}),R=x.extend({_class:"TextItem",_boundsSelected:!0,_applyMatrix:!1,_canApplyMatrix:!1,_serializeFields:{content:null},_boundsGetter:"getBounds",initialize:function(n){this._content="",this._lines=[];var i=n&&e.isPlainObject(n)&&n.x===t&&n.y===t;this._initialize(i&&n,!i&&u.read(arguments))},_equals:function(t){return this._content===t._content},_clone:function St(t,e,n){return t.setContent(this._content),St.base.call(this,t,e,n)},getContent:function(){return this._content},setContent:function(t){this._content=""+t,this._lines=this._content.split(/\r\n|\n|\r/gm),this._changed(265)},isEmpty:function(){return!this._content},getCharacterStyle:"#getStyle",setCharacterStyle:"#setStyle",getParagraphStyle:"#getStyle",setParagraphStyle:"#setStyle"}),D=R.extend({_class:"PointText",initialize:function(){R.apply(this,arguments)},clone:function(t){return this._clone(new D(x.NO_INSERT),t)},getPoint:function(){var t=this._matrix.getTranslation();return new c(t.x,t.y,this,"setPoint")},setPoint:function(){var t=u.read(arguments);this.translate(t.subtract(this._matrix.getTranslation()))},_draw:function(t){if(this._content){this._setStyles(t);var e=this._style,n=this._lines,i=e.getLeading(),r=t.shadowColor;t.font=e.getFontStyle(),t.textAlign=e.getJustification();for(var s=0,a=n.length;a>s;s++){t.shadowColor=r;var o=n[s];e.hasFill()&&(t.fillText(o,0,0),t.shadowColor="rgba(0,0,0,0)"),e.hasStroke()&&t.strokeText(o,0,0),t.translate(0,i)}}},_getBounds:function(t,e){var n=this._style,i=this._lines,r=i.length,s=n.getJustification(),a=n.getLeading(),o=this.getView().getTextWidth(n.getFontStyle(),i),h=0;"left"!==s&&(h-=o/("center"===s?2:1));var u=new _(h,r?-.75*a:0,o,r*a);return e?e._transformBounds(u,u):u}}),F=e.extend(new function(){function t(t){var e,i=t.match(/^#(\w{1,2})(\w{1,2})(\w{1,2})$/);if(i){e=[0,0,0];for(var r=0;3>r;r++){var a=i[r+1];e[r]=parseInt(1==a.length?a+a:a,16)/255}}else if(i=t.match(/^rgba?\((.*)\)$/)){e=i[1].split(",");for(var r=0,o=e.length;o>r;r++){var a=+e[r];e[r]=3>r?a/255:a}}else{var h=s[t];if(!h){n||(n=et.getContext(1,1),n.globalCompositeOperation="copy"),n.fillStyle="rgba(0,0,0,0)",n.fillStyle=t,n.fillRect(0,0,1,1);var u=n.getImageData(0,0,1,1).data;h=s[t]=[u[0]/255,u[1]/255,u[2]/255]}e=h.slice()}return e}var n,i={gray:["gray"],rgb:["red","green","blue"],hsb:["hue","saturation","brightness"],hsl:["hue","saturation","lightness"],gradient:["gradient","origin","destination","highlight"]},r={},s={},o=[[0,3,1],[2,0,1],[1,0,3],[1,2,0],[3,1,0],[0,1,2]],l={"rgb-hsb":function(t,e,n){var i=Math.max(t,e,n),r=Math.min(t,e,n),s=i-r,a=0===s?0:60*(i==t?(e-n)/s+(n>e?6:0):i==e?(n-t)/s+2:(t-e)/s+4);return[a,0===i?0:s/i,i]},"hsb-rgb":function(t,e,n){t=(t/60%6+6)%6;var i=Math.floor(t),r=t-i,i=o[i],s=[n,n*(1-e),n*(1-e*r),n*(1-e*(1-r))];return[s[i[0]],s[i[1]],s[i[2]]]},"rgb-hsl":function(t,e,n){var i=Math.max(t,e,n),r=Math.min(t,e,n),s=i-r,a=0===s,o=a?0:60*(i==t?(e-n)/s+(n>e?6:0):i==e?(n-t)/s+2:(t-e)/s+4),h=(i+r)/2,u=a?0:.5>h?s/(i+r):s/(2-i-r);return[o,u,h]},"hsl-rgb":function(t,e,n){if(t=(t/360%1+1)%1,0===e)return[n,n,n];for(var i=[t+1/3,t,t-1/3],r=.5>n?n*(1+e):n+e-n*e,s=2*n-r,a=[],o=0;3>o;o++){var h=i[o];0>h&&(h+=1),h>1&&(h-=1),a[o]=1>6*h?s+6*(r-s)*h:1>2*h?r:2>3*h?s+(r-s)*(2/3-h)*6:s}return a},"rgb-gray":function(t,e,n){return[.2989*t+.587*e+.114*n]},"gray-rgb":function(t){return[t,t,t]},"gray-hsb":function(t){return[0,0,t]},"gray-hsl":function(t){return[0,0,t]},"gradient-rgb":function(){return[]},"rgb-gradient":function(){return[]}};return e.each(i,function(t,n){r[n]=[],e.each(t,function(t,s){var a=e.capitalize(t),o=/^(hue|saturation)$/.test(t),h=r[n][s]="gradient"===t?function(t){var e=this._components[0];return t=q.read(Array.isArray(t)?t:arguments,0,{readNull:!0}),e!==t&&(e&&e._removeOwner(this),t&&t._addOwner(this)),t}:"gradient"===n?function(){return u.read(arguments,0,{readNull:"highlight"===t,clone:!0})}:function(t){return null==t||isNaN(t)?0:t};this["get"+a]=function(){return this._type===n||o&&/^hs[bl]$/.test(this._type)?this._components[s]:this._convert(n)[s]},this["set"+a]=function(t){this._type===n||o&&/^hs[bl]$/.test(this._type)||(this._components=this._convert(n),this._properties=i[n],this._type=n),this._components[s]=h.call(this,t),this._changed()}},this)},{_class:"Color",_readIndex:!0,initialize:function c(e){var n,s,a,o,u=Array.prototype.slice,l=arguments,d=0;Array.isArray(e)&&(l=e,e=l[0]);var f=null!=e&&typeof e;if("string"===f&&e in i&&(n=e,e=l[1],Array.isArray(e)?(s=e,a=l[2]):(this.__read&&(d=1),l=u.call(l,1),f=typeof e)),!s){if(o="number"===f?l:"object"===f&&null!=e.length?e:null){n||(n=o.length>=3?"rgb":"gray");var _=i[n].length;a=o[_],this.__read&&(d+=o===arguments?_+(null!=a?1:0):1),o.length>_&&(o=u.call(o,0,_))}else if("string"===f)n="rgb",s=t(e),4===s.length&&(a=s[3],s.length--);else if("object"===f)if(e.constructor===c){if(n=e._type,s=e._components.slice(),a=e._alpha,"gradient"===n)for(var g=1,p=s.length;p>g;g++){var v=s[g];v&&(s[g]=v.clone())}}else if(e.constructor===q)n="gradient",o=l;else{n="hue"in e?"lightness"in e?"hsl":"hsb":"gradient"in e||"stops"in e||"radial"in e?"gradient":"gray"in e?"gray":"rgb";var m=i[n];w=r[n],this._components=s=[];for(var g=0,p=m.length;p>g;g++){var y=e[m[g]];null==y&&0===g&&"gradient"===n&&"stops"in e&&(y={stops:e.stops,radial:e.radial}),y=w[g].call(this,y),null!=y&&(s[g]=y)}a=e.alpha}this.__read&&n&&(d=1)}if(this._type=n||"rgb",this._id=h.get(c),!s){this._components=s=[];for(var w=r[this._type],g=0,p=w.length;p>g;g++){var y=w[g].call(this,o&&o[g]);null!=y&&(s[g]=y)}}this._components=s,this._properties=i[this._type],this._alpha=a,this.__read&&(this.__read=d)},_serialize:function(t,n){var i=this.getComponents();return e.serialize(/^(gray|rgb)$/.test(this._type)?i:[this._type].concat(i),t,!0,n)},_changed:function(){this._canvasStyle=null,this._owner&&this._owner._changed(65)},_convert:function(t){var e;return this._type===t?this._components.slice():(e=l[this._type+"-"+t])?e.apply(this,this._components):l["rgb-"+t].apply(this,l[this._type+"-rgb"].apply(this,this._components))},convert:function(t){return new F(t,this._convert(t),this._alpha)},getType:function(){return this._type},setType:function(t){this._components=this._convert(t),this._properties=i[t],this._type=t},getComponents:function(){var t=this._components.slice();return null!=this._alpha&&t.push(this._alpha),t},getAlpha:function(){return null!=this._alpha?this._alpha:1},setAlpha:function(t){this._alpha=null==t?null:Math.min(Math.max(t,0),1),this._changed()},hasAlpha:function(){return null!=this._alpha},equals:function(t){var n=e.isPlainValue(t,!0)?F.read(arguments):t;return n===this||n&&this._class===n._class&&this._type===n._type&&this._alpha===n._alpha&&e.equals(this._components,n._components)||!1},toString:function(){for(var t=this._properties,e=[],n="gradient"===this._type,i=a.instance,r=0,s=t.length;s>r;r++){var o=this._components[r];null!=o&&e.push(t[r]+": "+(n?o:i.number(o)))}return null!=this._alpha&&e.push("alpha: "+i.number(this._alpha)),"{ "+e.join(", ")+" }"},toCSS:function(t){function e(t){return Math.round(255*(0>t?0:t>1?1:t))}var n=this._convert("rgb"),i=t||null==this._alpha?1:this._alpha;return n=[e(n[0]),e(n[1]),e(n[2])],1>i&&n.push(0>i?0:i),t?"#"+((1<<24)+(n[0]<<16)+(n[1]<<8)+n[2]).toString(16).slice(1):(4==n.length?"rgba(":"rgb(")+n.join(",")+")"},toCanvasStyle:function(t){if(this._canvasStyle)return this._canvasStyle;if("gradient"!==this._type)return this._canvasStyle=this.toCSS();var e,n=this._components,i=n[0],r=i._stops,s=n[1],a=n[2];if(i._radial){var o=a.getDistance(s),h=n[3];if(h){var u=h.subtract(s);u.getLength()>o&&(h=s.add(u.normalize(o-.1)))}var l=h||s;e=t.createRadialGradient(l.x,l.y,0,s.x,s.y,o)}else e=t.createLinearGradient(s.x,s.y,a.x,a.y);for(var c=0,d=r.length;d>c;c++){var f=r[c];e.addColorStop(f._rampPoint,f._color.toCanvasStyle())}return this._canvasStyle=e},transform:function(t){if("gradient"===this._type){for(var e=this._components,n=1,i=e.length;i>n;n++){var r=e[n];t._transformPoint(r,r,!0)}this._changed()}},statics:{_types:i,random:function(){var t=Math.random;return new F(t(),t(),t())}}})},new function(){var t={add:function(t,e){return t+e},subtract:function(t,e){return t-e},multiply:function(t,e){return t*e},divide:function(t,e){return t/e}};return e.each(t,function(t,e){this[e]=function(e){e=F.read(arguments);for(var n=this._type,i=this._components,r=e._convert(n),s=0,a=i.length;a>s;s++)r[s]=t(i[s],r[s]);return new F(n,r,null!=this._alpha?t(this._alpha,e.getAlpha()):null)}},{})});e.each(F._types,function(t,n){var i=this[e.capitalize(n)+"Color"]=function(t){var e=null!=t&&typeof t,i="object"===e&&null!=t.length?t:"string"===e?null:arguments;return i?new F(n,i):new F(t)};if(3==n.length){var r=n.toUpperCase();F[r]=this[r+"Color"]=i}},e.exports);var q=e.extend({_class:"Gradient",initialize:function(t,e){this._id=h.get(),t&&this._set(t)&&(t=e=null),this._stops||this.setStops(t||["white","black"]),null==this._radial&&this.setRadial("string"==typeof e&&"radial"===e||e||!1)},_serialize:function(t,n){return n.add(this,function(){return e.serialize([this._stops,this._radial],t,!0,n)})},_changed:function(){for(var t=0,e=this._owners&&this._owners.length;e>t;t++)this._owners[t]._changed()},_addOwner:function(t){this._owners||(this._owners=[]),this._owners.push(t)},_removeOwner:function(e){var n=this._owners?this._owners.indexOf(e):-1;-1!=n&&(this._owners.splice(n,1),0===this._owners.length&&(this._owners=t))},clone:function(){for(var t=[],e=0,n=this._stops.length;n>e;e++)t[e]=this._stops[e].clone();return new q(t,this._radial)},getStops:function(){return this._stops},setStops:function(e){if(this.stops)for(var n=0,i=this._stops.length;i>n;n++)this._stops[n]._owner=t;if(e.length<2)throw Error("Gradient stop list needs to contain at least two stops.");this._stops=V.readAll(e,0,{clone:!0});for(var n=0,i=this._stops.length;i>n;n++){var r=this._stops[n];r._owner=this,r._defaultRamp&&r.setRampPoint(n/(i-1))}this._changed()},getRadial:function(){return this._radial},setRadial:function(t){this._radial=t,this._changed()},equals:function(t){if(t===this)return!0;if(t&&this._class===t._class&&this._stops.length===t._stops.length){for(var e=0,n=this._stops.length;n>e;e++)if(!this._stops[e].equals(t._stops[e]))return!1;return!0}return!1}}),V=e.extend({_class:"GradientStop",initialize:function(e,n){if(e){var i,r;n===t&&Array.isArray(e)?(i=e[0],r=e[1]):e.color?(i=e.color,r=e.rampPoint):(i=e,r=n),this.setColor(i),this.setRampPoint(r)}},clone:function(){return new V(this._color.clone(),this._rampPoint)},_serialize:function(t,n){return e.serialize([this._color,this._rampPoint],t,!0,n)},_changed:function(){this._owner&&this._owner._changed(65)},getRampPoint:function(){return this._rampPoint},setRampPoint:function(t){this._defaultRamp=null==t,this._rampPoint=t||0,this._changed()},getColor:function(){return this._color},setColor:function(t){this._color=F.read(arguments),this._color===t&&(this._color=t.clone()),this._color._owner=this,this._changed()},equals:function(t){return t===this||t&&this._class===t._class&&this._color.equals(t._color)&&this._rampPoint==t._rampPoint||!1}}),Z=e.extend(new function(){var n={fillColor:t,strokeColor:t,strokeWidth:1,strokeCap:"butt",strokeJoin:"miter",strokeScaling:!0,miterLimit:10,dashOffset:0,dashArray:[],windingRule:"nonzero",shadowColor:t,shadowBlur:0,shadowOffset:new u,selectedColor:t,fontFamily:"sans-serif",fontWeight:"normal",fontSize:12,font:"sans-serif",leading:null,justification:"left"},i={strokeWidth:97,strokeCap:97,strokeJoin:97,strokeScaling:105,miterLimit:97,fontFamily:9,fontWeight:9,fontSize:9,font:9,leading:9,justification:9},r={beans:!0},s={_defaults:n,_textDefaults:new e(n,{fillColor:new F}),beans:!0};return e.each(n,function(n,a){var o=/Color$/.test(a),h="shadowOffset"===a,l=e.capitalize(a),c=i[a],d="set"+l,f="get"+l;s[d]=function(e){var n=this._owner,i=n&&n._children;if(i&&i.length>0&&!(n instanceof N))for(var r=0,s=i.length;s>r;r++)i[r]._style[d](e);else{var h=this._values[a];h!==e&&(o&&(h&&(h._owner=t),e&&e.constructor===F&&(e._owner&&(e=e.clone()),e._owner=n)),this._values[a]=e,n&&n._changed(c||65))}},s[f]=function(n){var i,r=this._owner,s=r&&r._children;if(!s||0===s.length||n||r instanceof N){var i=this._values[a];if(i===t)i=this._defaults[a],i&&i.clone&&(i=i.clone());else{var l=o?F:h?u:null;!l||i&&i.constructor===l||(this._values[a]=i=l.read([i],0,{readNull:!0,clone:!0}),i&&o&&(i._owner=r))}return i}for(var c=0,d=s.length;d>c;c++){var _=s[c]._style[f]();if(0===c)i=_;else if(!e.equals(i,_))return t}return i},r[f]=function(t){return this._style[f](t)},r[d]=function(t){this._style[d](t)}}),x.inject(r),s},{_class:"Style",initialize:function(t,e,n){this._values={},this._owner=e,this._project=e&&e._project||n||paper.project,e instanceof R&&(this._defaults=this._textDefaults),t&&this.set(t)},set:function(t){var e=t instanceof Z,n=e?t._values:t;if(n)for(var i in n)if(i in this._defaults){var r=n[i];this[i]=r&&e&&r.clone?r.clone():r}},equals:function(t){return t===this||t&&this._class===t._class&&e.equals(this._values,t._values)||!1},hasFill:function(){return!!this.getFillColor()},hasStroke:function(){return!!this.getStrokeColor()&&this.getStrokeWidth()>0},hasShadow:function(){return!!this.getShadowColor()&&this.getShadowBlur()>0},getView:function(){return this._project.getView()},getFontStyle:function(){var t=this.getFontSize();return this.getFontWeight()+" "+t+(/[a-z]/i.test(t+"")?" ":"px ")+this.getFontFamily()},getFont:"#getFontFamily",setFont:"#setFontFamily",getLeading:function Pt(){var t=Pt.base.call(this),e=this.getFontSize();return/pt|em|%|px/.test(e)&&(e=this.getView().getPixelSize(e)),null!=t?t:1.2*e}}),U=new function(){function t(t,e,n,i){for(var r=["","webkit","moz","Moz","ms","o"],s=e[0].toUpperCase()+e.substring(1),a=0;6>a;a++){var o=r[a],h=o?o+s:e;if(h in t){if(!n)return t[h];t[h]=i;break}}}return{getStyles:function(t){var e=t&&9!==t.nodeType?t.ownerDocument:t,n=e&&e.defaultView;return n&&n.getComputedStyle(t,"")},getBounds:function(t,e){var n,i=t.ownerDocument,r=i.body,s=i.documentElement;try{n=t.getBoundingClientRect()}catch(a){n={left:0,top:0,width:0,height:0}}var o=n.left-(s.clientLeft||r.clientLeft||0),h=n.top-(s.clientTop||r.clientTop||0);if(!e){var u=i.defaultView;o+=u.pageXOffset||s.scrollLeft||r.scrollLeft,h+=u.pageYOffset||s.scrollTop||r.scrollTop}return new _(o,h,n.width,n.height)},getViewportBounds:function(t){var e=t.ownerDocument,n=e.defaultView,i=e.documentElement;return new _(0,0,n.innerWidth||i.clientWidth,n.innerHeight||i.clientHeight)},getOffset:function(t,e){return U.getBounds(t,e).getPoint()},getSize:function(t){return U.getBounds(t,!0).getSize()},isInvisible:function(t){return U.getSize(t).equals(new d(0,0))},isInView:function(t){return!U.isInvisible(t)&&U.getViewportBounds(t).intersects(U.getBounds(t,!0))},getPrefixed:function(e,n){return t(e,n)},setPrefixed:function(e,n,i){if("object"==typeof n)for(var r in n)t(e,r,!0,n[r]);else t(e,n,!0,i)}}},H={add:function(t,e){for(var n in e)for(var i=e[n],r=n.split(/[\s,]+/g),s=0,a=r.length;a>s;s++)t.addEventListener(r[s],i,!1)},remove:function(t,e){for(var n in e)for(var i=e[n],r=n.split(/[\s,]+/g),s=0,a=r.length;a>s;s++)t.removeEventListener(r[s],i,!1)},getPoint:function(t){var e=t.targetTouches?t.targetTouches.length?t.targetTouches[0]:t.changedTouches[0]:t;return new u(e.pageX||e.clientX+document.documentElement.scrollLeft,e.pageY||e.clientY+document.documentElement.scrollTop)},getTarget:function(t){return t.target||t.srcElement},getRelatedTarget:function(t){return t.relatedTarget||t.toElement},getOffset:function(t,e){return H.getPoint(t).subtract(U.getOffset(e||H.getTarget(t)))},stop:function(t){t.stopPropagation(),t.preventDefault()}};H.requestAnimationFrame=new function(){function t(){for(var e=s.length-1;e>=0;e--){var o=s[e],h=o[0],u=o[1];(!u||("true"==r.getAttribute(u,"keepalive")||a)&&U.isInView(u))&&(s.splice(e,1),h())}n&&(s.length?n(t):i=!1)}var e,n=U.getPrefixed(window,"requestAnimationFrame"),i=!1,s=[],a=!0;return H.add(window,{focus:function(){a=!0},blur:function(){a=!1}}),function(r,a){s.push([r,a]),n?i||(n(t),i=!0):e||(e=setInterval(t,1e3/60))}};var W=e.extend(n,{_class:"View",initialize:function kt(t,e){function n(t){return e[t]||parseInt(e.getAttribute(t),10)}function i(){var t=U.getSize(e);return t.isNaN()||t.isZero()?new d(n("width"),n("height")):t}this._project=t,this._scope=t._scope,this._element=e;var s;this._pixelRatio||(this._pixelRatio=window.devicePixelRatio||1),this._id=e.getAttribute("id"),null==this._id&&e.setAttribute("id",this._id="view-"+kt._id++),H.add(e,this._viewEvents);var a="none";if(U.setPrefixed(e.style,{userSelect:a,touchAction:a,touchCallout:a,contentZooming:a,userDrag:a,tapHighlightColor:"rgba(0,0,0,0)"}),r.hasAttribute(e,"resize")){var o=this;H.add(window,this._windowEvents={resize:function(){o.setViewSize(i())}})}if(this._setViewSize(s=i()),r.hasAttribute(e,"stats")&&"undefined"!=typeof Stats){this._stats=new Stats;var h=this._stats.domElement,u=h.style,l=U.getOffset(e);u.position="absolute",u.left=l.x+"px",u.top=l.y+"px",document.body.appendChild(h)}kt._views.push(this),kt._viewsById[this._id]=this,this._viewSize=s,(this._matrix=new p)._owner=this,this._zoom=1,kt._focused||(kt._focused=this),this._frameItems={},this._frameItemCount=0},remove:function(){return this._project?(W._focused===this&&(W._focused=null),W._views.splice(W._views.indexOf(this),1),delete W._viewsById[this._id],this._project._view===this&&(this._project._view=null),H.remove(this._element,this._viewEvents),H.remove(window,this._windowEvents),this._element=this._project=null,this.off("frame"),this._animate=!1,this._frameItems={},!0):!1},_events:{onFrame:{install:function(){this.play()},uninstall:function(){this.pause()}},onResize:{}},_animate:!1,_time:0,_count:0,_requestFrame:function(){var t=this;H.requestAnimationFrame(function(){t._requested=!1,t._animate&&(t._requestFrame(),t._handleFrame())},this._element),this._requested=!0},_handleFrame:function(){paper=this._scope;var t=Date.now()/1e3,n=this._before?t-this._before:0;this._before=t,this._handlingFrame=!0,this.emit("frame",new e({delta:n,time:this._time+=n,count:this._count++})),this._stats&&this._stats.update(),this._handlingFrame=!1,this.update()},_animateItem:function(t,e){var n=this._frameItems;e?(n[t._id]={item:t,time:0,count:0},1===++this._frameItemCount&&this.on("frame",this._handleFrameItems)):(delete n[t._id],0===--this._frameItemCount&&this.off("frame",this._handleFrameItems))},_handleFrameItems:function(t){for(var n in this._frameItems){var i=this._frameItems[n];i.item.emit("frame",new e(t,{time:i.time+=t.delta,count:i.count++}))}},_update:function(){this._project._needsUpdate=!0,this._handlingFrame||(this._animate?this._handleFrame():this.update())},_changed:function(t){1&t&&(this._project._needsUpdate=!0)},_transform:function(t){this._matrix.concatenate(t),this._bounds=null,this._update()},getElement:function(){return this._element},getPixelRatio:function(){return this._pixelRatio},getResolution:function(){return 72*this._pixelRatio},getViewSize:function(){var t=this._viewSize;return new f(t.width,t.height,this,"setViewSize")},setViewSize:function(){var t=d.read(arguments),e=t.subtract(this._viewSize);e.isZero()||(this._viewSize.set(t.width,t.height),this._setViewSize(t),this._bounds=null,this.emit("resize",{size:t,delta:e}),this._update())},_setViewSize:function(t){var e=this._element;e.width=t.width,e.height=t.height},getBounds:function(){return this._bounds||(this._bounds=this._matrix.inverted()._transformBounds(new _(new u,this._viewSize))),this._bounds},getSize:function(){return this.getBounds().getSize()},getCenter:function(){return this.getBounds().getCenter()},setCenter:function(){var t=u.read(arguments);this.scrollBy(t.subtract(this.getCenter()))},getZoom:function(){return this._zoom},setZoom:function(t){this._transform((new p).scale(t/this._zoom,this.getCenter())),this._zoom=t},isVisible:function(){return U.isInView(this._element)},scrollBy:function(){this._transform((new p).translate(u.read(arguments).negate()))},play:function(){this._animate=!0,this._requested||this._requestFrame()},pause:function(){this._animate=!1},draw:function(){this.update()},projectToView:function(){return this._matrix._transformPoint(u.read(arguments))},viewToProject:function(){return this._matrix._inverseTransform(u.read(arguments))}},{statics:{_views:[],_viewsById:{},_id:0,create:function(t,e){return"string"==typeof e&&(e=document.getElementById(e)),new G(t,e)}}},new function(){function t(t){var e=H.getTarget(t);return e.getAttribute&&W._viewsById[e.getAttribute("id")]}function e(t,e){return t.viewToProject(H.getOffset(e,t._element))}function n(){if(!W._focused||!W._focused.isVisible())for(var t=0,e=W._views.length;e>t;t++){var n=W._views[t];if(n&&n.isVisible()){W._focused=a=n;break}}}function i(t,e,n){t._handleEvent("mousemove",e,n);var i=t._scope.tool;return i&&i._handleEvent(l&&i.responds("mousedrag")?"mousedrag":"mousemove",e,n),t.update(),i}var r,s,a,o,h,u,l=!1,c=window.navigator;c.pointerEnabled||c.msPointerEnabled?(o="pointerdown MSPointerDown",h="pointermove MSPointerMove",u="pointerup pointercancel MSPointerUp MSPointerCancel"):(o="touchstart",h="touchmove",u="touchend touchcancel","ontouchstart"in window&&c.userAgent.match(/mobile|tablet|ip(ad|hone|od)|android|silk/i)||(o+=" mousedown",h+=" mousemove",u+=" mouseup"));var d={"selectstart dragstart":function(t){l&&t.preventDefault()}},f={mouseout:function(t){var n=W._focused,r=H.getRelatedTarget(t);!n||r&&"HTML"!==r.nodeName||i(n,e(n,t),t)},scroll:n};return d[o]=function(n){var i=W._focused=t(n),s=e(i,n);l=!0,i._handleEvent("mousedown",s,n),(r=i._scope.tool)&&r._handleEvent("mousedown",s,n),i.update()},f[h]=function(o){var h=W._focused;if(!l){var u=t(o);u?(h!==u&&i(h,e(h,o),o),s=h,h=W._focused=a=u):a&&a===h&&(h=W._focused=s,n())}if(h){var c=e(h,o);(l||h.getBounds().contains(c))&&(r=i(h,c,o))}},f[u]=function(t){var n=W._focused;if(n&&l){var i=e(n,t);l=!1,n._handleEvent("mouseup",i,t),r&&r._handleEvent("mouseup",i,t),n.update()}},H.add(document,f),H.add(window,{load:n}),{_viewEvents:d,_handleEvent:function(){},statics:{updateFocus:n}}}),G=W.extend({_class:"CanvasView",initialize:function(t,e){if(!(e instanceof HTMLCanvasElement)){var n=d.read(arguments,1);if(n.isZero())throw Error("Cannot create CanvasView with the provided argument: "+[].slice.call(arguments,1));e=et.getCanvas(n)}if(this._context=e.getContext("2d"),this._eventCounters={},this._pixelRatio=1,!/^off|false$/.test(r.getAttribute(e,"hidpi"))){var i=window.devicePixelRatio||1,s=U.getPrefixed(this._context,"backingStorePixelRatio")||1;this._pixelRatio=i/s}W.call(this,t,e)},_setViewSize:function(t){var e=this._element,n=this._pixelRatio,i=t.width,s=t.height;if(e.width=i*n,e.height=s*n,1!==n){if(!r.hasAttribute(e,"resize")){var a=e.style;a.width=i+"px",a.height=s+"px"}this._context.scale(n,n)}},getPixelSize:function(t){var e=this._context,n=e.font;return e.font=t+" serif",t=parseFloat(e.font),e.font=n,t},getTextWidth:function(t,e){var n=this._context,i=n.font,r=0;n.font=t;for(var s=0,a=e.length;a>s;s++)r=Math.max(r,n.measureText(e[s]).width);return n.font=i,r},update:function(t){var e=this._project;if(!e||!t&&!e._needsUpdate)return!1;var n=this._context,i=this._viewSize;return n.clearRect(0,0,i.width+1,i.height+1),e.draw(n,this._matrix,this._pixelRatio),e._needsUpdate=!1,!0}},new function(){function e(e,n,i,r,s,a){function o(e){return e.responds(n)&&(h||(h=new Y(n,i,r,s,a?r.subtract(a):null)),e.emit(n,h)&&h.isStopped)?(i.preventDefault(),!0):t}for(var h,u=s;u;){if(o(u))return!0;u=u.getParent()}return o(e)?!0:!1}var n,i,r,s,a,o,h,u,l;return{_handleEvent:function(t,c,d){if(this._eventCounters[t]){var f=this._project,_=f.hitTest(c,{tolerance:0,fill:!0,stroke:!0}),g=_&&_.item,p=!1;switch(t){case"mousedown":for(p=e(this,t,d,c,g),u=a==g&&Date.now()-l<300,s=a=g,n=i=r=c,h=!p&&g;h&&!h.responds("mousedrag");)h=h._parent;break;case"mouseup":p=e(this,t,d,c,g,n),h&&(i&&!i.equals(c)&&e(this,"mousedrag",d,c,h,i),g!==h&&(r=c,e(this,"mousemove",d,c,g,r))),!p&&g&&g===s&&(l=Date.now(),e(this,u&&s.responds("doubleclick")?"doubleclick":"click",d,n,g),u=!1),s=h=null;break;case"mousemove":h&&(p=e(this,"mousedrag",d,c,h,i)),p||(g!==o&&(r=c),p=e(this,t,d,c,g,r)),i=r=c,g!==o&&(e(this,"mouseleave",d,c,o),o=g,e(this,"mouseenter",d,c,g))}return p}}}}),$=e.extend({_class:"Event",initialize:function(t){this.event=t},isPrevented:!1,isStopped:!1,preventDefault:function(){this.isPrevented=!0,this.event.preventDefault()},stopPropagation:function(){this.isStopped=!0,this.event.stopPropagation()},stop:function(){this.stopPropagation(),this.preventDefault()},getModifiers:function(){return J.modifiers}}),X=$.extend({_class:"KeyEvent",initialize:function(t,e,n,i){$.call(this,i),this.type=t?"keydown":"keyup",this.key=e,this.character=n},toString:function(){return"{ type: '"+this.type+"', key: '"+this.key+"', character: '"+this.character+"', modifiers: "+this.getModifiers()+" }"}}),J=new function(){function t(t,n,r,h){var u,l=r?String.fromCharCode(r):"",c=i[n],d=c||l.toLowerCase(),f=t?"keydown":"keyup",_=W._focused,g=_&&_.isVisible()&&_._scope,p=g&&g.tool;o[d]=t,c&&(u=e.camelize(c))in s&&(s[u]=t),t?a[n]=r:delete a[n],p&&p.responds(f)&&(paper=g,p.emit(f,new X(t,d,l,h)),_&&_.update())}var n,i={8:"backspace",9:"tab",13:"enter",16:"shift",17:"control",18:"option",19:"pause",20:"caps-lock",27:"escape",32:"space",35:"end",36:"home",37:"left",38:"up",39:"right",40:"down",46:"delete",91:"command",93:"command",224:"command"},r={9:!0,13:!0,32:!0},s=new e({shift:!1,control:!1,option:!1,command:!1,capsLock:!1,space:!1}),a={},o={};return H.add(document,{keydown:function(e){var a=e.which||e.keyCode;a in i||s.command?t(!0,a,a in r||s.command?a:0,e):n=a},keypress:function(e){null!=n&&(t(!0,n,e.which||e.keyCode,e),n=null)},keyup:function(e){var n=e.which||e.keyCode;n in a&&t(!1,n,a[n],e)}}),H.add(window,{blur:function(e){for(var n in a)t(!1,n,a[n],e)}}),{modifiers:s,isDown:function(t){return!!o[t]}}},Y=$.extend({_class:"MouseEvent",initialize:function(t,e,n,i,r){$.call(this,e),this.type=t,this.point=n,this.target=i,this.delta=r},toString:function(){return"{ type: '"+this.type+"', point: "+this.point+", target: "+this.target+(this.delta?", delta: "+this.delta:"")+", modifiers: "+this.getModifiers()+" }"}}),K=$.extend({_class:"ToolEvent",_item:null,initialize:function(t,e,n){this.tool=t,this.type=e,this.event=n},_choosePoint:function(t,e){return t?t:e?e.clone():null},getPoint:function(){return this._choosePoint(this._point,this.tool._point)},setPoint:function(t){this._point=t},getLastPoint:function(){return this._choosePoint(this._lastPoint,this.tool._lastPoint)},setLastPoint:function(t){this._lastPoint=t},getDownPoint:function(){return this._choosePoint(this._downPoint,this.tool._downPoint)},setDownPoint:function(t){this._downPoint=t},getMiddlePoint:function(){return!this._middlePoint&&this.tool._lastPoint?this.tool._point.add(this.tool._lastPoint).divide(2):this._middlePoint},setMiddlePoint:function(t){this._middlePoint=t},getDelta:function(){return!this._delta&&this.tool._lastPoint?this.tool._point.subtract(this.tool._lastPoint):this._delta; -},setDelta:function(t){this._delta=t},getCount:function(){return/^mouse(down|up)$/.test(this.type)?this.tool._downCount:this.tool._count},setCount:function(t){this.tool[/^mouse(down|up)$/.test(this.type)?"downCount":"count"]=t},getItem:function(){if(!this._item){var t=this.tool._scope.project.hitTest(this.getPoint());if(t){for(var e=t.item,n=e._parent;/^(Group|CompoundPath)$/.test(n._class);)e=n,n=n._parent;this._item=e}}return this._item},setItem:function(t){this._item=t},toString:function(){return"{ type: "+this.type+", point: "+this.getPoint()+", count: "+this.getCount()+", modifiers: "+this.getModifiers()+" }"}}),Q=s.extend({_class:"Tool",_list:"tools",_reference:"tool",_events:["onActivate","onDeactivate","onEditOptions","onMouseDown","onMouseUp","onMouseDrag","onMouseMove","onKeyDown","onKeyUp"],initialize:function(t){s.call(this),this._firstMove=!0,this._count=0,this._downCount=0,this._set(t)},getMinDistance:function(){return this._minDistance},setMinDistance:function(t){this._minDistance=t,null!=t&&null!=this._maxDistance&&t>this._maxDistance&&(this._maxDistance=t)},getMaxDistance:function(){return this._maxDistance},setMaxDistance:function(t){this._maxDistance=t,null!=this._minDistance&&null!=t&&tu)return!1;if(null!=i&&0!=i)if(u>i)e=this._point.add(h.normalize(i));else if(a)return!1}if(s&&e.equals(this._point))return!1}switch(this._lastPoint=r&&"mousemove"==t?e:this._point,this._point=e,t){case"mousedown":this._lastPoint=this._downPoint,this._downPoint=this._point,this._downCount++;break;case"mouseup":this._lastPoint=this._downPoint}return this._count=r?0:this._count+1,!0},_fireEvent:function(t,e){var n=paper.project._removeSets;if(n){"mouseup"===t&&(n.mousedrag=null);var i=n[t];if(i){for(var r in i){var s=i[r];for(var a in n){var o=n[a];o&&o!=i&&delete o[s._id]}s.remove()}n[t]=null}}return this.responds(t)&&this.emit(t,new K(this,t,e))},_handleEvent:function(t,e,n){paper=this._scope;var i=!1;switch(t){case"mousedown":this._updateEvent(t,e,null,null,!0,!1,!1),i=this._fireEvent(t,n);break;case"mousedrag":for(var r=!1,s=!1;this._updateEvent(t,e,this.minDistance,this.maxDistance,!1,r,s);)i=this._fireEvent(t,n)||i,r=!0,s=!0;break;case"mouseup":!e.equals(this._point)&&this._updateEvent("mousedrag",e,this.minDistance,this.maxDistance,!1,!1,!1)&&(i=this._fireEvent("mousedrag",n)),this._updateEvent(t,e,null,this.maxDistance,!1,!1,!1),i=this._fireEvent(t,n)||i,this._updateEvent(t,e,null,null,!0,!1,!1),this._firstMove=!0;break;case"mousemove":for(;this._updateEvent(t,e,this.minDistance,this.maxDistance,this._firstMove,!0,!1);)i=this._fireEvent(t,n)||i,this._firstMove=!1}return i&&n.preventDefault(),i}}),tt={request:function(e,n,i,r){r=r===t?!0:r;var s=new(window.ActiveXObject||XMLHttpRequest)("Microsoft.XMLHTTP");return s.open(e.toUpperCase(),n,r),"overrideMimeType"in s&&s.overrideMimeType("text/plain"),s.onreadystatechange=function(){if(4===s.readyState){var t=s.status;if(0!==t&&200!==t)throw Error("Could not load "+n+" (Error "+t+")");i.call(s,s.responseText)}},s.send(null)}},et={canvases:[],getCanvas:function(t,e){var n,i=!0;"object"==typeof t&&(e=t.height,t=t.width),n=this.canvases.length?this.canvases.pop():document.createElement("canvas");var r=n.getContext("2d");return n.width===t&&n.height===e?i&&r.clearRect(0,0,t+1,e+1):(n.width=t,n.height=e),r.save(),n},getContext:function(t,e){return this.getCanvas(t,e).getContext("2d")},release:function(t){var e=t.canvas?t.canvas:t;e.getContext("2d").restore(),this.canvases.push(e)}},nt=new function(){function t(t,e,n){return.2989*t+.587*e+.114*n}function n(e,n,i,r){var s=r-t(e,n,i);f=e+s,_=n+s,g=i+s;var r=t(f,_,g),a=p(f,_,g),o=v(f,_,g);if(0>a){var h=r-a;f=r+(f-r)*r/h,_=r+(_-r)*r/h,g=r+(g-r)*r/h}if(o>255){var u=255-r,l=o-r;f=r+(f-r)*u/l,_=r+(_-r)*u/l,g=r+(g-r)*u/l}}function i(t,e,n){return v(t,e,n)-p(t,e,n)}function r(t,e,n,i){var r,s=[t,e,n],a=v(t,e,n),o=p(t,e,n);o=o===t?0:o===e?1:2,a=a===t?0:a===e?1:2,r=0===p(o,a)?1===v(o,a)?2:1:0,s[a]>s[o]?(s[r]=(s[r]-s[o])*i/(s[a]-s[o]),s[a]=i):s[r]=s[a]=0,s[o]=0,f=s[0],_=s[1],g=s[2]}var s,a,o,h,u,l,c,d,f,_,g,p=Math.min,v=Math.max,m=Math.abs,y={multiply:function(){f=u*s/255,_=l*a/255,g=c*o/255},screen:function(){f=u+s-u*s/255,_=l+a-l*a/255,g=c+o-c*o/255},overlay:function(){f=128>u?2*u*s/255:255-2*(255-u)*(255-s)/255,_=128>l?2*l*a/255:255-2*(255-l)*(255-a)/255,g=128>c?2*c*o/255:255-2*(255-c)*(255-o)/255},"soft-light":function(){var t=s*u/255;f=t+u*(255-(255-u)*(255-s)/255-t)/255,t=a*l/255,_=t+l*(255-(255-l)*(255-a)/255-t)/255,t=o*c/255,g=t+c*(255-(255-c)*(255-o)/255-t)/255},"hard-light":function(){f=128>s?2*s*u/255:255-2*(255-s)*(255-u)/255,_=128>a?2*a*l/255:255-2*(255-a)*(255-l)/255,g=128>o?2*o*c/255:255-2*(255-o)*(255-c)/255},"color-dodge":function(){f=0===u?0:255===s?255:p(255,255*u/(255-s)),_=0===l?0:255===a?255:p(255,255*l/(255-a)),g=0===c?0:255===o?255:p(255,255*c/(255-o))},"color-burn":function(){f=255===u?255:0===s?0:v(0,255-255*(255-u)/s),_=255===l?255:0===a?0:v(0,255-255*(255-l)/a),g=255===c?255:0===o?0:v(0,255-255*(255-c)/o)},darken:function(){f=s>u?u:s,_=a>l?l:a,g=o>c?c:o},lighten:function(){f=u>s?u:s,_=l>a?l:a,g=c>o?c:o},difference:function(){f=u-s,0>f&&(f=-f),_=l-a,0>_&&(_=-_),g=c-o,0>g&&(g=-g)},exclusion:function(){f=u+s*(255-u-u)/255,_=l+a*(255-l-l)/255,g=c+o*(255-c-c)/255},hue:function(){r(s,a,o,i(u,l,c)),n(f,_,g,t(u,l,c))},saturation:function(){r(u,l,c,i(s,a,o)),n(f,_,g,t(u,l,c))},luminosity:function(){n(u,l,c,t(s,a,o))},color:function(){n(s,a,o,t(u,l,c))},add:function(){f=p(u+s,255),_=p(l+a,255),g=p(c+o,255)},subtract:function(){f=v(u-s,0),_=v(l-a,0),g=v(c-o,0)},average:function(){f=(u+s)/2,_=(l+a)/2,g=(c+o)/2},negation:function(){f=255-m(255-s-u),_=255-m(255-a-l),g=255-m(255-o-c)}},w=this.nativeModes=e.each(["source-over","source-in","source-out","source-atop","destination-over","destination-in","destination-out","destination-atop","lighter","darker","copy","xor"],function(t){this[t]=!0},{}),x=et.getContext(1,1);e.each(y,function(t,e){var n="darken"===e,i=!1;x.save();try{x.fillStyle=n?"#300":"#a00",x.fillRect(0,0,1,1),x.globalCompositeOperation=e,x.globalCompositeOperation===e&&(x.fillStyle=n?"#a00":"#300",x.fillRect(0,0,1,1),i=x.getImageData(0,0,1,1).data[0]!==n?170:51)}catch(r){}x.restore(),w[e]=i}),et.release(x),this.process=function(t,e,n,i,r){var p=e.canvas,v="normal"===t;if(v||w[t])n.save(),n.setTransform(1,0,0,1,0,0),n.globalAlpha=i,v||(n.globalCompositeOperation=t),n.drawImage(p,r.x,r.y),n.restore();else{var m=y[t];if(!m)return;for(var x=n.getImageData(r.x,r.y,p.width,p.height),b=x.data,C=e.getImageData(0,0,p.width,p.height).data,S=0,P=b.length;P>S;S+=4){s=C[S],u=b[S],a=C[S+1],l=b[S+1],o=C[S+2],c=b[S+2],h=C[S+3],d=b[S+3],m();var k=h*i/255,M=1-k;b[S]=k*f+M*u,b[S+1]=k*_+M*l,b[S+2]=k*g+M*c,b[S+3]=h*i+M*d}n.putImageData(x,r.x,r.y)}}},it=e.each({fillColor:["fill","color"],strokeColor:["stroke","color"],strokeWidth:["stroke-width","number"],strokeCap:["stroke-linecap","string"],strokeJoin:["stroke-linejoin","string"],strokeScaling:["vector-effect","lookup",{"true":"none","false":"non-scaling-stroke"},function(t,e){return!e&&(t instanceof L||t instanceof S||t instanceof R)}],miterLimit:["stroke-miterlimit","number"],dashArray:["stroke-dasharray","array"],dashOffset:["stroke-dashoffset","number"],fontFamily:["font-family","string"],fontWeight:["font-weight","string"],fontSize:["font-size","number"],justification:["text-anchor","lookup",{left:"start",center:"middle",right:"end"}],opacity:["opacity","number"],blendMode:["mix-blend-mode","string"]},function(t,n){var i=e.capitalize(n),r=t[2];this[n]={type:t[1],property:n,attribute:t[0],toSVG:r,fromSVG:r&&e.each(r,function(t,e){this[t]=e},{}),exportFilter:t[3],get:"get"+i,set:"set"+i}},{}),rt={href:"http://www.w3.org/1999/xlink",xlink:"http://www.w3.org/2000/xmlns"};return new function(){function t(t,e){for(var n in e){var i=e[n],r=rt[n];"number"==typeof i&&(i=S.number(i)),r?t.setAttributeNS(r,n,i):t.setAttribute(n,i)}return t}function n(e,n){return t(document.createElementNS("http://www.w3.org/2000/svg",e),n)}function r(t,n,i){var r=new e,s=t.getTranslation();if(n){t=t.shiftless();var a=t._inverseTransform(s);r[i?"cx":"x"]=a.x,r[i?"cy":"y"]=a.y,s=null}if(!t.isIdentity()){var h=t.decompose();if(h&&!h.shearing){var u=[],l=h.rotation,c=h.scaling;s&&!s.isZero()&&u.push("translate("+S.point(s)+")"),o.isZero(c.x-1)&&o.isZero(c.y-1)||u.push("scale("+S.point(c)+")"),l&&u.push("rotate("+S.number(l)+")"),r.transform=u.join(" ")}else r.transform="matrix("+t.getValues().join(",")+")"}return r}function s(e,i){for(var s=r(e._matrix),a=e._children,o=n("g",s),h=0,u=a.length;u>h;h++){var l=a[h],c=b(l,i);if(c)if(l.isClipMask()){var d=n("clipPath");d.appendChild(c),m(l,d,"clip"),t(o,{"clip-path":"url(#"+d.id+")"})}else o.appendChild(c)}return o}function h(t){var e=r(t._matrix,!0),i=t.getSize();return e.x-=i.width/2,e.y-=i.height/2,e.width=i.width,e.height=i.height,e.href=t.toDataURL(),n("image",e)}function u(t,e){if(e.matchShapes){var s=t.toShape(!1);if(s)return c(s,e)}var a,o=t._segments,h=r(t._matrix);if(0===o.length)return null;if(t.isPolygon())if(o.length>=3){a=t._closed?"polygon":"polyline";var u=[];for(i=0,l=o.length;il;l++){var d=u[l],f=d._color,_=f.getAlpha();i={offset:d._rampPoint,"stop-color":f.toCSS(!0)},1>_&&(i["stop-opacity"]=_),e.appendChild(n("stop",i))}m(t,e,"color")}return"url(#"+e.id+")"}function g(t){var e=n("text",r(t._matrix,!0));return e.textContent=t._content,e}function p(n,i,r){var s={},a=!r&&n.getParent();return null!=n._name&&(s.id=n._name),e.each(it,function(t){var i=t.get,r=t.type,o=n[i]();if(t.exportFilter?t.exportFilter(n,o):!a||!e.equals(a[i](),o)){if("color"===r&&null!=o){var h=o.getAlpha();1>h&&(s[t.attribute+"-opacity"]=h)}s[t.attribute]=null==o?"none":"number"===r?S.number(o):"color"===r?o.gradient?_(o,n):o.toCSS(!0):"array"===r?o.join(","):"lookup"===r?t.toSVG[o]:o}}),1===s.opacity&&delete s.opacity,n._visible||(s.visibility="hidden"),t(i,s)}function v(t,e){return P||(P={ids:{},svgs:{}}),t&&P.svgs[e+"-"+t._id]}function m(t,e,n){P||v();var i=P.ids[n]=(P.ids[n]||0)+1;e.id=n+"-"+i,P.svgs[n+"-"+t._id]=e}function w(t,e){var i=t,r=null;if(P){i="svg"===t.nodeName.toLowerCase()&&t;for(var s in P.svgs)r||(i||(i=n("svg"),i.appendChild(t)),r=i.insertBefore(n("defs"),i.firstChild)),r.appendChild(P.svgs[s]);P=null}return e.asString?(new XMLSerializer).serializeToString(i):i}function b(t,e,n){var i=k[t._class],r=i&&i(t,e);if(r){var s=e.onExport;s&&(r=s(t,r,e)||r);var a=JSON.stringify(t._data);a&&"{}"!==a&&"null"!==a&&r.setAttribute("data-paper-data",a)}return r&&p(t,r,n)}function C(t){return t||(t={}),S=new a(t.precision),t}var S,P,k={Group:s,Layer:s,Raster:h,Path:u,Shape:c,CompoundPath:d,PlacedSymbol:f,PointText:g};x.inject({exportSVG:function(t){return t=C(t),w(b(this,t,!0),t)}}),y.inject({exportSVG:function(t){t=C(t);var e=this.layers,i=this.getView(),s=i.getViewSize(),a=n("svg",{x:0,y:0,width:s.width,height:s.height,version:"1.1",xmlns:"http://www.w3.org/2000/svg","xmlns:xlink":"http://www.w3.org/1999/xlink"}),o=a,h=i._matrix;h.isIdentity()||(o=a.appendChild(n("g",r(h))));for(var u=0,l=e.length;l>u;u++)o.appendChild(b(e[u],t,!0));return w(a,t)}})},new function(){function n(t,e,n,i){var r=rt[e],s=r?t.getAttributeNS(r,e):t.getAttribute(e);return"null"===s&&(s=null),null==s?i?null:n?"":0:n?s:parseFloat(s)}function i(t,e,i,r){return e=n(t,e,!1,r),i=n(t,i,!1,r),!r||null!=e&&null!=i?new u(e,i):null}function r(t,e,i,r){return e=n(t,e,!1,r),i=n(t,i,!1,r),!r||null!=e&&null!=i?new d(e,i):null}function s(t,e,n){return"none"===t?null:"number"===e?parseFloat(t):"array"===e?t?t.split(/[\s,]+/g).map(parseFloat):[]:"color"===e?m(t)||t:"lookup"===e?n[t]:t}function a(t,e,n,i){var r=t.childNodes,s="clippath"===e,a=new b,o=a._project,h=o._currentStyle,u=[];if(s||(a=v(a,t,i),o._currentStyle=a._style.clone()),i)for(var l=t.querySelectorAll("defs"),c=0,d=l.length;d>c;c++)C(l[c],n,!1);for(var c=0,d=r.length;d>c;c++){var f,_=r[c];1!==_.nodeType||"defs"===_.nodeName.toLowerCase()||!(f=C(_,n,!1))||f instanceof w||u.push(f)}return a.addChildren(u),s&&(a=v(a.reduce(),t,i)),o._currentStyle=h,(s||"defs"===e)&&(a.remove(),a=null),a}function o(t,e){for(var n=t.getAttribute("points").match(/[+-]?(?:\d*\.\d+|\d+\.?)(?:[eE][+-]?\d+)?/g),i=[],r=0,s=n.length;s>r;r+=2)i.push(new u(parseFloat(n[r]),parseFloat(n[r+1])));var a=new E(i);return"polygon"===e&&a.closePath(),a}function h(t){var e=t.getAttribute("d"),n={pathData:e};return(e.match(/m/gi)||[]).length>1||/z\S+/i.test(e)?new N(n):new E(n)}function l(t,e){var r,s=(n(t,"href",!0)||"").substring(1),a="radialgradient"===e;if(s)r=I[s].getGradient();else{for(var o=t.childNodes,h=[],u=0,l=o.length;l>u;u++){var c=o[u];1===c.nodeType&&h.push(v(new V,c))}r=new q(h,a)}var d,f,_;return a?(d=i(t,"cx","cy"),f=d.add(n(t,"r"),0),_=i(t,"fx","fy",!0)):(d=i(t,"x1","y1"),f=i(t,"x2","y2")),v(new F(r,d,f,_),t),null}function c(t,e,n,i){for(var r=(i.getAttribute(n)||"").split(/\)\s*/g),s=new p,a=0,o=r.length;o>a;a++){var h=r[a];if(!h)break;for(var u=h.split(/\(\s*/),l=u[0],c=u[1].split(/[\s,]+/g),d=0,f=c.length;f>d;d++)c[d]=parseFloat(c[d]);switch(l){case"matrix":s.concatenate(new p(c[0],c[1],c[2],c[3],c[4],c[5]));break;case"rotate":s.rotate(c[0],c[1],c[2]);break;case"translate":s.translate(c[0],c[1]);break;case"scale":s.scale(c);break;case"skewX":s.skew(c[0],0);break;case"skewY":s.skew(0,c[0])}}t.transform(s)}function f(t,e,n){var i=t["fill-opacity"===n?"getFillColor":"getStrokeColor"]();i&&i.setAlpha(parseFloat(e))}function g(n,i,r){var s=n.attributes[i],a=s&&s.value;if(!a){var o=e.camelize(i);a=n.style[o],a||r.node[o]===r.parent[o]||(a=r.node[o])}return a?"none"===a?null:a:t}function v(n,i,r){var s={node:U.getStyles(i)||{},parent:!r&&U.getStyles(i.parentNode)||{}};return e.each(M,function(r,a){var o=g(i,a,s);o!==t&&(n=e.pick(r(n,o,a,i,s),n))}),n}function m(t){var e=t&&t.match(/\((?:#|)([^)']+)/);return e&&I[e[1]]}function C(t,n,i){function r(t){paper=a;var e=C(t,n,i),r=n.onLoad,s=a.project&&a.getView();r&&r.call(this,e),s.update()}if(!t)return null;n?"function"==typeof n&&(n={onLoad:n}):n={};var s=t,a=paper;if(i)if("string"!=typeof t||/^.*s;s++){var o=r[s];if(1===o.nodeType){var h=o.nextSibling;document.body.appendChild(o);var u=C(o,n,i);return h?t.insertBefore(o,h):t.appendChild(o),u}}},g:a,svg:a,clippath:a,polygon:o,polyline:o,path:h,lineargradient:l,radialgradient:l,image:function(t){var e=new P(n(t,"href",!0));return e.on("load",function(){var e=r(t,"width","height");this.setSize(e);var n=this._matrix._transformPoint(i(t,"x","y").add(e.divide(2)));this.translate(n)}),e},symbol:function(t,e,n,i){return new w(a(t,e,n,i),!0)},defs:a,use:function(t){var e=(n(t,"href",!0)||"").substring(1),r=I[e],s=i(t,"x","y");return r?r instanceof w?r.place(s):r.clone().translate(s):null},circle:function(t){return new S.Circle(i(t,"cx","cy"),n(t,"r"))},ellipse:function(t){return new S.Ellipse({center:i(t,"cx","cy"),radius:r(t,"rx","ry")})},rect:function(t){var e=i(t,"x","y"),n=r(t,"width","height"),s=r(t,"rx","ry");return new S.Rectangle(new _(e,n),s)},line:function(t){return new E.Line(i(t,"x1","y1"),i(t,"x2","y2"))},text:function(t){var e=new D(i(t,"x","y").add(i(t,"dx","dy")));return e.setContent(t.textContent.trim()||""),e}},M=e.set(e.each(it,function(t){this[t.attribute]=function(e,n){if(e[t.set](s(n,t.type,t.fromSVG)),"color"===t.type&&e instanceof S){var i=e[t.get]();i&&i.transform((new p).translate(e.getPosition(!0).negate()))}}},{}),{id:function(t,e){I[e]=t,t.setName&&t.setName(e)},"clip-path":function(t,e){var n=m(e);if(n){if(n=n.clone(),n.setClipMask(!0),!(t instanceof b))return new b(n,t);t.insertChild(0,n)}},gradientTransform:c,transform:c,"fill-opacity":f,"stroke-opacity":f,visibility:function(t,e){t.setVisible("visible"===e)},display:function(t,e){t.setVisible(null!==e)},"stop-color":function(t,e){t.setColor&&t.setColor(e)},"stop-opacity":function(t,e){t._color&&t._color.setAlpha(parseFloat(e))},offset:function(t,e){var n=e.match(/(.*)%$/);t.setRampPoint(n?n[1]/100:parseFloat(e))},viewBox:function(t,e,n,i,a){var o=new _(s(e,"array")),h=r(i,"width","height",!0);if(t instanceof b){var u=h?o.getSize().divide(h):1,l=(new p).translate(o.getPoint()).scale(u);t.transform(l.inverted())}else if(t instanceof w){h&&o.setSize(h);var c="visible"!=g(i,"overflow",a),d=t._definition;c&&!o.contains(d.getBounds())&&(c=new S.Rectangle(o).transform(d._matrix),c.setClipMask(!0),d.addChild(c))}}}),I={};x.inject({importSVG:function(t,e){return this.addChild(C(t,e,!0))}}),y.inject({importSVG:function(t,e){return this.activate(),C(t,e,!0)}})},e.exports.PaperScript=function(){function t(t,e,n){var i=g[e];if(t&&t[i]){var r=t[i](n);return"!="===e?!r:r}switch(e){case"+":return t+n;case"-":return t-n;case"*":return t*n;case"/":return t/n;case"%":return t%n;case"==":return t==n;case"!=":return t!=n}}function n(t,e){var n=p[t];if(n&&e&&e[n])return e[n]();switch(t){case"+":return+e;case"-":return-e}}function i(t,e){return _.acorn.parse(t,e)}function s(t,e,n){function r(t){for(var e=0,n=u.length;n>e;e++){var i=u[e];if(i[0]>=t)break;t+=i[1]}return t}function s(e){return t.substring(r(e.range[0]),r(e.range[1]))}function a(e,n){return t.substring(r(e.range[1]),r(n.range[0]))}function o(e,n){for(var i=r(e.range[0]),s=r(e.range[1]),a=0,o=u.length-1;o>=0;o--)if(i>u[o][0]){a=o+1;break}u.splice(a,0,[i,n.length-s+i]),t=t.substring(0,i)+n+t.substring(s)}function h(t,e){if(t){for(var n in t)if("range"!==n&&"loc"!==n){var i=t[n];if(Array.isArray(i))for(var r=0,u=i.length;u>r;r++)h(i[r],t);else i&&"object"==typeof i&&h(i,t)}switch(t.type){case"UnaryExpression":if(t.operator in p&&"Literal"!==t.argument.type){var l=s(t.argument);o(t,'$__("'+t.operator+'", '+l+")")}break;case"BinaryExpression":if(t.operator in g&&"Literal"!==t.left.type){var c=s(t.left),d=s(t.right),f=a(t.left,t.right),_=t.operator;o(t,"__$__("+c+","+f.replace(RegExp("\\"+_),'"'+_+'"')+", "+d+")")}break;case"UpdateExpression":case"AssignmentExpression":var v=e&&e.type;if(!("ForStatement"===v||"BinaryExpression"===v&&/^[=!<>]/.test(e.operator)||"MemberExpression"===v&&e.computed))if("UpdateExpression"===t.type){var l=s(t.argument),m="__$__("+l+', "'+t.operator[0]+'", 1)',y=l+" = "+m;t.prefix||"AssignmentExpression"!==v&&"VariableDeclarator"!==v||(s(e.left||e.id)===l&&(y=m),y=l+"; "+y),o(t,y)}else if(/^.=$/.test(t.operator)&&"Literal"!==t.left.type){var c=s(t.left),d=s(t.right);o(t,c+" = __$__("+c+', "'+t.operator[0]+'", '+d+")")}}}}if(!t)return"";n=n||{},e=e||"";var u=[],l=null,c=paper.browser,d=c.versionNumber,f=/\r\n|\n|\r/gm;if(c.chrome&&d>=30||c.webkit&&d>=537.76||c.firefox&&d>=23){var _=0;if(0===window.location.href.indexOf(e)){var v=document.getElementsByTagName("html")[0].innerHTML;_=v.substr(0,v.indexOf(t)+1).match(f).length+1}var m=["AAAA"];m.length=(t.match(f)||[]).length+1+_,l={version:3,file:e,names:[],mappings:m.join(";AACA"),sourceRoot:"",sources:[e]};var y=n.source||!e&&t;y&&(l.sourcesContent=[y])}return h(i(t,{ranges:!0})),l&&(t=Array(_+1).join("\n")+t+"\n//# sourceMappingURL=data:application/json;base64,"+btoa(unescape(encodeURIComponent(JSON.stringify(l))))+"\n//# sourceURL="+(e||"paperscript")),t}function a(i,r,a,o){function h(t,e){for(var n in t)!e&&/^_/.test(n)||!RegExp("([\\b\\s\\W]|^)"+n.replace(/\$/g,"\\$")+"\\b").test(i)||(g.push(n),p.push(t[n]))}paper=r;var l,c=r.getView(),d=/\s+on(?:Key|Mouse)(?:Up|Down|Move|Drag)\b/.test(i)?new Q:null,f=d?d._events:[],_=["onFrame","onResize"].concat(f),g=[],p=[];i=s(i,a,o),h({__$__:t,$__:n,paper:r,view:c,tool:d},!0),h(r),_=e.each(_,function(t){RegExp("\\s+"+t+"\\b").test(i)&&(g.push(t),this.push(t+": "+t))},[]).join(", "),_&&(i+="\nreturn { "+_+" };");var v=paper.browser;if(v.chrome||v.firefox){var m=document.createElement("script"),y=document.head||document.getElementsByTagName("head")[0];v.firefox&&(i="\n"+i),m.appendChild(document.createTextNode("paper._execute = function("+g+") {"+i+"\n}")),y.appendChild(m),l=paper._execute,delete paper._execute,y.removeChild(m)}else l=Function(g,i);var w=l.apply(r,p)||{};e.each(f,function(t){var e=w[t];e&&(d[t]=e)}),c&&(w.onResize&&c.setOnResize(w.onResize),c.emit("resize",{size:c.size,delta:new u}),w.onFrame&&c.setOnFrame(w.onFrame),c.update())}function o(t){if(/^text\/(?:x-|)paperscript$/.test(t.type)&&"true"!==r.getAttribute(t,"ignore")){var e=r.getAttribute(t,"canvas"),n=document.getElementById(e),i=t.src,s=r.hasAttribute(t,"asyc"),o="data-paper-scope";if(!n)throw Error('Unable to find canvas with id "'+e+'"');var h=r.get(n.getAttribute(o))||(new r).setup(n);return n.setAttribute(o,h._id),i?tt.request("get",i,function(t){a(t,h,i)},s):a(t.innerHTML,h,t.baseURI),t.setAttribute("data-paper-ignore","true"),h}}function h(){e.each(document.getElementsByTagName("script"),o)}function l(t){return t?o(t):h()}var c,f,_=this;!function(t,e){return"object"==typeof c&&"object"==typeof module?e(c):"function"==typeof f&&f.amd?f(["exports"],e):void e(t.acorn||(t.acorn={}))}(this,function(t){"use strict";function e(t){ct=t||{};for(var e in gt)Object.prototype.hasOwnProperty.call(ct,e)||(ct[e]=gt[e]);_t=ct.sourceFile||null}function n(t,e){var n=pt(dt,t);e+=" ("+n.line+":"+n.column+")";var i=new SyntaxError(e);throw i.pos=t,i.loc=n,i.raisedAt=vt,i}function i(t){function e(t){if(1==t.length)return n+="return str === "+JSON.stringify(t[0])+";";n+="switch(str){";for(var e=0;e3){i.sort(function(t,e){return e.length-t.length}),n+="switch(str.length){";for(var r=0;rvt&&10!==n&&13!==n&&8232!==n&&8233!==n;)++vt,n=dt.charCodeAt(vt);ct.onComment&&ct.onComment(!1,dt.slice(t+2,vt),t,vt,e,ct.locations&&new r)}function u(){for(;ft>vt;){var t=dt.charCodeAt(vt);if(32===t)++vt;else if(13===t){++vt;var e=dt.charCodeAt(vt);10===e&&++vt,ct.locations&&(++Pt,kt=vt)}else if(10===t||8232===t||8233===t)++vt,ct.locations&&(++Pt,kt=vt);else if(t>8&&14>t)++vt;else if(47===t){var e=dt.charCodeAt(vt+1);if(42===e)o();else{if(47!==e)break;h()}}else if(160===t)++vt;else{if(!(t>=5760&&Ue.test(String.fromCharCode(t))))break;++vt}}}function l(){var t=dt.charCodeAt(vt+1);return t>=48&&57>=t?S(!0):(++vt,a(we))}function c(){var t=dt.charCodeAt(vt+1);return St?(++vt,x()):61===t?w(Se,2):w(be,1)}function d(){var t=dt.charCodeAt(vt+1);return 61===t?w(Se,2):w(Be,1)}function f(t){var e=dt.charCodeAt(vt+1);return e===t?w(124===t?Ie:ze,2):61===e?w(Se,2):w(124===t?Ae:Te,1)}function _(){var t=dt.charCodeAt(vt+1);return 61===t?w(Se,2):w(Oe,1)}function g(t){var e=dt.charCodeAt(vt+1);return e===t?45==e&&62==dt.charCodeAt(vt+2)&&Xe.test(dt.slice(It,vt))?(vt+=3,h(),u(),y()):w(ke,2):61===e?w(Se,2):w(Pe,1)}function p(t){var e=dt.charCodeAt(vt+1),n=1;return e===t?(n=62===t&&62===dt.charCodeAt(vt+2)?3:2,61===dt.charCodeAt(vt+n)?w(Se,n+1):w(Ne,n)):33==e&&60==t&&45==dt.charCodeAt(vt+2)&&45==dt.charCodeAt(vt+3)?(vt+=4,h(),u(),y()):(61===e&&(n=61===dt.charCodeAt(vt+2)?3:2),w(Ee,n))}function v(t){var e=dt.charCodeAt(vt+1);return 61===e?w(Le,61===dt.charCodeAt(vt+2)?3:2):w(61===t?Ce:Me,1)}function m(t){switch(t){case 46:return l();case 40:return++vt,a(ge);case 41:return++vt,a(pe);case 59:return++vt,a(me);case 44:return++vt,a(ve);case 91:return++vt,a(ce);case 93:return++vt,a(de);case 123:return++vt,a(fe);case 125:return++vt,a(_e);case 58:return++vt,a(ye);case 63:return++vt,a(xe);case 48:var e=dt.charCodeAt(vt+1);if(120===e||88===e)return C();case 49:case 50:case 51:case 52:case 53:case 54:case 55:case 56:case 57:return S(!1);case 34:case 39:return P(t);case 47:return c(t);case 37:case 42:return d();case 124:case 38:return f(t);case 94:return _();case 43:case 45:return g(t);case 60:case 62:return p(t);case 61:case 33:return v(t);case 126:return w(Me,1)}return!1}function y(t){if(t?vt=mt+1:mt=vt,ct.locations&&(wt=new r),t)return x();if(vt>=ft)return a(Rt);var e=dt.charCodeAt(vt);if(Ye(e)||92===e)return I();var i=m(e);if(i===!1){var s=String.fromCharCode(e);if("\\"===s||Ge.test(s))return I();n(vt,"Unexpected character '"+s+"'")}return i}function w(t,e){var n=dt.slice(vt,vt+e);vt+=e,a(t,n)}function x(){for(var t,e,i="",r=vt;;){vt>=ft&&n(r,"Unterminated regular expression");var s=dt.charAt(vt);if(Xe.test(s)&&n(r,"Unterminated regular expression"),t)t=!1;else{if("["===s)e=!0;else if("]"===s&&e)e=!1;else if("/"===s&&!e)break;t="\\"===s}++vt}var i=dt.slice(r,vt);++vt;var o=M();return o&&!/^[gmsiy]*$/.test(o)&&n(r,"Invalid regexp flag"),a(Nt,RegExp(i,o))}function b(t,e){for(var n=vt,i=0,r=0,s=null==e?1/0:e;s>r;++r){var a,o=dt.charCodeAt(vt);if(a=o>=97?o-97+10:o>=65?o-65+10:o>=48&&57>=o?o-48:1/0,a>=t)break;++vt,i=i*t+a}return vt===n||null!=e&&vt-n!==e?null:i}function C(){vt+=2;var t=b(16);return null==t&&n(mt+2,"Expected hexadecimal number"),Ye(dt.charCodeAt(vt))&&n(vt,"Identifier directly after number"),a(Et,t)}function S(t){var e=vt,i=!1,r=48===dt.charCodeAt(vt);t||null!==b(10)||n(e,"Invalid number"),46===dt.charCodeAt(vt)&&(++vt,b(10),i=!0);var s=dt.charCodeAt(vt);(69===s||101===s)&&(s=dt.charCodeAt(++vt),(43===s||45===s)&&++vt,null===b(10)&&n(e,"Invalid number"),i=!0),Ye(dt.charCodeAt(vt))&&n(vt,"Identifier directly after number");var o,h=dt.slice(e,vt);return i?o=parseFloat(h):r&&1!==h.length?/[89]/.test(h)||Tt?n(e,"Invalid number"):o=parseInt(h,8):o=parseInt(h,10),a(Et,o)}function P(t){vt++;for(var e="";;){vt>=ft&&n(mt,"Unterminated string constant");var i=dt.charCodeAt(vt);if(i===t)return++vt,a(Bt,e);if(92===i){i=dt.charCodeAt(++vt);var r=/^[0-7]+/.exec(dt.slice(vt,vt+3));for(r&&(r=r[0]);r&&parseInt(r,8)>255;)r=r.slice(0,r.length-1);if("0"===r&&(r=null),++vt,r)Tt&&n(vt-2,"Octal literal in strict mode"),e+=String.fromCharCode(parseInt(r,8)),vt+=r.length-1;else switch(i){case 110:e+="\n";break;case 114:e+="\r";break;case 120:e+=String.fromCharCode(k(2));break;case 117:e+=String.fromCharCode(k(4));break;case 85:e+=String.fromCharCode(k(8));break;case 116:e+=" ";break;case 98:e+="\b";break;case 118:e+=" ";break;case 102:e+="\f";break;case 48:e+="\x00";break;case 13:10===dt.charCodeAt(vt)&&++vt;case 10:ct.locations&&(kt=vt,++Pt);break;default:e+=String.fromCharCode(i)}}else(13===i||10===i||8232===i||8233===i)&&n(mt,"Unterminated string constant"),e+=String.fromCharCode(i),++vt}}function k(t){var e=b(16,t);return null===e&&n(mt,"Bad character escape sequence"),e}function M(){Re=!1;for(var t,e=!0,i=vt;;){var r=dt.charCodeAt(vt);if(Ke(r))Re&&(t+=dt.charAt(vt)),++vt;else{if(92!==r)break;Re||(t=dt.slice(i,vt)),Re=!0,117!=dt.charCodeAt(++vt)&&n(vt,"Expecting Unicode escape sequence \\uXXXX"),++vt;var s=k(4),a=String.fromCharCode(s);a||n(vt-1,"Invalid Unicode escape"),(e?Ye(s):Ke(s))||n(vt-4,"Invalid Unicode escape"),t+=a}e=!1}return Re?t:dt.slice(i,vt)}function I(){var t=M(),e=jt;return Re||(Ze(t)?e=le[t]:(ct.forbidReserved&&(3===ct.ecmaVersion?De:Fe)(t)||Tt&&qe(t))&&n(mt,"The keyword '"+t+"' is reserved")),a(e,t)}function z(){Mt=mt,It=yt,zt=xt,y()}function A(t){if(Tt=t,vt=It,ct.locations)for(;kt>vt;)kt=dt.lastIndexOf("\n",kt-2)+1,--Pt;u(),y()}function O(){this.type=null,this.start=mt,this.end=null}function T(){this.start=wt,this.end=null,null!==_t&&(this.source=_t)}function L(){var t=new O;return ct.locations&&(t.loc=new T),ct.ranges&&(t.range=[mt,0]),t}function E(t){var e=new O;return e.start=t.start,ct.locations&&(e.loc=new T,e.loc.start=t.loc.start),ct.ranges&&(e.range=[t.range[0],0]),e}function N(t,e){return t.type=e,t.end=It,ct.locations&&(t.loc.end=zt),ct.ranges&&(t.range[1]=It),t}function B(t){return ct.ecmaVersion>=5&&"ExpressionStatement"===t.type&&"Literal"===t.expression.type&&"use strict"===t.expression.value}function j(t){return bt===t?(z(),!0):void 0}function R(){return!ct.strictSemicolons&&(bt===Rt||bt===_e||Xe.test(dt.slice(It,mt)))}function D(){j(me)||R()||q()}function F(t){bt===t?z():q()}function q(){n(mt,"Unexpected token")}function V(t){"Identifier"!==t.type&&"MemberExpression"!==t.type&&n(t.start,"Assigning to rvalue"),Tt&&"Identifier"===t.type&&Ve(t.name)&&n(t.start,"Assigning to "+t.name+" in strict mode")}function Z(t){Mt=It=vt,ct.locations&&(zt=new r),At=Tt=null,Ot=[],y();var e=t||L(),n=!0;for(t||(e.body=[]);bt!==Rt;){var i=U();e.body.push(i),n&&B(i)&&A(!0),n=!1}return N(e,"Program")}function U(){(bt===be||bt===Se&&"/="==Ct)&&y(!0);var t=bt,e=L();switch(t){case Dt:case Vt:z();var i=t===Dt;j(me)||R()?e.label=null:bt!==jt?q():(e.label=lt(),D());for(var r=0;re){var r=E(t);r.left=t,r.operator=Ct,z(),r.right=tt(et(),i,n);var s=N(r,/&&|\|\|/.test(r.operator)?"LogicalExpression":"BinaryExpression");return tt(s,e,n)}return t}function et(){if(bt.prefix){var t=L(),e=bt.isUpdate;return t.operator=Ct,t.prefix=!0,St=!0,z(),t.argument=et(),e?V(t.argument):Tt&&"delete"===t.operator&&"Identifier"===t.argument.type&&n(t.start,"Deleting local variable in strict mode"),N(t,e?"UpdateExpression":"UnaryExpression")}for(var i=nt();bt.postfix&&!R();){var t=E(i);t.operator=Ct,t.prefix=!1,t.argument=i,V(i),z(),i=N(t,"UpdateExpression")}return i}function nt(){return it(rt())}function it(t,e){if(j(we)){var n=E(t);return n.object=t,n.property=lt(!0),n.computed=!1,it(N(n,"MemberExpression"),e)}if(j(ce)){var n=E(t);return n.object=t,n.property=J(),n.computed=!0,F(de),it(N(n,"MemberExpression"),e)}if(!e&&j(ge)){var n=E(t);return n.callee=t,n.arguments=ut(pe,!1),it(N(n,"CallExpression"),e)}return t}function rt(){switch(bt){case se:var t=L();return z(),N(t,"ThisExpression");case jt:return lt();case Et:case Bt:case Nt:var t=L();return t.value=Ct,t.raw=dt.slice(mt,yt),z(),N(t,"Literal");case ae:case oe:case he:var t=L();return t.value=bt.atomValue,t.raw=bt.keyword,z(),N(t,"Literal");case ge:var e=wt,n=mt;z();var i=J();return i.start=n,i.end=yt,ct.locations&&(i.loc.start=e,i.loc.end=xt),ct.ranges&&(i.range=[n,yt]),F(pe),i;case ce:var t=L();return z(),t.elements=ut(de,!0,!0),N(t,"ArrayExpression");case fe:return at();case Xt:var t=L();return z(),ht(t,!1);case re:return st();default:q()}}function st(){var t=L();return z(),t.callee=it(rt(),!0),t.arguments=j(ge)?ut(pe,!1):Lt,N(t,"NewExpression")}function at(){var t=L(),e=!0,i=!1;for(t.properties=[],z();!j(_e);){if(e)e=!1;else if(F(ve),ct.allowTrailingCommas&&j(_e))break;var r,s={key:ot()},a=!1;if(j(ye)?(s.value=J(!0),r=s.kind="init"):ct.ecmaVersion>=5&&"Identifier"===s.key.type&&("get"===s.key.name||"set"===s.key.name)?(a=i=!0,r=s.kind=s.key.name,s.key=ot(),bt!==ge&&q(),s.value=ht(L(),!1)):q(),"Identifier"===s.key.type&&(Tt||i))for(var o=0;oa?t.id:t.params[a];if((qe(o.name)||Ve(o.name))&&n(o.start,"Defining '"+o.name+"' in strict mode"),a>=0)for(var h=0;a>h;++h)o.name===t.params[h].name&&n(o.start,"Argument name clash in strict mode")}return N(t,e?"FunctionDeclaration":"FunctionExpression")}function ut(t,e,n){for(var i=[],r=!0;!j(t);){if(r)r=!1;else if(F(ve),e&&ct.allowTrailingCommas&&j(t))break;n&&bt===ve?i.push(null):i.push(J(!0))}return i}function lt(t){var e=L();return e.name=bt===jt?Ct:t&&!ct.forbidReserved&&bt.keyword||q(),St=!1,z(),N(e,"Identifier")}t.version="0.4.0";var ct,dt,ft,_t;t.parse=function(t,n){return dt=t+"",ft=dt.length,e(n),s(),Z(ct.program)};var gt=t.defaultOptions={ecmaVersion:5,strictSemicolons:!1,allowTrailingCommas:!0,forbidReserved:!1,locations:!1,onComment:null,ranges:!1,program:null,sourceFile:null},pt=t.getLineInfo=function(t,e){for(var n=1,i=0;;){Je.lastIndex=i;var r=Je.exec(t);if(!(r&&r.indext?36===t:91>t?!0:97>t?95===t:123>t?!0:t>=170&&Ge.test(String.fromCharCode(t))},Ke=t.isIdentifierChar=function(t){return 48>t?36===t:58>t?!0:65>t?!1:91>t?!0:97>t?95===t:123>t?!0:t>=170&&$e.test(String.fromCharCode(t))},Qe={kind:"loop"},tn={kind:"switch"}});var g={"+":"__add","-":"__subtract","*":"__multiply","/":"__divide","%":"__modulo","==":"equals","!=":"equals"},p={"-":"__negate","+":null},v=e.each(["add","subtract","multiply","divide","modulo","negate"],function(t){this["__"+t]="#"+t},{});return u.inject(v),d.inject(v),F.inject(v),"complete"===document.readyState?setTimeout(h):H.add(window,{load:h}),{compile:s,execute:a,load:l,parse:i}}.call(this),paper=new(r.inject(e.exports,{enumerable:!0,Base:e,Numerical:o,Key:J})),"function"==typeof define&&define.amd?define("paper",paper):"object"==typeof module&&module&&(module.exports=paper),paper}; \ No newline at end of file diff --git a/example/src/main/resources/logback.xml b/example/src/main/resources/logback.xml deleted file mode 100644 index 6a1caa1..0000000 --- a/example/src/main/resources/logback.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n - - - - - - - \ No newline at end of file diff --git a/example/src/main/scala/stormlantern/consul/example/Boot.scala b/example/src/main/scala/stormlantern/consul/example/Boot.scala deleted file mode 100644 index a11b2dc..0000000 --- a/example/src/main/scala/stormlantern/consul/example/Boot.scala +++ /dev/null @@ -1,47 +0,0 @@ -package stormlantern.consul.example - -import java.net.URL - -import akka.actor.ActorSystem -import akka.io.IO -import akka.pattern._ -import akka.util.Timeout -import spray.can.Http -import spray.json.{ JsString, JsObject } -import stormlantern.consul.client.discovery.{ ConnectionStrategy, ServiceDefinition, ConnectionProvider } -import stormlantern.consul.client.loadbalancers.RoundRobinLoadBalancer -import stormlantern.consul.client.ServiceBroker -import stormlantern.consul.client.DNS - -import scala.concurrent.Future -import scala.concurrent.duration._ - -object Boot extends App { - implicit val system = ActorSystem("reactive-consul") - implicit val executionContext = system.dispatcher - - val service = system.actorOf(ReactiveConsulHttpServiceActor.props(), "webservice") - - implicit val timeout = Timeout(5.seconds) - - IO(Http) ? Http.Bind(service, interface = "0.0.0.0", port = 8080) - - def connectionProviderFactory = (host: String, port: Int) => new ConnectionProvider { - val client = new SprayExampleServiceClient(new URL(s"http://$host:$port")) - override def getConnection: Future[Any] = Future.successful(client) - } - val connectionStrategy1 = ConnectionStrategy("example-service-1", connectionProviderFactory) - val connectionStrategy2 = ConnectionStrategy("example-service-2", connectionProviderFactory) - - val services = Set(connectionStrategy1, connectionStrategy2) - val serviceBroker = ServiceBroker(DNS.lookup("consul-8500.service.consul"), services) - - system.scheduler.schedule(5.seconds, 5.seconds) { - serviceBroker.withService("example-service-1") { client: SprayExampleServiceClient => - client.identify - }.foreach(println) - serviceBroker.withService("example-service-2") { client: SprayExampleServiceClient => - client.identify - }.foreach(println) - } -} diff --git a/example/src/main/scala/stormlantern/consul/example/ReactiveConsulHttpServiceActor.scala b/example/src/main/scala/stormlantern/consul/example/ReactiveConsulHttpServiceActor.scala deleted file mode 100644 index 760786d..0000000 --- a/example/src/main/scala/stormlantern/consul/example/ReactiveConsulHttpServiceActor.scala +++ /dev/null @@ -1,35 +0,0 @@ -package stormlantern.consul.example - -import akka.actor.{ Actor, Props } -import spray.routing.HttpService - -import scala.concurrent.ExecutionContext - -class ReactiveConsulHttpServiceActor extends Actor with ReactiveConsulHttpService { - - def actorRefFactory = context - - def receive = runRoute(reactiveConsulRoute) -} - -object ReactiveConsulHttpServiceActor { - def props() = Props(classOf[ReactiveConsulHttpServiceActor]) -} - -trait ReactiveConsulHttpService extends HttpService { - implicit def executionContext: ExecutionContext = actorRefFactory.dispatcher - - val reactiveConsulRoute = - pathPrefix("api") { - path("identify") { - get { - complete(s"Hi, I'm a ${System.getenv("SERVICE_NAME")} called ${System.getenv("INSTANCE_NAME")}") - } - } ~ - path("talk") { - get { - complete("pong") - } - } - } -} \ No newline at end of file diff --git a/example/src/main/scala/stormlantern/consul/example/SprayExampleServiceClient.scala b/example/src/main/scala/stormlantern/consul/example/SprayExampleServiceClient.scala deleted file mode 100644 index f811e26..0000000 --- a/example/src/main/scala/stormlantern/consul/example/SprayExampleServiceClient.scala +++ /dev/null @@ -1,22 +0,0 @@ -package stormlantern.consul.example - -import java.net.URL - -import akka.actor.ActorSystem -import spray.client.pipelining._ -import spray.http.{ HttpResponse, HttpRequest } - -import scala.concurrent.Future - -class SprayExampleServiceClient(host: URL)(implicit actorSystem: ActorSystem) { - - implicit val executionContext = actorSystem.dispatcher - - val pipeline: HttpRequest => Future[HttpResponse] = sendReceive - def stringFromResponse: HttpResponse => String = (response) => response.entity.asString - - def identify: Future[String] = { - val myPipeline: HttpRequest => Future[String] = pipeline ~> stringFromResponse - myPipeline(Get(s"$host/api/identify")) - } -} diff --git a/project/Config.scala b/project/Config.scala new file mode 100644 index 0000000..52dc824 --- /dev/null +++ b/project/Config.scala @@ -0,0 +1,25 @@ +import sbt.Keys._ +import sbt._ + +object Config { + val CustomIntegrationTest = config("it") extend Test + + private lazy val testAll = TaskKey[Unit]("tests") + + private lazy val unitSettings = Seq( + Test / fork := true, + Test / parallelExecution := false + ) + + private lazy val itSettings = + inConfig(CustomIntegrationTest)(Defaults.testSettings) ++ + Seq( + CustomIntegrationTest / fork := false, + CustomIntegrationTest / parallelExecution := false, + CustomIntegrationTest / scalaSource := baseDirectory.value / "src/it/scala" + ) ++ inConfig(IntegrationTest)(Defaults.testSettings) + + lazy val testSettings = itSettings ++ unitSettings ++ Seq( + testAll := (CustomIntegrationTest / test).dependsOn(Test / test).value + ) +} diff --git a/project/Dependencies.scala b/project/Dependencies.scala index 483c1d3..9cc127f 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -2,24 +2,8 @@ import sbt._ object Dependencies { - val resolutionRepos = Seq( - "spray repo" at "https://repo.spray.io", - "softprops-maven" at "https://dl.bintray.com/content/softprops/maven" - ) + val PekkoVersion = "1.0.0" + val PekkoHttpVersion = "1.0.0" - val akkaVersion = "2.5.30" - val akkaHttpVersion = "10.1.11" - - val akkaHttp = "com.typesafe.akka" %% "akka-http-core" % akkaHttpVersion - val akkaHttpSprayJson = "com.typesafe.akka" %% "akka-http-spray-json" % akkaHttpVersion - val akkaActor = "com.typesafe.akka" %% "akka-actor" % akkaVersion - val akkaStream = "com.typesafe.akka" %% "akka-stream" % akkaVersion - val akkaSlf4j = "com.typesafe.akka" %% "akka-slf4j" % akkaVersion - val slf4j = "org.slf4j" % "slf4j-api" % "1.7.30" - val logback = "ch.qos.logback" % "logback-classic" % "1.1.7" - val spotifyDocker = "com.spotify" % "docker-client" % "3.6.8" - val spotifyDns = "com.spotify" % "dns" % "3.2.2" - val scalaTest = "org.scalatest" %% "scalatest" % "3.1.1" - val scalaMock = "org.scalamock" %% "scalamock" % "4.4.0" - val akkaTestKit = "com.typesafe.akka" %% "akka-testkit" % akkaVersion + val testDependencies = Seq("org.scalatest" %% "scalatest" % "3.2.15", "org.scalamock" %% "scalamock" % "5.2.0") } diff --git a/project/build.properties b/project/build.properties index a919a9b..72413de 100644 --- a/project/build.properties +++ b/project/build.properties @@ -1 +1 @@ -sbt.version=1.3.8 +sbt.version=1.8.3 diff --git a/project/plugins.sbt b/project/plugins.sbt index f790a81..3196239 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -1,7 +1,6 @@ -addSbtPlugin("io.spray" % "sbt-revolver" % "0.9.1") -addSbtPlugin("org.scalariform" % "sbt-scalariform" % "1.8.2") -addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.3.6") -addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.1.2-1") -addSbtPlugin("com.timushev.sbt" % "sbt-updates" % "0.3.4") -addSbtPlugin("com.github.gseitz" % "sbt-release" % "1.0.9") -addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "2.3") \ No newline at end of file +addSbtPlugin("com.github.sbt" % "sbt-pgp" % "2.1.2") +addSbtPlugin("com.github.gseitz" % "sbt-release" % "1.0.13") +addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "3.9.4") +addSbtPlugin("com.lucidchart" % "sbt-scalafmt" % "1.16") +addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.5.1") +addSbtPlugin("com.codecommit" % "sbt-github-actions" % "0.14.2") \ No newline at end of file From b37668bc47f87a3eeb0f926af1a6d384ba3a5674 Mon Sep 17 00:00:00 2001 From: Leonard Wolters Date: Thu, 2 Nov 2023 16:32:32 +0100 Subject: [PATCH 20/33] Cleaning up project, upgrading project to latest standards --- .scalafmt.conf | 2 +- build.sbt | 20 +++-- .../client/ServiceBrokerIntegrationTest.scala | 15 ++-- .../consul/client/ServiceBroker.scala | 14 ++-- .../consul/client/ServiceBrokerActor.scala | 64 ++++++++------- .../client/dao/ConsulHttpProtocol.scala | 20 ++--- .../client/dao/IndexedServiceInstances.scala | 2 +- .../dao/akka/AkkaHttpConsulClient.scala | 80 +++++++++---------- .../client/discovery/ConnectionProvider.scala | 2 +- .../client/discovery/ConnectionStrategy.scala | 12 +-- .../discovery/ServiceAvailabilityActor.scala | 6 +- .../client/election/LeaderFollowerActor.scala | 12 +-- .../loadbalancers/LoadBalancerActor.scala | 30 ++++--- .../consul/client/session/SessionActor.scala | 8 +- .../consul/client/util/RetryPolicy.scala | 10 +-- .../client/ServiceBrokerActorSpec.scala | 18 ++--- .../consul/client/ServiceBrokerSpec.scala | 12 +-- .../ServiceAvailabilityActorSpec.scala | 6 +- .../election/LeaderFollowerActorSpec.scala | 12 +-- project/Dependencies.scala | 5 +- 20 files changed, 179 insertions(+), 171 deletions(-) diff --git a/.scalafmt.conf b/.scalafmt.conf index 46d822d..a8e32e9 100644 --- a/.scalafmt.conf +++ b/.scalafmt.conf @@ -10,7 +10,7 @@ align { {code = "%", owner = "Term.ApplyInfix"} {code = "%%", owner = "Term.ApplyInfix"} {code = "%%%", owner = "Term.ApplyInfix"} - {code = "⇒", owner = "Case"} + {code = "=>", owner = "Case"} {code = "<-", owner = "Enumerator.Generator"} {code = "←", owner = "Enumerator.Generator"} {code = "->", owner = "Term.ApplyInfix"} diff --git a/build.sbt b/build.sbt index f08476b..088308b 100644 --- a/build.sbt +++ b/build.sbt @@ -1,8 +1,8 @@ -import Dependencies._ + // Scala Formatting ThisBuild / scalafmtVersion := "1.5.1" -ThisBuild / scalafmtOnCompile := false // all projects +ThisBuild / scalafmtOnCompile := false // all projects ThisBuild / scalafmtTestOnCompile := false // all projects releaseCrossBuild := true @@ -66,10 +66,14 @@ lazy val client: Project = (project in file("client")) name := "client", sbtrelease.ReleasePlugin.autoImport.releasePublishArtifactsAction := PgpKeys.publishSigned.value, libraryDependencies ++= Seq( - "org.apache.pekko" %% "pekko-actor" % Dependencies.PekkoVersion, - "org.apache.pekko" %% "pekko-stream" % Dependencies.PekkoVersion, - "org.apache.pekko" %% "pekko-http" % Dependencies.PekkoHttpVersion, - "com.typesafe.scala-logging" %% "scala-logging" % "3.9.5", - "joda-time" % "joda-time" % "2.12.5" - ) ++ Seq("org.apache.pekko" %% "pekko-testkit" % Dependencies.PekkoVersion % Test) ++ Dependencies.testDependencies.map(_ % Test) + "ch.qos.logback" % "logback-classic" % "1.4.7", + "io.spray" %% "spray-json" % "1.3.6", + "org.apache.pekko" %% "pekko-actor" % Dependencies.PekkoVersion, + "org.apache.pekko" %% "pekko-stream" % Dependencies.PekkoVersion, + "org.apache.pekko" %% "pekko-http" % Dependencies.PekkoHttpVersion, + // test dependencies + "org.apache.pekko" %% "pekko-testkit" % Dependencies.PekkoVersion % Test, + "org.scalatest" %% "scalatest" % "3.2.15" % Test, + "org.scalamock" %% "scalamock" % "5.2.0" % Test + ) ) \ No newline at end of file diff --git a/client/src/it/scala/stormlantern/consul/client/ServiceBrokerIntegrationTest.scala b/client/src/it/scala/stormlantern/consul/client/ServiceBrokerIntegrationTest.scala index 1d624bc..00fc04d 100644 --- a/client/src/it/scala/stormlantern/consul/client/ServiceBrokerIntegrationTest.scala +++ b/client/src/it/scala/stormlantern/consul/client/ServiceBrokerIntegrationTest.scala @@ -1,15 +1,13 @@ package stormlantern.consul.client -import java.net.URL - -import org.scalatest._ -import org.scalatest.concurrent.{ Eventually, IntegrationPatience, ScalaFutures } -import stormlantern.consul.client.dao.org.apache.pekko.AkkaHttpConsulClient -import stormlantern.consul.client.dao.{ ConsulHttpClient, ServiceRegistration } -import stormlantern.consul.client.discovery.{ ConnectionProvider, ConnectionProviderFactory, ConnectionStrategy, ServiceDefinition } +import org.scalatest.concurrent.{Eventually, IntegrationPatience, ScalaFutures} +import stormlantern.consul.client.dao.akka.AkkaHttpConsulClient +import stormlantern.consul.client.dao.{ConsulHttpClient, ServiceRegistration} +import stormlantern.consul.client.discovery.{ConnectionProvider, ConnectionProviderFactory, ConnectionStrategy, ServiceDefinition} import stormlantern.consul.client.loadbalancers.RoundRobinLoadBalancer -import stormlantern.consul.client.util.{ ConsulDockerContainer, Logging, TestActorSystem } +import stormlantern.consul.client.util.{ConsulDockerContainer, Logging, TestActorSystem} +import java.net.URL import scala.concurrent.Future class ServiceBrokerIntegrationTest extends FlatSpec with Matchers with ScalaFutures with Eventually with IntegrationPatience with ConsulDockerContainer with TestActorSystem with Logging { @@ -26,6 +24,7 @@ class ServiceBrokerIntegrationTest extends FlatSpec with Matchers with ScalaFutu override def create(host: String, port: Int): ConnectionProvider = new ConnectionProvider { logger.info(s"Asked to create connection provider for $host:$port") val httpClient: ConsulHttpClient = new AkkaHttpConsulClient(new URL(s"http://$host:$port")) + override def getConnection: Future[Any] = Future.successful(httpClient) } } diff --git a/client/src/main/scala/stormlantern/consul/client/ServiceBroker.scala b/client/src/main/scala/stormlantern/consul/client/ServiceBroker.scala index dbd20ff..d8bac78 100644 --- a/client/src/main/scala/stormlantern/consul/client/ServiceBroker.scala +++ b/client/src/main/scala/stormlantern/consul/client/ServiceBroker.scala @@ -10,7 +10,7 @@ import org.apache.pekko.util.Timeout import org.apache.pekko.pattern.ask import stormlantern.consul.client.dao._ -import stormlantern.consul.client.dao.org.apache.pekko.AkkaHttpConsulClient +import stormlantern.consul.client.dao.akka.AkkaHttpConsulClient import stormlantern.consul.client.discovery._ import stormlantern.consul.client.election.LeaderInfo import stormlantern.consul.client.loadbalancers.LoadBalancerActor @@ -20,12 +20,12 @@ class ServiceBroker(serviceBrokerActor: ActorRef, consulClient: ConsulHttpClient private[this] implicit val timeout = Timeout(10.seconds) - def withService[A, B](name: String)(f: A ⇒ Future[B]): Future[B] = { + def withService[A, B](name: String)(f: A => Future[B]): Future[B] = { logger.info(s"Trying to get connection for service $name") - serviceBrokerActor.ask(ServiceBrokerActor.GetServiceConnection(name)).mapTo[ConnectionHolder].flatMap { connectionHolder ⇒ + serviceBrokerActor.ask(ServiceBrokerActor.GetServiceConnection(name)).mapTo[ConnectionHolder].flatMap { connectionHolder => logger.info(s"Received connectionholder $connectionHolder") try { - connectionHolder.connection.flatMap(c ⇒ f(c.asInstanceOf[A])) + connectionHolder.connection.flatMap(c => f(c.asInstanceOf[A])) } finally { connectionHolder.loadBalancer ! LoadBalancerActor.ReturnConnection(connectionHolder) } @@ -33,7 +33,7 @@ class ServiceBroker(serviceBrokerActor: ActorRef, consulClient: ConsulHttpClient } def registerService(registration: ServiceRegistration): Future[Unit] = { - consulClient.putService(registration).map { serviceId ⇒ + consulClient.putService(registration).map { serviceId => // Add shutdown hook val deregisterService = new Runnable { override def run(): Unit = consulClient.deleteService(serviceId) @@ -42,7 +42,7 @@ class ServiceBroker(serviceBrokerActor: ActorRef, consulClient: ConsulHttpClient } } - def withLeader[A](key: String)(f: Option[LeaderInfo] ⇒ Future[A]): Future[A] = { + def withLeader[A](key: String)(f: Option[LeaderInfo] => Future[A]): Future[A] = { ??? } @@ -55,7 +55,7 @@ object ServiceBroker { def apply(rootActor: ActorSystem, httpClient: ConsulHttpClient, services: Set[ConnectionStrategy]): ServiceBroker = { implicit val ec = ExecutionContext.Implicits.global - val serviceAvailabilityActorFactory = (factory: ActorRefFactory, service: ServiceDefinition, listener: ActorRef, onlyHealthyServices: Boolean) ⇒ + val serviceAvailabilityActorFactory = (factory: ActorRefFactory, service: ServiceDefinition, listener: ActorRef, onlyHealthyServices: Boolean) => factory.actorOf(ServiceAvailabilityActor.props(httpClient, service, listener, onlyHealthyServices)) val actorRef = rootActor.actorOf(ServiceBrokerActor.props(services, serviceAvailabilityActorFactory), "ServiceBroker") new ServiceBroker(actorRef, httpClient) diff --git a/client/src/main/scala/stormlantern/consul/client/ServiceBrokerActor.scala b/client/src/main/scala/stormlantern/consul/client/ServiceBrokerActor.scala index a9bcf93..148f1eb 100644 --- a/client/src/main/scala/stormlantern/consul/client/ServiceBrokerActor.scala +++ b/client/src/main/scala/stormlantern/consul/client/ServiceBrokerActor.scala @@ -1,28 +1,27 @@ package stormlantern.consul.client -import java.util.UUID - import org.apache.pekko.actor.Status.Failure import org.apache.pekko.actor._ import org.apache.pekko.util.Timeout +import stormlantern.consul.client.ServiceBrokerActor._ import stormlantern.consul.client.dao.ServiceInstance -import stormlantern.consul.client.discovery.{ ConnectionStrategy, ServiceAvailabilityActor, ServiceDefinition } +import stormlantern.consul.client.discovery.ServiceAvailabilityActor._ +import stormlantern.consul.client.discovery.{ConnectionStrategy, ServiceDefinition} import stormlantern.consul.client.loadbalancers.LoadBalancerActor -import stormlantern.consul.client.loadbalancers.LoadBalancerActor.{ GetConnection, HasAvailableConnectionProvider } -import ServiceAvailabilityActor._ -import stormlantern.consul.client.ServiceBrokerActor._ +import stormlantern.consul.client.loadbalancers.LoadBalancerActor.{GetConnection, HasAvailableConnectionProvider} +import java.util.UUID import scala.collection.mutable -import scala.concurrent.{ ExecutionContext, Future } import scala.concurrent.duration._ +import scala.concurrent.{ExecutionContext, Future} class ServiceBrokerActor( - services: Set[ConnectionStrategy], - serviceAvailabilityActorFactory: (ActorRefFactory, ServiceDefinition, ActorRef, Boolean) ⇒ ActorRef)(implicit ec: ExecutionContext) - extends Actor with ActorLogging with Stash { + services: Set[ConnectionStrategy], + serviceAvailabilityActorFactory: (ActorRefFactory, ServiceDefinition, ActorRef, Boolean) => ActorRef)(implicit ec: ExecutionContext) + extends Actor with ActorLogging with Stash { // Actor state - val indexedServices: Map[String, ConnectionStrategy] = services.map(s ⇒ (s.serviceDefinition.key, s)).toMap + val indexedServices: Map[String, ConnectionStrategy] = services.map(s => (s.serviceDefinition.key, s)).toMap val loadbalancers: mutable.Map[String, ActorRef] = mutable.Map.empty val serviceAvailability: mutable.Set[ActorRef] = mutable.Set.empty val sessionId: Option[UUID] = None @@ -30,7 +29,7 @@ class ServiceBrokerActor( override def preStart(): Unit = { indexedServices.foreach { - case (key, strategy) ⇒ + case (key, strategy) => loadbalancers.put(key, strategy.loadBalancerFactory(context)) log.info(s"Starting service availability Actor for $key") val serviceAvailabilityActorRef = serviceAvailabilityActorFactory(context, strategy.serviceDefinition, self, strategy.onlyHealthyServices) @@ -40,45 +39,43 @@ class ServiceBrokerActor( } def receive: Receive = { - case Started ⇒ + case Started => log.debug(s"Service availability initialized for ${sender()}") initializationCountdown -= 1 if (initializationCountdown == 0) { unstashAll() } - case ServiceAvailabilityUpdate(key, added, removed) ⇒ + case ServiceAvailabilityUpdate(key, added, removed) => log.debug(s"Adding connection providers for $key: $added") addConnectionProviders(key, added) log.debug(s"Removing conection providers for $key: $removed") removeConnectionProviders(key, removed) - case GetServiceConnection(key: String) ⇒ + case GetServiceConnection(key: String) => if (initializationCountdown != 0) { stash() } else { log.debug(s"Getting a service connection for $key") loadbalancers.get(key) match { - case Some(loadbalancer) ⇒ - loadbalancer forward GetConnection - case None ⇒ - sender ! Failure(ServiceUnavailableException(key)) + case Some(loadbalancer) => loadbalancer forward GetConnection + case None => sender() ! Failure(ServiceUnavailableException(key)) } } - case HasAvailableConnectionProviderFor(key: String) ⇒ + case HasAvailableConnectionProviderFor(key: String) => loadbalancers.get(key) match { - case Some(loadbalancer) ⇒ + case Some(loadbalancer) => loadbalancer forward HasAvailableConnectionProvider - case None ⇒ - sender ! false + case None => + sender() ! false } - case AllConnectionProvidersAvailable ⇒ + case AllConnectionProvidersAvailable => import org.apache.pekko.pattern.pipe - queryConnectionProviderAvailability pipeTo sender - case JoinElection(key) ⇒ + queryConnectionProviderAvailability pipeTo sender() + case JoinElection(key) => } // Internal methods def addConnectionProviders(key: String, added: Set[ServiceInstance]): Unit = { - added.foreach { s ⇒ + added.foreach { s => val host = if (s.serviceAddress.isEmpty) s.address else s.serviceAddress val connectionProvider = indexedServices(key).connectionProviderFactory.create(host, s.servicePort) loadbalancers(key) ! LoadBalancerActor.AddConnectionProvider(s.serviceId, connectionProvider) @@ -86,7 +83,7 @@ class ServiceBrokerActor( } def removeConnectionProviders(key: String, removed: Set[ServiceInstance]): Unit = { - removed.foreach { s ⇒ + removed.foreach { s => loadbalancers(key) ! LoadBalancerActor.RemoveConnectionProvider(s.serviceId) } } @@ -95,19 +92,24 @@ class ServiceBrokerActor( implicit val timeout: Timeout = 1.second import org.apache.pekko.pattern.ask Future.sequence(loadbalancers.values.map(_.ask(LoadBalancerActor.HasAvailableConnectionProvider).mapTo[Boolean])) - .map(_.forall(p ⇒ p)) + .map(_.forall(p => p)) } } object ServiceBrokerActor { // Constructors def props( - services: Set[ConnectionStrategy], - serviceAvailabilityActorFactory: (ActorRefFactory, ServiceDefinition, ActorRef, Boolean) ⇒ ActorRef)(implicit ec: ExecutionContext): Props = + services: Set[ConnectionStrategy], + serviceAvailabilityActorFactory: (ActorRefFactory, ServiceDefinition, ActorRef, Boolean) => ActorRef)(implicit ec: ExecutionContext): Props = Props(new ServiceBrokerActor(services, serviceAvailabilityActorFactory)) + case class GetServiceConnection(key: String) + case object Stop + case class HasAvailableConnectionProviderFor(key: String) + case object AllConnectionProvidersAvailable + case class JoinElection(key: String) } diff --git a/client/src/main/scala/stormlantern/consul/client/dao/ConsulHttpProtocol.scala b/client/src/main/scala/stormlantern/consul/client/dao/ConsulHttpProtocol.scala index 62673af..d8976d6 100644 --- a/client/src/main/scala/stormlantern/consul/client/dao/ConsulHttpProtocol.scala +++ b/client/src/main/scala/stormlantern/consul/client/dao/ConsulHttpProtocol.scala @@ -11,12 +11,12 @@ trait ConsulHttpProtocol extends DefaultJsonProtocol { implicit val uuidFormat = new JsonFormat[UUID] { override def read(json: JsValue): UUID = json match { - case JsString(uuid) ⇒ try { + case JsString(uuid) => try { UUID.fromString(uuid) } catch { - case NonFatal(e) ⇒ deserializationError("Expected UUID, but got " + uuid) + case NonFatal(e) => deserializationError("Expected UUID, but got " + uuid) } - case x ⇒ deserializationError("Expected UUID as JsString, but got " + x) + case x => deserializationError("Expected UUID as JsString, but got " + x) } override def write(obj: UUID): JsValue = JsString(obj.toString) @@ -24,19 +24,19 @@ trait ConsulHttpProtocol extends DefaultJsonProtocol { implicit val binaryDataFormat = new JsonFormat[BinaryData] { override def read(json: JsValue): BinaryData = json match { - case JsString(data) ⇒ try { + case JsString(data) => try { BinaryData(Base64.getMimeDecoder.decode(data)) } catch { - case NonFatal(e) ⇒ deserializationError("Expected base64 encoded binary data, but got " + data) + case NonFatal(e) => deserializationError("Expected base64 encoded binary data, but got " + data) } - case x ⇒ deserializationError("Expected base64 encoded binary data as JsString, but got " + x) + case x => deserializationError("Expected base64 encoded binary data as JsString, but got " + x) } override def write(obj: BinaryData): JsValue = JsString(Base64.getMimeEncoder.encodeToString(obj.data)) } implicit val serviceFormat: RootJsonFormat[ServiceInstance] = jsonFormat( - (node: String, address: String, serviceId: String, serviceName: String, serviceTags: Option[Set[String]], serviceAddress: String, servicePort: Int) ⇒ + (node: String, address: String, serviceId: String, serviceName: String, serviceTags: Option[Set[String]], serviceAddress: String, servicePort: Int) => ServiceInstance(node, address, serviceId, serviceName, serviceTags.getOrElse(Set.empty), serviceAddress, servicePort), "Node", "Address", "ServiceID", "ServiceName", "ServiceTags", "ServiceAddress", "ServicePort" ) @@ -51,9 +51,9 @@ trait ConsulHttpProtocol extends DefaultJsonProtocol { implicit val checkWriter = lift { new JsonWriter[HealthCheck] { override def write(obj: HealthCheck): JsValue = obj match { - case obj: ScriptHealthCheck ⇒ obj.toJson - case obj: HttpHealthCheck ⇒ obj.toJson - case obj: TTLHealthCheck ⇒ obj.toJson + case obj: ScriptHealthCheck => obj.toJson + case obj: HttpHealthCheck => obj.toJson + case obj: TTLHealthCheck => obj.toJson } } } diff --git a/client/src/main/scala/stormlantern/consul/client/dao/IndexedServiceInstances.scala b/client/src/main/scala/stormlantern/consul/client/dao/IndexedServiceInstances.scala index 8c55595..6edc3b8 100644 --- a/client/src/main/scala/stormlantern/consul/client/dao/IndexedServiceInstances.scala +++ b/client/src/main/scala/stormlantern/consul/client/dao/IndexedServiceInstances.scala @@ -98,7 +98,7 @@ case class HealthServiceInstance(node: Node, service: Service) { case class IndexedServiceInstances(index: Long, resource: Set[ServiceInstance]) extends Indexed[Set[ServiceInstance]] { def filterForTags(tags: Set[String]): IndexedServiceInstances = { - this.copy(resource = resource.filter { s ⇒ + this.copy(resource = resource.filter { s => tags.forall(s.serviceTags.contains) }) } diff --git a/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala b/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala index 031b261..03b4a4c 100644 --- a/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala +++ b/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala @@ -29,16 +29,16 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) extends // Services // ///////////////// def getService(service: String, tag: Option[String] = None, index: Option[Long] = None, wait: Option[String] = None, dataCenter: Option[String] = None): Future[IndexedServiceInstances] = { - val dcParameter = dataCenter.map(dc ⇒ s"dc=$dc") - val waitParameter = wait.map(w ⇒ s"wait=$w") - val indexParameter = index.map(i ⇒ s"index=$i") - val tagParameter = tag.map(t ⇒ s"tag=$t") + val dcParameter = dataCenter.map(dc => s"dc=$dc") + val waitParameter = wait.map(w => s"wait=$w") + val indexParameter = index.map(i => s"index=$i") + val tagParameter = tag.map(t => s"tag=$t") val parameters = Seq(dcParameter, tagParameter, waitParameter, indexParameter).flatten.mkString("&") val request: HttpRequest = HttpRequest(HttpMethods.GET).withUri(s"$host/v1/catalog/service/$service?$parameters") retry[IndexedServiceInstances]() { - getResponse(request, JsonMediaType).flatMap { response ⇒ - validIndex(response).map { idx ⇒ + getResponse(request, JsonMediaType).flatMap { response => + validIndex(response).map { idx => val services = response.body.parseJson.convertTo[Option[Set[ServiceInstance]]] IndexedServiceInstances(idx, services.getOrElse(Set.empty[ServiceInstance])) } @@ -47,17 +47,17 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) extends } def getServiceHealthAware(service: String, tag: Option[String] = None, index: Option[Long] = None, wait: Option[String] = None, dataCenter: Option[String] = None): Future[IndexedServiceInstances] = { - val dcParameter = dataCenter.map(dc ⇒ s"dc=$dc") - val waitParameter = wait.map(w ⇒ s"wait=$w") - val indexParameter = index.map(i ⇒ s"index=$i") - val tagParameter = tag.map(t ⇒ s"tag=$t") + val dcParameter = dataCenter.map(dc => s"dc=$dc") + val waitParameter = wait.map(w => s"wait=$w") + val indexParameter = index.map(i => s"index=$i") + val tagParameter = tag.map(t => s"tag=$t") val passingParameter = Some(s"passing=true") val parameters = Seq(dcParameter, tagParameter, waitParameter, indexParameter, passingParameter).flatten.mkString("&") val request: HttpRequest = HttpRequest(HttpMethods.GET).withUri(s"$host/v1/health/service/$service?$parameters") retry[IndexedServiceInstances]() { - getResponse(request, JsonMediaType).flatMap { response ⇒ - validIndex(response).map { idx ⇒ + getResponse(request, JsonMediaType).flatMap { response => + validIndex(response).map { idx => val services = response.body.parseJson.convertTo[Option[Set[HealthServiceInstance]]] IndexedServiceInstances(idx, services.getOrElse(Set.empty[HealthServiceInstance]).map(_.asServiceInstance)) } @@ -71,7 +71,7 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) extends retry[ConsulResponse]() { getResponse(request, TextMediaType) - }.map(r ⇒ registration.id.getOrElse(registration.name)) + }.map(r => registration.id.getOrElse(registration.name)) } def deleteService(serviceId: String): Future[Unit] = { @@ -79,35 +79,35 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) extends retry[ConsulResponse]() { getResponse(request, TextMediaType) - }.map(r ⇒ ()) + }.map(r => ()) } // // Sessions // ///////////////// def putSession(sessionCreation: Option[SessionCreation], dataCenter: Option[String]): Future[UUID] = { - val dcParameter = dataCenter.map(dc ⇒ s"dc=$dc") + val dcParameter = dataCenter.map(dc => s"dc=$dc") val parameters = Seq(dcParameter).flatten.mkString("&") val request = sessionCreation.map(_.toJson.asJsObject.toString.getBytes) match { - case None ⇒ HttpRequest(HttpMethods.PUT).withUri(s"$host/v1/session/create?$parameters") - case Some(entity) ⇒ HttpRequest(HttpMethods.PUT).withUri(s"$host/v1/session/create?$parameters").withEntity(entity) + case None => HttpRequest(HttpMethods.PUT).withUri(s"$host/v1/session/create?$parameters") + case Some(entity) => HttpRequest(HttpMethods.PUT).withUri(s"$host/v1/session/create?$parameters").withEntity(entity) } retry[UUID]() { - getResponse(request, JsonMediaType).map { response ⇒ + getResponse(request, JsonMediaType).map { response => response.body.parseJson.asJsObject.fields("ID").convertTo[UUID] } } } def getSessionInfo(sessionId: UUID, index: Option[Long], dataCenter: Option[String]): Future[Option[SessionInfo]] = { - val dcParameter = dataCenter.map(dc ⇒ s"dc=$dc") - val indexParameter = index.map(i ⇒ s"index=$i") + val dcParameter = dataCenter.map(dc => s"dc=$dc") + val indexParameter = index.map(i => s"index=$i") val parameters = Seq(dcParameter, indexParameter).flatten.mkString("&") val request = HttpRequest(HttpMethods.GET).withUri(s"$host/v1/session/info/$sessionId?$parameters") retry[Option[SessionInfo]]() { - getResponse(request, JsonMediaType).map { response ⇒ + getResponse(request, JsonMediaType).map { response => response.body.parseJson.convertTo[Option[Set[SessionInfo]]].getOrElse(Set.empty).headOption } } @@ -120,8 +120,8 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) extends import StatusCodes._ val opParameter = sessionOp.map { - case AcquireSession(id) ⇒ s"acquire=$id" - case ReleaseSession(id) ⇒ s"release=$id" + case AcquireSession(id) => s"acquire=$id" + case ReleaseSession(id) => s"release=$id" } val parameters = opParameter.getOrElse("") val request = HttpRequest(HttpMethods.PUT).withUri(s"$host/v1/kv/$key?$parameters").withEntity(value) @@ -130,24 +130,24 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) extends retry[Boolean]() { getResponse(request, JsonMediaType, validator).flatMap { - case ConsulResponse(OK, _, body) ⇒ Future successful Option(body.toBoolean).getOrElse(false) - case ConsulResponse(InternalServerError, _, "Invalid session") ⇒ Future successful false - case ConsulResponse(status, _, body) ⇒ Future failed new Exception(s"Request returned status code $status - $body") + case ConsulResponse(OK, _, body) => Future successful Option(body.toBoolean).getOrElse(false) + case ConsulResponse(InternalServerError, _, "Invalid session") => Future successful false + case ConsulResponse(status, _, body) => Future failed new Exception(s"Request returned status code $status - $body") } } } def getKeyValuePair(key: String, index: Option[Long], wait: Option[String], recurse: Boolean, keysOnly: Boolean): Future[Seq[KeyData]] = { - val waitParameter = wait.map(p ⇒ s"wait=$p") - val indexParameter = index.map(p ⇒ s"index=$p") + val waitParameter = wait.map(p => s"wait=$p") + val indexParameter = index.map(p => s"index=$p") val recurseParameter = if (recurse) Some("recurse") else None val keysOnlyParameter = if (keysOnly) Some("keys") else None val parameters = Seq(indexParameter, waitParameter, recurseParameter, keysOnlyParameter).flatten.mkString("&") val request = HttpRequest(HttpMethods.GET).withUri(s"$host/v1/kv/$key?$parameters") retry[Seq[KeyData]]() { - getResponse(request, JsonMediaType, _ ⇒ true).map { response ⇒ + getResponse(request, JsonMediaType, _ => true).map { response => if (response.status == StatusCodes.NotFound) { Seq.empty } else { @@ -160,12 +160,12 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) extends // // Internal Helpers // ////////////////////////// - private def getResponse[T, U](request: HttpRequest, expectedMediaType: MediaType, validator: HttpResponse ⇒ Boolean = (in) ⇒ in.status.isSuccess()): Future[ConsulResponse] = { + private def getResponse[T, U](request: HttpRequest, expectedMediaType: MediaType, validator: HttpResponse => Boolean = (in) => in.status.isSuccess()): Future[ConsulResponse] = { def validStatus(response: HttpResponse) = if (validator(response)) { Future successful response } else { - parseBody(response).flatMap { body ⇒ Future failed ConsulException(s"Bad status code: ${response.status.intValue()} with body $body") } + parseBody(response).flatMap { body => Future failed ConsulException(s"Bad status code: ${response.status.intValue()} with body $body") } } // @@ -174,9 +174,9 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) extends // ///////////////////// def validContenType(resp: HttpResponse) = { val expected = resp.status match { - case st if st.isSuccess() ⇒ expectedMediaType - case st if st.isFailure() ⇒ TextMediaType - case st if st.isRedirection() ⇒ TextMediaType // this is a guess + case st if st.isSuccess() => expectedMediaType + case st if st.isFailure() => TextMediaType + case st if st.isRedirection() => TextMediaType // this is a guess } if (resp.entity.contentType.mediaType == expected) { @@ -195,18 +195,18 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) extends .singleRequest(request) .flatMap(validStatus) .flatMap(validContenType) - .flatMap { response: HttpResponse ⇒ - parseBody(response).map { body: String ⇒ + .flatMap { response: HttpResponse => + parseBody(response).map { body: String => ConsulResponse(response.status, response.headers, body) } } } private def validIndex(response: ConsulResponse): Future[Long] = response.headers.find(_.name() == "X-Consul-Index") match { - case None ⇒ Future failed ConsulException("X-Consul-Index header not found") - case Some(hdr) ⇒ Try(hdr.value.toLong) match { - case Success(idx) ⇒ Future successful idx - case Failure(ex) ⇒ Future failed ConsulException("X-Consul-Index header was not numeric") + case None => Future failed ConsulException("X-Consul-Index header not found") + case Some(hdr) => Try(hdr.value.toLong) match { + case Success(idx) => Future successful idx + case Failure(ex) => Future failed ConsulException("X-Consul-Index header was not numeric") } } } diff --git a/client/src/main/scala/stormlantern/consul/client/discovery/ConnectionProvider.scala b/client/src/main/scala/stormlantern/consul/client/discovery/ConnectionProvider.scala index 64d8730..9927ea1 100644 --- a/client/src/main/scala/stormlantern/consul/client/discovery/ConnectionProvider.scala +++ b/client/src/main/scala/stormlantern/consul/client/discovery/ConnectionProvider.scala @@ -9,7 +9,7 @@ trait ConnectionProvider { def getConnection: Future[Any] def returnConnection(connectionHolder: ConnectionHolder): Unit = () def destroy(): Unit = () - def getConnectionHolder(i: String, lb: ActorRef): Future[ConnectionHolder] = getConnection.map { connection ⇒ + def getConnectionHolder(i: String, lb: ActorRef): Future[ConnectionHolder] = getConnection.map { connection => new ConnectionHolder { override def connection: Future[Any] = getConnection override val loadBalancer: ActorRef = lb diff --git a/client/src/main/scala/stormlantern/consul/client/discovery/ConnectionStrategy.scala b/client/src/main/scala/stormlantern/consul/client/discovery/ConnectionStrategy.scala index 626afcc..330442d 100644 --- a/client/src/main/scala/stormlantern/consul/client/discovery/ConnectionStrategy.scala +++ b/client/src/main/scala/stormlantern/consul/client/discovery/ConnectionStrategy.scala @@ -19,27 +19,27 @@ object ServiceDefinition { case class ConnectionStrategy( serviceDefinition: ServiceDefinition, connectionProviderFactory: ConnectionProviderFactory, - loadBalancerFactory: ActorRefFactory ⇒ ActorRef, + loadBalancerFactory: ActorRefFactory => ActorRef, onlyHealthyServices: Boolean ) object ConnectionStrategy { def apply(serviceDefinition: ServiceDefinition, connectionProviderFactory: ConnectionProviderFactory, loadBalancer: LoadBalancer, onlyHealthyServices: Boolean): ConnectionStrategy = - ConnectionStrategy(serviceDefinition, connectionProviderFactory, ctx ⇒ ctx.actorOf(LoadBalancerActor.props(loadBalancer, serviceDefinition.key)), onlyHealthyServices) + ConnectionStrategy(serviceDefinition, connectionProviderFactory, ctx => ctx.actorOf(LoadBalancerActor.props(loadBalancer, serviceDefinition.key)), onlyHealthyServices) - def apply(serviceDefinition: ServiceDefinition, connectionProviderFactory: (String, Int) ⇒ ConnectionProvider, loadBalancer: LoadBalancer, onlyHealthyServices: Boolean): ConnectionStrategy = { + def apply(serviceDefinition: ServiceDefinition, connectionProviderFactory: (String, Int) => ConnectionProvider, loadBalancer: LoadBalancer, onlyHealthyServices: Boolean): ConnectionStrategy = { val cpf = new ConnectionProviderFactory { override def create(host: String, port: Int): ConnectionProvider = connectionProviderFactory(host, port) } - ConnectionStrategy(serviceDefinition, cpf, ctx ⇒ ctx.actorOf(LoadBalancerActor.props(loadBalancer, serviceDefinition.key)), onlyHealthyServices) + ConnectionStrategy(serviceDefinition, cpf, ctx => ctx.actorOf(LoadBalancerActor.props(loadBalancer, serviceDefinition.key)), onlyHealthyServices) } - def apply(serviceName: String, connectionProviderFactory: (String, Int) ⇒ ConnectionProvider, loadBalancer: LoadBalancer): ConnectionStrategy = { + def apply(serviceName: String, connectionProviderFactory: (String, Int) => ConnectionProvider, loadBalancer: LoadBalancer): ConnectionStrategy = { ConnectionStrategy(ServiceDefinition(serviceName), connectionProviderFactory, loadBalancer, onlyHealthyServices = false) } - def apply(serviceName: String, connectionProviderFactory: (String, Int) ⇒ ConnectionProvider): ConnectionStrategy = { + def apply(serviceName: String, connectionProviderFactory: (String, Int) => ConnectionProvider): ConnectionStrategy = { ConnectionStrategy(serviceName, connectionProviderFactory, new RoundRobinLoadBalancer) } diff --git a/client/src/main/scala/stormlantern/consul/client/discovery/ServiceAvailabilityActor.scala b/client/src/main/scala/stormlantern/consul/client/discovery/ServiceAvailabilityActor.scala index c0d74ac..a8438bf 100644 --- a/client/src/main/scala/stormlantern/consul/client/discovery/ServiceAvailabilityActor.scala +++ b/client/src/main/scala/stormlantern/consul/client/discovery/ServiceAvailabilityActor.scala @@ -16,16 +16,16 @@ class ServiceAvailabilityActor(httpClient: ConsulHttpClient, serviceDefinition: var serviceAvailabilityState: IndexedServiceInstances = IndexedServiceInstances.empty def receive: Receive = { - case Start ⇒ + case Start => self ! UpdateServiceAvailability(None) - case UpdateServiceAvailability(services: Option[IndexedServiceInstances]) ⇒ + case UpdateServiceAvailability(services: Option[IndexedServiceInstances]) => val (update, serviceChange) = updateServiceAvailability(services.getOrElse(IndexedServiceInstances.empty)) update.foreach(listener ! _) if (!initialized && services.isDefined) { initialized = true listener ! Started } - serviceChange.map(changes ⇒ UpdateServiceAvailability(Some(changes))) pipeTo self + serviceChange.map(changes => UpdateServiceAvailability(Some(changes))) pipeTo self } def updateServiceAvailability(services: IndexedServiceInstances): (Option[ServiceAvailabilityUpdate], Future[IndexedServiceInstances]) = { diff --git a/client/src/main/scala/stormlantern/consul/client/election/LeaderFollowerActor.scala b/client/src/main/scala/stormlantern/consul/client/election/LeaderFollowerActor.scala index ff43016..792a8b1 100644 --- a/client/src/main/scala/stormlantern/consul/client/election/LeaderFollowerActor.scala +++ b/client/src/main/scala/stormlantern/consul/client/election/LeaderFollowerActor.scala @@ -19,19 +19,19 @@ class LeaderFollowerActor(httpClient: ConsulHttpClient, sessionId: UUID, key: St // Behavior def receive = { - case Participate ⇒ + case Participate => httpClient.putKeyValuePair(key, leaderInfoBytes, Some(AcquireSession(sessionId))).map { - case true ⇒ + case true => self ! SetElectionState(Some(Leader)) self ! MonitorLock(0) - case false ⇒ + case false => self ! MonitorLock(0) } - case SetElectionState(state) ⇒ + case SetElectionState(state) => electionState = state - case MonitorLock(index) ⇒ + case MonitorLock(index) => httpClient.getKeyValuePair(key, index = Some(index), wait = Some("1s")).map { - case Seq(KeyData(_, _, newIndex, _, _, BinaryData(data), session)) ⇒ + case Seq(KeyData(_, _, newIndex, _, _, BinaryData(data), session)) => if (newIndex > index) { if (session.isEmpty) { self ! SetElectionState(None) diff --git a/client/src/main/scala/stormlantern/consul/client/loadbalancers/LoadBalancerActor.scala b/client/src/main/scala/stormlantern/consul/client/loadbalancers/LoadBalancerActor.scala index a79555b..0595e62 100644 --- a/client/src/main/scala/stormlantern/consul/client/loadbalancers/LoadBalancerActor.scala +++ b/client/src/main/scala/stormlantern/consul/client/loadbalancers/LoadBalancerActor.scala @@ -1,12 +1,13 @@ package stormlantern.consul.client.loadbalancers import org.apache.pekko.actor.Status.Failure -import org.apache.pekko.actor.{ Props, Actor, ActorLogging } -import LoadBalancerActor._ -import stormlantern.consul.client.discovery.{ ConnectionProvider, ConnectionHolder } +import org.apache.pekko.actor.{Actor, ActorLogging, Props} import stormlantern.consul.client.ServiceUnavailableException -import scala.concurrent.ExecutionContext.Implicits.global +import stormlantern.consul.client.discovery.{ConnectionHolder, ConnectionProvider} +import stormlantern.consul.client.loadbalancers.LoadBalancerActor._ + import scala.collection.mutable +import scala.concurrent.ExecutionContext.Implicits.global class LoadBalancerActor(loadBalancer: LoadBalancer, key: String) extends Actor with ActorLogging { @@ -22,19 +23,19 @@ class LoadBalancerActor(loadBalancer: LoadBalancer, key: String) extends Actor w def receive: PartialFunction[Any, Unit] = { - case GetConnection ⇒ + case GetConnection => selectConnection match { - case Some((id, connectionProvider)) ⇒ connectionProvider.getConnectionHolder(id, self) pipeTo sender - case None ⇒ sender ! Failure(ServiceUnavailableException(key)) + case Some((id, connectionProvider)) => connectionProvider.getConnectionHolder(id, self) pipeTo sender() + case None => sender() ! Failure(ServiceUnavailableException(key)) } - case ReturnConnection(connection) ⇒ returnConnection(connection) - case AddConnectionProvider(id, provider) ⇒ addConnectionProvider(id, provider) - case RemoveConnectionProvider(id) ⇒ removeConnectionProvider(id) - case HasAvailableConnectionProvider ⇒ sender ! connectionProviders.nonEmpty + case ReturnConnection(connection) => returnConnection(connection) + case AddConnectionProvider(id, provider) => addConnectionProvider(id, provider) + case RemoveConnectionProvider(id) => removeConnectionProvider(id) + case HasAvailableConnectionProvider => sender() ! connectionProviders.nonEmpty } def selectConnection: Option[(String, ConnectionProvider)] = - loadBalancer.selectConnection.flatMap(id ⇒ connectionProviders.get(id).map(id -> _)) + loadBalancer.selectConnection.flatMap(id => connectionProviders.get(id).map(id -> _)) def returnConnection(connection: ConnectionHolder): Unit = { connectionProviders.get(connection.id).foreach(_.returnConnection(connection)) @@ -55,10 +56,15 @@ class LoadBalancerActor(loadBalancer: LoadBalancer, key: String) extends Actor w object LoadBalancerActor { // Props def props(loadBalancer: LoadBalancer, key: String) = Props(new LoadBalancerActor(loadBalancer, key)) + // Messsages case object GetConnection + case class ReturnConnection(connection: ConnectionHolder) + case class AddConnectionProvider(id: String, provider: ConnectionProvider) + case class RemoveConnectionProvider(id: String) + case object HasAvailableConnectionProvider } diff --git a/client/src/main/scala/stormlantern/consul/client/session/SessionActor.scala b/client/src/main/scala/stormlantern/consul/client/session/SessionActor.scala index 13d15b9..c19a960 100644 --- a/client/src/main/scala/stormlantern/consul/client/session/SessionActor.scala +++ b/client/src/main/scala/stormlantern/consul/client/session/SessionActor.scala @@ -16,20 +16,20 @@ class SessionActor(httpClient: ConsulHttpClient, listener: ActorRef) extends Act var sessionId: Option[UUID] = None def receive = { - case StartSession ⇒ startSession().map { id ⇒ + case StartSession => startSession().map { id => self ! SessionAcquired(id) } - case SessionAcquired(id) ⇒ + case SessionAcquired(id) => sessionId = Some(id) listener ! SessionAcquired(id) self ! MonitorSession(0) - case MonitorSession(lastIndex) ⇒ + case MonitorSession(lastIndex) => } // Internal methods def startSession(): Future[UUID] = { - httpClient.putSession().map { id ⇒ + httpClient.putSession().map { id => sessionId = Some(id) id } diff --git a/client/src/main/scala/stormlantern/consul/client/util/RetryPolicy.scala b/client/src/main/scala/stormlantern/consul/client/util/RetryPolicy.scala index edf19b6..190aa51 100644 --- a/client/src/main/scala/stormlantern/consul/client/util/RetryPolicy.scala +++ b/client/src/main/scala/stormlantern/consul/client/util/RetryPolicy.scala @@ -13,11 +13,11 @@ trait RetryPolicy { delay: FiniteDuration = 500.milli, retries: Int = 4, backoff: Int = 2, - predicate: T ⇒ Boolean = (r: T) ⇒ true - )(f: ⇒ Future[T])(implicit ec: ExecutionContext, s: Scheduler): Future[T] = { + predicate: T => Boolean = (r: T) => true + )(f: => Future[T])(implicit ec: ExecutionContext, s: Scheduler): Future[T] = { f.map { - case r if !predicate(r) ⇒ throw new IllegalStateException("Result does not satisfy the predicate specified") - case r ⇒ r - } recoverWith { case _ if retries > 0 ⇒ after(delay, s)(retry(delay * backoff, retries - 1, backoff, predicate)(f)) } + case r if !predicate(r) => throw new IllegalStateException("Result does not satisfy the predicate specified") + case r => r + } recoverWith { case _ if retries > 0 => after(delay, s)(retry(delay * backoff, retries - 1, backoff, predicate)(f)) } } } diff --git a/client/src/test/scala/stormlantern/consul/client/ServiceBrokerActorSpec.scala b/client/src/test/scala/stormlantern/consul/client/ServiceBrokerActorSpec.scala index 08e7840..d2d6981 100644 --- a/client/src/test/scala/stormlantern/consul/client/ServiceBrokerActorSpec.scala +++ b/client/src/test/scala/stormlantern/consul/client/ServiceBrokerActorSpec.scala @@ -24,8 +24,8 @@ class ServiceBrokerActorSpec(_system: ActorSystem) extends TestKit(_system) with trait TestScope { val httpClient: ConsulHttpClient = mock[ConsulHttpClient] - val serviceAvailabilityActorFactory: (ActorRefFactory, ServiceDefinition, ActorRef, Boolean) ⇒ ActorRef = - mock[(ActorRefFactory, ServiceDefinition, ActorRef, Boolean) ⇒ ActorRef] + val serviceAvailabilityActorFactory: (ActorRefFactory, ServiceDefinition, ActorRef, Boolean) => ActorRef = + mock[(ActorRefFactory, ServiceDefinition, ActorRef, Boolean) => ActorRef] val connectionProviderFactory: ConnectionProviderFactory = mock[ConnectionProviderFactory] val connectionProvider: ConnectionProvider = mock[ConnectionProvider] val connectionHolder: ConnectionHolder = mock[ConnectionHolder] @@ -33,8 +33,8 @@ class ServiceBrokerActorSpec(_system: ActorSystem) extends TestKit(_system) with val service2 = ServiceDefinition("service2Key", "service2") val loadBalancerProbeForService1 = TestProbe("LoadBalancerActorForService1") val loadBalancerProbeForService2 = TestProbe("LoadBalancerActorForService2") - val connectionStrategyForService1 = ConnectionStrategy(service1, connectionProviderFactory, ctx ⇒ loadBalancerProbeForService1.ref, onlyHealthyServices = true) - val connectionStrategyForService2 = ConnectionStrategy(service2, connectionProviderFactory, ctx ⇒ loadBalancerProbeForService2.ref, onlyHealthyServices = false) + val connectionStrategyForService1 = ConnectionStrategy(service1, connectionProviderFactory, ctx => loadBalancerProbeForService1.ref, onlyHealthyServices = true) + val connectionStrategyForService2 = ConnectionStrategy(service2, connectionProviderFactory, ctx => loadBalancerProbeForService2.ref, onlyHealthyServices = false) } "The ServiceBrokerActor" should "create a child actor per service" in new TestScope { @@ -113,7 +113,7 @@ class ServiceBrokerActorSpec(_system: ActorSystem) extends TestKit(_system) with serviceAvailabilityProbe.expectMsg(Start) sut ! ServiceBrokerActor.HasAvailableConnectionProviderFor(service1.key) loadBalancerProbeForService1.expectMsgPF() { - case LoadBalancerActor.HasAvailableConnectionProvider ⇒ loadBalancerProbeForService1.sender() ! true + case LoadBalancerActor.HasAvailableConnectionProvider => loadBalancerProbeForService1.sender() ! true } expectMsg(true) sut.stop() @@ -130,10 +130,10 @@ class ServiceBrokerActorSpec(_system: ActorSystem) extends TestKit(_system) with service2AvailabilityProbe.expectMsg(Start) sut ! ServiceBrokerActor.AllConnectionProvidersAvailable loadBalancerProbeForService1.expectMsgPF() { - case LoadBalancerActor.HasAvailableConnectionProvider ⇒ loadBalancerProbeForService1.sender() ! true + case LoadBalancerActor.HasAvailableConnectionProvider => loadBalancerProbeForService1.sender() ! true } loadBalancerProbeForService2.expectMsgPF() { - case LoadBalancerActor.HasAvailableConnectionProvider ⇒ loadBalancerProbeForService2.sender() ! false + case LoadBalancerActor.HasAvailableConnectionProvider => loadBalancerProbeForService2.sender() ! false } expectMsg(false) sut.stop() @@ -150,10 +150,10 @@ class ServiceBrokerActorSpec(_system: ActorSystem) extends TestKit(_system) with service2AvailabilityProbe.expectMsg(Start) sut ! ServiceBrokerActor.AllConnectionProvidersAvailable loadBalancerProbeForService1.expectMsgPF() { - case LoadBalancerActor.HasAvailableConnectionProvider ⇒ loadBalancerProbeForService1.sender() ! true + case LoadBalancerActor.HasAvailableConnectionProvider => loadBalancerProbeForService1.sender() ! true } loadBalancerProbeForService2.expectMsgPF() { - case LoadBalancerActor.HasAvailableConnectionProvider ⇒ loadBalancerProbeForService2.sender() ! true + case LoadBalancerActor.HasAvailableConnectionProvider => loadBalancerProbeForService2.sender() ! true } expectMsg(true) sut.stop() diff --git a/client/src/test/scala/stormlantern/consul/client/ServiceBrokerSpec.scala b/client/src/test/scala/stormlantern/consul/client/ServiceBrokerSpec.scala index fd816f8..231d9e2 100644 --- a/client/src/test/scala/stormlantern/consul/client/ServiceBrokerSpec.scala +++ b/client/src/test/scala/stormlantern/consul/client/ServiceBrokerSpec.scala @@ -34,11 +34,11 @@ class ServiceBrokerSpec(_system: ActorSystem) extends TestKit(_system) with Impl (connectionHolder.connection _).expects().returns(Future.successful(true)) (connectionHolder.loadBalancer _).expects().returns(loadBalancer) val sut = new ServiceBroker(self, httpClient) - val result: Future[Boolean] = sut.withService("service1") { service: Boolean ⇒ + val result: Future[Boolean] = sut.withService("service1") { service: Boolean => Future.successful(service) } expectMsgPF() { - case ServiceBrokerActor.GetServiceConnection("service1") ⇒ + case ServiceBrokerActor.GetServiceConnection("service1") => lastSender ! connectionHolder result.map(_ shouldEqual true).futureValue } @@ -49,11 +49,11 @@ class ServiceBrokerSpec(_system: ActorSystem) extends TestKit(_system) with Impl (connectionHolder.connection _).expects().returns(Future.successful(true)) (connectionHolder.loadBalancer _).expects().returns(loadBalancer) val sut = new ServiceBroker(self, httpClient) - val result: Future[Boolean] = sut.withService[Boolean, Boolean]("service1") { service: Boolean ⇒ + val result: Future[Boolean] = sut.withService[Boolean, Boolean]("service1") { service: Boolean => throw new RuntimeException() } expectMsgPF() { - case ServiceBrokerActor.GetServiceConnection("service1") ⇒ + case ServiceBrokerActor.GetServiceConnection("service1") => lastSender ! connectionHolder an[RuntimeException] should be thrownBy result.futureValue } @@ -62,11 +62,11 @@ class ServiceBrokerSpec(_system: ActorSystem) extends TestKit(_system) with Impl it should "throw an error when an excpetion is returned" in new TestScope { val sut = new ServiceBroker(self, httpClient) - val result: Future[Boolean] = sut.withService("service1") { service: Boolean ⇒ + val result: Future[Boolean] = sut.withService("service1") { service: Boolean => Future.successful(service) } expectMsgPF() { - case ServiceBrokerActor.GetServiceConnection("service1") ⇒ + case ServiceBrokerActor.GetServiceConnection("service1") => lastSender ! Failure(new RuntimeException()) an[RuntimeException] should be thrownBy result.futureValue } diff --git a/client/src/test/scala/stormlantern/consul/client/discovery/ServiceAvailabilityActorSpec.scala b/client/src/test/scala/stormlantern/consul/client/discovery/ServiceAvailabilityActorSpec.scala index bf56281..926c1fc 100644 --- a/client/src/test/scala/stormlantern/consul/client/discovery/ServiceAvailabilityActorSpec.scala +++ b/client/src/test/scala/stormlantern/consul/client/discovery/ServiceAvailabilityActorSpec.scala @@ -25,7 +25,7 @@ class ServiceAvailabilityActorSpec(_system: ActorSystem) extends TestKit(_system val httpClient: ConsulHttpClient = mock[ConsulHttpClient] val sut = TestActorRef(ServiceAvailabilityActor.props(httpClient, ServiceDefinition("bogus123", "bogus"), self, onlyHealthyServices = false)) (httpClient.getService _).expects("bogus", None, Some(0L), Some("1s"), None).returns(Future.successful(IndexedServiceInstances(1, Set.empty))) - (httpClient.getService _).expects("bogus", None, Some(1L), Some("1s"), None).onCall { p ⇒ + (httpClient.getService _).expects("bogus", None, Some(1L), Some("1s"), None).onCall { p => sut.stop() Future.successful(IndexedServiceInstances(1, Set.empty)) } @@ -41,7 +41,7 @@ class ServiceAvailabilityActorSpec(_system: ActorSystem) extends TestKit(_system val service = ModelHelpers.createService("bogus123", "bogus") (httpClient.getService _).expects("bogus", None, Some(0L), Some("1s"), None).returns(Future.successful(IndexedServiceInstances(1, Set.empty))) (httpClient.getService _).expects("bogus", None, Some(1L), Some("1s"), None).returns(Future.successful(IndexedServiceInstances(2, Set(service)))) - (httpClient.getService _).expects("bogus", None, Some(2L), Some("1s"), None).onCall { p ⇒ + (httpClient.getService _).expects("bogus", None, Some(2L), Some("1s"), None).onCall { p => sut.stop() Future.successful(IndexedServiceInstances(2, Set(service))) } @@ -59,7 +59,7 @@ class ServiceAvailabilityActorSpec(_system: ActorSystem) extends TestKit(_system val matchingService = nonMatchingservice.copy(serviceTags = Set("one", "two")) (httpClient.getService _).expects("bogus", Some("one"), Some(0L), Some("1s"), None).returns(Future.successful(IndexedServiceInstances(1, Set.empty))) (httpClient.getService _).expects("bogus", Some("one"), Some(1L), Some("1s"), None).returns(Future.successful(IndexedServiceInstances(2, Set(nonMatchingservice, matchingService)))) - (httpClient.getService _).expects("bogus", Some("one"), Some(2L), Some("1s"), None).onCall { p ⇒ + (httpClient.getService _).expects("bogus", Some("one"), Some(2L), Some("1s"), None).onCall { p => sut.stop() Future.successful(IndexedServiceInstances(2, Set(nonMatchingservice, matchingService))) } diff --git a/client/src/test/scala/stormlantern/consul/client/election/LeaderFollowerActorSpec.scala b/client/src/test/scala/stormlantern/consul/client/election/LeaderFollowerActorSpec.scala index 038b5f1..3b57f33 100644 --- a/client/src/test/scala/stormlantern/consul/client/election/LeaderFollowerActorSpec.scala +++ b/client/src/test/scala/stormlantern/consul/client/election/LeaderFollowerActorSpec.scala @@ -32,7 +32,7 @@ class LeaderFollowerActorSpec(_system: ActorSystem) extends TestKit(_system) wit "The LeaderFollowerActor" should "participate in an election, win, watch for changes and participate again when session is lost" in new TestScope { val sut = TestActorRef(LeaderFollowerActor.props(consulHttpClient, sessionId, key, host, port)) - (consulHttpClient.putKeyValuePair _).expects(where { (k, lib, op) ⇒ + (consulHttpClient.putKeyValuePair _).expects(where { (k, lib, op) => k == key && util.Arrays.equals(lib, leaderInfoBytes) && op.contains(AcquireSession(sessionId)) }).returns(Future.successful(true)) (consulHttpClient.getKeyValuePair _).expects(key, Some(0L), Some("1s"), false, false).returns { @@ -41,9 +41,9 @@ class LeaderFollowerActorSpec(_system: ActorSystem) extends TestKit(_system) wit (consulHttpClient.getKeyValuePair _).expects(key, Some(1L), Some("1s"), false, false).returns { Future.successful(Seq(KeyData(key, 1, 2, 1, 0, BinaryData(leaderInfoBytes), None))) } - (consulHttpClient.putKeyValuePair _).expects(where { (k, lib, op) ⇒ + (consulHttpClient.putKeyValuePair _).expects(where { (k, lib, op) => k == key && util.Arrays.equals(lib, leaderInfoBytes) && op.contains(AcquireSession(sessionId)) - }).onCall { p ⇒ + }).onCall { p => sut.stop() Future.successful(false) } @@ -53,7 +53,7 @@ class LeaderFollowerActorSpec(_system: ActorSystem) extends TestKit(_system) wit it should "participate in an election, lose, watch for changes and participate again when session is lost" in new TestScope { val otherSessionId: UUID = UUID.fromString("9A3BB9C-E2E7-43DF-BFD5-845417146553") val sut = TestActorRef(LeaderFollowerActor.props(consulHttpClient, sessionId, key, host, port)) - (consulHttpClient.putKeyValuePair _).expects(where { (k, lib, op) ⇒ + (consulHttpClient.putKeyValuePair _).expects(where { (k, lib, op) => k == key && util.Arrays.equals(lib, leaderInfoBytes) && op.contains(AcquireSession(sessionId)) }).returns(Future.successful(false)) (consulHttpClient.getKeyValuePair _).expects(key, Some(0L), Some("1s"), false, false).returns { @@ -62,9 +62,9 @@ class LeaderFollowerActorSpec(_system: ActorSystem) extends TestKit(_system) wit (consulHttpClient.getKeyValuePair _).expects(key, Some(1L), Some("1s"), false, false).returns { Future.successful(Seq(KeyData(key, 1, 2, 1, 0, BinaryData(leaderInfoBytes), None))) } - (consulHttpClient.putKeyValuePair _).expects(where { (k, lib, op) ⇒ + (consulHttpClient.putKeyValuePair _).expects(where { (k, lib, op) => k == key && util.Arrays.equals(lib, leaderInfoBytes) && op.contains(AcquireSession(sessionId)) - }).onCall { p ⇒ + }).onCall { p => sut.stop() Future.successful(true) } diff --git a/project/Dependencies.scala b/project/Dependencies.scala index 9cc127f..b14ac54 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -1,9 +1,6 @@ -import sbt._ -object Dependencies { +object Dependencies { val PekkoVersion = "1.0.0" val PekkoHttpVersion = "1.0.0" - - val testDependencies = Seq("org.scalatest" %% "scalatest" % "3.2.15", "org.scalamock" %% "scalamock" % "5.2.0") } From a3854a2487a7e51532b699ee8f560c64b8718388 Mon Sep 17 00:00:00 2001 From: Leonard Wolters Date: Thu, 2 Nov 2023 16:39:22 +0100 Subject: [PATCH 21/33] Cleaning up project, upgrading project to latest standards --- client/src/it/resources/application.conf | 2 +- .../consul/client/ClientITSpec.scala | 48 +++++++++++++++++++ .../client/ServiceBrokerIntegrationTest.scala | 6 ++- .../AkkaHttpConsulClientIntegrationTest.scala | 2 +- .../consul/client/util/TestActorSystem.scala | 14 ------ client/src/main/resources/reference.conf | 2 +- client/src/test/resources/application.conf | 2 +- client/src/test/resources/logback-test.xml | 14 +++--- 8 files changed, 63 insertions(+), 27 deletions(-) create mode 100644 client/src/it/scala/stormlantern/consul/client/ClientITSpec.scala delete mode 100644 client/src/it/scala/stormlantern/consul/client/util/TestActorSystem.scala diff --git a/client/src/it/resources/application.conf b/client/src/it/resources/application.conf index bd255d6..21fa2a1 100644 --- a/client/src/it/resources/application.conf +++ b/client/src/it/resources/application.conf @@ -1,4 +1,4 @@ -akka { +pekko { loggers = ["org.apache.pekko.event.slf4j.Slf4jLogger"] loglevel = "INFO" logging-filter = "org.apache.pekko.event.slf4j.Slf4jLoggingFilter" diff --git a/client/src/it/scala/stormlantern/consul/client/ClientITSpec.scala b/client/src/it/scala/stormlantern/consul/client/ClientITSpec.scala new file mode 100644 index 0000000..b082a5d --- /dev/null +++ b/client/src/it/scala/stormlantern/consul/client/ClientITSpec.scala @@ -0,0 +1,48 @@ +package stormlantern.consul.client + +import com.typesafe.config.{Config, ConfigFactory} +import org.apache.pekko.actor.ActorSystem +import org.apache.pekko.testkit.TestKit +import org.scalatest.concurrent.{Eventually, IntegrationPatience, ScalaFutures} +import org.scalatest.flatspec.AnyFlatSpecLike +import org.scalatest.matchers.should.Matchers +import stormlantern.consul.client.dao.akka.AkkaHttpConsulClient +import stormlantern.consul.client.dao.{ConsulHttpClient, ServiceRegistration} +import stormlantern.consul.client.discovery.{ConnectionProvider, ConnectionProviderFactory, ConnectionStrategy, ServiceDefinition} +import stormlantern.consul.client.loadbalancers.RoundRobinLoadBalancer +import stormlantern.consul.client.util.{ConsulDockerContainer, Logging} + +import java.net.URL +import scala.concurrent.Future + +abstract class ClientITSpec(val config: Config = ConfigFactory.load()) + extends TestKit(ActorSystem("TestSystem", config.getConfig("crobox.clickhouse.client"))) + with AnyFlatSpecLike + with Matchers + with ScalaFutures with Eventually with IntegrationPatience with ConsulDockerContainer with Logging { + + import scala.concurrent.ExecutionContext.Implicits.global + + "The ServiceBroker" should "provide a usable connection to consul" in withConsulHost { (host, port) => + val akkaHttpClient = new AkkaHttpConsulClient(new URL(s"http://$host:$port")) + // Register the HTTP interface + akkaHttpClient.putService(ServiceRegistration("consul-http", Some("consul-http-1"), address = Some(host), port = Some(port))) + akkaHttpClient.putService(ServiceRegistration("consul-http", Some("consul-http-2"), address = Some(host), port = Some(port))) + val connectionProviderFactory = new ConnectionProviderFactory { + override def create(host: String, port: Int): ConnectionProvider = new ConnectionProvider { + logger.info(s"Asked to create connection provider for $host:$port") + val httpClient: ConsulHttpClient = new AkkaHttpConsulClient(new URL(s"http://$host:$port")) + + override def getConnection: Future[Any] = Future.successful(httpClient) + } + } + val connectionStrategy = ConnectionStrategy(ServiceDefinition("consul-http"), connectionProviderFactory, new RoundRobinLoadBalancer, onlyHealthyServices = true) + val sut = ServiceBroker(system, akkaHttpClient, Set(connectionStrategy)) + eventually { + sut.withService("consul-http") { connection: ConsulHttpClient => + connection.getService("bogus").map(_.resource should have size 0) + } + sut + } + } +} diff --git a/client/src/it/scala/stormlantern/consul/client/ServiceBrokerIntegrationTest.scala b/client/src/it/scala/stormlantern/consul/client/ServiceBrokerIntegrationTest.scala index 00fc04d..0253030 100644 --- a/client/src/it/scala/stormlantern/consul/client/ServiceBrokerIntegrationTest.scala +++ b/client/src/it/scala/stormlantern/consul/client/ServiceBrokerIntegrationTest.scala @@ -1,16 +1,18 @@ package stormlantern.consul.client import org.scalatest.concurrent.{Eventually, IntegrationPatience, ScalaFutures} +import org.scalatest.flatspec.AnyFlatSpecLike +import org.scalatest.matchers.should.Matchers import stormlantern.consul.client.dao.akka.AkkaHttpConsulClient import stormlantern.consul.client.dao.{ConsulHttpClient, ServiceRegistration} import stormlantern.consul.client.discovery.{ConnectionProvider, ConnectionProviderFactory, ConnectionStrategy, ServiceDefinition} import stormlantern.consul.client.loadbalancers.RoundRobinLoadBalancer -import stormlantern.consul.client.util.{ConsulDockerContainer, Logging, TestActorSystem} +import stormlantern.consul.client.util.{ConsulDockerContainer, Logging} import java.net.URL import scala.concurrent.Future -class ServiceBrokerIntegrationTest extends FlatSpec with Matchers with ScalaFutures with Eventually with IntegrationPatience with ConsulDockerContainer with TestActorSystem with Logging { +class ServiceBrokerIntegrationTest extends AnyFlatSpecLike with Matchers with ScalaFutures with Eventually with IntegrationPatience with ConsulDockerContainer with TestActorSystem with Logging { import scala.concurrent.ExecutionContext.Implicits.global diff --git a/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIntegrationTest.scala b/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIntegrationTest.scala index c33c84a..5d692fc 100644 --- a/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIntegrationTest.scala +++ b/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIntegrationTest.scala @@ -6,7 +6,7 @@ import java.util.UUID import org.scalatest._ import org.scalatest.concurrent.{ Eventually, IntegrationPatience, ScalaFutures } import stormlantern.consul.client.dao.org.apache.pekko.AkkaHttpConsulClient -import stormlantern.consul.client.util.{ ConsulDockerContainer, Logging, RetryPolicy, TestActorSystem } +import stormlantern.consul.client.util.{ ConsulDockerContainer, Logging, RetryPolicy } class AkkaHttpConsulClientIntegrationTest extends FlatSpec with Matchers with ScalaFutures with Eventually with IntegrationPatience with ConsulDockerContainer with TestActorSystem with RetryPolicy with Logging { diff --git a/client/src/it/scala/stormlantern/consul/client/util/TestActorSystem.scala b/client/src/it/scala/stormlantern/consul/client/util/TestActorSystem.scala deleted file mode 100644 index 239af0f..0000000 --- a/client/src/it/scala/stormlantern/consul/client/util/TestActorSystem.scala +++ /dev/null @@ -1,14 +0,0 @@ -package stormlantern.consul.client.util - -import org.apache.pekko.actor.ActorSystem - -trait TestActorSystem { - def withActorSystem[T](f: ActorSystem => T): T = { - val actorSystem = ActorSystem("test") - try { - f(actorSystem) - } finally { - actorSystem.terminate() - } - } -} diff --git a/client/src/main/resources/reference.conf b/client/src/main/resources/reference.conf index 28dcbae..4469aa2 100644 --- a/client/src/main/resources/reference.conf +++ b/client/src/main/resources/reference.conf @@ -1,4 +1,4 @@ -akka { +pekko { loggers = ["org.apache.pekko.event.slf4j.Slf4jLogger"] loglevel = "DEBUG" logging-filter = "org.apache.pekko.event.slf4j.Slf4jLoggingFilter" diff --git a/client/src/test/resources/application.conf b/client/src/test/resources/application.conf index 2726de1..6a4a17a 100644 --- a/client/src/test/resources/application.conf +++ b/client/src/test/resources/application.conf @@ -1,4 +1,4 @@ -akka { +pekko { loggers = ["org.apache.pekko.event.slf4j.Slf4jLogger"] loglevel = "INFO" logger-startup-timeout = 30s diff --git a/client/src/test/resources/logback-test.xml b/client/src/test/resources/logback-test.xml index 6a1caa1..a5e424a 100644 --- a/client/src/test/resources/logback-test.xml +++ b/client/src/test/resources/logback-test.xml @@ -1,14 +1,14 @@ - + + - - %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + %d{ISO8601} %-5level [%logger] - %m%n - - - + + + + \ No newline at end of file From 86ac1db1e94945089842cd30d5d7e24943d8c6ee Mon Sep 17 00:00:00 2001 From: Leonard Wolters Date: Thu, 2 Nov 2023 16:46:21 +0100 Subject: [PATCH 22/33] Cleaning code --- .../consul/client/ClientITSpec.scala | 39 +++------------ .../consul/client/ServiceBrokerIT.scala | 50 +++++++++++++++++++ .../client/ServiceBrokerIntegrationTest.scala | 43 ---------------- ...est.scala => AkkaHttpConsulClientIT.scala} | 11 ++-- .../client/util/ConsulDockerContainer.scala | 17 ------- .../ConsulRegistratorDockerContainer.scala | 31 ------------ .../stormlantern/consul/client/Dns.scala | 13 ----- docker-compose.yml | 9 ++++ .../dockertestkit/DockerClientProvider.scala | 25 ---------- .../dockertestkit/DockerContainer.scala | 17 ------- .../dockertestkit/DockerContainers.scala | 31 ------------ .../dockertestkit/client/Container.scala | 39 --------------- .../orchestration/Orchestration.scala | 5 -- 13 files changed, 70 insertions(+), 260 deletions(-) create mode 100644 client/src/it/scala/stormlantern/consul/client/ServiceBrokerIT.scala delete mode 100644 client/src/it/scala/stormlantern/consul/client/ServiceBrokerIntegrationTest.scala rename client/src/it/scala/stormlantern/consul/client/dao/{AkkaHttpConsulClientIntegrationTest.scala => AkkaHttpConsulClientIT.scala} (93%) delete mode 100644 client/src/it/scala/stormlantern/consul/client/util/ConsulDockerContainer.scala delete mode 100644 client/src/it/scala/stormlantern/consul/client/util/ConsulRegistratorDockerContainer.scala delete mode 100644 dns-helper/src/main/scala/stormlantern/consul/client/Dns.scala create mode 100644 docker-compose.yml delete mode 100644 docker-testkit/src/main/scala/stormlantern/dockertestkit/DockerClientProvider.scala delete mode 100644 docker-testkit/src/main/scala/stormlantern/dockertestkit/DockerContainer.scala delete mode 100644 docker-testkit/src/main/scala/stormlantern/dockertestkit/DockerContainers.scala delete mode 100644 docker-testkit/src/main/scala/stormlantern/dockertestkit/client/Container.scala delete mode 100644 docker-testkit/src/main/scala/stormlantern/dockertestkit/orchestration/Orchestration.scala diff --git a/client/src/it/scala/stormlantern/consul/client/ClientITSpec.scala b/client/src/it/scala/stormlantern/consul/client/ClientITSpec.scala index b082a5d..899da3c 100644 --- a/client/src/it/scala/stormlantern/consul/client/ClientITSpec.scala +++ b/client/src/it/scala/stormlantern/consul/client/ClientITSpec.scala @@ -6,43 +6,16 @@ import org.apache.pekko.testkit.TestKit import org.scalatest.concurrent.{Eventually, IntegrationPatience, ScalaFutures} import org.scalatest.flatspec.AnyFlatSpecLike import org.scalatest.matchers.should.Matchers -import stormlantern.consul.client.dao.akka.AkkaHttpConsulClient -import stormlantern.consul.client.dao.{ConsulHttpClient, ServiceRegistration} -import stormlantern.consul.client.discovery.{ConnectionProvider, ConnectionProviderFactory, ConnectionStrategy, ServiceDefinition} -import stormlantern.consul.client.loadbalancers.RoundRobinLoadBalancer -import stormlantern.consul.client.util.{ConsulDockerContainer, Logging} -import java.net.URL -import scala.concurrent.Future +import scala.concurrent.ExecutionContext abstract class ClientITSpec(val config: Config = ConfigFactory.load()) - extends TestKit(ActorSystem("TestSystem", config.getConfig("crobox.clickhouse.client"))) + extends TestKit(ActorSystem("TestSystem", config.getConfig("crobox.clickhouse.client"))) with AnyFlatSpecLike with Matchers - with ScalaFutures with Eventually with IntegrationPatience with ConsulDockerContainer with Logging { + with ScalaFutures + with Eventually + with IntegrationPatience { - import scala.concurrent.ExecutionContext.Implicits.global - - "The ServiceBroker" should "provide a usable connection to consul" in withConsulHost { (host, port) => - val akkaHttpClient = new AkkaHttpConsulClient(new URL(s"http://$host:$port")) - // Register the HTTP interface - akkaHttpClient.putService(ServiceRegistration("consul-http", Some("consul-http-1"), address = Some(host), port = Some(port))) - akkaHttpClient.putService(ServiceRegistration("consul-http", Some("consul-http-2"), address = Some(host), port = Some(port))) - val connectionProviderFactory = new ConnectionProviderFactory { - override def create(host: String, port: Int): ConnectionProvider = new ConnectionProvider { - logger.info(s"Asked to create connection provider for $host:$port") - val httpClient: ConsulHttpClient = new AkkaHttpConsulClient(new URL(s"http://$host:$port")) - - override def getConnection: Future[Any] = Future.successful(httpClient) - } - } - val connectionStrategy = ConnectionStrategy(ServiceDefinition("consul-http"), connectionProviderFactory, new RoundRobinLoadBalancer, onlyHealthyServices = true) - val sut = ServiceBroker(system, akkaHttpClient, Set(connectionStrategy)) - eventually { - sut.withService("consul-http") { connection: ConsulHttpClient => - connection.getService("bogus").map(_.resource should have size 0) - } - sut - } - } + implicit val ec: ExecutionContext = scala.concurrent.ExecutionContext.Implicits.global } diff --git a/client/src/it/scala/stormlantern/consul/client/ServiceBrokerIT.scala b/client/src/it/scala/stormlantern/consul/client/ServiceBrokerIT.scala new file mode 100644 index 0000000..7e5c40b --- /dev/null +++ b/client/src/it/scala/stormlantern/consul/client/ServiceBrokerIT.scala @@ -0,0 +1,50 @@ +package stormlantern.consul.client + +import stormlantern.consul.client.dao.akka.AkkaHttpConsulClient +import stormlantern.consul.client.dao.{ConsulHttpClient, ServiceRegistration} +import stormlantern.consul.client.discovery.{ + ConnectionProvider, + ConnectionProviderFactory, + ConnectionStrategy, + ServiceDefinition +} +import stormlantern.consul.client.loadbalancers.RoundRobinLoadBalancer +import stormlantern.consul.client.util.Logging + +import java.net.URL +import scala.concurrent.Future + +class ServiceBrokerIT extends ClientITSpec with Logging { + + "The ServiceBroker" should "provide a usable connection to consul" in withConsulHost { (host, port) => + val akkaHttpClient = new AkkaHttpConsulClient(new URL(s"http://$host:$port")) + + // Register the HTTP interface + akkaHttpClient.putService( + ServiceRegistration("consul-http", Some("consul-http-1"), address = Some(host), port = Some(port)) + ) + akkaHttpClient.putService( + ServiceRegistration("consul-http", Some("consul-http-2"), address = Some(host), port = Some(port)) + ) + + val connectionProviderFactory = new ConnectionProviderFactory { + override def create(host: String, port: Int): ConnectionProvider = new ConnectionProvider { + logger.info(s"Asked to create connection provider for $host:$port") + val httpClient: ConsulHttpClient = new AkkaHttpConsulClient(new URL(s"http://$host:$port")) + + override def getConnection: Future[Any] = Future.successful(httpClient) + } + } + val connectionStrategy = ConnectionStrategy(ServiceDefinition("consul-http"), + connectionProviderFactory, + new RoundRobinLoadBalancer, + onlyHealthyServices = true) + val sut = ServiceBroker(system, akkaHttpClient, Set(connectionStrategy)) + eventually { + sut.withService("consul-http") { connection: ConsulHttpClient => + connection.getService("bogus").map(_.resource should have size 0) + } + sut + } + } +} diff --git a/client/src/it/scala/stormlantern/consul/client/ServiceBrokerIntegrationTest.scala b/client/src/it/scala/stormlantern/consul/client/ServiceBrokerIntegrationTest.scala deleted file mode 100644 index 0253030..0000000 --- a/client/src/it/scala/stormlantern/consul/client/ServiceBrokerIntegrationTest.scala +++ /dev/null @@ -1,43 +0,0 @@ -package stormlantern.consul.client - -import org.scalatest.concurrent.{Eventually, IntegrationPatience, ScalaFutures} -import org.scalatest.flatspec.AnyFlatSpecLike -import org.scalatest.matchers.should.Matchers -import stormlantern.consul.client.dao.akka.AkkaHttpConsulClient -import stormlantern.consul.client.dao.{ConsulHttpClient, ServiceRegistration} -import stormlantern.consul.client.discovery.{ConnectionProvider, ConnectionProviderFactory, ConnectionStrategy, ServiceDefinition} -import stormlantern.consul.client.loadbalancers.RoundRobinLoadBalancer -import stormlantern.consul.client.util.{ConsulDockerContainer, Logging} - -import java.net.URL -import scala.concurrent.Future - -class ServiceBrokerIntegrationTest extends AnyFlatSpecLike with Matchers with ScalaFutures with Eventually with IntegrationPatience with ConsulDockerContainer with TestActorSystem with Logging { - - import scala.concurrent.ExecutionContext.Implicits.global - - "The ServiceBroker" should "provide a usable connection to consul" in withConsulHost { (host, port) => - withActorSystem { implicit actorSystem => - val akkaHttpClient = new AkkaHttpConsulClient(new URL(s"http://$host:$port")) - // Register the HTTP interface - akkaHttpClient.putService(ServiceRegistration("consul-http", Some("consul-http-1"), address = Some(host), port = Some(port))) - akkaHttpClient.putService(ServiceRegistration("consul-http", Some("consul-http-2"), address = Some(host), port = Some(port))) - val connectionProviderFactory = new ConnectionProviderFactory { - override def create(host: String, port: Int): ConnectionProvider = new ConnectionProvider { - logger.info(s"Asked to create connection provider for $host:$port") - val httpClient: ConsulHttpClient = new AkkaHttpConsulClient(new URL(s"http://$host:$port")) - - override def getConnection: Future[Any] = Future.successful(httpClient) - } - } - val connectionStrategy = ConnectionStrategy(ServiceDefinition("consul-http"), connectionProviderFactory, new RoundRobinLoadBalancer, onlyHealthyServices = true) - val sut = ServiceBroker(actorSystem, akkaHttpClient, Set(connectionStrategy)) - eventually { - sut.withService("consul-http") { connection: ConsulHttpClient => - connection.getService("bogus").map(_.resource should have size 0) - } - sut - } - } - } -} diff --git a/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIntegrationTest.scala b/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIT.scala similarity index 93% rename from client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIntegrationTest.scala rename to client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIT.scala index 5d692fc..14522b2 100644 --- a/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIntegrationTest.scala +++ b/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIT.scala @@ -2,21 +2,20 @@ package stormlantern.consul.client.dao import java.net.URL import java.util.UUID - import org.scalatest._ -import org.scalatest.concurrent.{ Eventually, IntegrationPatience, ScalaFutures } +import org.scalatest.concurrent.{Eventually, IntegrationPatience, ScalaFutures} +import stormlantern.consul.client.ClientITSpec +import stormlantern.consul.client.dao.akka.AkkaHttpConsulClient import stormlantern.consul.client.dao.org.apache.pekko.AkkaHttpConsulClient -import stormlantern.consul.client.util.{ ConsulDockerContainer, Logging, RetryPolicy } +import stormlantern.consul.client.util.{Logging, RetryPolicy} -class AkkaHttpConsulClientIntegrationTest extends FlatSpec with Matchers with ScalaFutures with Eventually with IntegrationPatience with ConsulDockerContainer with TestActorSystem with RetryPolicy with Logging { +class AkkaHttpConsulClientIT extends ClientITSpec with RetryPolicy with Logging { import scala.concurrent.ExecutionContext.Implicits.global def withConsulHttpClient[T](f: ConsulHttpClient => T): T = withConsulHost { (host, port) => - withActorSystem { implicit actorSystem => val subject: ConsulHttpClient = new AkkaHttpConsulClient(new URL(s"http://$host:$port")) f(subject) - } } "The AkkaHttpConsulClient" should "retrieve a single Consul service from a freshly started Consul instance" in withConsulHttpClient { subject => diff --git a/client/src/it/scala/stormlantern/consul/client/util/ConsulDockerContainer.scala b/client/src/it/scala/stormlantern/consul/client/util/ConsulDockerContainer.scala deleted file mode 100644 index 9bf9c42..0000000 --- a/client/src/it/scala/stormlantern/consul/client/util/ConsulDockerContainer.scala +++ /dev/null @@ -1,17 +0,0 @@ -package stormlantern.consul.client -package util - -import com.spotify.docker.client.messages.ContainerConfig -import org.scalatest.Suite -import stormlantern.dockertestkit.{ DockerClientProvider, DockerContainer } - -import scala.collection.JavaConversions._ - -trait ConsulDockerContainer extends DockerContainer { this: Suite => - - def image: String = "progrium/consul" - def command: Seq[String] = Seq("-server", "-bootstrap", DockerClientProvider.hostname) - override def containerConfig = ContainerConfig.builder().image(image).hostConfig(hostConfig).cmd(command).build() - - def withConsulHost[T](f: (String, Int) => T): T = super.withDockerHost("8500/tcp")(f) -} diff --git a/client/src/it/scala/stormlantern/consul/client/util/ConsulRegistratorDockerContainer.scala b/client/src/it/scala/stormlantern/consul/client/util/ConsulRegistratorDockerContainer.scala deleted file mode 100644 index 95221ca..0000000 --- a/client/src/it/scala/stormlantern/consul/client/util/ConsulRegistratorDockerContainer.scala +++ /dev/null @@ -1,31 +0,0 @@ -package stormlantern.consul.client.util - -import com.spotify.docker.client.messages.ContainerConfig -import org.scalatest.Suite -import stormlantern.dockertestkit.{ DockerClientProvider, DockerContainers } - -import scala.collection.JavaConversions._ - -trait ConsulRegistratorDockerContainer extends DockerContainers { this: Suite => - - def consulContainerConfig = { - val image: String = "progrium/consul" - val command: Seq[String] = Seq("-server", "-bootstrap", "-advertise", DockerClientProvider.hostname) - ContainerConfig.builder().image(image).cmd(command).build() - } - - def registratorContainerConfig = { - val hostname = DockerClientProvider.hostname - val image: String = "progrium/registrator" - val command: String = s"consul://$hostname:8500" - val volume: String = "/var/run/docker.sock:/tmp/docker.sock" - ContainerConfig.builder().image(image).cmd(command).hostname(hostname).volumes(volume).build() - } - - override def containerConfigs = Set(consulContainerConfig, registratorContainerConfig) - - def withConsulHost[T](f: (String, Int) => T): T = super.withDockerHosts(Set("8500/tcp")) { pb => - val (h, p) = pb("8500/tcp") - f(h, p) - } -} \ No newline at end of file diff --git a/dns-helper/src/main/scala/stormlantern/consul/client/Dns.scala b/dns-helper/src/main/scala/stormlantern/consul/client/Dns.scala deleted file mode 100644 index 10da1c5..0000000 --- a/dns-helper/src/main/scala/stormlantern/consul/client/Dns.scala +++ /dev/null @@ -1,13 +0,0 @@ -package stormlantern.consul.client - -import java.net.URL -import com.spotify.dns.DnsSrvResolvers -import collection.JavaConverters._ - -object DNS { - def lookup(consulAddress: String): URL = { - val resolver = DnsSrvResolvers.newBuilder().build() - val lookupResult = resolver.resolve(consulAddress).asScala.headOption.getOrElse(throw new RuntimeException(s"No record found for ${consulAddress}")) - new URL(s"http://${lookupResult.host()}:${lookupResult.port()}") - } -} \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..ef779a4 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,9 @@ +version: '3' +services: + consul: + image: consul:1.5.3 + expose: + - "8500" + ports: + - "${CI_JOB-8500:}8500" + command: "agent -dev -client 0.0.0.0" \ No newline at end of file diff --git a/docker-testkit/src/main/scala/stormlantern/dockertestkit/DockerClientProvider.scala b/docker-testkit/src/main/scala/stormlantern/dockertestkit/DockerClientProvider.scala deleted file mode 100644 index 0837790..0000000 --- a/docker-testkit/src/main/scala/stormlantern/dockertestkit/DockerClientProvider.scala +++ /dev/null @@ -1,25 +0,0 @@ -package stormlantern.dockertestkit - -import java.net.URI - -import com.spotify.docker.client.DockerClient.ListContainersParam -import com.spotify.docker.client.{ DefaultDockerClient, DockerClient } - -import scala.collection.JavaConverters._ - -object DockerClientProvider { - - lazy val client: DockerClient = DefaultDockerClient.fromEnv().build() - - lazy val hostname: String = { - val uri = new URI(sys.env.getOrElse("DOCKER_HOST", "unix:///var/run/docker.sock")) - uri.getScheme match { - case "tcp" => uri.getHost - case "unix" => "localhost" - } - } - - def cleanUp(): Unit = { - client.listContainers(ListContainersParam.allContainers()).asScala.foreach(c => client.removeContainer(c.id())) - } -} diff --git a/docker-testkit/src/main/scala/stormlantern/dockertestkit/DockerContainer.scala b/docker-testkit/src/main/scala/stormlantern/dockertestkit/DockerContainer.scala deleted file mode 100644 index b53ae75..0000000 --- a/docker-testkit/src/main/scala/stormlantern/dockertestkit/DockerContainer.scala +++ /dev/null @@ -1,17 +0,0 @@ -package stormlantern.dockertestkit - -import com.spotify.docker.client.messages.ContainerConfig -import org.scalatest.Suite - -trait DockerContainer extends DockerContainers { this: Suite => - - def containerConfig: ContainerConfig - override def containerConfigs: Set[ContainerConfig] = Set(containerConfig) - - def withDockerHost[T](port: String)(f: (String, Int) => T): T = withDockerHosts(Set(port)) { hosts => - val (h, p) = hosts(port) - f(h, p) - } - -} - diff --git a/docker-testkit/src/main/scala/stormlantern/dockertestkit/DockerContainers.scala b/docker-testkit/src/main/scala/stormlantern/dockertestkit/DockerContainers.scala deleted file mode 100644 index 7881263..0000000 --- a/docker-testkit/src/main/scala/stormlantern/dockertestkit/DockerContainers.scala +++ /dev/null @@ -1,31 +0,0 @@ -package stormlantern.dockertestkit - -import com.spotify.docker.client.messages.{ ContainerConfig, HostConfig } -import org.scalatest.{ BeforeAndAfterAll, Suite } -import stormlantern.dockertestkit.client.Container - -trait DockerContainers extends BeforeAndAfterAll { this: Suite => - - def containerConfigs: Set[ContainerConfig] - val hostConfig = HostConfig.builder() - .publishAllPorts(true) - .networkMode("bridge") - .build() - val containers = containerConfigs.map(new Container(_)) - - def withDockerHosts[T](ports: Set[String])(f: Map[String, (String, Int)] => T): T = { - // Find the mapped available ports in the network settings - f(ports.zip(ports.flatMap(p => containers.map(c => c.mappedPort(p).headOption))).map { - case (port, Some(binding)) => port -> (DockerClientProvider.hostname, binding.hostPort().toInt) - case (port, None) => throw new IndexOutOfBoundsException(s"Cannot find mapped port $port") - }.toMap) - } - - override def beforeAll(): Unit = containers.foreach(_.start()) - - override def afterAll(): Unit = containers.foreach { container => - container.stop() - container.remove() - } -} - diff --git a/docker-testkit/src/main/scala/stormlantern/dockertestkit/client/Container.scala b/docker-testkit/src/main/scala/stormlantern/dockertestkit/client/Container.scala deleted file mode 100644 index 93852fd..0000000 --- a/docker-testkit/src/main/scala/stormlantern/dockertestkit/client/Container.scala +++ /dev/null @@ -1,39 +0,0 @@ -package stormlantern.dockertestkit.client - -import java.util - -import com.spotify.docker.client.messages._ -import stormlantern.dockertestkit.DockerClientProvider - -import scala.collection.JavaConverters._ - -class Container(config: ContainerConfig) { - - private val docker = DockerClientProvider.client - private lazy val container: ContainerCreation = docker.createContainer(config) - private def id: String = container.id() - - def start(): Unit = { - docker.startContainer(id) - val info: ContainerInfo = docker.inspectContainer(id) - if (!info.state().running()) { - throw new IllegalStateException("Could not start Docker container") - } - } - - def stop(): Unit = { - docker.killContainer(id) - docker.waitContainer(id) - } - - def remove(): Unit = { - docker.removeContainer(id) - } - - def mappedPort(port: String): Seq[PortBinding] = { - val ports: util.Map[String, util.List[PortBinding]] = Option(docker.inspectContainer(id).networkSettings().ports()) - .getOrElse(throw new IllegalStateException(s"No ports found for on container with id $id")) - Option(ports.get(port)).getOrElse(throw new IllegalStateException(s"Port $port not found on caintainer with id $id")).asScala.toSeq - } -} - diff --git a/docker-testkit/src/main/scala/stormlantern/dockertestkit/orchestration/Orchestration.scala b/docker-testkit/src/main/scala/stormlantern/dockertestkit/orchestration/Orchestration.scala deleted file mode 100644 index f9468a1..0000000 --- a/docker-testkit/src/main/scala/stormlantern/dockertestkit/orchestration/Orchestration.scala +++ /dev/null @@ -1,5 +0,0 @@ -package stormlantern.dockertestkit.orchestration - -class Orchestration { - -} From ea70fc790fe1e2a9985a6e8078164c7952d087b2 Mon Sep 17 00:00:00 2001 From: Leonard Wolters Date: Thu, 2 Nov 2023 19:15:07 +0100 Subject: [PATCH 23/33] First test is passing --- RELEASE.md | 61 +++++++ build.sbt | 21 ++- .../consul/client/ClientITSpec.scala | 3 + .../consul/client/ServiceBrokerIT.scala | 9 +- .../client/dao/AkkaHttpConsulClientIT.scala | 167 ++++++++++-------- .../dao/akka/AkkaHttpConsulClient.scala | 148 +++++++++------- .../consul/client/ClientSpec.scala | 25 +++ ...pec.scala => ServiceBrokerActorTest.scala} | 117 +++++++----- ...okerSpec.scala => ServiceBrokerTest.scala} | 25 +-- ...ala => ServiceAvailabilityActorTest.scala} | 55 +++--- .../election/LeaderFollowerActorSpec.scala | 73 -------- .../election/LeaderFollowerActorTest.scala | 71 ++++++++ ...Spec.scala => LoadBalancerActorTest.scala} | 25 +-- ...scala => RoundRobinLoadBalancerTest.scala} | 5 +- 14 files changed, 476 insertions(+), 329 deletions(-) create mode 100644 RELEASE.md create mode 100644 client/src/test/scala/stormlantern/consul/client/ClientSpec.scala rename client/src/test/scala/stormlantern/consul/client/{ServiceBrokerActorSpec.scala => ServiceBrokerActorTest.scala} (67%) rename client/src/test/scala/stormlantern/consul/client/{ServiceBrokerSpec.scala => ServiceBrokerTest.scala} (71%) rename client/src/test/scala/stormlantern/consul/client/discovery/{ServiceAvailabilityActorSpec.scala => ServiceAvailabilityActorTest.scala} (52%) delete mode 100644 client/src/test/scala/stormlantern/consul/client/election/LeaderFollowerActorSpec.scala create mode 100644 client/src/test/scala/stormlantern/consul/client/election/LeaderFollowerActorTest.scala rename client/src/test/scala/stormlantern/consul/client/loadbalancers/{LoadBalancerActorSpec.scala => LoadBalancerActorTest.scala} (81%) rename client/src/test/scala/stormlantern/consul/client/loadbalancers/{RoundRobinLoadBalancerSpec.scala => RoundRobinLoadBalancerTest.scala} (81%) diff --git a/RELEASE.md b/RELEASE.md new file mode 100644 index 0000000..78c8d11 --- /dev/null +++ b/RELEASE.md @@ -0,0 +1,61 @@ +from http://www.scala-sbt.org/release/docs/Using-Sonatype.html + +The credentials for your Sonatype OSSRH account need to be stored somewhere safe (e.g. NOT in the repository). +Common convention is a ~/.sbt/1.0/sonatype.sbt or (~/.sbt/.credentials) file with the following: + +``` +credentials += Credentials("Sonatype Nexus Repository Manager", + "oss.sonatype.org", + "", + "") +``` + +If not done already, generate a key and upload it to a keyserver. + +``` +$ gpg --gen-key +$ gpg --list-secret-keys +$ gpg --keyserver keyserver.ubuntu.com --send-keys 2BE.......E804D85663F +``` + +Release the client is done using the following three steps: + +``` +1. Release Code +2. Publish Artifacts +3. Releese Artifacts +``` + +## Release Code + +To release and publish a version to oss.sonatype for both scala 2.12 and scala 2.13 run: + +``` +sbt release +``` +It's likely that *after* pushing all artifacts to the online repository you'll see an error complaining that the +tag already exists. That's ok. + +## Publish Artifacts + +To only publish a certain version after f.e. the tags has been build, but your PGP was not correctly unlocked you can +run + +``` +sbt publishSigned +``` + +If you want to publish only a single SCALA version, use `sbt ++2.11.12 publishSigned` + +## Release Artifacts + +To close and release the staging repository on Sonatype you can either go to the web interface or use + +``` +sbt sonatypeRelease +``` + +If something goes wrong and you want to cleanup/rollback, use `sbt sonatypeDropAll` + +You can verify if all has been published correctly by visiting the following url:
+https://oss.sonatype.org/#nexus-search;quick~reactive%20crobox \ No newline at end of file diff --git a/build.sbt b/build.sbt index 088308b..e4e8f02 100644 --- a/build.sbt +++ b/build.sbt @@ -1,8 +1,6 @@ - - // Scala Formatting ThisBuild / scalafmtVersion := "1.5.1" -ThisBuild / scalafmtOnCompile := false // all projects +ThisBuild / scalafmtOnCompile := false // all projects ThisBuild / scalafmtTestOnCompile := false // all projects releaseCrossBuild := true @@ -66,14 +64,15 @@ lazy val client: Project = (project in file("client")) name := "client", sbtrelease.ReleasePlugin.autoImport.releasePublishArtifactsAction := PgpKeys.publishSigned.value, libraryDependencies ++= Seq( - "ch.qos.logback" % "logback-classic" % "1.4.7", - "io.spray" %% "spray-json" % "1.3.6", - "org.apache.pekko" %% "pekko-actor" % Dependencies.PekkoVersion, - "org.apache.pekko" %% "pekko-stream" % Dependencies.PekkoVersion, - "org.apache.pekko" %% "pekko-http" % Dependencies.PekkoHttpVersion, + "ch.qos.logback" % "logback-classic" % "1.4.7", + "io.spray" %% "spray-json" % "1.3.6", + "org.apache.pekko" %% "pekko-actor" % Dependencies.PekkoVersion, + "org.apache.pekko" %% "pekko-slf4j" % Dependencies.PekkoVersion, + "org.apache.pekko" %% "pekko-stream" % Dependencies.PekkoVersion, + "org.apache.pekko" %% "pekko-http" % Dependencies.PekkoHttpVersion, // test dependencies "org.apache.pekko" %% "pekko-testkit" % Dependencies.PekkoVersion % Test, - "org.scalatest" %% "scalatest" % "3.2.15" % Test, - "org.scalamock" %% "scalamock" % "5.2.0" % Test + "org.scalatest" %% "scalatest" % "3.2.15" % Test, + "org.scalamock" %% "scalamock" % "5.2.0" % Test ) - ) \ No newline at end of file + ) diff --git a/client/src/it/scala/stormlantern/consul/client/ClientITSpec.scala b/client/src/it/scala/stormlantern/consul/client/ClientITSpec.scala index 899da3c..a6c213d 100644 --- a/client/src/it/scala/stormlantern/consul/client/ClientITSpec.scala +++ b/client/src/it/scala/stormlantern/consul/client/ClientITSpec.scala @@ -18,4 +18,7 @@ abstract class ClientITSpec(val config: Config = ConfigFactory.load()) with IntegrationPatience { implicit val ec: ExecutionContext = scala.concurrent.ExecutionContext.Implicits.global + + val host = "localhost" + val port = 8500 } diff --git a/client/src/it/scala/stormlantern/consul/client/ServiceBrokerIT.scala b/client/src/it/scala/stormlantern/consul/client/ServiceBrokerIT.scala index 7e5c40b..e379167 100644 --- a/client/src/it/scala/stormlantern/consul/client/ServiceBrokerIT.scala +++ b/client/src/it/scala/stormlantern/consul/client/ServiceBrokerIT.scala @@ -2,12 +2,7 @@ package stormlantern.consul.client import stormlantern.consul.client.dao.akka.AkkaHttpConsulClient import stormlantern.consul.client.dao.{ConsulHttpClient, ServiceRegistration} -import stormlantern.consul.client.discovery.{ - ConnectionProvider, - ConnectionProviderFactory, - ConnectionStrategy, - ServiceDefinition -} +import stormlantern.consul.client.discovery._ import stormlantern.consul.client.loadbalancers.RoundRobinLoadBalancer import stormlantern.consul.client.util.Logging @@ -16,7 +11,7 @@ import scala.concurrent.Future class ServiceBrokerIT extends ClientITSpec with Logging { - "The ServiceBroker" should "provide a usable connection to consul" in withConsulHost { (host, port) => + "The ServiceBroker" should "provide a usable connection to consul" in { val akkaHttpClient = new AkkaHttpConsulClient(new URL(s"http://$host:$port")) // Register the HTTP interface diff --git a/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIT.scala b/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIT.scala index 14522b2..2ccabea 100644 --- a/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIT.scala +++ b/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIT.scala @@ -1,74 +1,81 @@ package stormlantern.consul.client.dao -import java.net.URL -import java.util.UUID -import org.scalatest._ -import org.scalatest.concurrent.{Eventually, IntegrationPatience, ScalaFutures} import stormlantern.consul.client.ClientITSpec import stormlantern.consul.client.dao.akka.AkkaHttpConsulClient -import stormlantern.consul.client.dao.org.apache.pekko.AkkaHttpConsulClient import stormlantern.consul.client.util.{Logging, RetryPolicy} -class AkkaHttpConsulClientIT extends ClientITSpec with RetryPolicy with Logging { +import java.net.URL +import java.util.UUID - import scala.concurrent.ExecutionContext.Implicits.global +class AkkaHttpConsulClientIT extends ClientITSpec with RetryPolicy with Logging { - def withConsulHttpClient[T](f: ConsulHttpClient => T): T = withConsulHost { (host, port) => - val subject: ConsulHttpClient = new AkkaHttpConsulClient(new URL(s"http://$host:$port")) - f(subject) - } + val subject = new AkkaHttpConsulClient(new URL(s"http://$host:$port")) - "The AkkaHttpConsulClient" should "retrieve a single Consul service from a freshly started Consul instance" in withConsulHttpClient { subject => + "The AkkaHttpConsulClient" should "retrieve a single Consul service from a freshly started Consul instance" in { eventually { - subject.getService("consul").map { result => - result.resource should have size 1 - result.resource.head.serviceName shouldEqual "consul" - }.futureValue + subject + .getService("consul") + .map { result => + result.resource should have size 1 + result.resource.head.serviceName shouldEqual "consul" + } + .futureValue } } - it should "retrieve a single health aware Consul service from a freshly started Consul instance" in withConsulHttpClient { - subject => - eventually { - subject.getServiceHealthAware("consul").map { result => + it should "retrieve a single health aware Consul service from a freshly started Consul instance" in { + eventually { + subject + .getServiceHealthAware("consul") + .map { result => result.resource should have size 1 result.resource.head.serviceName shouldEqual "consul" - }.futureValue - } + } + .futureValue + } } - it should "retrieve no unknown service from a freshly started Consul instance" in withConsulHttpClient { subject => + it should "retrieve no unknown service from a freshly started Consul instance" in { eventually { - subject.getService("bogus").map { result => - logger.info(s"Index is ${result.index}") - result.resource should have size 0 - }.futureValue + subject + .getService("bogus") + .map { result => + logger.info(s"Index is ${result.index}") + result.resource should have size 0 + } + .futureValue } } - it should "retrieve a single Consul service from a freshly started Consul instance and timeout after the second request if nothing changes" in withConsulHttpClient { subject => + it should "retrieve a single Consul service from a freshly started Consul instance and timeout after the second request if nothing changes" in { eventually { - subject.getService("consul").flatMap { result => - result.resource should have size 1 - result.resource.head.serviceName shouldEqual "consul" - subject.getService("consul", None, Some(result.index), Some("500ms")).map { secondResult => - secondResult.resource should have size 1 - secondResult.index shouldEqual result.index + subject + .getService("consul") + .flatMap { result => + result.resource should have size 1 + result.resource.head.serviceName shouldEqual "consul" + subject.getService("consul", None, Some(result.index), Some("500ms")).map { secondResult => + secondResult.resource should have size 1 + secondResult.index shouldEqual result.index + } } - }.futureValue + .futureValue } } - it should "register and deregister a new service with Consul" in withConsulHttpClient { subject => - subject.putService(ServiceRegistration("newservice", Some("newservice-1"))) - .futureValue should equal("newservice-1") - subject.putService(ServiceRegistration("newservice", Some("newservice-2"), check = Some(TTLHealthCheck("2s")))) + it should "register and deregister a new service with Consul" in { + subject.putService(ServiceRegistration("newservice", Some("newservice-1"))).futureValue should equal("newservice-1") + subject + .putService(ServiceRegistration("newservice", Some("newservice-2"), check = Some(TTLHealthCheck("2s")))) .futureValue should equal("newservice-2") eventually { - subject.getService("newservice").map { result => - result.resource should have size 2 - result.resource.head.serviceName shouldEqual "newservice" - }.futureValue + subject + .getService("newservice") + .map { result => + result.resource should have size 2 + result.resource.head.serviceName shouldEqual "newservice" + } + .futureValue } subject.deleteService("newservice-1").futureValue should equal(()) eventually { @@ -79,63 +86,79 @@ class AkkaHttpConsulClientIT extends ClientITSpec with RetryPolicy with Logging } } - it should "retrieve a service matching tags and leave out others" in withConsulHttpClient { subject => - subject.putService(ServiceRegistration("newservice", Some("newservice-1"), Set("tag1", "tag2"))) + it should "retrieve a service matching tags and leave out others" in { + subject + .putService(ServiceRegistration("newservice", Some("newservice-1"), Set("tag1", "tag2"))) .futureValue should equal("newservice-1") - subject.putService(ServiceRegistration("newservice", Some("newservice-2"), Set("tag2", "tag3"))) + subject + .putService(ServiceRegistration("newservice", Some("newservice-2"), Set("tag2", "tag3"))) .futureValue should equal("newservice-2") eventually { - subject.getService("newservice").map { result => - result.resource should have size 2 - result.resource.head.serviceName shouldEqual "newservice" - }.futureValue - subject.getService("newservice", Some("tag2")).map { result => - result.resource should have size 2 - result.resource.head.serviceName shouldEqual "newservice" - }.futureValue - subject.getService("newservice", Some("tag3")).map { result => - result.resource should have size 1 - result.resource.head.serviceName shouldEqual "newservice" - result.resource.head.serviceId shouldEqual "newservice-2" - }.futureValue + subject + .getService("newservice") + .map { result => + result.resource should have size 2 + result.resource.head.serviceName shouldEqual "newservice" + } + .futureValue + subject + .getService("newservice", Some("tag2")) + .map { result => + result.resource should have size 2 + result.resource.head.serviceName shouldEqual "newservice" + } + .futureValue + subject + .getService("newservice", Some("tag3")) + .map { result => + result.resource should have size 1 + result.resource.head.serviceName shouldEqual "newservice" + result.resource.head.serviceId shouldEqual "newservice-2" + } + .futureValue } } - it should "register a session and get it's ID then read it back" in withConsulHttpClient { subject => + it should "register a session and get it's ID then read it back" in { val id: UUID = subject.putSession(Some(SessionCreation(name = Some("MySession")))).futureValue - subject.getSessionInfo(id).map { sessionInfo => - sessionInfo should be('defined) - sessionInfo.get.id shouldEqual id - }.futureValue + subject + .getSessionInfo(id) + .map { sessionInfo => + sessionInfo should be(defined) + sessionInfo.get.id shouldEqual id + } + .futureValue } - it should "get a session lock on a key/value pair and fail to get a second lock" in withConsulHttpClient { subject => + it should "get a session lock on a key/value pair and fail to get a second lock" in { val id: UUID = subject.putSession(Some(SessionCreation(name = Some("MySession")))).futureValue - val payload = """ { "name" : "test" } """.getBytes("UTF-8") + val payload = """ { "name" : "test" } """.getBytes("UTF-8") subject.putKeyValuePair("my/key", payload, Some(AcquireSession(id))).futureValue should be(true) subject.putKeyValuePair("my/key", payload, Some(AcquireSession(id))).futureValue should be(false) subject.putKeyValuePair("my/key", payload, Some(ReleaseSession(id))).futureValue should be(true) } - it should "get a session lock on a key/value pair and get a second lock after release" in withConsulHttpClient { subject => + it should "get a session lock on a key/value pair and get a second lock after release" in { val id: UUID = subject.putSession(Some(SessionCreation(name = Some("MySession")))).futureValue - val payload = """ { "name" : "test" } """.getBytes("UTF-8") + val payload = """ { "name" : "test" } """.getBytes("UTF-8") subject.putKeyValuePair("my/key", payload, Some(AcquireSession(id))).futureValue should be(true) subject.putKeyValuePair("my/key", payload, Some(ReleaseSession(id))).futureValue should be(true) subject.putKeyValuePair("my/key", payload, Some(AcquireSession(id))).futureValue should be(true) subject.putKeyValuePair("my/key", payload, Some(ReleaseSession(id))).futureValue should be(true) } - it should "write a key/value pair and read it back" in withConsulHttpClient { subject => + it should "write a key/value pair and read it back" in { val payload = """ { "name" : "test" } """.getBytes("UTF-8") subject.putKeyValuePair("my/key", payload).futureValue should be(true) val keyDataSeq = subject.getKeyValuePair("my/key").futureValue keyDataSeq.head.value should equal(BinaryData(payload)) } - it should "fail when aquiring a lock on a key with a non-existent session" in withConsulHttpClient { subject => - val payload = """ { "name" : "test" } """.getBytes("UTF-8") + it should "fail when aquiring a lock on a key with a non-existent session" in { + val payload = """ { "name" : "test" } """.getBytes("UTF-8") val nonExistentSessionId = UUID.fromString("9A3BB9C-E2E7-43DF-BFD5-845417146552") - val result = subject.putKeyValuePair("my/key", payload, Some(AcquireSession(nonExistentSessionId))).futureValue should be(false) + val result = subject + .putKeyValuePair("my/key", payload, Some(AcquireSession(nonExistentSessionId))) + .futureValue should be(false) } } diff --git a/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala b/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala index 03b4a4c..1d46745 100644 --- a/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala +++ b/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala @@ -1,26 +1,26 @@ package stormlantern.consul.client.dao.akka -import java.net.URL -import java.util.UUID - -import org.apache.pekko.actor.{ ActorSystem, Scheduler } +import org.apache.pekko.actor.{ActorSystem, Scheduler} import org.apache.pekko.http.scaladsl.Http -import org.apache.pekko.http.scaladsl.model.{ HttpHeader, StatusCode, _ } -import org.apache.pekko.stream.{ ActorMaterializer, Materializer } +import org.apache.pekko.http.scaladsl.model._ import org.apache.pekko.util.ByteString import spray.json._ import stormlantern.consul.client.dao._ -import stormlantern.consul.client.util.{ Logging, RetryPolicy } +import stormlantern.consul.client.util.{Logging, RetryPolicy} -import scala.concurrent.{ ExecutionContextExecutor, Future } -import scala.util.{ Failure, Success, Try } +import java.net.URL +import java.util.UUID +import scala.concurrent.{ExecutionContextExecutor, Future} +import scala.util.{Failure, Success, Try} -class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) extends ConsulHttpClient - with ConsulHttpProtocol with RetryPolicy with Logging { +class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) + extends ConsulHttpClient + with ConsulHttpProtocol + with RetryPolicy + with Logging { implicit val executionContext: ExecutionContextExecutor = actorSystem.dispatcher - implicit val scheduler: Scheduler = actorSystem.scheduler - implicit val materializer: Materializer = ActorMaterializer() + implicit val scheduler: Scheduler = actorSystem.scheduler private val JsonMediaType = ContentTypes.`application/json`.mediaType private val TextMediaType = ContentTypes.`text/plain(UTF-8)`.mediaType @@ -28,12 +28,16 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) extends // // Services // ///////////////// - def getService(service: String, tag: Option[String] = None, index: Option[Long] = None, wait: Option[String] = None, dataCenter: Option[String] = None): Future[IndexedServiceInstances] = { - val dcParameter = dataCenter.map(dc => s"dc=$dc") - val waitParameter = wait.map(w => s"wait=$w") - val indexParameter = index.map(i => s"index=$i") - val tagParameter = tag.map(t => s"tag=$t") - val parameters = Seq(dcParameter, tagParameter, waitParameter, indexParameter).flatten.mkString("&") + def getService(service: String, + tag: Option[String] = None, + index: Option[Long] = None, + wait: Option[String] = None, + dataCenter: Option[String] = None): Future[IndexedServiceInstances] = { + val dcParameter = dataCenter.map(dc => s"dc=$dc") + val waitParameter = wait.map(w => s"wait=$w") + val indexParameter = index.map(i => s"index=$i") + val tagParameter = tag.map(t => s"tag=$t") + val parameters = Seq(dcParameter, tagParameter, waitParameter, indexParameter).flatten.mkString("&") val request: HttpRequest = HttpRequest(HttpMethods.GET).withUri(s"$host/v1/catalog/service/$service?$parameters") retry[IndexedServiceInstances]() { @@ -46,13 +50,18 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) extends } } - def getServiceHealthAware(service: String, tag: Option[String] = None, index: Option[Long] = None, wait: Option[String] = None, dataCenter: Option[String] = None): Future[IndexedServiceInstances] = { - val dcParameter = dataCenter.map(dc => s"dc=$dc") - val waitParameter = wait.map(w => s"wait=$w") - val indexParameter = index.map(i => s"index=$i") - val tagParameter = tag.map(t => s"tag=$t") + def getServiceHealthAware(service: String, + tag: Option[String] = None, + index: Option[Long] = None, + wait: Option[String] = None, + dataCenter: Option[String] = None): Future[IndexedServiceInstances] = { + val dcParameter = dataCenter.map(dc => s"dc=$dc") + val waitParameter = wait.map(w => s"wait=$w") + val indexParameter = index.map(i => s"index=$i") + val tagParameter = tag.map(t => s"tag=$t") val passingParameter = Some(s"passing=true") - val parameters = Seq(dcParameter, tagParameter, waitParameter, indexParameter, passingParameter).flatten.mkString("&") + val parameters = + Seq(dcParameter, tagParameter, waitParameter, indexParameter, passingParameter).flatten.mkString("&") val request: HttpRequest = HttpRequest(HttpMethods.GET).withUri(s"$host/v1/health/service/$service?$parameters") retry[IndexedServiceInstances]() { @@ -66,7 +75,8 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) extends } def putService(registration: ServiceRegistration): Future[String] = { - val request = HttpRequest(HttpMethods.PUT).withUri(s"$host/v1/agent/service/register") + val request = HttpRequest(HttpMethods.PUT) + .withUri(s"$host/v1/agent/service/register") .withEntity(registration.toJson.asJsObject().toString.getBytes) retry[ConsulResponse]() { @@ -87,10 +97,11 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) extends // ///////////////// def putSession(sessionCreation: Option[SessionCreation], dataCenter: Option[String]): Future[UUID] = { val dcParameter = dataCenter.map(dc => s"dc=$dc") - val parameters = Seq(dcParameter).flatten.mkString("&") + val parameters = Seq(dcParameter).flatten.mkString("&") val request = sessionCreation.map(_.toJson.asJsObject.toString.getBytes) match { - case None => HttpRequest(HttpMethods.PUT).withUri(s"$host/v1/session/create?$parameters") - case Some(entity) => HttpRequest(HttpMethods.PUT).withUri(s"$host/v1/session/create?$parameters").withEntity(entity) + case None => HttpRequest(HttpMethods.PUT).withUri(s"$host/v1/session/create?$parameters") + case Some(entity) => + HttpRequest(HttpMethods.PUT).withUri(s"$host/v1/session/create?$parameters").withEntity(entity) } retry[UUID]() { @@ -101,10 +112,10 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) extends } def getSessionInfo(sessionId: UUID, index: Option[Long], dataCenter: Option[String]): Future[Option[SessionInfo]] = { - val dcParameter = dataCenter.map(dc => s"dc=$dc") + val dcParameter = dataCenter.map(dc => s"dc=$dc") val indexParameter = index.map(i => s"index=$i") - val parameters = Seq(dcParameter, indexParameter).flatten.mkString("&") - val request = HttpRequest(HttpMethods.GET).withUri(s"$host/v1/session/info/$sessionId?$parameters") + val parameters = Seq(dcParameter, indexParameter).flatten.mkString("&") + val request = HttpRequest(HttpMethods.GET).withUri(s"$host/v1/session/info/$sessionId?$parameters") retry[Option[SessionInfo]]() { getResponse(request, JsonMediaType).map { response => @@ -124,27 +135,33 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) extends case ReleaseSession(id) => s"release=$id" } val parameters = opParameter.getOrElse("") - val request = HttpRequest(HttpMethods.PUT).withUri(s"$host/v1/kv/$key?$parameters").withEntity(value) + val request = HttpRequest(HttpMethods.PUT).withUri(s"$host/v1/kv/$key?$parameters").withEntity(value) - def validator(response: HttpResponse): Boolean = response.status.isSuccess() || response.status == InternalServerError + def validator(response: HttpResponse): Boolean = + response.status.isSuccess() || response.status == InternalServerError retry[Boolean]() { getResponse(request, JsonMediaType, validator).flatMap { case ConsulResponse(OK, _, body) => Future successful Option(body.toBoolean).getOrElse(false) case ConsulResponse(InternalServerError, _, "Invalid session") => Future successful false - case ConsulResponse(status, _, body) => Future failed new Exception(s"Request returned status code $status - $body") + case ConsulResponse(status, _, body) => + Future failed new Exception(s"Request returned status code $status - $body") } } } - def getKeyValuePair(key: String, index: Option[Long], wait: Option[String], recurse: Boolean, keysOnly: Boolean): Future[Seq[KeyData]] = { + def getKeyValuePair(key: String, + index: Option[Long], + wait: Option[String], + recurse: Boolean, + keysOnly: Boolean): Future[Seq[KeyData]] = { - val waitParameter = wait.map(p => s"wait=$p") - val indexParameter = index.map(p => s"index=$p") - val recurseParameter = if (recurse) Some("recurse") else None + val waitParameter = wait.map(p => s"wait=$p") + val indexParameter = index.map(p => s"index=$p") + val recurseParameter = if (recurse) Some("recurse") else None val keysOnlyParameter = if (keysOnly) Some("keys") else None - val parameters = Seq(indexParameter, waitParameter, recurseParameter, keysOnlyParameter).flatten.mkString("&") - val request = HttpRequest(HttpMethods.GET).withUri(s"$host/v1/kv/$key?$parameters") + val parameters = Seq(indexParameter, waitParameter, recurseParameter, keysOnlyParameter).flatten.mkString("&") + val request = HttpRequest(HttpMethods.GET).withUri(s"$host/v1/kv/$key?$parameters") retry[Seq[KeyData]]() { getResponse(request, JsonMediaType, _ => true).map { response => @@ -160,13 +177,20 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) extends // // Internal Helpers // ////////////////////////// - private def getResponse[T, U](request: HttpRequest, expectedMediaType: MediaType, validator: HttpResponse => Boolean = (in) => in.status.isSuccess()): Future[ConsulResponse] = { - - def validStatus(response: HttpResponse) = if (validator(response)) { - Future successful response - } else { - parseBody(response).flatMap { body => Future failed ConsulException(s"Bad status code: ${response.status.intValue()} with body $body") } - } + private def getResponse[T, U]( + request: HttpRequest, + expectedMediaType: MediaType, + validator: HttpResponse => Boolean = (in) => in.status.isSuccess() + ): Future[ConsulResponse] = { + + def validStatus(response: HttpResponse) = + if (validator(response)) { + Future successful response + } else { + parseBody(response).flatMap { body => + Future failed ConsulException(s"Bad status code: ${response.status.intValue()} with body $body") + } + } // // Consul does not return the Charset with the Response Content Type, so just MediaType comparison @@ -182,13 +206,15 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) extends if (resp.entity.contentType.mediaType == expected) { Future successful resp } else { - Future failed ConsulException(resp.status, s"Unexpected content type: ${resp.entity.contentType}, expected $expectedMediaType") + Future failed ConsulException( + resp.status, + s"Unexpected content type: ${resp.entity.contentType}, expected $expectedMediaType" + ) } } - def parseBody(response: HttpResponse): Future[String] = { + def parseBody(response: HttpResponse): Future[String] = response.entity.dataBytes.runFold(ByteString(""))(_ ++ _).map(_.utf8String) - } // make the call Http() @@ -202,13 +228,15 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) extends } } - private def validIndex(response: ConsulResponse): Future[Long] = response.headers.find(_.name() == "X-Consul-Index") match { - case None => Future failed ConsulException("X-Consul-Index header not found") - case Some(hdr) => Try(hdr.value.toLong) match { - case Success(idx) => Future successful idx - case Failure(ex) => Future failed ConsulException("X-Consul-Index header was not numeric") + private def validIndex(response: ConsulResponse): Future[Long] = + response.headers.find(_.name() == "X-Consul-Index") match { + case None => Future failed ConsulException("X-Consul-Index header not found") + case Some(hdr) => + Try(hdr.value.toLong) match { + case Success(idx) => Future successful idx + case Failure(ex) => Future failed ConsulException("X-Consul-Index header was not numeric") + } } - } } // @@ -216,9 +244,11 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) extends // ////////////////////////// case class ConsulResponse(status: StatusCode, headers: Seq[HttpHeader], body: String) -case class ConsulException(message: String, response: HttpResponse, status: Option[StatusCode] = None) extends Exception(message) +case class ConsulException(message: String, response: HttpResponse, status: Option[StatusCode] = None) + extends Exception(message) + object ConsulException { def apply(status: StatusCode, msg: String) = new ConsulException(msg, null, Option(status)) // I feel dirty after this - def apply(msg: String) = new ConsulException(msg, null) // I feel dirty after this + def apply(msg: String) = new ConsulException(msg, null) // I feel dirty after this } diff --git a/client/src/test/scala/stormlantern/consul/client/ClientSpec.scala b/client/src/test/scala/stormlantern/consul/client/ClientSpec.scala new file mode 100644 index 0000000..33b918b --- /dev/null +++ b/client/src/test/scala/stormlantern/consul/client/ClientSpec.scala @@ -0,0 +1,25 @@ +package stormlantern.consul.client + +import com.typesafe.config.{Config, ConfigFactory} +import org.apache.pekko.actor.ActorSystem +import org.apache.pekko.testkit.{ImplicitSender, TestKit} +import org.scalamock.scalatest.MockFactory +import org.scalatest.BeforeAndAfterAll +import org.scalatest.concurrent.ScalaFutures +import org.scalatest.flatspec.AnyFlatSpecLike +import org.scalatest.matchers.should.Matchers +import stormlantern.consul.client.helpers.CallingThreadExecutionContext +import stormlantern.consul.client.util.Logging + +class ClientSpec(val config: Config = ConfigFactory.load()) + extends TestKit(ActorSystem("TestSystem", config)) + with ImplicitSender + with AnyFlatSpecLike + with Matchers + with ScalaFutures + with BeforeAndAfterAll + with MockFactory + with Logging { + + implicit val ec = CallingThreadExecutionContext() +} diff --git a/client/src/test/scala/stormlantern/consul/client/ServiceBrokerActorSpec.scala b/client/src/test/scala/stormlantern/consul/client/ServiceBrokerActorTest.scala similarity index 67% rename from client/src/test/scala/stormlantern/consul/client/ServiceBrokerActorSpec.scala rename to client/src/test/scala/stormlantern/consul/client/ServiceBrokerActorTest.scala index d2d6981..cd52e4f 100644 --- a/client/src/test/scala/stormlantern/consul/client/ServiceBrokerActorSpec.scala +++ b/client/src/test/scala/stormlantern/consul/client/ServiceBrokerActorTest.scala @@ -2,46 +2,46 @@ package stormlantern.consul.client import org.apache.pekko.actor.Status.Failure import org.apache.pekko.actor._ -import org.apache.pekko.testkit.{ ImplicitSender, TestActorRef, TestKit, TestProbe } -import org.scalamock.scalatest.MockFactory -import org.scalatest.{ BeforeAndAfterAll, FlatSpecLike, Matchers } -import stormlantern.consul.client.dao.{ ConsulHttpClient, ServiceInstance } -import stormlantern.consul.client.discovery.ServiceAvailabilityActor.{ Start, Started } +import org.apache.pekko.testkit.{TestActorRef, TestProbe} +import stormlantern.consul.client.dao.{ConsulHttpClient, ServiceInstance} +import stormlantern.consul.client.discovery.ServiceAvailabilityActor.{Start, Started} import stormlantern.consul.client.discovery._ -import stormlantern.consul.client.helpers.{ CallingThreadExecutionContext, ModelHelpers } +import stormlantern.consul.client.helpers.ModelHelpers import stormlantern.consul.client.loadbalancers.LoadBalancerActor -import stormlantern.consul.client.util.Logging -class ServiceBrokerActorSpec(_system: ActorSystem) extends TestKit(_system) with ImplicitSender with FlatSpecLike - with Matchers with BeforeAndAfterAll with MockFactory with Logging { - - implicit val ec = CallingThreadExecutionContext() - def this() = this(ActorSystem("ServiceBrokerActorSpec")) - - override def afterAll() { - TestKit.shutdownActorSystem(system) - } +class ServiceBrokerActorTest extends ClientSpec { trait TestScope { val httpClient: ConsulHttpClient = mock[ConsulHttpClient] + val serviceAvailabilityActorFactory: (ActorRefFactory, ServiceDefinition, ActorRef, Boolean) => ActorRef = mock[(ActorRefFactory, ServiceDefinition, ActorRef, Boolean) => ActorRef] val connectionProviderFactory: ConnectionProviderFactory = mock[ConnectionProviderFactory] - val connectionProvider: ConnectionProvider = mock[ConnectionProvider] - val connectionHolder: ConnectionHolder = mock[ConnectionHolder] - val service1 = ServiceDefinition("service1Id", "service1") - val service2 = ServiceDefinition("service2Key", "service2") - val loadBalancerProbeForService1 = TestProbe("LoadBalancerActorForService1") - val loadBalancerProbeForService2 = TestProbe("LoadBalancerActorForService2") - val connectionStrategyForService1 = ConnectionStrategy(service1, connectionProviderFactory, ctx => loadBalancerProbeForService1.ref, onlyHealthyServices = true) - val connectionStrategyForService2 = ConnectionStrategy(service2, connectionProviderFactory, ctx => loadBalancerProbeForService2.ref, onlyHealthyServices = false) + val connectionProvider: ConnectionProvider = mock[ConnectionProvider] + val connectionHolder: ConnectionHolder = mock[ConnectionHolder] + val service1 = ServiceDefinition("service1Id", "service1") + val service2 = ServiceDefinition("service2Key", "service2") + val loadBalancerProbeForService1 = TestProbe("LoadBalancerActorForService1") + val loadBalancerProbeForService2 = TestProbe("LoadBalancerActorForService2") + + val connectionStrategyForService1 = ConnectionStrategy(service1, + connectionProviderFactory, + ctx => loadBalancerProbeForService1.ref, + onlyHealthyServices = true) + + val connectionStrategyForService2 = ConnectionStrategy(service2, + connectionProviderFactory, + ctx => loadBalancerProbeForService2.ref, + onlyHealthyServices = false) } "The ServiceBrokerActor" should "create a child actor per service" in new TestScope { val serviceAvailabilityProbe = TestProbe("ServiceAvailabilityActor") (serviceAvailabilityActorFactory.apply _).expects(*, service1, *, *).returns(serviceAvailabilityProbe.ref) - val sut: TestActorRef[ServiceBrokerActor] = TestActorRef[ServiceBrokerActor](ServiceBrokerActor.props( - Set(connectionStrategyForService1), serviceAvailabilityActorFactory)) + + val sut: TestActorRef[ServiceBrokerActor] = TestActorRef[ServiceBrokerActor]( + ServiceBrokerActor.props(Set(connectionStrategyForService1), serviceAvailabilityActorFactory) + ) serviceAvailabilityProbe.expectMsg(Start) sut.underlyingActor.loadbalancers.keys should contain(service1.key) sut.stop() @@ -50,26 +50,38 @@ class ServiceBrokerActorSpec(_system: ActorSystem) extends TestKit(_system) with it should "create a load balancer for each new service" in new TestScope { val serviceAvailabilityProbe = TestProbe("ServiceAvailabilityActor") (serviceAvailabilityActorFactory.apply _).expects(*, service1, *, *).returns(serviceAvailabilityProbe.ref) - val sut: TestActorRef[ServiceBrokerActor] = TestActorRef[ServiceBrokerActor](ServiceBrokerActor.props( - Set(connectionStrategyForService1), serviceAvailabilityActorFactory)) + + val sut: TestActorRef[ServiceBrokerActor] = TestActorRef[ServiceBrokerActor]( + ServiceBrokerActor.props(Set(connectionStrategyForService1), serviceAvailabilityActorFactory) + ) serviceAvailabilityProbe.expectMsg(Start) val service: ServiceInstance = ModelHelpers.createService("service1:Id", "service1") - (connectionProviderFactory.create _).expects(service.serviceAddress, service.servicePort).returns(connectionProvider) + (connectionProviderFactory.create _) + .expects(service.serviceAddress, service.servicePort) + .returns(connectionProvider) serviceAvailabilityProbe.send( - sut, ServiceAvailabilityActor.ServiceAvailabilityUpdate(service1.key, added = Set(service), removed = Set.empty)) - loadBalancerProbeForService1.expectMsg(LoadBalancerActor.AddConnectionProvider(service.serviceId, connectionProvider)) + sut, + ServiceAvailabilityActor.ServiceAvailabilityUpdate(service1.key, added = Set(service), removed = Set.empty) + ) + loadBalancerProbeForService1.expectMsg( + LoadBalancerActor.AddConnectionProvider(service.serviceId, connectionProvider) + ) sut.stop() } it should "remove the load balancer for each old service" in new TestScope { val serviceAvailabilityProbe = TestProbe("ServiceAvailabilityActor") (serviceAvailabilityActorFactory.apply _).expects(*, service1, *, *).returns(serviceAvailabilityProbe.ref) - val sut: TestActorRef[ServiceBrokerActor] = TestActorRef[ServiceBrokerActor](ServiceBrokerActor.props( - Set(connectionStrategyForService1), serviceAvailabilityActorFactory)) + + val sut: TestActorRef[ServiceBrokerActor] = TestActorRef[ServiceBrokerActor]( + ServiceBrokerActor.props(Set(connectionStrategyForService1), serviceAvailabilityActorFactory) + ) serviceAvailabilityProbe.expectMsg(Start) val service: ServiceInstance = ModelHelpers.createService("service1:Id", "service1") serviceAvailabilityProbe.send( - sut, ServiceAvailabilityActor.ServiceAvailabilityUpdate(service1.key, added = Set.empty, removed = Set(service))) + sut, + ServiceAvailabilityActor.ServiceAvailabilityUpdate(service1.key, added = Set.empty, removed = Set(service)) + ) loadBalancerProbeForService1.expectMsg(LoadBalancerActor.RemoveConnectionProvider(service.serviceId)) sut.stop() } @@ -77,8 +89,10 @@ class ServiceBrokerActorSpec(_system: ActorSystem) extends TestKit(_system) with it should "initialize after all services have been seen" in new TestScope { val serviceAvailabilityProbe = TestProbe("ServiceAvailabilityActor") (serviceAvailabilityActorFactory.apply _).expects(*, service1, *, *).returns(serviceAvailabilityProbe.ref) - val sut: TestActorRef[ServiceBrokerActor] = TestActorRef[ServiceBrokerActor](ServiceBrokerActor.props( - Set(connectionStrategyForService1), serviceAvailabilityActorFactory)) + + val sut: TestActorRef[ServiceBrokerActor] = TestActorRef[ServiceBrokerActor]( + ServiceBrokerActor.props(Set(connectionStrategyForService1), serviceAvailabilityActorFactory) + ) serviceAvailabilityProbe.expectMsg(Start) val service: ServiceInstance = ModelHelpers.createService(service1) } @@ -86,8 +100,10 @@ class ServiceBrokerActorSpec(_system: ActorSystem) extends TestKit(_system) with it should "request a connection from a loadbalancer" in new TestScope { val serviceAvailabilityProbe = TestProbe("ServiceAvailabilityActor") (serviceAvailabilityActorFactory.apply _).expects(*, service1, *, *).returns(serviceAvailabilityProbe.ref) - val sut: TestActorRef[ServiceBrokerActor] = TestActorRef[ServiceBrokerActor](ServiceBrokerActor.props( - Set(connectionStrategyForService1), serviceAvailabilityActorFactory)) + + val sut: TestActorRef[ServiceBrokerActor] = TestActorRef[ServiceBrokerActor]( + ServiceBrokerActor.props(Set(connectionStrategyForService1), serviceAvailabilityActorFactory) + ) serviceAvailabilityProbe.expectMsg(Start) val service: ServiceInstance = ModelHelpers.createService(service1) sut ! ServiceBrokerActor.GetServiceConnection(service.serviceId) @@ -97,8 +113,9 @@ class ServiceBrokerActorSpec(_system: ActorSystem) extends TestKit(_system) with } it should "return a failure if a service name cannot be found" in new TestScope { - val sut: TestActorRef[ServiceBrokerActor] = TestActorRef[ServiceBrokerActor](ServiceBrokerActor.props( - Set.empty, serviceAvailabilityActorFactory)) + + val sut: TestActorRef[ServiceBrokerActor] = + TestActorRef[ServiceBrokerActor](ServiceBrokerActor.props(Set.empty, serviceAvailabilityActorFactory)) val service: ServiceInstance = ModelHelpers.createService(service1) sut ! ServiceBrokerActor.GetServiceConnection(service.serviceId) expectMsg(Failure(ServiceUnavailableException(service.serviceId))) @@ -108,8 +125,10 @@ class ServiceBrokerActorSpec(_system: ActorSystem) extends TestKit(_system) with it should "forward a query for connection provider availability" in new TestScope { val serviceAvailabilityProbe = TestProbe("ServiceAvailabilityActor") (serviceAvailabilityActorFactory.apply _).expects(*, service1, *, *).returns(serviceAvailabilityProbe.ref) - val sut: TestActorRef[ServiceBrokerActor] = TestActorRef[ServiceBrokerActor](ServiceBrokerActor.props( - Set(connectionStrategyForService1), serviceAvailabilityActorFactory)) + + val sut: TestActorRef[ServiceBrokerActor] = TestActorRef[ServiceBrokerActor]( + ServiceBrokerActor.props(Set(connectionStrategyForService1), serviceAvailabilityActorFactory) + ) serviceAvailabilityProbe.expectMsg(Start) sut ! ServiceBrokerActor.HasAvailableConnectionProviderFor(service1.key) loadBalancerProbeForService1.expectMsgPF() { @@ -124,8 +143,11 @@ class ServiceBrokerActorSpec(_system: ActorSystem) extends TestKit(_system) with val service2AvailabilityProbe = TestProbe("Service2AvailabilityActor") (serviceAvailabilityActorFactory.apply _).expects(*, service1, *, *).returns(service1AvailabilityProbe.ref) (serviceAvailabilityActorFactory.apply _).expects(*, service2, *, *).returns(service2AvailabilityProbe.ref) - val sut: TestActorRef[ServiceBrokerActor] = TestActorRef[ServiceBrokerActor](ServiceBrokerActor.props( - Set(connectionStrategyForService1, connectionStrategyForService2), serviceAvailabilityActorFactory)) + + val sut: TestActorRef[ServiceBrokerActor] = TestActorRef[ServiceBrokerActor]( + ServiceBrokerActor.props(Set(connectionStrategyForService1, connectionStrategyForService2), + serviceAvailabilityActorFactory) + ) service1AvailabilityProbe.expectMsg(Start) service2AvailabilityProbe.expectMsg(Start) sut ! ServiceBrokerActor.AllConnectionProvidersAvailable @@ -144,8 +166,11 @@ class ServiceBrokerActorSpec(_system: ActorSystem) extends TestKit(_system) with val service2AvailabilityProbe = TestProbe("Service2AvailabilityActor") (serviceAvailabilityActorFactory.apply _).expects(*, service1, *, *).returns(service1AvailabilityProbe.ref) (serviceAvailabilityActorFactory.apply _).expects(*, service2, *, *).returns(service2AvailabilityProbe.ref) - val sut: TestActorRef[ServiceBrokerActor] = TestActorRef[ServiceBrokerActor](ServiceBrokerActor.props( - Set(connectionStrategyForService1, connectionStrategyForService2), serviceAvailabilityActorFactory)) + + val sut: TestActorRef[ServiceBrokerActor] = TestActorRef[ServiceBrokerActor]( + ServiceBrokerActor.props(Set(connectionStrategyForService1, connectionStrategyForService2), + serviceAvailabilityActorFactory) + ) service1AvailabilityProbe.expectMsg(Start) service2AvailabilityProbe.expectMsg(Start) sut ! ServiceBrokerActor.AllConnectionProvidersAvailable diff --git a/client/src/test/scala/stormlantern/consul/client/ServiceBrokerSpec.scala b/client/src/test/scala/stormlantern/consul/client/ServiceBrokerTest.scala similarity index 71% rename from client/src/test/scala/stormlantern/consul/client/ServiceBrokerSpec.scala rename to client/src/test/scala/stormlantern/consul/client/ServiceBrokerTest.scala index 231d9e2..8170d59 100644 --- a/client/src/test/scala/stormlantern/consul/client/ServiceBrokerSpec.scala +++ b/client/src/test/scala/stormlantern/consul/client/ServiceBrokerTest.scala @@ -1,39 +1,26 @@ package stormlantern.consul.client -import org.apache.pekko.actor.{ ActorRef, ActorSystem } +import org.apache.pekko.actor.ActorRef import org.apache.pekko.actor.Status.Failure -import org.apache.pekko.testkit.{ ImplicitSender, TestKit } -import org.scalamock.scalatest.MockFactory -import org.scalatest.concurrent.ScalaFutures -import org.scalatest.{ BeforeAndAfterAll, FlatSpecLike, Matchers } import stormlantern.consul.client.dao.ConsulHttpClient import stormlantern.consul.client.discovery.ConnectionHolder -import stormlantern.consul.client.helpers.CallingThreadExecutionContext import stormlantern.consul.client.loadbalancers.LoadBalancerActor -import stormlantern.consul.client.util.Logging import scala.concurrent.Future -class ServiceBrokerSpec(_system: ActorSystem) extends TestKit(_system) with ImplicitSender with FlatSpecLike - with Matchers with ScalaFutures with BeforeAndAfterAll with MockFactory with Logging { - - implicit val ec = CallingThreadExecutionContext() - def this() = this(ActorSystem("ServiceBrokerSpec")) - - override def afterAll() { - TestKit.shutdownActorSystem(system) - } +class ServiceBrokerTest extends ClientSpec { trait TestScope { val connectionHolder: ConnectionHolder = mock[ConnectionHolder] - val httpClient: ConsulHttpClient = mock[ConsulHttpClient] - val loadBalancer: ActorRef = self + val httpClient: ConsulHttpClient = mock[ConsulHttpClient] + val loadBalancer: ActorRef = self } "The ServiceBroker" should "return a service connection when requested" in new TestScope { (connectionHolder.connection _).expects().returns(Future.successful(true)) (connectionHolder.loadBalancer _).expects().returns(loadBalancer) val sut = new ServiceBroker(self, httpClient) + val result: Future[Boolean] = sut.withService("service1") { service: Boolean => Future.successful(service) } @@ -49,6 +36,7 @@ class ServiceBrokerSpec(_system: ActorSystem) extends TestKit(_system) with Impl (connectionHolder.connection _).expects().returns(Future.successful(true)) (connectionHolder.loadBalancer _).expects().returns(loadBalancer) val sut = new ServiceBroker(self, httpClient) + val result: Future[Boolean] = sut.withService[Boolean, Boolean]("service1") { service: Boolean => throw new RuntimeException() } @@ -62,6 +50,7 @@ class ServiceBrokerSpec(_system: ActorSystem) extends TestKit(_system) with Impl it should "throw an error when an excpetion is returned" in new TestScope { val sut = new ServiceBroker(self, httpClient) + val result: Future[Boolean] = sut.withService("service1") { service: Boolean => Future.successful(service) } diff --git a/client/src/test/scala/stormlantern/consul/client/discovery/ServiceAvailabilityActorSpec.scala b/client/src/test/scala/stormlantern/consul/client/discovery/ServiceAvailabilityActorTest.scala similarity index 52% rename from client/src/test/scala/stormlantern/consul/client/discovery/ServiceAvailabilityActorSpec.scala rename to client/src/test/scala/stormlantern/consul/client/discovery/ServiceAvailabilityActorTest.scala index 926c1fc..0cdf410 100644 --- a/client/src/test/scala/stormlantern/consul/client/discovery/ServiceAvailabilityActorSpec.scala +++ b/client/src/test/scala/stormlantern/consul/client/discovery/ServiceAvailabilityActorTest.scala @@ -1,30 +1,25 @@ package stormlantern.consul.client.discovery -import org.apache.pekko.actor.ActorSystem -import org.apache.pekko.testkit.{ ImplicitSender, TestActorRef, TestKit } -import org.scalamock.scalatest.MockFactory -import org.scalatest.{ BeforeAndAfterAll, FlatSpecLike, Matchers } -import stormlantern.consul.client.dao.{ ConsulHttpClient, IndexedServiceInstances } +import org.apache.pekko.testkit.TestActorRef +import stormlantern.consul.client.ClientSpec +import stormlantern.consul.client.dao.{ConsulHttpClient, IndexedServiceInstances} import stormlantern.consul.client.discovery.ServiceAvailabilityActor.Start import stormlantern.consul.client.helpers.ModelHelpers -import stormlantern.consul.client.util.Logging import scala.concurrent.Future import scala.concurrent.duration._ -class ServiceAvailabilityActorSpec(_system: ActorSystem) extends TestKit(_system) with ImplicitSender with FlatSpecLike - with Matchers with BeforeAndAfterAll with MockFactory with Logging { - - def this() = this(ActorSystem("ServiceAvailabilityActorSpec")) - - override def afterAll() { - TestKit.shutdownActorSystem(system) - } +class ServiceAvailabilityActorTest extends ClientSpec { "The ServiceAvailabilityActor" should "receive one service update when there are no changes" in { val httpClient: ConsulHttpClient = mock[ConsulHttpClient] - val sut = TestActorRef(ServiceAvailabilityActor.props(httpClient, ServiceDefinition("bogus123", "bogus"), self, onlyHealthyServices = false)) - (httpClient.getService _).expects("bogus", None, Some(0L), Some("1s"), None).returns(Future.successful(IndexedServiceInstances(1, Set.empty))) + val sut = TestActorRef( + ServiceAvailabilityActor + .props(httpClient, ServiceDefinition("bogus123", "bogus"), self, onlyHealthyServices = false) + ) + (httpClient.getService _) + .expects("bogus", None, Some(0L), Some("1s"), None) + .returns(Future.successful(IndexedServiceInstances(1, Set.empty))) (httpClient.getService _).expects("bogus", None, Some(1L), Some("1s"), None).onCall { p => sut.stop() Future.successful(IndexedServiceInstances(1, Set.empty)) @@ -37,10 +32,17 @@ class ServiceAvailabilityActorSpec(_system: ActorSystem) extends TestKit(_system it should "receive two service updates when there is a change" in { val httpClient: ConsulHttpClient = mock[ConsulHttpClient] - lazy val sut = TestActorRef(ServiceAvailabilityActor.props(httpClient, ServiceDefinition("bogus123", "bogus"), self, onlyHealthyServices = false)) + lazy val sut = TestActorRef( + ServiceAvailabilityActor + .props(httpClient, ServiceDefinition("bogus123", "bogus"), self, onlyHealthyServices = false) + ) val service = ModelHelpers.createService("bogus123", "bogus") - (httpClient.getService _).expects("bogus", None, Some(0L), Some("1s"), None).returns(Future.successful(IndexedServiceInstances(1, Set.empty))) - (httpClient.getService _).expects("bogus", None, Some(1L), Some("1s"), None).returns(Future.successful(IndexedServiceInstances(2, Set(service)))) + (httpClient.getService _) + .expects("bogus", None, Some(0L), Some("1s"), None) + .returns(Future.successful(IndexedServiceInstances(1, Set.empty))) + (httpClient.getService _) + .expects("bogus", None, Some(1L), Some("1s"), None) + .returns(Future.successful(IndexedServiceInstances(2, Set(service)))) (httpClient.getService _).expects("bogus", None, Some(2L), Some("1s"), None).onCall { p => sut.stop() Future.successful(IndexedServiceInstances(2, Set(service))) @@ -54,11 +56,18 @@ class ServiceAvailabilityActorSpec(_system: ActorSystem) extends TestKit(_system it should "receive one service update when there are two with different tags" in { val httpClient: ConsulHttpClient = mock[ConsulHttpClient] - lazy val sut = TestActorRef(ServiceAvailabilityActor.props(httpClient, ServiceDefinition("bogus123", "bogus", Set("one", "two")), self, onlyHealthyServices = false)) + lazy val sut = TestActorRef( + ServiceAvailabilityActor + .props(httpClient, ServiceDefinition("bogus123", "bogus", Set("one", "two")), self, onlyHealthyServices = false) + ) val nonMatchingservice = ModelHelpers.createService("bogus123", "bogus", tags = Set("one")) - val matchingService = nonMatchingservice.copy(serviceTags = Set("one", "two")) - (httpClient.getService _).expects("bogus", Some("one"), Some(0L), Some("1s"), None).returns(Future.successful(IndexedServiceInstances(1, Set.empty))) - (httpClient.getService _).expects("bogus", Some("one"), Some(1L), Some("1s"), None).returns(Future.successful(IndexedServiceInstances(2, Set(nonMatchingservice, matchingService)))) + val matchingService = nonMatchingservice.copy(serviceTags = Set("one", "two")) + (httpClient.getService _) + .expects("bogus", Some("one"), Some(0L), Some("1s"), None) + .returns(Future.successful(IndexedServiceInstances(1, Set.empty))) + (httpClient.getService _) + .expects("bogus", Some("one"), Some(1L), Some("1s"), None) + .returns(Future.successful(IndexedServiceInstances(2, Set(nonMatchingservice, matchingService)))) (httpClient.getService _).expects("bogus", Some("one"), Some(2L), Some("1s"), None).onCall { p => sut.stop() Future.successful(IndexedServiceInstances(2, Set(nonMatchingservice, matchingService))) diff --git a/client/src/test/scala/stormlantern/consul/client/election/LeaderFollowerActorSpec.scala b/client/src/test/scala/stormlantern/consul/client/election/LeaderFollowerActorSpec.scala deleted file mode 100644 index 3b57f33..0000000 --- a/client/src/test/scala/stormlantern/consul/client/election/LeaderFollowerActorSpec.scala +++ /dev/null @@ -1,73 +0,0 @@ -package stormlantern.consul.client.election - -import java.util -import java.util.UUID - -import org.apache.pekko.actor.ActorSystem -import org.apache.pekko.testkit.{ TestActorRef, ImplicitSender, TestKit } -import org.scalamock.scalatest.MockFactory -import org.scalatest.{ BeforeAndAfterAll, Matchers, FlatSpecLike } -import stormlantern.consul.client.dao.{ BinaryData, KeyData, AcquireSession, ConsulHttpClient } -import stormlantern.consul.client.election.LeaderFollowerActor.Participate - -import scala.concurrent.Future - -class LeaderFollowerActorSpec(_system: ActorSystem) extends TestKit(_system) with ImplicitSender with FlatSpecLike - with Matchers with BeforeAndAfterAll with MockFactory { - - def this() = this(ActorSystem("LeaderFollowerActorSpec")) - - override def afterAll() { - TestKit.shutdownActorSystem(system) - } - - trait TestScope { - val sessionId: UUID = UUID.fromString("9A3BB9C-E2E7-43DF-BFD5-845417146552") - val key = "path/to/our/key" - val host = "myhost.mynetwork.net" - val port = 1337 - val consulHttpClient: ConsulHttpClient = mock[ConsulHttpClient] - val leaderInfoBytes: Array[Byte] = s"""{"host":"$host","port":$port}""".getBytes("UTF-8") - } - - "The LeaderFollowerActor" should "participate in an election, win, watch for changes and participate again when session is lost" in new TestScope { - val sut = TestActorRef(LeaderFollowerActor.props(consulHttpClient, sessionId, key, host, port)) - (consulHttpClient.putKeyValuePair _).expects(where { (k, lib, op) => - k == key && util.Arrays.equals(lib, leaderInfoBytes) && op.contains(AcquireSession(sessionId)) - }).returns(Future.successful(true)) - (consulHttpClient.getKeyValuePair _).expects(key, Some(0L), Some("1s"), false, false).returns { - Future.successful(Seq(KeyData(key, 1, 1, 1, 0, BinaryData(leaderInfoBytes), Some(sessionId)))) - } - (consulHttpClient.getKeyValuePair _).expects(key, Some(1L), Some("1s"), false, false).returns { - Future.successful(Seq(KeyData(key, 1, 2, 1, 0, BinaryData(leaderInfoBytes), None))) - } - (consulHttpClient.putKeyValuePair _).expects(where { (k, lib, op) => - k == key && util.Arrays.equals(lib, leaderInfoBytes) && op.contains(AcquireSession(sessionId)) - }).onCall { p => - sut.stop() - Future.successful(false) - } - sut ! Participate - } - - it should "participate in an election, lose, watch for changes and participate again when session is lost" in new TestScope { - val otherSessionId: UUID = UUID.fromString("9A3BB9C-E2E7-43DF-BFD5-845417146553") - val sut = TestActorRef(LeaderFollowerActor.props(consulHttpClient, sessionId, key, host, port)) - (consulHttpClient.putKeyValuePair _).expects(where { (k, lib, op) => - k == key && util.Arrays.equals(lib, leaderInfoBytes) && op.contains(AcquireSession(sessionId)) - }).returns(Future.successful(false)) - (consulHttpClient.getKeyValuePair _).expects(key, Some(0L), Some("1s"), false, false).returns { - Future.successful(Seq(KeyData(key, 1, 1, 1, 0, BinaryData(leaderInfoBytes), Some(otherSessionId)))) - } - (consulHttpClient.getKeyValuePair _).expects(key, Some(1L), Some("1s"), false, false).returns { - Future.successful(Seq(KeyData(key, 1, 2, 1, 0, BinaryData(leaderInfoBytes), None))) - } - (consulHttpClient.putKeyValuePair _).expects(where { (k, lib, op) => - k == key && util.Arrays.equals(lib, leaderInfoBytes) && op.contains(AcquireSession(sessionId)) - }).onCall { p => - sut.stop() - Future.successful(true) - } - sut ! Participate - } -} diff --git a/client/src/test/scala/stormlantern/consul/client/election/LeaderFollowerActorTest.scala b/client/src/test/scala/stormlantern/consul/client/election/LeaderFollowerActorTest.scala new file mode 100644 index 0000000..73d5408 --- /dev/null +++ b/client/src/test/scala/stormlantern/consul/client/election/LeaderFollowerActorTest.scala @@ -0,0 +1,71 @@ +package stormlantern.consul.client.election + +import org.apache.pekko.testkit.TestActorRef +import stormlantern.consul.client.ClientSpec +import stormlantern.consul.client.dao.{AcquireSession, BinaryData, ConsulHttpClient, KeyData} +import stormlantern.consul.client.election.LeaderFollowerActor.Participate + +import java.util +import java.util.UUID +import scala.concurrent.Future + +class LeaderFollowerActorTest extends ClientSpec { + + trait TestScope { + val sessionId: UUID = UUID.fromString("9A3BB9C-E2E7-43DF-BFD5-845417146552") + val key = "path/to/our/key" + val host = "myhost.mynetwork.net" + val port = 1337 + val consulHttpClient: ConsulHttpClient = mock[ConsulHttpClient] + val leaderInfoBytes: Array[Byte] = s"""{"host":"$host","port":$port}""".getBytes("UTF-8") + } + + "The LeaderFollowerActor" should "participate in an election, win, watch for changes and participate again when session is lost" in new TestScope { + val sut = TestActorRef(LeaderFollowerActor.props(consulHttpClient, sessionId, key, host, port)) + (consulHttpClient.putKeyValuePair _) + .expects(where { (k, lib, op) => + k == key && util.Arrays.equals(lib, leaderInfoBytes) && op.contains(AcquireSession(sessionId)) + }) + .returns(Future.successful(true)) + (consulHttpClient.getKeyValuePair _).expects(key, Some(0L), Some("1s"), false, false).returns { + Future.successful(Seq(KeyData(key, 1, 1, 1, 0, BinaryData(leaderInfoBytes), Some(sessionId)))) + } + (consulHttpClient.getKeyValuePair _).expects(key, Some(1L), Some("1s"), false, false).returns { + Future.successful(Seq(KeyData(key, 1, 2, 1, 0, BinaryData(leaderInfoBytes), None))) + } + (consulHttpClient.putKeyValuePair _) + .expects(where { (k, lib, op) => + k == key && util.Arrays.equals(lib, leaderInfoBytes) && op.contains(AcquireSession(sessionId)) + }) + .onCall { p => + sut.stop() + Future.successful(false) + } + sut ! Participate + } + + it should "participate in an election, lose, watch for changes and participate again when session is lost" in new TestScope { + val otherSessionId: UUID = UUID.fromString("9A3BB9C-E2E7-43DF-BFD5-845417146553") + val sut = TestActorRef(LeaderFollowerActor.props(consulHttpClient, sessionId, key, host, port)) + (consulHttpClient.putKeyValuePair _) + .expects(where { (k, lib, op) => + k == key && util.Arrays.equals(lib, leaderInfoBytes) && op.contains(AcquireSession(sessionId)) + }) + .returns(Future.successful(false)) + (consulHttpClient.getKeyValuePair _).expects(key, Some(0L), Some("1s"), false, false).returns { + Future.successful(Seq(KeyData(key, 1, 1, 1, 0, BinaryData(leaderInfoBytes), Some(otherSessionId)))) + } + (consulHttpClient.getKeyValuePair _).expects(key, Some(1L), Some("1s"), false, false).returns { + Future.successful(Seq(KeyData(key, 1, 2, 1, 0, BinaryData(leaderInfoBytes), None))) + } + (consulHttpClient.putKeyValuePair _) + .expects(where { (k, lib, op) => + k == key && util.Arrays.equals(lib, leaderInfoBytes) && op.contains(AcquireSession(sessionId)) + }) + .onCall { p => + sut.stop() + Future.successful(true) + } + sut ! Participate + } +} diff --git a/client/src/test/scala/stormlantern/consul/client/loadbalancers/LoadBalancerActorSpec.scala b/client/src/test/scala/stormlantern/consul/client/loadbalancers/LoadBalancerActorTest.scala similarity index 81% rename from client/src/test/scala/stormlantern/consul/client/loadbalancers/LoadBalancerActorSpec.scala rename to client/src/test/scala/stormlantern/consul/client/loadbalancers/LoadBalancerActorTest.scala index 2d66d7f..0d7c7f2 100644 --- a/client/src/test/scala/stormlantern/consul/client/loadbalancers/LoadBalancerActorSpec.scala +++ b/client/src/test/scala/stormlantern/consul/client/loadbalancers/LoadBalancerActorTest.scala @@ -1,29 +1,18 @@ package stormlantern.consul.client.loadbalancers -import org.apache.pekko.actor.ActorSystem import org.apache.pekko.actor.Status.Failure -import org.apache.pekko.testkit.{ ImplicitSender, TestActorRef, TestKit } -import org.scalamock.scalatest.MockFactory -import org.scalatest.{ BeforeAndAfterAll, FlatSpecLike, Matchers } -import stormlantern.consul.client.ServiceUnavailableException -import stormlantern.consul.client.discovery.{ ConnectionHolder, ConnectionProvider } -import stormlantern.consul.client.util.Logging +import org.apache.pekko.testkit.TestActorRef +import stormlantern.consul.client.discovery.{ConnectionHolder, ConnectionProvider} +import stormlantern.consul.client.{ClientSpec, ServiceUnavailableException} import scala.concurrent.Future -class LoadBalancerActorSpec(_system: ActorSystem) extends TestKit(_system) with ImplicitSender with FlatSpecLike - with Matchers with BeforeAndAfterAll with MockFactory with Logging { - - def this() = this(ActorSystem("LoadBalancerActorSpec")) - - override def afterAll() { - TestKit.shutdownActorSystem(system) - } +class LoadBalancerActorTest extends ClientSpec { trait TestScope { - val connectionHolder = mock[ConnectionHolder] + val connectionHolder = mock[ConnectionHolder] val connectionProvider = mock[ConnectionProvider] - val loadBalancer = mock[LoadBalancer] + val loadBalancer = mock[LoadBalancer] } "The LoadBalancerActor" should "hand out a connection holder when requested" in new TestScope { @@ -39,7 +28,7 @@ class LoadBalancerActorSpec(_system: ActorSystem) extends TestKit(_system) with } it should "return an error when a connectionprovider fails to provide a connection" in new TestScope { - val instanceKey = "instanceKey" + val instanceKey = "instanceKey" val expectedException = new ServiceUnavailableException("service1") (loadBalancer.selectConnection _).expects().returns(Some(instanceKey)) val sut = TestActorRef(new LoadBalancerActor(loadBalancer, "service1")) diff --git a/client/src/test/scala/stormlantern/consul/client/loadbalancers/RoundRobinLoadBalancerSpec.scala b/client/src/test/scala/stormlantern/consul/client/loadbalancers/RoundRobinLoadBalancerTest.scala similarity index 81% rename from client/src/test/scala/stormlantern/consul/client/loadbalancers/RoundRobinLoadBalancerSpec.scala rename to client/src/test/scala/stormlantern/consul/client/loadbalancers/RoundRobinLoadBalancerTest.scala index 652d2dc..2d7af41 100644 --- a/client/src/test/scala/stormlantern/consul/client/loadbalancers/RoundRobinLoadBalancerSpec.scala +++ b/client/src/test/scala/stormlantern/consul/client/loadbalancers/RoundRobinLoadBalancerTest.scala @@ -1,8 +1,9 @@ package stormlantern.consul.client.loadbalancers -import org.scalatest.{ Matchers, FlatSpecLike } +import org.scalatest.flatspec.AnyFlatSpecLike +import org.scalatest.matchers.should.Matchers -class RoundRobinLoadBalancerSpec extends FlatSpecLike with Matchers { +class RoundRobinLoadBalancerTest extends AnyFlatSpecLike with Matchers { "The RoundRobinLoadBalancer" should "select a connection" in { val sut = new RoundRobinLoadBalancer From 6f3f92f57c853c1d08546170f006c6960faa3549 Mon Sep 17 00:00:00 2001 From: Leonard Wolters Date: Thu, 2 Nov 2023 19:19:00 +0100 Subject: [PATCH 24/33] All tests are passing, working on IT tests now --- .../src/it/scala/stormlantern/consul/client/ClientITSpec.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/it/scala/stormlantern/consul/client/ClientITSpec.scala b/client/src/it/scala/stormlantern/consul/client/ClientITSpec.scala index a6c213d..9d71ff6 100644 --- a/client/src/it/scala/stormlantern/consul/client/ClientITSpec.scala +++ b/client/src/it/scala/stormlantern/consul/client/ClientITSpec.scala @@ -10,7 +10,7 @@ import org.scalatest.matchers.should.Matchers import scala.concurrent.ExecutionContext abstract class ClientITSpec(val config: Config = ConfigFactory.load()) - extends TestKit(ActorSystem("TestSystem", config.getConfig("crobox.clickhouse.client"))) + extends TestKit(ActorSystem("TestSystem", config)) with AnyFlatSpecLike with Matchers with ScalaFutures From a99463745dc4f20d5398704dd777bd0039960aa7 Mon Sep 17 00:00:00 2001 From: Leonard Wolters Date: Thu, 2 Nov 2023 19:21:34 +0100 Subject: [PATCH 25/33] IT tests are passing --- project/Dependencies.scala | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/project/Dependencies.scala b/project/Dependencies.scala index b14ac54..67b4770 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -1,6 +1,4 @@ - - object Dependencies { - val PekkoVersion = "1.0.0" + val PekkoVersion = "1.0.1" val PekkoHttpVersion = "1.0.0" } From ba1f56fb7fcf898ceaa5c97b8b3029f0a4773b6d Mon Sep 17 00:00:00 2001 From: Leonard Wolters Date: Thu, 2 Nov 2023 19:30:13 +0100 Subject: [PATCH 26/33] IT tests are passing --- .../consul/client/dao/akka/AkkaHttpConsulClient.scala | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala b/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala index 1d46745..50f97c6 100644 --- a/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala +++ b/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala @@ -183,7 +183,7 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) validator: HttpResponse => Boolean = (in) => in.status.isSuccess() ): Future[ConsulResponse] = { - def validStatus(response: HttpResponse) = + def validStatus(response: HttpResponse): Future[HttpResponse] = if (validator(response)) { Future successful response } else { @@ -196,7 +196,7 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) // Consul does not return the Charset with the Response Content Type, so just MediaType comparison // Furthermore, when an error is returned the content type is text/plain, thank you HashiCorp ... // ///////////////////// - def validContenType(resp: HttpResponse) = { + def validContentType(resp: HttpResponse): Future[HttpResponse] = { val expected = resp.status match { case st if st.isSuccess() => expectedMediaType case st if st.isFailure() => TextMediaType @@ -250,5 +250,4 @@ case class ConsulException(message: String, response: HttpResponse, status: Opti object ConsulException { def apply(status: StatusCode, msg: String) = new ConsulException(msg, null, Option(status)) // I feel dirty after this def apply(msg: String) = new ConsulException(msg, null) // I feel dirty after this - } From bef40620473653bedd1b6f3f36dd5b5635769742 Mon Sep 17 00:00:00 2001 From: Leonard Wolters Date: Thu, 2 Nov 2023 19:45:35 +0100 Subject: [PATCH 27/33] Fixed logging --- client/src/it/resources/logback-test.xml | 14 -------------- .../consul/client/dao/AkkaHttpConsulClientIT.scala | 1 - .../client/dao/akka/AkkaHttpConsulClient.scala | 11 ++++++----- 3 files changed, 6 insertions(+), 20 deletions(-) delete mode 100644 client/src/it/resources/logback-test.xml diff --git a/client/src/it/resources/logback-test.xml b/client/src/it/resources/logback-test.xml deleted file mode 100644 index 6a1caa1..0000000 --- a/client/src/it/resources/logback-test.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n - - - - - - - \ No newline at end of file diff --git a/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIT.scala b/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIT.scala index 2ccabea..828bb2d 100644 --- a/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIT.scala +++ b/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIT.scala @@ -8,7 +8,6 @@ import java.net.URL import java.util.UUID class AkkaHttpConsulClientIT extends ClientITSpec with RetryPolicy with Logging { - val subject = new AkkaHttpConsulClient(new URL(s"http://$host:$port")) "The AkkaHttpConsulClient" should "retrieve a single Consul service from a freshly started Consul instance" in { diff --git a/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala b/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala index 50f97c6..7a4fa10 100644 --- a/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala +++ b/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala @@ -204,11 +204,11 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) } if (resp.entity.contentType.mediaType == expected) { - Future successful resp + Future.successful(resp) } else { - Future failed ConsulException( - resp.status, - s"Unexpected content type: ${resp.entity.contentType}, expected $expectedMediaType" + Future.failed( + ConsulException(resp.status, + s"Unexpected content type: ${resp.entity.contentType}, expected $expectedMediaType") ) } } @@ -219,8 +219,9 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) // make the call Http() .singleRequest(request) + .map(response => { logger.info("RESPONSE: " + response); response }) .flatMap(validStatus) - .flatMap(validContenType) + .flatMap(validContentType) .flatMap { response: HttpResponse => parseBody(response).map { body: String => ConsulResponse(response.status, response.headers, body) From 547c0f78930ab6dd3e1d79e26b69d15ec9efe103 Mon Sep 17 00:00:00 2001 From: Leonard Wolters Date: Thu, 2 Nov 2023 20:17:46 +0100 Subject: [PATCH 28/33] Fixing IT tests --- .../dao/akka/AkkaHttpConsulClient.scala | 46 ++++++++----------- .../consul/client/dao/akka/package.scala | 16 +++++++ 2 files changed, 34 insertions(+), 28 deletions(-) create mode 100644 client/src/main/scala/stormlantern/consul/client/dao/akka/package.scala diff --git a/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala b/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala index 7a4fa10..6ab9f34 100644 --- a/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala +++ b/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala @@ -142,10 +142,12 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) retry[Boolean]() { getResponse(request, JsonMediaType, validator).flatMap { - case ConsulResponse(OK, _, body) => Future successful Option(body.toBoolean).getOrElse(false) - case ConsulResponse(InternalServerError, _, "Invalid session") => Future successful false + case ConsulResponse(OK, _, body) => + Future.successful(Option(body.toBoolean).getOrElse(false)) + case ConsulResponse(InternalServerError, _, "Invalid session") => + Future.successful(false) case ConsulResponse(status, _, body) => - Future failed new Exception(s"Request returned status code $status - $body") + Future.failed(new Exception(s"Request returned status code $status - $body")) } } } @@ -180,22 +182,18 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) private def getResponse[T, U]( request: HttpRequest, expectedMediaType: MediaType, - validator: HttpResponse => Boolean = (in) => in.status.isSuccess() + validator: HttpResponse => Boolean = in => in.status.isSuccess() ): Future[ConsulResponse] = { def validStatus(response: HttpResponse): Future[HttpResponse] = if (validator(response)) { - Future successful response + Future.successful(response) } else { parseBody(response).flatMap { body => - Future failed ConsulException(s"Bad status code: ${response.status.intValue()} with body $body") + Future.failed(ConsulException(s"Bad status code: ${response.status.intValue()} with body $body")) } } - // - // Consul does not return the Charset with the Response Content Type, so just MediaType comparison - // Furthermore, when an error is returned the content type is text/plain, thank you HashiCorp ... - // ///////////////////// def validContentType(resp: HttpResponse): Future[HttpResponse] = { val expected = resp.status match { case st if st.isSuccess() => expectedMediaType @@ -213,19 +211,22 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) } } - def parseBody(response: HttpResponse): Future[String] = - response.entity.dataBytes.runFold(ByteString(""))(_ ++ _).map(_.utf8String) + def parseBody(response: HttpResponse): Future[String] = { + val body = response.entity.dataBytes.runFold(ByteString(""))(_ ++ _).map(_.utf8String) + response.entity.contentType match { + case ContentTypes.`application/json` => body.map(_.parseJson.toString()) + case _ => body + } + } // make the call Http() .singleRequest(request) - .map(response => { logger.info("RESPONSE: " + response); response }) +// .map(response => { logger.info("RESPONSE: " + response); response }) .flatMap(validStatus) .flatMap(validContentType) .flatMap { response: HttpResponse => - parseBody(response).map { body: String => - ConsulResponse(response.status, response.headers, body) - } + parseBody(response).map(body => ConsulResponse(response.status, response.headers, body)) } } @@ -240,15 +241,4 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) } } -// -// Internal Objects -// ////////////////////////// -case class ConsulResponse(status: StatusCode, headers: Seq[HttpHeader], body: String) - -case class ConsulException(message: String, response: HttpResponse, status: Option[StatusCode] = None) - extends Exception(message) - -object ConsulException { - def apply(status: StatusCode, msg: String) = new ConsulException(msg, null, Option(status)) // I feel dirty after this - def apply(msg: String) = new ConsulException(msg, null) // I feel dirty after this -} +object AkkaHttpConsulClient {} diff --git a/client/src/main/scala/stormlantern/consul/client/dao/akka/package.scala b/client/src/main/scala/stormlantern/consul/client/dao/akka/package.scala new file mode 100644 index 0000000..489eea3 --- /dev/null +++ b/client/src/main/scala/stormlantern/consul/client/dao/akka/package.scala @@ -0,0 +1,16 @@ +package stormlantern.consul.client.dao + +import org.apache.pekko.http.scaladsl.model.{HttpHeader, HttpResponse, StatusCode} + +package object akka { + case class ConsulResponse(status: StatusCode, headers: Seq[HttpHeader], body: String) + + case class ConsulException(message: String, response: HttpResponse, status: Option[StatusCode] = None) + extends Exception(message) + + object ConsulException { + def apply(status: StatusCode, msg: String) = new ConsulException(msg, null, Option(status)) + + def apply(msg: String) = new ConsulException(msg, null) + } +} From 5a4d45516ae5eab3e9b0484dea784fa2ae77dfe3 Mon Sep 17 00:00:00 2001 From: Leonard Wolters Date: Thu, 2 Nov 2023 20:59:38 +0100 Subject: [PATCH 29/33] Fixing IT tests --- .../dao/akka/AkkaHttpConsulClient.scala | 95 +++++++++---------- .../consul/client/dao/akka/package.scala | 4 +- 2 files changed, 46 insertions(+), 53 deletions(-) diff --git a/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala b/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala index 6ab9f34..8736924 100644 --- a/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala +++ b/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala @@ -41,7 +41,7 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) val request: HttpRequest = HttpRequest(HttpMethods.GET).withUri(s"$host/v1/catalog/service/$service?$parameters") retry[IndexedServiceInstances]() { - getResponse(request, JsonMediaType).flatMap { response => + getResponse(request).flatMap { response => validIndex(response).map { idx => val services = response.body.parseJson.convertTo[Option[Set[ServiceInstance]]] IndexedServiceInstances(idx, services.getOrElse(Set.empty[ServiceInstance])) @@ -65,7 +65,7 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) val request: HttpRequest = HttpRequest(HttpMethods.GET).withUri(s"$host/v1/health/service/$service?$parameters") retry[IndexedServiceInstances]() { - getResponse(request, JsonMediaType).flatMap { response => + getResponse(request).flatMap { response => validIndex(response).map { idx => val services = response.body.parseJson.convertTo[Option[Set[HealthServiceInstance]]] IndexedServiceInstances(idx, services.getOrElse(Set.empty[HealthServiceInstance]).map(_.asServiceInstance)) @@ -77,19 +77,15 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) def putService(registration: ServiceRegistration): Future[String] = { val request = HttpRequest(HttpMethods.PUT) .withUri(s"$host/v1/agent/service/register") - .withEntity(registration.toJson.asJsObject().toString.getBytes) + .withEntity(registration.toJson.compactPrint.getBytes) - retry[ConsulResponse]() { - getResponse(request, TextMediaType) - }.map(r => registration.id.getOrElse(registration.name)) + retry[ConsulResponse]()(getResponse(request)).map(_ => registration.id.getOrElse(registration.name)) } def deleteService(serviceId: String): Future[Unit] = { val request = HttpRequest(HttpMethods.PUT).withUri(s"$host/v1/agent/service/deregister/$serviceId") - retry[ConsulResponse]() { - getResponse(request, TextMediaType) - }.map(r => ()) + retry[ConsulResponse]()(getResponse(request)).map(r => ()) } // @@ -98,16 +94,14 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) def putSession(sessionCreation: Option[SessionCreation], dataCenter: Option[String]): Future[UUID] = { val dcParameter = dataCenter.map(dc => s"dc=$dc") val parameters = Seq(dcParameter).flatten.mkString("&") - val request = sessionCreation.map(_.toJson.asJsObject.toString.getBytes) match { + val request = sessionCreation.map(_.toJson.compactPrint.getBytes) match { case None => HttpRequest(HttpMethods.PUT).withUri(s"$host/v1/session/create?$parameters") case Some(entity) => HttpRequest(HttpMethods.PUT).withUri(s"$host/v1/session/create?$parameters").withEntity(entity) } retry[UUID]() { - getResponse(request, JsonMediaType).map { response => - response.body.parseJson.asJsObject.fields("ID").convertTo[UUID] - } + getResponse(request).map(response => response.body.parseJson.asJsObject.fields("ID").convertTo[UUID]) } } @@ -117,9 +111,30 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) val parameters = Seq(dcParameter, indexParameter).flatten.mkString("&") val request = HttpRequest(HttpMethods.GET).withUri(s"$host/v1/session/info/$sessionId?$parameters") + def toSession(fields: Map[String, JsValue]) = + SessionInfo( + lockDelay = fields("LockDelay").convertTo[Long], + checks = fields + .get("Checks") + .map { + case JsArray(elements) => elements.map(_.convertTo[String]).toSet + case _ => Set.empty[String] + } + .getOrElse(Set.empty[String]), + node = fields("Node").convertTo[String], + id = fields("ID").convertTo[UUID], + createIndex = fields("CreateIndex").convertTo[Long], + name = fields.get("Name").map(_.convertTo[String]), + behavior = fields("Behavior").convertTo[String], + TTL = fields("TTL").convertTo[String] + ) + retry[Option[SessionInfo]]() { - getResponse(request, JsonMediaType).map { response => - response.body.parseJson.convertTo[Option[Set[SessionInfo]]].getOrElse(Set.empty).headOption + getResponse(request).map { response => + response.body.parseJson match { + case JsArray(elements) => elements.map(element => toSession(element.asJsObject.fields)).headOption + case other => Option(toSession(other.asJsObject.fields)) + } } } } @@ -141,12 +156,12 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) response.status.isSuccess() || response.status == InternalServerError retry[Boolean]() { - getResponse(request, JsonMediaType, validator).flatMap { - case ConsulResponse(OK, _, body) => - Future.successful(Option(body.toBoolean).getOrElse(false)) - case ConsulResponse(InternalServerError, _, "Invalid session") => + getResponse(request, validator).flatMap { + case ConsulResponse(OK, MediaTypes.`application/json`, _, body) => + Future.successful(Option(body.parseJson.convertTo[Boolean]).getOrElse(false)) + case ConsulResponse(InternalServerError, _, _, "Invalid session") => Future.successful(false) - case ConsulResponse(status, _, body) => + case ConsulResponse(status, _, _, body) => Future.failed(new Exception(s"Request returned status code $status - $body")) } } @@ -166,7 +181,7 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) val request = HttpRequest(HttpMethods.GET).withUri(s"$host/v1/kv/$key?$parameters") retry[Seq[KeyData]]() { - getResponse(request, JsonMediaType, _ => true).map { response => + getResponse(request, _ => true).map { response => if (response.status == StatusCodes.NotFound) { Seq.empty } else { @@ -181,7 +196,6 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) // ////////////////////////// private def getResponse[T, U]( request: HttpRequest, - expectedMediaType: MediaType, validator: HttpResponse => Boolean = in => in.status.isSuccess() ): Future[ConsulResponse] = { @@ -194,39 +208,18 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) } } - def validContentType(resp: HttpResponse): Future[HttpResponse] = { - val expected = resp.status match { - case st if st.isSuccess() => expectedMediaType - case st if st.isFailure() => TextMediaType - case st if st.isRedirection() => TextMediaType // this is a guess - } - - if (resp.entity.contentType.mediaType == expected) { - Future.successful(resp) - } else { - Future.failed( - ConsulException(resp.status, - s"Unexpected content type: ${resp.entity.contentType}, expected $expectedMediaType") - ) - } - } - - def parseBody(response: HttpResponse): Future[String] = { - val body = response.entity.dataBytes.runFold(ByteString(""))(_ ++ _).map(_.utf8String) - response.entity.contentType match { - case ContentTypes.`application/json` => body.map(_.parseJson.toString()) - case _ => body - } - } + def parseBody(response: HttpResponse): Future[String] = + response.entity.dataBytes.runFold(ByteString(""))(_ ++ _).map(_.utf8String) // make the call Http() .singleRequest(request) -// .map(response => { logger.info("RESPONSE: " + response); response }) .flatMap(validStatus) - .flatMap(validContentType) .flatMap { response: HttpResponse => - parseBody(response).map(body => ConsulResponse(response.status, response.headers, body)) + parseBody(response).map(body => { +// logger.info("BODY: " + body) + ConsulResponse(response.status, response.entity.contentType.mediaType, response.headers, body) + }) } } @@ -235,8 +228,8 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) case None => Future failed ConsulException("X-Consul-Index header not found") case Some(hdr) => Try(hdr.value.toLong) match { - case Success(idx) => Future successful idx - case Failure(ex) => Future failed ConsulException("X-Consul-Index header was not numeric") + case Success(idx) => Future.successful(idx) + case Failure(ex) => Future.failed(ConsulException("X-Consul-Index header was not numeric")) } } } diff --git a/client/src/main/scala/stormlantern/consul/client/dao/akka/package.scala b/client/src/main/scala/stormlantern/consul/client/dao/akka/package.scala index 489eea3..59dfa99 100644 --- a/client/src/main/scala/stormlantern/consul/client/dao/akka/package.scala +++ b/client/src/main/scala/stormlantern/consul/client/dao/akka/package.scala @@ -1,9 +1,9 @@ package stormlantern.consul.client.dao -import org.apache.pekko.http.scaladsl.model.{HttpHeader, HttpResponse, StatusCode} +import org.apache.pekko.http.scaladsl.model.{HttpHeader, HttpResponse, MediaType, StatusCode} package object akka { - case class ConsulResponse(status: StatusCode, headers: Seq[HttpHeader], body: String) + case class ConsulResponse(status: StatusCode, contentType: MediaType, headers: Seq[HttpHeader], body: String) {} case class ConsulException(message: String, response: HttpResponse, status: Option[StatusCode] = None) extends Exception(message) From d4c5d54d3dd24c109c30972efa1f49e60d393532 Mon Sep 17 00:00:00 2001 From: Leonard Wolters Date: Thu, 2 Nov 2023 21:22:03 +0100 Subject: [PATCH 30/33] Fixing IT tests --- .../client/dao/AkkaHttpConsulClientIT.scala | 25 +++++++++++-------- .../dao/akka/AkkaHttpConsulClient.scala | 7 ++++-- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIT.scala b/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIT.scala index 828bb2d..7eaf4bb 100644 --- a/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIT.scala +++ b/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIT.scala @@ -6,8 +6,10 @@ import stormlantern.consul.client.util.{Logging, RetryPolicy} import java.net.URL import java.util.UUID +import scala.util.Random class AkkaHttpConsulClientIT extends ClientITSpec with RetryPolicy with Logging { + val rnd = new Random() val subject = new AkkaHttpConsulClient(new URL(s"http://$host:$port")) "The AkkaHttpConsulClient" should "retrieve a single Consul service from a freshly started Consul instance" in { @@ -132,31 +134,34 @@ class AkkaHttpConsulClientIT extends ClientITSpec with RetryPolicy with Logging it should "get a session lock on a key/value pair and fail to get a second lock" in { val id: UUID = subject.putSession(Some(SessionCreation(name = Some("MySession")))).futureValue val payload = """ { "name" : "test" } """.getBytes("UTF-8") - subject.putKeyValuePair("my/key", payload, Some(AcquireSession(id))).futureValue should be(true) - subject.putKeyValuePair("my/key", payload, Some(AcquireSession(id))).futureValue should be(false) - subject.putKeyValuePair("my/key", payload, Some(ReleaseSession(id))).futureValue should be(true) + val key = "my/key" + rnd.nextInt(100000) + subject.putKeyValuePair(key, payload, Some(AcquireSession(id))).futureValue should be(true) + subject.putKeyValuePair(key, payload, Some(AcquireSession(id))).futureValue should be(false) + subject.putKeyValuePair(key, payload, Some(ReleaseSession(id))).futureValue should be(true) } it should "get a session lock on a key/value pair and get a second lock after release" in { val id: UUID = subject.putSession(Some(SessionCreation(name = Some("MySession")))).futureValue val payload = """ { "name" : "test" } """.getBytes("UTF-8") - subject.putKeyValuePair("my/key", payload, Some(AcquireSession(id))).futureValue should be(true) - subject.putKeyValuePair("my/key", payload, Some(ReleaseSession(id))).futureValue should be(true) - subject.putKeyValuePair("my/key", payload, Some(AcquireSession(id))).futureValue should be(true) - subject.putKeyValuePair("my/key", payload, Some(ReleaseSession(id))).futureValue should be(true) + val key = "my/key" + rnd.nextInt(100000) + subject.putKeyValuePair(key, payload, Some(AcquireSession(id))).futureValue should be(true) + subject.putKeyValuePair(key, payload, Some(ReleaseSession(id))).futureValue should be(true) + subject.putKeyValuePair(key, payload, Some(AcquireSession(id))).futureValue should be(true) + subject.putKeyValuePair(key, payload, Some(ReleaseSession(id))).futureValue should be(true) } it should "write a key/value pair and read it back" in { val payload = """ { "name" : "test" } """.getBytes("UTF-8") - subject.putKeyValuePair("my/key", payload).futureValue should be(true) - val keyDataSeq = subject.getKeyValuePair("my/key").futureValue + val key = "my/key" + rnd.nextInt(100000) + subject.putKeyValuePair(key, payload).futureValue should be(true) + val keyDataSeq = subject.getKeyValuePair(key).futureValue keyDataSeq.head.value should equal(BinaryData(payload)) } it should "fail when aquiring a lock on a key with a non-existent session" in { val payload = """ { "name" : "test" } """.getBytes("UTF-8") val nonExistentSessionId = UUID.fromString("9A3BB9C-E2E7-43DF-BFD5-845417146552") - val result = subject + subject .putKeyValuePair("my/key", payload, Some(AcquireSession(nonExistentSessionId))) .futureValue should be(false) } diff --git a/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala b/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala index 8736924..76bb88c 100644 --- a/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala +++ b/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala @@ -157,8 +157,11 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) retry[Boolean]() { getResponse(request, validator).flatMap { - case ConsulResponse(OK, MediaTypes.`application/json`, _, body) => - Future.successful(Option(body.parseJson.convertTo[Boolean]).getOrElse(false)) + case ConsulResponse(OK, mediaType, _, body) => + mediaType match { + case JsonMediaType => Future.successful(Option(body.parseJson.convertTo[Boolean]).getOrElse(false)) + case TextMediaType => Future.successful(Option(body.toBoolean).getOrElse(false)) + } case ConsulResponse(InternalServerError, _, _, "Invalid session") => Future.successful(false) case ConsulResponse(status, _, _, body) => From 5cfa21fafaf75f1c1906e1122f4313881884520f Mon Sep 17 00:00:00 2001 From: Leonard Wolters Date: Thu, 2 Nov 2023 21:25:33 +0100 Subject: [PATCH 31/33] Almost there, fixing last value --- .../consul/client/dao/AkkaHttpConsulClientIT.scala | 3 ++- .../consul/client/dao/akka/AkkaHttpConsulClient.scala | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIT.scala b/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIT.scala index 7eaf4bb..57f6355 100644 --- a/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIT.scala +++ b/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIT.scala @@ -161,8 +161,9 @@ class AkkaHttpConsulClientIT extends ClientITSpec with RetryPolicy with Logging it should "fail when aquiring a lock on a key with a non-existent session" in { val payload = """ { "name" : "test" } """.getBytes("UTF-8") val nonExistentSessionId = UUID.fromString("9A3BB9C-E2E7-43DF-BFD5-845417146552") + val key = "my/key" + rnd.nextInt(100000) subject - .putKeyValuePair("my/key", payload, Some(AcquireSession(nonExistentSessionId))) + .putKeyValuePair(key, payload, Some(AcquireSession(nonExistentSessionId))) .futureValue should be(false) } } diff --git a/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala b/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala index 76bb88c..74a2c2c 100644 --- a/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala +++ b/client/src/main/scala/stormlantern/consul/client/dao/akka/AkkaHttpConsulClient.scala @@ -162,7 +162,7 @@ class AkkaHttpConsulClient(host: URL)(implicit actorSystem: ActorSystem) case JsonMediaType => Future.successful(Option(body.parseJson.convertTo[Boolean]).getOrElse(false)) case TextMediaType => Future.successful(Option(body.toBoolean).getOrElse(false)) } - case ConsulResponse(InternalServerError, _, _, "Invalid session") => + case ConsulResponse(InternalServerError, _, _, body) if body.startsWith("invalid session") => Future.successful(false) case ConsulResponse(status, _, _, body) => Future.failed(new Exception(s"Request returned status code $status - $body")) From 4fa09245ac5ff1e072b6c4e5d0f9fb89d81126d3 Mon Sep 17 00:00:00 2001 From: Leonard Wolters Date: Thu, 2 Nov 2023 21:27:56 +0100 Subject: [PATCH 32/33] Fixed all tests --- .../client/dao/AkkaHttpConsulClientIT.scala | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIT.scala b/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIT.scala index 57f6355..13e4957 100644 --- a/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIT.scala +++ b/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIT.scala @@ -9,8 +9,9 @@ import java.util.UUID import scala.util.Random class AkkaHttpConsulClientIT extends ClientITSpec with RetryPolicy with Logging { - val rnd = new Random() - val subject = new AkkaHttpConsulClient(new URL(s"http://$host:$port")) + val rnd = new Random() + val subject = new AkkaHttpConsulClient(new URL(s"http://$host:$port")) + val nonExistentSessionId: UUID = UUID.fromString("9A3BB9C-E2E7-43DF-BFD5-845417146552") "The AkkaHttpConsulClient" should "retrieve a single Consul service from a freshly started Consul instance" in { eventually { @@ -136,7 +137,10 @@ class AkkaHttpConsulClientIT extends ClientITSpec with RetryPolicy with Logging val payload = """ { "name" : "test" } """.getBytes("UTF-8") val key = "my/key" + rnd.nextInt(100000) subject.putKeyValuePair(key, payload, Some(AcquireSession(id))).futureValue should be(true) - subject.putKeyValuePair(key, payload, Some(AcquireSession(id))).futureValue should be(false) + subject + .putKeyValuePair(key, payload, Some(AcquireSession(id))) + .futureValue should be(true) // subsequent calls for same ID is ok + subject.putKeyValuePair(key, payload, Some(AcquireSession(nonExistentSessionId))).futureValue should be(false) subject.putKeyValuePair(key, payload, Some(ReleaseSession(id))).futureValue should be(true) } @@ -159,9 +163,8 @@ class AkkaHttpConsulClientIT extends ClientITSpec with RetryPolicy with Logging } it should "fail when aquiring a lock on a key with a non-existent session" in { - val payload = """ { "name" : "test" } """.getBytes("UTF-8") - val nonExistentSessionId = UUID.fromString("9A3BB9C-E2E7-43DF-BFD5-845417146552") - val key = "my/key" + rnd.nextInt(100000) + val payload = """ { "name" : "test" } """.getBytes("UTF-8") + val key = "my/key" + rnd.nextInt(100000) subject .putKeyValuePair(key, payload, Some(AcquireSession(nonExistentSessionId))) .futureValue should be(false) From 0edd5fdc392c7352f38f633843b7565c50ef750b Mon Sep 17 00:00:00 2001 From: Leonard Wolters Date: Thu, 2 Nov 2023 21:28:49 +0100 Subject: [PATCH 33/33] Fixed all tests --- .../stormlantern/consul/client/dao/AkkaHttpConsulClientIT.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIT.scala b/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIT.scala index 13e4957..48da26d 100644 --- a/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIT.scala +++ b/client/src/it/scala/stormlantern/consul/client/dao/AkkaHttpConsulClientIT.scala @@ -162,7 +162,7 @@ class AkkaHttpConsulClientIT extends ClientITSpec with RetryPolicy with Logging keyDataSeq.head.value should equal(BinaryData(payload)) } - it should "fail when aquiring a lock on a key with a non-existent session" in { + it should "fail when acquiring a lock on a key with a non-existent session" in { val payload = """ { "name" : "test" } """.getBytes("UTF-8") val key = "my/key" + rnd.nextInt(100000) subject