Skip to content

Commit

Permalink
chore(pact-jvm-server): Converted main Scala App to Kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Nov 20, 2024
1 parent 9fe90db commit 73697db
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 85 deletions.
17 changes: 2 additions & 15 deletions pact-jvm-server/build.gradle
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
plugins {
id 'au.com.dius.pact.kotlin-application-conventions'
id 'scala'
id 'maven-publish'
id 'signing'
}

group = 'au.com.dius.pact'
mainClassName = 'au.com.dius.pact.server.Server'
mainClassName = 'au.com.dius.pact.server.PactJvmServer'

dependencies {
implementation project(':consumer')
implementation project(':core:pactbroker')
implementation 'ch.qos.logback:logback-core:1.5.6'
implementation 'ch.qos.logback:logback-classic:1.5.6'
implementation 'com.github.scopt:scopt_2.12:3.5.0'
implementation('com.typesafe.scala-logging:scala-logging_2.12:3.7.2') {
exclude group: 'org.scala-lang'
}
implementation( 'ws.unfiltered:unfiltered-netty-server_2.12:0.10.4') {
exclude module: 'netty-transport-native-kqueue'
exclude module: 'netty-transport-native-epoll'
}
implementation 'io.github.oshai:kotlin-logging-jvm'
implementation 'org.apache.commons:commons-io:1.3.2'
implementation 'org.apache.commons:commons-lang3'
Expand All @@ -32,6 +23,7 @@ dependencies {
implementation 'io.ktor:ktor-server-netty'
implementation 'io.ktor:ktor-network-tls-certificates'
implementation 'io.ktor:ktor-server-call-logging'
implementation 'com.github.ajalt.clikt:clikt:5.0.1'

testImplementation 'org.apache.groovy:groovy'
testImplementation 'org.apache.groovy:groovy-json'
Expand All @@ -50,11 +42,6 @@ java {
withSourcesJar()
}

compileScala {
dependsOn compileKotlin
classpath = classpath.plus(files(compileKotlin.destinationDirectory))
}

test {
dependsOn(':pact-jvm-server:installDist')
systemProperty('appExecutable', (new File(buildDir, 'install/pact-jvm-server/bin/pact-jvm-server')).path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ data class Config @JvmOverloads constructor(
val broker: String = "",
val authToken: String = ""
) {
// Scala can't access the copy method correctly
// Scala/Groovy 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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package au.com.dius.pact.server

import au.com.dius.pact.core.support.isNotEmpty
import ch.qos.logback.classic.Level
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.core.main
import com.github.ajalt.clikt.parameters.arguments.argument
import com.github.ajalt.clikt.parameters.arguments.optional
import com.github.ajalt.clikt.parameters.options.flag
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.types.int
import org.slf4j.Logger
import org.slf4j.LoggerFactory

class PactJvmServer: CliktCommand() {
val port: Int? by argument(help = "port to run on (defaults to 29999)").int().optional()
val host by option("-h", "--host", help = "host to bind to (defaults to localhost)")
val portLowerBound: Int? by option("-l", "--mock-port-lower", help = "lower bound to allocate mock ports (defaults to 20000)").int()
val portUpperBound: Int? by option("-u", "--mock-port-upper", help = "upper bound to allocate mock ports (defaults to 40000)").int()
val daemon by option("--daemon", "-d", help = "run as a daemon process").flag(default = false)
val debug by option("--debug", help = "run with debug logging").flag(default = false)
val pactVersion: Int? by option("-v", "--pact-version", help = "pact version to generate for (2 or 3)").int()
val keystorePath by option("-k", "--keystore-path", help = "Path to keystore")
val keystorePassword by option("-p", "--keystore-password", help = "Keystore password")
val sslPort: Int? by option("-s", "--ssl-port", help = "Ssl port the mock server should run on. lower and upper bounds are ignored").int()
val brokerUrl by option("-b", "--broker", help = "URL of broker where to publish contracts to")
val brokerToken by option("-t", "--token", help = "Auth token for publishing the pact to broker")

override fun run() {
var config = Config(daemon = daemon, debug = debug)
if (port != null) config = config.copy(port = port!!)
if (host != null) config = config.copy(host = host!!)
if (portLowerBound != null) config = config.copy(portLowerBound = portLowerBound!!)
if (portUpperBound != null) config = config.copy(portUpperBound = portUpperBound!!)
if (pactVersion != null) config = config.copy(pactVersion = pactVersion!!)
if (keystorePath != null) config = config.copy(keystorePath = keystorePath!!)
if (keystorePassword != null) config = config.copy(keystorePassword = keystorePassword!!)
if (sslPort != null) config = config.copy(sslPort = sslPort!!)
if (brokerUrl != null) config = config.copy(broker = brokerUrl!!)
if (brokerToken != null) config = config.copy(authToken = brokerToken!!)

val logger = LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME) as ch.qos.logback.classic.Logger
if (debug) {
logger.setLevel(Level.DEBUG)
} else {
logger.setLevel(Level.INFO)
}

val mainServer = MainServer(ServerStateStore(), config)

if (keystorePath.isNotEmpty()) {
echo("Using keystore '${keystorePath}' for mock https server")
}

echo("starting main server at ${config.host} on port ${config.port}")
if (!config.daemon) {
mainServer.server.start(false)
echo("press enter to stop server:\n")
readLine()
mainServer.server.stop(100, 1000)
} else {
mainServer.server.start(true)
}
}

companion object {
@JvmStatic
fun main(args: Array<String>) = PactJvmServer().main(args)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package au.com.dius.pact.server

import spock.lang.Specification
import static scala.collection.JavaConverters.mapAsScalaMap

class ListServersSpec extends Specification {
def 'empty state'() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,28 @@ class MainSpec extends Specification {

then:
result == 0
out == '''Usage: pact-jvm-server [options] [port]
out == '''Usage: pact-jvm-server [<options>] [<port>]
|
| port port to run on (defaults to 29999)
| --help prints this usage text
| -h, --host <value> host to bind to (defaults to localhost)
| -l, --mock-port-lower <value>
| lower bound to allocate mock ports (defaults to 20000)
| -u, --mock-port-upper <value>
| upper bound to allocate mock ports (defaults to 40000)
| -d, --daemon run as a daemon process
| --debug run with debug logging
| -v, --pact-version <value>
| pact version to generate for (2 or 3)
| -k, --keystore-path <value>
| Path to keystore
| -p, --keystore-password <value>
| Keystore password
| -s, --ssl-port <value> Ssl port the mock server should run on. lower and upper bounds are ignored
| -b, --broker <value> URL of broker where to publish contracts to
| -t, --token <value> Auth token for publishing the pact to broker
|Options:
| -h, --host=<text> host to bind to (defaults to localhost)
| -l, --mock-port-lower=<int> lower bound to allocate mock ports (defaults to
| 20000)
| -u, --mock-port-upper=<int> upper bound to allocate mock ports (defaults to
| 40000)
| -d, --daemon run as a daemon process
| --debug run with debug logging
| -v, --pact-version=<int> pact version to generate for (2 or 3)
| -k, --keystore-path=<text> Path to keystore
| -p, --keystore-password=<text>
| Keystore password
| -s, --ssl-port=<int> Ssl port the mock server should run on. lower
| and upper bounds are ignored
| -b, --broker=<text> URL of broker where to publish contracts to
| -t, --token=<text> Auth token for publishing the pact to broker
| --help Show this message and exit
|
|Arguments:
| <port> port to run on (defaults to 29999)
|'''.stripMargin('|')
}

Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ if (System.getenv('GITHUB_WORKFLOW') == null) {
}

// Scala does not support JDK 20+
//include 'pact-jvm-server'
include 'pact-jvm-server'
include 'pact-specification-test'
include 'pact-publish'
include 'compatibility-suite'

0 comments on commit 73697db

Please sign in to comment.