complete startup module

This commit is contained in:
InsanusMokrassar 2022-12-05 22:31:15 +06:00
parent 2a4570eafc
commit b17931e7bd
6 changed files with 80 additions and 52 deletions

View File

@ -10,7 +10,6 @@ kotlin {
commonMain {
dependencies {
api internalProject("micro_utils.startup.plugin")
api libs.kslog
}
}
}

View File

@ -1,34 +0,0 @@
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
object ServerLauncher : ServerPlugin {
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,57 @@
package dev.inmo.micro_utils.startup.launcher
import dev.inmo.kslog.common.i
import dev.inmo.kslog.common.taggedLogger
import dev.inmo.kslog.common.w
import dev.inmo.micro_utils.coroutines.runCatchingSafely
import dev.inmo.micro_utils.startup.plugin.ServerPlugin
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.joinAll
import kotlinx.coroutines.launch
import kotlinx.serialization.json.JsonObject
import org.koin.core.Koin
import org.koin.core.module.Module
import org.koin.dsl.module
object StartupLauncher : ServerPlugin {
internal val logger = taggedLogger(this)
override fun Module.setupDI(config: JsonObject) {
val pluginsConfig = defaultJson.decodeFromJsonElement(Config.serializer(), config)
single { pluginsConfig }
single { CoroutineScope(Dispatchers.Default) }
includes(
pluginsConfig.plugins.mapNotNull {
runCatching {
module {
with(it) {
setupDI(config)
}
}
}.onFailure { e ->
logger.w("Unable to load DI part of $it", e)
}.getOrNull()
}
)
}
override suspend fun startPlugin(koin: Koin) {
val scope = koin.get<CoroutineScope>()
koin.get<Config>().plugins.map { plugin ->
scope.launch {
runCatchingSafely {
logger.i("Start loading of $plugin")
with(plugin) {
startPlugin(koin)
}
}.onFailure { e ->
logger.w("Unable to load bot part of $plugin", e)
}.onSuccess {
logger.i("Complete loading of $plugin")
}
}
}.joinAll()
}
}

View File

@ -2,31 +2,35 @@ 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 dev.inmo.micro_utils.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.serialization.json.jsonObject
import org.koin.core.KoinApplication
import org.koin.core.context.GlobalContext
import org.koin.dsl.module
import java.io.File
package dev.inmo.micro_utils.startup.launcher
import dev.inmo.
class ServerLauncher : ServerPlugin {
val defaultJson = Json {
ignoreUnknownKeys = true
}
suspend fun main(args: Array<String>) {
KSLog.default = KSLog("PlaguBot")
KSLog.default = KSLog("ServerLauncher")
val (configPath) = args
val file = File(configPath)
KSLog.i("Start read config from ${file.absolutePath}")
val json = defaultJson.parseToJsonElement(file.readText()).jsonObject
KSLog.i("Config has been read")
ServerLauncher.start().join()
with(StartupLauncher) {
logger.i("Start initialization")
val koinApp = KoinApplication.init()
koinApp.modules(
module {
setupDI(json)
}
)
logger.i("Modules loaded")
GlobalContext.startKoin(koinApp)
logger.i("Koin started")
startPlugin(koinApp.koin)
logger.i("Behaviour builder has been setup")
}
}

View File

@ -11,6 +11,8 @@ kotlin {
dependencies {
api libs.koin
api libs.kt.serialization
api libs.kslog
api project(":micro_utils.coroutines")
}
}
}

View File

@ -14,7 +14,7 @@ import org.koin.core.module.Module
interface ServerPlugin {
fun Module.setupDI(config: JsonObject) {}
suspend fun Koin.startPlugin() {}
suspend fun startPlugin(koin: Koin) {}
companion object : KSerializer<ServerPlugin> {
override val descriptor: SerialDescriptor