mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-17 05:43:50 +00:00
initialize startup module
This commit is contained in:
parent
c5abbbbd2d
commit
369ff26627
@ -4,6 +4,8 @@ kt = "1.7.20"
|
||||
kt-serialization = "1.4.1"
|
||||
kt-coroutines = "1.6.4"
|
||||
|
||||
kslog = "0.5.4"
|
||||
|
||||
jb-compose = "1.2.1"
|
||||
jb-exposed = "0.41.1"
|
||||
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-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" }
|
||||
uuid = { module = "com.benasher44:uuid", version.ref = "uuid" }
|
||||
|
@ -23,7 +23,7 @@ kotlin {
|
||||
commonMain {
|
||||
dependencies {
|
||||
implementation kotlin('stdlib')
|
||||
implementation libs.kt.serialization
|
||||
api libs.kt.serialization
|
||||
}
|
||||
}
|
||||
commonTest {
|
||||
|
@ -23,7 +23,7 @@ kotlin {
|
||||
commonMain {
|
||||
dependencies {
|
||||
implementation kotlin('stdlib')
|
||||
implementation libs.kt.serialization
|
||||
api libs.kt.serialization
|
||||
implementation compose.runtime
|
||||
}
|
||||
}
|
||||
|
@ -32,6 +32,8 @@ String[] includes = [
|
||||
":serialization:base64",
|
||||
":serialization:encapsulator",
|
||||
":serialization:typed_serializer",
|
||||
":startup:plugin",
|
||||
":startup:launcher",
|
||||
|
||||
":fsm:common",
|
||||
":fsm:repos:common",
|
||||
|
17
startup/launcher/build.gradle
Normal file
17
startup/launcher/build.gradle
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
9
startup/launcher/src/commonMain/kotlin/Config.kt
Normal file
9
startup/launcher/src/commonMain/kotlin/Config.kt
Normal 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>
|
||||
)
|
38
startup/launcher/src/commonMain/kotlin/ServerLauncher.kt
Normal file
38
startup/launcher/src/commonMain/kotlin/ServerLauncher.kt
Normal 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()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
24
startup/launcher/src/jvmMain/kotlin/ServerLauncher.kt
Normal file
24
startup/launcher/src/jvmMain/kotlin/ServerLauncher.kt
Normal 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()
|
||||
}
|
17
startup/plugin/build.gradle
Normal file
17
startup/plugin/build.gradle
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
34
startup/plugin/src/commonMain/kotlin/ServerPlugin.kt
Normal file
34
startup/plugin/src/commonMain/kotlin/ServerPlugin.kt
Normal 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
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user