diff options
| author | mjkwiatkowski <mati.rewa@gmail.com> | 2026-02-21 18:01:20 +0100 |
|---|---|---|
| committer | mjkwiatkowski <mati.rewa@gmail.com> | 2026-02-21 18:01:20 +0100 |
| commit | daad473975cc3e6eba0536d5a8fe750cf8b2fa7d (patch) | |
| tree | f4496fcef64999005d55b5276a7a621496a8f1ad /opendc-common | |
| parent | f5da60e4275ca1172128c3994298691e12d5e1f8 (diff) | |
Diffstat (limited to 'opendc-common')
4 files changed, 33 insertions, 110 deletions
diff --git a/opendc-common/build.gradle.kts b/opendc-common/build.gradle.kts index 0c87303c..9fe4710c 100644 --- a/opendc-common/build.gradle.kts +++ b/opendc-common/build.gradle.kts @@ -31,6 +31,7 @@ plugins { } repositories { + mavenCentral() maven(url = "https://packages.confluent.io/maven/") } @@ -38,7 +39,6 @@ repositories { val serializationVersion = "1.6.0" dependencies { - //@Mateusz: for the postgresql database implementation("org.postgresql:postgresql:42.7.10") @@ -65,6 +65,9 @@ dependencies { // Source: https://mvnrepository.com/artifact/com.fasterxml.jackson.dataformat/jackson-dataformat-toml implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-toml:2.21.0") + + implementation("io.javalin:javalin:6.7.0") + } diff --git a/opendc-common/src/main/kotlin/org/opendc/common/utils/ConfigParser.kt b/opendc-common/src/main/kotlin/org/opendc/common/utils/ConfigParser.kt deleted file mode 100644 index 8261f6f0..00000000 --- a/opendc-common/src/main/kotlin/org/opendc/common/utils/ConfigParser.kt +++ /dev/null @@ -1,106 +0,0 @@ -package org.opendc.common.utils - -import kotlinx.serialization.ExperimentalSerializationApi -import kotlinx.serialization.Serializable -import kotlinx.serialization.json.Json -import kotlinx.serialization.json.decodeFromStream - -import java.io.File -import java.io.IOException -import java.io.InputStream -import java.io.OutputStream -import java.net.Socket -import java.sql.Connection - -/** - * @property name - * @property backlog the amount of connections to accept - * @property address IPv4 address - * @property port - * @property postgresql Postgresql port - * @property username Postgresql user - * @property password Postgresql password - * @property database Postgresql database - * @property topic Kafka topic and database table name - * @property kafka Kafka port - * @author Mateusz - */ -/* - - Use `by lazy` here. - Use design patterns - singleton. - */ -@Serializable -public data class Config( - val name: String = "", - var backlog: Int = 0, - val address: String = "", - val port: Int = 0, - val postgresql: Int = 0, - val username : String = "", - val password : String = "", - val database: String = "", - val topic : String = "", - val kafka: Int = 0, -){ - - public companion object{ - public var input: InputStream? = null - public var output: OutputStream? = null - public var connection : Connection? = null - public var kafka : Kafka? = null - public var database : PostgresqlDB? = null - - public var socket: Socket? = null - - public fun setConfigSocket(socket: Socket?){ - this.socket = socket - // no try catch if the exception is not from Java - // do not use raw sockets, use a service for the communication - // use redis instead of HTTP GET (consider it, but not bound in stone) - // make an API KTor - try { - input = socket?.getInputStream() - output = socket?.getOutputStream() - } catch (e: IOException){ - print("${e.message}") - } - } - - public fun getConfigReader() : InputStream? { - return input - } - - public fun getConfigWriter() : OutputStream? { - return output - } - - public fun setKafkaInstance(kafka : Kafka) { - this.kafka = kafka - } - - public fun getKafkaInstance() : Kafka? { - return this.kafka - } - - public fun setDB(db : PostgresqlDB){ - this.database = db - } - - public fun getDB() : PostgresqlDB?{ - return this.database - } - } -} -/** - * @author Mateusz - * Reads `config.json` into Config data class. - */ -public class ConfigReader { - private val jsonReader = Json - public fun read(file: File): Config = read(file.inputStream()) - @OptIn(ExperimentalSerializationApi::class) - public fun read(input: InputStream): Config { - return jsonReader.decodeFromStream<Config>(input) - } -} diff --git a/opendc-common/src/main/kotlin/org/opendc/common/utils/JavalinRunner.kt b/opendc-common/src/main/kotlin/org/opendc/common/utils/JavalinRunner.kt new file mode 100644 index 00000000..9db7bfaf --- /dev/null +++ b/opendc-common/src/main/kotlin/org/opendc/common/utils/JavalinRunner.kt @@ -0,0 +1,25 @@ +package org.opendc.common.utils + +import io.javalin.Javalin +import io.javalin.http.Context +import io.javalin.http.Handler + +public class JavalinRunner { + + private val handleHello: Handler = Handler { ctx -> + ctx.status(200) + ctx.contentType("application/x-protobuf") + ctx.result("Hello world") + } + + init { + val app = Javalin.create().start() + + app.get("/hello", handleHello) + + app.exception<Exception?>(Exception::class.java, { e: Exception?, ctx: Context? -> + e!!.printStackTrace() + ctx!!.status(500) + }) + } +}
\ No newline at end of file diff --git a/opendc-common/src/main/kotlin/org/opendc/common/utils/PostgresqlDB.kt b/opendc-common/src/main/kotlin/org/opendc/common/utils/PostgresqlDB.kt index 361925ee..03fd902c 100644 --- a/opendc-common/src/main/kotlin/org/opendc/common/utils/PostgresqlDB.kt +++ b/opendc-common/src/main/kotlin/org/opendc/common/utils/PostgresqlDB.kt @@ -24,7 +24,7 @@ public class PostgresqlDB { properties = TomlMapper().readerFor(Properties().javaClass) .readValue(PostgresqlDB::class.java.getResource("/database.toml")) connection = DriverManager.getConnection( - properties.getProperty("address").asJdbc(properties.getProperty("table")), + properties.getProperty("address").asJdbc(properties.getProperty("database")), properties.getProperty("user"), properties.getProperty("password")) clear() @@ -45,8 +45,9 @@ public class PostgresqlDB { } } - private fun String.asJdbc(table : String) : String { - return "jdbc:postgresql://$this/$table" + private fun String.asJdbc(database : String) : String { + return "jdbc:postgresql://$this/$database" + } }
\ No newline at end of file |
