mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-12-18 14:47:15 +00:00
commit
cd9cd7cc5d
@ -1,5 +1,14 @@
|
||||
# Changelog
|
||||
|
||||
## 0.15.1
|
||||
|
||||
* `Startup`:
|
||||
* Inited :)
|
||||
* `Plugin`:
|
||||
* Inited :)
|
||||
* `Launcher`:
|
||||
* Inited :)
|
||||
|
||||
## 0.15.0
|
||||
|
||||
* `Repos`:
|
||||
|
@ -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
|
||||
|
@ -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",
|
||||
|
26
startup/launcher/build.gradle
Normal file
26
startup/launcher/build.gradle
Normal file
@ -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
|
||||
}
|
22
startup/launcher/src/commonMain/kotlin/Config.kt
Normal file
22
startup/launcher/src/commonMain/kotlin/Config.kt
Normal file
@ -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<StartPlugin>
|
||||
)
|
7
startup/launcher/src/commonMain/kotlin/DefaultJson.kt
Normal file
7
startup/launcher/src/commonMain/kotlin/DefaultJson.kt
Normal file
@ -0,0 +1,7 @@
|
||||
package dev.inmo.micro_utils.startup.launcher
|
||||
|
||||
import kotlinx.serialization.json.Json
|
||||
|
||||
val defaultJson = Json {
|
||||
ignoreUnknownKeys = true
|
||||
}
|
13
startup/launcher/src/commonMain/kotlin/HelloWorldPlugin.kt
Normal file
13
startup/launcher/src/commonMain/kotlin/HelloWorldPlugin.kt
Normal file
@ -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")
|
||||
}
|
||||
}
|
31
startup/launcher/src/commonMain/kotlin/Start.kt
Normal file
31
startup/launcher/src/commonMain/kotlin/Start.kt
Normal file
@ -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")
|
||||
}
|
||||
}
|
@ -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<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()
|
||||
}
|
||||
}
|
37
startup/launcher/src/jvmMain/kotlin/Main.kt
Normal file
37
startup/launcher/src/jvmMain/kotlin/Main.kt
Normal file
@ -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<String>) {
|
||||
|
||||
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)
|
||||
}
|
39
startup/launcher/src/jvmTest/kotlin/StartupLaunchingTests.kt
Normal file
39
startup/launcher/src/jvmTest/kotlin/StartupLaunchingTests.kt
Normal file
@ -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()
|
||||
}
|
||||
}
|
||||
}
|
20
startup/plugin/build.gradle
Normal file
20
startup/plugin/build.gradle
Normal file
@ -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")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
31
startup/plugin/src/commonMain/kotlin/StartPlugin.kt
Normal file
31
startup/plugin/src/commonMain/kotlin/StartPlugin.kt
Normal file
@ -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) {}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package dev.inmo.micro_utils.startup.plugin
|
||||
|
||||
import kotlinx.serialization.KSerializer
|
||||
|
||||
expect object StartPluginSerializer : KSerializer<StartPlugin>
|
23
startup/plugin/src/jvmMain/kotlin/StartPluginSerializer.kt
Normal file
23
startup/plugin/src/jvmMain/kotlin/StartPluginSerializer.kt
Normal file
@ -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<StartPlugin> {
|
||||
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
|
||||
)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user