From ca29ef0769b93ace9566c337959e2be1f9ac78a8 Mon Sep 17 00:00:00 2001 From: Ronald Holshausen Date: Wed, 30 Oct 2024 09:42:21 +1100 Subject: [PATCH] chore(pact-jvm-server): Move Config to Kotlin --- pact-jvm-server/build.gradle | 5 ++ .../kotlin/au/com/dius/pact/server/Config.kt | 30 +++++++++++ .../au/com/dius/pact/server/Create.scala | 10 ++-- .../au/com/dius/pact/server/Publish.scala | 4 +- .../au/com/dius/pact/server/Server.scala | 52 +++++++------------ 5 files changed, 61 insertions(+), 40 deletions(-) create mode 100644 pact-jvm-server/src/main/kotlin/au/com/dius/pact/server/Config.kt diff --git a/pact-jvm-server/build.gradle b/pact-jvm-server/build.gradle index 1f5aab28fa..b058079194 100644 --- a/pact-jvm-server/build.gradle +++ b/pact-jvm-server/build.gradle @@ -42,6 +42,11 @@ java { withSourcesJar() } +compileScala { + dependsOn compileKotlin + classpath = classpath.plus(files(compileKotlin.destinationDirectory)) +} + test { dependsOn(':pact-jvm-server:assembleDist') systemProperty('appExecutable', (new File(buildDir, 'install/pact-jvm-server/bin/pact-jvm-server')).path) diff --git a/pact-jvm-server/src/main/kotlin/au/com/dius/pact/server/Config.kt b/pact-jvm-server/src/main/kotlin/au/com/dius/pact/server/Config.kt new file mode 100644 index 0000000000..ab547569bc --- /dev/null +++ b/pact-jvm-server/src/main/kotlin/au/com/dius/pact/server/Config.kt @@ -0,0 +1,30 @@ +package au.com.dius.pact.server + +data class Config @JvmOverloads constructor( + val port: Int = 29999, + val host: String = "localhost", + val daemon: Boolean = false, + val portLowerBound: Int = 20000, + val portUpperBound: Int = 40000, + val debug: Boolean = false, + val pactVersion: Int = 2, + val keystorePath: String = "", + val keystorePassword: String = "", + val sslPort : Int = 8443, + val broker: String = "", + val authToken: String = "" +) { + // Scala can't access the copy method correctly + fun copyPort(port: Int) = this.copy(port = port) + fun copyHost(host: String) = this.copy(host = host) + fun copyDaemon(daemon: Boolean) = this.copy(daemon = daemon) + fun copyPortLowerBound(portLowerBound: Int) = this.copy(portLowerBound = portLowerBound) + fun copyPortUpperBound(portUpperBound: Int) = this.copy(portUpperBound = portUpperBound) + fun copyDebug(debug: Boolean) = this.copy(debug = debug) + fun copyPactVersion(pactVersion: Int) = this.copy(pactVersion = pactVersion) + fun copyKeystorePath(keystorePath: String) = this.copy(keystorePath = keystorePath) + fun copyKeystorePassword(keystorePassword: String) = this.copy(keystorePassword = keystorePassword) + fun copySslPort(sslPort : Int) = this.copy(sslPort = sslPort) + fun copyBroker(broker: String) = this.copy(broker = broker) + fun copyAuthToken(authToken: String) = this.copy(authToken = authToken) +} diff --git a/pact-jvm-server/src/main/scala/au/com/dius/pact/server/Create.scala b/pact-jvm-server/src/main/scala/au/com/dius/pact/server/Create.scala index c28af776eb..bd3aa40eee 100644 --- a/pact-jvm-server/src/main/scala/au/com/dius/pact/server/Create.scala +++ b/pact-jvm-server/src/main/scala/au/com/dius/pact/server/Create.scala @@ -16,14 +16,14 @@ object Create extends StrictLogging { val pact = DefaultPactReader.INSTANCE.loadPact(requestBody).asInstanceOf[RequestResponsePact] val mockConfig : MockProviderConfig = { - if(!config.keystorePath.isEmpty) { + if (config.getKeystorePath.nonEmpty) { MockHttpsKeystoreProviderConfig - .httpsKeystoreConfig(config.host, config.sslPort, config.keystorePath, config.keystorePassword, - PactSpecVersion.fromInt(config.pactVersion)) + .httpsKeystoreConfig(config.getHost, config.getSslPort, config.getKeystorePath, config.getKeystorePassword, + PactSpecVersion.fromInt(config.getPactVersion)) } else { - new MockProviderConfig(config.host, randomPort(config.portLowerBound, config.portUpperBound), - PactSpecVersion.fromInt(config.pactVersion)) + new MockProviderConfig(config.getHost, randomPort(config.getPortLowerBound, config.getPortUpperBound), + PactSpecVersion.fromInt(config.getPactVersion)) } } val server = DefaultMockProvider.apply(mockConfig) diff --git a/pact-jvm-server/src/main/scala/au/com/dius/pact/server/Publish.scala b/pact-jvm-server/src/main/scala/au/com/dius/pact/server/Publish.scala index 788f60c61b..10ee7f3573 100644 --- a/pact-jvm-server/src/main/scala/au/com/dius/pact/server/Publish.scala +++ b/pact-jvm-server/src/main/scala/au/com/dius/pact/server/Publish.scala @@ -17,7 +17,7 @@ object Publish extends StrictLogging { val provider: Option[String] = getVarFromJson("provider", jsonBody) val tags: Option[::[String]] = getListFromJson("tags", jsonBody) val broker: Option[String] = getBrokerUrlFromConfig(config) - val authToken: Option[String] = getVarFromConfig(config.authToken) + val authToken: Option[String] = getVarFromConfig(config.getAuthToken) var response = new Response(500, ResponseUtils.CrossSiteHeaders.asJava) if (broker.isDefined) { @@ -86,7 +86,7 @@ object Publish extends StrictLogging { } def getBrokerUrlFromConfig(config: Config): Option[String] = { - if (!config.broker.isEmpty && config.broker.startsWith("http")) Some(config.broker) + if (config.getBroker.nonEmpty && config.getBroker.startsWith("http")) Some(config.getBroker) else None } diff --git a/pact-jvm-server/src/main/scala/au/com/dius/pact/server/Server.scala b/pact-jvm-server/src/main/scala/au/com/dius/pact/server/Server.scala index 576ab924b4..119379cec7 100644 --- a/pact-jvm-server/src/main/scala/au/com/dius/pact/server/Server.scala +++ b/pact-jvm-server/src/main/scala/au/com/dius/pact/server/Server.scala @@ -18,56 +18,42 @@ object ListServers { case class Result(response: Response, newState: ServerState) -case class Config(port: Int = 29999, - host: String = "localhost", - daemon: Boolean = false, - portLowerBound: Int = 20000, - portUpperBound: Int = 40000, - debug: Boolean = false, - pactVersion: Int = 2, - keystorePath: String = "", - keystorePassword: String = "", - sslPort : Int = 8443, - broker: String = "", - authToken: String = "" - ) - object Server extends App { val parser = new scopt.OptionParser[Config]("pact-jvm-server") { - arg[Int]("port") optional() action { (x, c) => c.copy(port = x) } text("port to run on (defaults to 29999)") + arg[Int]("port") optional() action { (x, c) => c.copyPort(x) } text("port to run on (defaults to 29999)") help("help") text("prints this usage text") - opt[String]('h', "host") action { (x, c) => c.copy(host = x) } text("host to bind to (defaults to localhost)") - opt[Int]('l', "mock-port-lower") action { (x, c) => c.copy(portLowerBound = x) } text("lower bound to allocate mock ports (defaults to 20000)") - opt[Int]('u', "mock-port-upper") action { (x, c) => c.copy(portUpperBound = x) } text("upper bound to allocate mock ports (defaults to 40000)") - opt[Unit]('d', "daemon") action { (_, c) => c.copy(daemon = true) } text("run as a daemon process") - opt[Unit]("debug") action { (_, c) => c.copy(debug = true) } text("run with debug logging") - opt[Int]('v', "pact-version") action { (x, c) => c.copy(pactVersion = x) } text("pact version to generate for (2 or 3)") - opt[String]('k', "keystore-path") action { (x, c) => c.copy(keystorePath = x) } text("Path to keystore") - opt[String]('p', "keystore-password") action { (x, c) => c.copy(keystorePassword = x) } text("Keystore password") - opt[Int]('s', "ssl-port") action { (x, c) => c.copy(sslPort = x) } text("Ssl port the mock server should run on. lower and upper bounds are ignored") - opt[String]('b', "broker") action {(x, c) => c.copy(broker = x)} text("URL of broker where to publish contracts to") - opt[String]('t', "token") action {(x, c) => c.copy(authToken = x)} text("Auth token for publishing the pact to broker") + opt[String]('h', "host") action { (x, c) => c.copyHost(x) } text("host to bind to (defaults to localhost)") + opt[Int]('l', "mock-port-lower") action { (x, c) => c.copyPortLowerBound(x) } text("lower bound to allocate mock ports (defaults to 20000)") + opt[Int]('u', "mock-port-upper") action { (x, c) => c.copyPortUpperBound(x) } text("upper bound to allocate mock ports (defaults to 40000)") + opt[Unit]('d', "daemon") action { (_, c) => c.copyDaemon(true) } text("run as a daemon process") + opt[Unit]("debug") action { (_, c) => c.copyDebug(true) } text("run with debug logging") + opt[Int]('v', "pact-version") action { (x, c) => c.copyPactVersion(x) } text("pact version to generate for (2 or 3)") + opt[String]('k', "keystore-path") action { (x, c) => c.copyKeystorePath(x) } text("Path to keystore") + opt[String]('p', "keystore-password") action { (x, c) => c.copyKeystorePassword(x) } text("Keystore password") + opt[Int]('s', "ssl-port") action { (x, c) => c.copySslPort(x) } text("Ssl port the mock server should run on. lower and upper bounds are ignored") + opt[String]('b', "broker") action {(x, c) => c.copyBroker(x)} text("URL of broker where to publish contracts to") + opt[String]('t', "token") action {(x, c) => c.copyAuthToken(x)} text("Auth token for publishing the pact to broker") } - parser.parse(args, Config()) match { + parser.parse(args, new Config()) match { case Some(config) => val logger = LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME).asInstanceOf[ch.qos.logback.classic.Logger] - if (config.debug) { + if (config.getDebug) { logger.setLevel(Level.DEBUG) } else { logger.setLevel(Level.INFO) } - val server = _root_.unfiltered.netty.Server.http(config.port, config.host) + val server = _root_.unfiltered.netty.Server.http(config.getPort, config.getHost) .handler(RequestHandler(new ServerStateStore(), config)) - if(!config.keystorePath.isEmpty) { - println(s"Using keystore '${config.keystorePath}' for mock https server") + if (config.getKeystorePath.nonEmpty) { + println(s"Using keystore '${config.getKeystorePath}' for mock https server") } - println(s"starting unfiltered app at ${config.host} on port ${config.port}") + println(s"starting unfiltered app at ${config.getHost} on port ${config.getPort}") server.start() - if (!config.daemon) { + if (!config.getDaemon) { readLine("press enter to stop server:\n") server.stop() }