initialize startup module

This commit is contained in:
InsanusMokrassar 2022-12-05 21:19:38 +06:00
parent c5abbbbd2d
commit 369ff26627
10 changed files with 146 additions and 2 deletions

View File

@ -4,6 +4,8 @@ kt = "1.7.20"
kt-serialization = "1.4.1" kt-serialization = "1.4.1"
kt-coroutines = "1.6.4" kt-coroutines = "1.6.4"
kslog = "0.5.4"
jb-compose = "1.2.1" jb-compose = "1.2.1"
jb-exposed = "0.41.1" jb-exposed = "0.41.1"
jb-dokka = "1.7.20" jb-dokka = "1.7.20"
@ -60,6 +62,7 @@ ktor-server-websockets = { module = "io.ktor:ktor-server-websockets", version.re
ktor-server-statusPages = { module = "io.ktor:ktor-server-status-pages", version.ref = "ktor" } ktor-server-statusPages = { module = "io.ktor:ktor-server-status-pages", version.ref = "ktor" }
ktor-server-content-negotiation = { module = "io.ktor:ktor-server-content-negotiation", version.ref = "ktor" } ktor-server-content-negotiation = { module = "io.ktor:ktor-server-content-negotiation", version.ref = "ktor" }
kslog = { module = "dev.inmo:kslog", version.ref = "kslog" }
klock = { module = "com.soywiz.korlibs.klock:klock", version.ref = "klock" } klock = { module = "com.soywiz.korlibs.klock:klock", version.ref = "klock" }
uuid = { module = "com.benasher44:uuid", version.ref = "uuid" } uuid = { module = "com.benasher44:uuid", version.ref = "uuid" }

View File

@ -23,7 +23,7 @@ kotlin {
commonMain { commonMain {
dependencies { dependencies {
implementation kotlin('stdlib') implementation kotlin('stdlib')
implementation libs.kt.serialization api libs.kt.serialization
} }
} }
commonTest { commonTest {

View File

@ -23,7 +23,7 @@ kotlin {
commonMain { commonMain {
dependencies { dependencies {
implementation kotlin('stdlib') implementation kotlin('stdlib')
implementation libs.kt.serialization api libs.kt.serialization
implementation compose.runtime implementation compose.runtime
} }
} }

View File

@ -32,6 +32,8 @@ String[] includes = [
":serialization:base64", ":serialization:base64",
":serialization:encapsulator", ":serialization:encapsulator",
":serialization:typed_serializer", ":serialization:typed_serializer",
":startup:plugin",
":startup:launcher",
":fsm:common", ":fsm:common",
":fsm:repos:common", ":fsm:repos:common",

View File

@ -0,0 +1,17 @@
plugins {
id "org.jetbrains.kotlin.multiplatform"
id "org.jetbrains.kotlin.plugin.serialization"
}
apply from: "$mppJavaProjectPresetPath"
kotlin {
sourceSets {
commonMain {
dependencies {
api internalProject("micro_utils.startup.plugin")
api libs.kslog
}
}
}
}

View File

@ -0,0 +1,9 @@
package dev.inmo.micro_utils.startup.launcher
import dev.inmo.micro_utils.startup.plugin.ServerPlugin
import kotlinx.serialization.Serializable
@Serializable
data class Config(
val plugins: List<ServerPlugin>
)

View File

@ -0,0 +1,38 @@
package dev.inmo.micro_utils.startup.launcher
import dev.inmo.micro_utils.startup.plugin.ServerPlugin
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObject
import org.koin.core.Koin
import org.koin.core.module.Module
import org.koin.dsl.module
class ServerLauncher : ServerPlugin {
val defaultJson = Json {
ignoreUnknownKeys = true
}
override fun Module.setupDI(config: JsonObject) {
val pluginsConfig = defaultJson.decodeFromJsonElement(Config.serializer(), config)
single { pluginsConfig }
includes(
pluginsConfig.plugins.map {
module {
with(it) {
setupDI(config)
}
}
}
)
}
override suspend fun Koin.startPlugin() {
get<Config>().plugins.forEach {
with(it) {
startPlugin()
}
}
}
}

View File

@ -0,0 +1,24 @@
package dev.inmo.micro_utils.startup.launcher
import dev.inmo.kslog.common.KSLog
import dev.inmo.kslog.common.i
import dev.inmo.micro_utils.startup.plugin.ServerPlugin
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObject
import org.koin.core.Koin
import org.koin.core.module.Module
import org.koin.dsl.module
import java.io.File
suspend fun main(args: Array<String>) {
KSLog.default = KSLog("PlaguBot")
val (configPath) = args
val file = File(configPath)
KSLog.i("Start read config from ${file.absolutePath}")
val json = defaultJsonFormat.parseToJsonElement(file.readText()).jsonObject
val config = defaultJsonFormat.decodeFromJsonElement(Config.serializer(), json)
KSLog.i("Config has been read")
PlaguBot(json, config).start().join()
}

View File

@ -0,0 +1,17 @@
plugins {
id "org.jetbrains.kotlin.multiplatform"
id "org.jetbrains.kotlin.plugin.serialization"
}
apply from: "$mppJavaProjectPresetPath"
kotlin {
sourceSets {
commonMain {
dependencies {
api libs.koin
api libs.kt.serialization
}
}
}
}

View File

@ -0,0 +1,34 @@
package dev.inmo.micro_utils.startup.plugin
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.JsonObject
import org.koin.core.Koin
import org.koin.core.module.Module
@Serializable(ServerPlugin.Companion::class)
interface ServerPlugin {
fun Module.setupDI(config: JsonObject) {}
suspend fun Koin.startPlugin() {}
companion object : KSerializer<ServerPlugin> {
override val descriptor: SerialDescriptor
get() = String.serializer().descriptor
override fun deserialize(decoder: Decoder): ServerPlugin {
val kclass = Class.forName(decoder.decodeString()).kotlin
return (kclass.objectInstance ?: kclass.constructors.first { it.parameters.isEmpty() }.call()) as ServerPlugin
}
override fun serialize(encoder: Encoder, value: ServerPlugin) {
encoder.encodeString(
value::class.java.canonicalName
)
}
}
}