diff --git a/CHANGELOG.md b/CHANGELOG.md index dd51bd38dfd..1f98baaf72b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,14 @@ # Changelog +## 0.15.1 + +* `Startup`: + * Inited :) + * `Plugin`: + * Inited :) + * `Launcher`: + * Inited :) + ## 0.15.0 * `Repos`: diff --git a/gradle.properties b/gradle.properties index 19ccba28486..62d5245814b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -14,5 +14,5 @@ crypto_js_version=4.1.1 # Project data group=dev.inmo -version=0.15.0 -android_code_version=166 +version=0.15.1 +android_code_version=167 diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 493ed2a0e72..b5c564a71a4 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -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" } diff --git a/mppProjectWithSerialization.gradle b/mppProjectWithSerialization.gradle index 615ec80d132..05177943155 100644 --- a/mppProjectWithSerialization.gradle +++ b/mppProjectWithSerialization.gradle @@ -23,7 +23,7 @@ kotlin { commonMain { dependencies { implementation kotlin('stdlib') - implementation libs.kt.serialization + api libs.kt.serialization } } commonTest { diff --git a/mppProjectWithSerializationAndCompose.gradle b/mppProjectWithSerializationAndCompose.gradle index eff5f7fe0c0..e5fa8355c23 100644 --- a/mppProjectWithSerializationAndCompose.gradle +++ b/mppProjectWithSerializationAndCompose.gradle @@ -23,7 +23,7 @@ kotlin { commonMain { dependencies { implementation kotlin('stdlib') - implementation libs.kt.serialization + api libs.kt.serialization implementation compose.runtime } } diff --git a/settings.gradle b/settings.gradle index ccd2dc06d84..069bd208c00 100644 --- a/settings.gradle +++ b/settings.gradle @@ -32,6 +32,8 @@ String[] includes = [ ":serialization:base64", ":serialization:encapsulator", ":serialization:typed_serializer", + ":startup:plugin", + ":startup:launcher", ":fsm:common", ":fsm:repos:common", diff --git a/startup/launcher/build.gradle b/startup/launcher/build.gradle new file mode 100644 index 00000000000..c4905f21051 --- /dev/null +++ b/startup/launcher/build.gradle @@ -0,0 +1,26 @@ +plugins { + id "org.jetbrains.kotlin.multiplatform" + id "org.jetbrains.kotlin.plugin.serialization" + id "application" +} + +apply from: "$mppJavaProjectPresetPath" + +kotlin { + sourceSets { + commonMain { + dependencies { + api internalProject("micro_utils.startup.plugin") + } + } + } +} + +application { + mainClassName = "dev.inmo.micro_utils.startup.launcher.ServerLauncherKt" +} + +java { + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 +} diff --git a/startup/launcher/src/commonMain/kotlin/Config.kt b/startup/launcher/src/commonMain/kotlin/Config.kt new file mode 100644 index 00000000000..55e36c68857 --- /dev/null +++ b/startup/launcher/src/commonMain/kotlin/Config.kt @@ -0,0 +1,22 @@ +package dev.inmo.micro_utils.startup.launcher + +import dev.inmo.micro_utils.startup.plugin.StartPlugin +import kotlinx.serialization.Serializable + +/** + * Contains just [List] of [StartPlugin]s. In json this config should look like: + * + * ```json + * { + * "plugins": [ + * "dev.inmo.micro_utils.startup.launcher.HelloWorldPlugin" + * ] + * } + * ``` + * + * In the sample above [HelloWorldPlugin] will be loaded during startup of application + */ +@Serializable +data class Config( + val plugins: List +) diff --git a/startup/launcher/src/commonMain/kotlin/DefaultJson.kt b/startup/launcher/src/commonMain/kotlin/DefaultJson.kt new file mode 100644 index 00000000000..4e921791e87 --- /dev/null +++ b/startup/launcher/src/commonMain/kotlin/DefaultJson.kt @@ -0,0 +1,7 @@ +package dev.inmo.micro_utils.startup.launcher + +import kotlinx.serialization.json.Json + +val defaultJson = Json { + ignoreUnknownKeys = true +} diff --git a/startup/launcher/src/commonMain/kotlin/HelloWorldPlugin.kt b/startup/launcher/src/commonMain/kotlin/HelloWorldPlugin.kt new file mode 100644 index 00000000000..4770025e066 --- /dev/null +++ b/startup/launcher/src/commonMain/kotlin/HelloWorldPlugin.kt @@ -0,0 +1,13 @@ +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.StartPlugin +import org.koin.core.Koin + +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 new file mode 100644 index 00000000000..7526adb8de9 --- /dev/null +++ b/startup/launcher/src/commonMain/kotlin/Start.kt @@ -0,0 +1,31 @@ +package dev.inmo.micro_utils.startup.launcher + +import dev.inmo.kslog.common.i +import kotlinx.serialization.json.JsonObject +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(StartLauncherPlugin) { + logger.i("Start initialization") + val koinApp = KoinApplication.init() + koinApp.modules( + module { + setupDI(rawConfig) + } + ) + logger.i("Modules loaded") + GlobalContext.startKoin(koinApp) + logger.i("Koin started") + startPlugin(koinApp.koin) + logger.i("App has been setup") + } +} diff --git a/startup/launcher/src/commonMain/kotlin/StartLauncherPlugin.kt b/startup/launcher/src/commonMain/kotlin/StartLauncherPlugin.kt new file mode 100644 index 00000000000..fb4b21260d0 --- /dev/null +++ b/startup/launcher/src/commonMain/kotlin/StartLauncherPlugin.kt @@ -0,0 +1,76 @@ +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.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 + +/** + * 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 { + runCatching { + module { + with(it) { + setupDI(config) + } + } + }.onFailure { e -> + logger.w("Unable to load DI part of $it", e) + }.getOrNull() + } + ) + } + + /** + * 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 -> + 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() + } +} diff --git a/startup/launcher/src/jvmMain/kotlin/Main.kt b/startup/launcher/src/jvmMain/kotlin/Main.kt new file mode 100644 index 00000000000..23bb111cefa --- /dev/null +++ b/startup/launcher/src/jvmMain/kotlin/Main.kt @@ -0,0 +1,37 @@ +package dev.inmo.micro_utils.startup.launcher + +import dev.inmo.kslog.common.KSLog +import dev.inmo.kslog.common.i +import kotlinx.serialization.json.jsonObject +import java.io.File + +/** + * It is expected, that [args] will contain ONE argument with path to the config json. Sample of launching: + * + * ```bash + * ./gradlew run --args="sample.config.json" + * ``` + * + * Content of `sample.config.json` described in [Config] KDocs. + * + * You may build runnable app using: + * + * ```bash + * ./gradlew assembleDist + * ``` + * + * In that case in `build/distributions` folder you will be able to find zip and tar files with all required + * tools for application running (via their `bin/app_name` binary). In that case yoy will not need to pass + * `--args=...` and launch will look like `./bin/app_name sample.config.json` + */ +suspend fun main(args: Array) { + + 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") + + start(json) +} diff --git a/startup/launcher/src/jvmTest/kotlin/StartupLaunchingTests.kt b/startup/launcher/src/jvmTest/kotlin/StartupLaunchingTests.kt new file mode 100644 index 00000000000..7f9f80aea50 --- /dev/null +++ b/startup/launcher/src/jvmTest/kotlin/StartupLaunchingTests.kt @@ -0,0 +1,39 @@ +import dev.inmo.micro_utils.coroutines.launchSynchronously +import dev.inmo.micro_utils.startup.launcher.Config +import dev.inmo.micro_utils.startup.launcher.HelloWorldPlugin +import dev.inmo.micro_utils.startup.launcher.defaultJson +import dev.inmo.micro_utils.startup.launcher.start +import kotlinx.coroutines.launch +import kotlinx.serialization.json.jsonObject +import kotlin.test.Test + +class StartupLaunchingTests { + @Test(timeout = 1000L) + fun CheckThatEmptyPluginsListLeadsToEndOfMain() { + val emptyJson = defaultJson.encodeToJsonElement( + Config.serializer(), + Config(emptyList()) + ).jsonObject + + launchSynchronously { + val job = launch { + start(emptyJson) + } + job.join() + } + } + @Test(timeout = 1000L) + fun CheckThatHelloWorldPluginsListLeadsToEndOfMain() { + val emptyJson = defaultJson.encodeToJsonElement( + Config.serializer(), + Config(listOf(HelloWorldPlugin)) + ).jsonObject + + launchSynchronously { + val job = launch { + start(emptyJson) + } + job.join() + } + } +} diff --git a/startup/plugin/build.gradle b/startup/plugin/build.gradle new file mode 100644 index 00000000000..9177a9e7e61 --- /dev/null +++ b/startup/plugin/build.gradle @@ -0,0 +1,20 @@ +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 + api libs.kslog + api libs.kt.reflect + api project(":micro_utils.coroutines") + } + } + } +} 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/StartPluginSerializer.kt b/startup/plugin/src/commonMain/kotlin/StartPluginSerializer.kt new file mode 100644 index 00000000000..15e4a6be604 --- /dev/null +++ b/startup/plugin/src/commonMain/kotlin/StartPluginSerializer.kt @@ -0,0 +1,5 @@ +package dev.inmo.micro_utils.startup.plugin + +import kotlinx.serialization.KSerializer + +expect object StartPluginSerializer : KSerializer diff --git a/startup/plugin/src/jvmMain/kotlin/StartPluginSerializer.kt b/startup/plugin/src/jvmMain/kotlin/StartPluginSerializer.kt new file mode 100644 index 00000000000..0e650c70df5 --- /dev/null +++ b/startup/plugin/src/jvmMain/kotlin/StartPluginSerializer.kt @@ -0,0 +1,23 @@ +package dev.inmo.micro_utils.startup.plugin + +import kotlinx.serialization.KSerializer +import kotlinx.serialization.builtins.serializer +import kotlinx.serialization.descriptors.SerialDescriptor +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder + +actual object StartPluginSerializer : KSerializer { + override val descriptor: SerialDescriptor + get() = String.serializer().descriptor + + override fun deserialize(decoder: Decoder): StartPlugin { + val kclass = Class.forName(decoder.decodeString()).kotlin + return (kclass.objectInstance ?: kclass.constructors.first { it.parameters.isEmpty() }.call()) as StartPlugin + } + + override fun serialize(encoder: Encoder, value: StartPlugin) { + encoder.encodeString( + value::class.java.canonicalName + ) + } +}