Skip to content
This repository was archived by the owner on Jan 6, 2023. It is now read-only.

Commit 4280863

Browse files
committed
Use configuration
1 parent c73baff commit 4280863

3 files changed

Lines changed: 57 additions & 18 deletions

File tree

build.gradle

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ compileTestKotlin {
2828
}
2929

3030
group 'de.debuglevel.sparkutils'
31-
version '0.0.6'
31+
version '0.0.8'
3232

3333
sourceCompatibility = 1.8
3434

@@ -50,6 +50,9 @@ dependencies {
5050

5151
// Spark (REST Server)
5252
compileOnly 'com.sparkjava:spark-kotlin:1.0.0-alpha'
53+
54+
// Configuration
55+
implementation 'com.natpryce:konfig:1.6.9.0'
5356
}
5457

5558
dokka {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package de.debuglevel.sparkutils.configuration
2+
3+
import com.natpryce.konfig.*
4+
import com.natpryce.konfig.ConfigurationProperties.Companion.systemProperties
5+
import java.io.File
6+
7+
/**
8+
* Configuration of the microservice.
9+
*
10+
* Provides some usual configurations for a microservice (e.g. used port). Furthermore, the `configuration` variable
11+
* exposes access to any defined configuration parameter defined.
12+
*
13+
* There are multiple possible sources for configurations, where the former override the later:
14+
* - Java system properties
15+
* - Environment variables
16+
* - file `configuration.properties` on the working directory
17+
* - file `defaults.properties` in the class path
18+
*
19+
* For environment variables, "SERVER_PORT" can be used where "server.port" would actually be needed.
20+
*/
21+
object MicroserviceConfiguration {
22+
private val key_server_port = Key("server.port", intType)
23+
private val key_port = Key("port", intType)
24+
25+
val configuration: Configuration
26+
27+
init {
28+
var config: Configuration = systemProperties()
29+
30+
config = config overriding
31+
EnvironmentVariables()
32+
33+
config = config overriding
34+
ConfigurationProperties.fromOptionalFile(File("configuration.properties"))
35+
36+
val defaultsPropertiesFilename = "defaults.properties"
37+
if (ClassLoader.getSystemClassLoader().getResource(defaultsPropertiesFilename) != null) {
38+
config = config overriding
39+
ConfigurationProperties.fromResource(defaultsPropertiesFilename)
40+
}
41+
42+
configuration = config
43+
}
44+
45+
/**
46+
* Port on which the REST server listens.
47+
*
48+
* Defaults to 4567 if none is configured.
49+
*/
50+
val port = configuration.getOrNull(key_port) ?: configuration.getOrNull(key_server_port) ?: 4567
51+
}

src/main/kotlin/de/debuglevel/sparkutils/port/SparkPortUtils.kt

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,11 @@
11
package de.debuglevel.sparkutils.port
22

3+
import de.debuglevel.sparkutils.configuration.MicroserviceConfiguration
34
import mu.KotlinLogging
45
import spark.kotlin.port
56

67
private val logger = KotlinLogging.logger {}
78

8-
/**
9-
* Gets the value of the PORT environment variable, or 4567 if no such environment variable is set.
10-
*
11-
* @return value of the PORT environment variable, or 4567 if no such environment variable is set.
12-
*/
13-
private fun getEnvironmentPort(): Int {
14-
// TODO: add more options to figure out a port configuration
15-
16-
val processBuilder = ProcessBuilder()
17-
return if (processBuilder.environment()["PORT"] != null) {
18-
Integer.parseInt(processBuilder.environment()["PORT"])
19-
} else {
20-
4567
21-
}
22-
}
23-
249
/**
2510
* Sets the port to a configured port.
2611
*
@@ -29,7 +14,7 @@ private fun getEnvironmentPort(): Int {
2914
fun configuredPort() {
3015
// TODO: Spark fails silently if port is already taken. There should be thrown an exception
3116

32-
val port = getEnvironmentPort()
17+
val port = MicroserviceConfiguration.port
3318
logger.info("Setting port to $port...")
3419
port(port)
3520
//logger.info("Setting port to $port succeeded.")

0 commit comments

Comments
 (0)