Skip to content

Commit

Permalink
chore(pact-jvm-server): Move Config to Kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Oct 29, 2024
1 parent 51330be commit ca29ef0
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 40 deletions.
5 changes: 5 additions & 0 deletions pact-jvm-server/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 30 additions & 0 deletions pact-jvm-server/src/main/kotlin/au/com/dius/pact/server/Config.kt
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}

Expand Down
52 changes: 19 additions & 33 deletions pact-jvm-server/src/main/scala/au/com/dius/pact/server/Server.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down

0 comments on commit ca29ef0

Please sign in to comment.