diff --git a/startup/launcher/src/commonMain/kotlin/Config.kt b/startup/launcher/src/commonMain/kotlin/Config.kt index f52ad983aea..3609170ad13 100644 --- a/startup/launcher/src/commonMain/kotlin/Config.kt +++ b/startup/launcher/src/commonMain/kotlin/Config.kt @@ -1,9 +1,9 @@ package dev.inmo.micro_utils.startup.launcher -import dev.inmo.micro_utils.startup.plugin.ServerPlugin +import dev.inmo.micro_utils.startup.plugin.StartPlugin import kotlinx.serialization.Serializable @Serializable data class Config( - val plugins: List + val plugins: List ) diff --git a/startup/launcher/src/commonMain/kotlin/HelloWorldPlugin.kt b/startup/launcher/src/commonMain/kotlin/HelloWorldPlugin.kt index 81842523b23..4770025e066 100644 --- a/startup/launcher/src/commonMain/kotlin/HelloWorldPlugin.kt +++ b/startup/launcher/src/commonMain/kotlin/HelloWorldPlugin.kt @@ -2,10 +2,10 @@ package dev.inmo.micro_utils.startup.launcher import dev.inmo.kslog.common.i import dev.inmo.kslog.common.logger -import dev.inmo.micro_utils.startup.plugin.ServerPlugin +import dev.inmo.micro_utils.startup.plugin.StartPlugin import org.koin.core.Koin -object HelloWorldPlugin : ServerPlugin { +object HelloWorldPlugin : StartPlugin { override suspend fun startPlugin(koin: Koin) { super.startPlugin(koin) logger.i("Hello world") diff --git a/startup/launcher/src/commonMain/kotlin/Start.kt b/startup/launcher/src/commonMain/kotlin/Start.kt index ae17d7a668b..7526adb8de9 100644 --- a/startup/launcher/src/commonMain/kotlin/Start.kt +++ b/startup/launcher/src/commonMain/kotlin/Start.kt @@ -6,8 +6,15 @@ import org.koin.core.KoinApplication import org.koin.core.context.GlobalContext import org.koin.dsl.module +/** + * Will create [KoinApplication], init, load modules using [StartLauncherPlugin] and start plugins using the same base + * plugin + * + * @param rawConfig It is expected that this [JsonObject] will contain serialized [Config] ([StartLauncherPlugin] will + * deserialize it in its [StartLauncherPlugin.setupDI] + */ suspend fun start(rawConfig: JsonObject) { - with(StartupLauncherPlugin) { + with(StartLauncherPlugin) { logger.i("Start initialization") val koinApp = KoinApplication.init() koinApp.modules( diff --git a/startup/launcher/src/commonMain/kotlin/StartupLauncherPlugin.kt b/startup/launcher/src/commonMain/kotlin/StartLauncherPlugin.kt similarity index 64% rename from startup/launcher/src/commonMain/kotlin/StartupLauncherPlugin.kt rename to startup/launcher/src/commonMain/kotlin/StartLauncherPlugin.kt index 4e98131747c..fb4b21260d0 100644 --- a/startup/launcher/src/commonMain/kotlin/StartupLauncherPlugin.kt +++ b/startup/launcher/src/commonMain/kotlin/StartLauncherPlugin.kt @@ -4,23 +4,38 @@ 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 dev.inmo.micro_utils.startup.plugin.StartPlugin import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.joinAll import kotlinx.coroutines.launch +import kotlinx.serialization.SerialFormat +import kotlinx.serialization.StringFormat import kotlinx.serialization.json.JsonObject import org.koin.core.Koin import org.koin.core.module.Module +import org.koin.dsl.binds import org.koin.dsl.module -object StartupLauncherPlugin : ServerPlugin { +/** + * Default startup plugin. See [setupDI] and [startPlugin] for more info + */ +object StartLauncherPlugin : StartPlugin { internal val logger = taggedLogger(this) + + /** + * Will deserialize [Config] from [config], register it in receiver [Module] (as well as [CoroutineScope] and + * [kotlinx.serialization.json.Json]) + * + * Besides, in this method will be called [StartPlugin.setupDI] on each plugin from [Config.plugins]. In case when + * some plugin will not be loaded correctly it will be reported throw the [logger] + */ override fun Module.setupDI(config: JsonObject) { val pluginsConfig = defaultJson.decodeFromJsonElement(Config.serializer(), config) single { pluginsConfig } single { CoroutineScope(Dispatchers.Default) } + single { defaultJson } binds arrayOf(StringFormat::class, SerialFormat::class) includes( pluginsConfig.plugins.mapNotNull { @@ -37,6 +52,10 @@ object StartupLauncherPlugin : ServerPlugin { ) } + /** + * Takes [CoroutineScope] and [Config] from the [koin], and call starting of each plugin from [Config.plugins] + * ASYNCHRONOUSLY. Just like in [setupDI], in case of fail in some plugin it will be reported using [logger] + */ override suspend fun startPlugin(koin: Koin) { val scope = koin.get() koin.get().plugins.map { plugin -> diff --git a/startup/plugin/src/commonMain/kotlin/ServerPlugin.kt b/startup/plugin/src/commonMain/kotlin/ServerPlugin.kt deleted file mode 100644 index f40ad8c6db5..00000000000 --- a/startup/plugin/src/commonMain/kotlin/ServerPlugin.kt +++ /dev/null @@ -1,13 +0,0 @@ -package dev.inmo.micro_utils.startup.plugin - -import kotlinx.serialization.Serializable -import kotlinx.serialization.json.JsonObject -import org.koin.core.Koin -import org.koin.core.module.Module - -@Serializable(ServerPluginSerializer::class) -interface ServerPlugin { - fun Module.setupDI(config: JsonObject) {} - - suspend fun startPlugin(koin: Koin) {} -} diff --git a/startup/plugin/src/commonMain/kotlin/StartPlugin.kt b/startup/plugin/src/commonMain/kotlin/StartPlugin.kt new file mode 100644 index 00000000000..dfd4963e3f1 --- /dev/null +++ b/startup/plugin/src/commonMain/kotlin/StartPlugin.kt @@ -0,0 +1,31 @@ +package dev.inmo.micro_utils.startup.plugin + +import kotlinx.serialization.Serializable +import kotlinx.serialization.json.JsonObject +import org.koin.core.Koin +import org.koin.core.module.Module + +/** + * Default plugin for start of your app + */ +@Serializable(StartPluginSerializer::class) +interface StartPlugin { + /** + * This method will be called first to configure [Koin] [Module] related to this plugin. You may use + * [org.koin.core.scope.Scope.get] in your koin definitions like [Module.single] to retrieve + * [kotlinx.coroutines.CoroutineScope], [kotlinx.serialization.json.Json] or [dev.inmo.micro_utils.startup.launcher.Config] + */ + fun Module.setupDI(config: JsonObject) {} + + /** + * This method will be called after all other [StartPlugin] will [setupDI] + * + * It is allowed to lock end of this method in case you require to prevent application to end its run (for example, + * you are starting some web server) + * + * @param koin Will contains everything you will register in [setupDI] (as well as other [StartPlugin]s) and + * [kotlinx.coroutines.CoroutineScope], [kotlinx.serialization.json.Json] and [dev.inmo.micro_utils.startup.launcher.Config] + * by their types + */ + suspend fun startPlugin(koin: Koin) {} +} diff --git a/startup/plugin/src/commonMain/kotlin/ServerPluginSerializer.kt b/startup/plugin/src/commonMain/kotlin/StartPluginSerializer.kt similarity index 57% rename from startup/plugin/src/commonMain/kotlin/ServerPluginSerializer.kt rename to startup/plugin/src/commonMain/kotlin/StartPluginSerializer.kt index 72cc877b175..15e4a6be604 100644 --- a/startup/plugin/src/commonMain/kotlin/ServerPluginSerializer.kt +++ b/startup/plugin/src/commonMain/kotlin/StartPluginSerializer.kt @@ -2,4 +2,4 @@ package dev.inmo.micro_utils.startup.plugin import kotlinx.serialization.KSerializer -expect object ServerPluginSerializer : KSerializer +expect object StartPluginSerializer : KSerializer diff --git a/startup/plugin/src/jvmMain/kotlin/ServerPluginSerializer.kt b/startup/plugin/src/jvmMain/kotlin/StartPluginSerializer.kt similarity index 70% rename from startup/plugin/src/jvmMain/kotlin/ServerPluginSerializer.kt rename to startup/plugin/src/jvmMain/kotlin/StartPluginSerializer.kt index 3875b65ac53..0e650c70df5 100644 --- a/startup/plugin/src/jvmMain/kotlin/ServerPluginSerializer.kt +++ b/startup/plugin/src/jvmMain/kotlin/StartPluginSerializer.kt @@ -6,16 +6,16 @@ import kotlinx.serialization.descriptors.SerialDescriptor import kotlinx.serialization.encoding.Decoder import kotlinx.serialization.encoding.Encoder -actual object ServerPluginSerializer : KSerializer { +actual object StartPluginSerializer : KSerializer { override val descriptor: SerialDescriptor get() = String.serializer().descriptor - override fun deserialize(decoder: Decoder): ServerPlugin { + override fun deserialize(decoder: Decoder): StartPlugin { val kclass = Class.forName(decoder.decodeString()).kotlin - return (kclass.objectInstance ?: kclass.constructors.first { it.parameters.isEmpty() }.call()) as ServerPlugin + return (kclass.objectInstance ?: kclass.constructors.first { it.parameters.isEmpty() }.call()) as StartPlugin } - override fun serialize(encoder: Encoder, value: ServerPlugin) { + override fun serialize(encoder: Encoder, value: StartPlugin) { encoder.encodeString( value::class.java.canonicalName )