mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-17 13:53:49 +00:00
upgrades and filling of README
This commit is contained in:
parent
2fe4f08059
commit
5af91981f1
92
startup/launcher/README.md
Normal file
92
startup/launcher/README.md
Normal file
@ -0,0 +1,92 @@
|
||||
# Startup Plugin Launcher
|
||||
|
||||
This module contains tools to start your plugin system.
|
||||
|
||||
## Config
|
||||
|
||||
Base config is pretty simple:
|
||||
|
||||
```json
|
||||
{
|
||||
"plugins": [
|
||||
"dev.inmo.micro_utils.startup.launcher.HelloWorldPlugin"
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
So, `"dev.inmo.micro_utils.startup.launcher.HelloWorldPlugin"` is the fully qualified name of plugin you wish to be
|
||||
included in the server.
|
||||
|
||||
> JS note: In JS there are no opportunity to determine object type by its full name. Because of it, in JS developers
|
||||
> should prefer to use `Config` in their kotlin code directly instead of json config passing. More info see in [JS](#js)
|
||||
> section
|
||||
|
||||
## JVM
|
||||
|
||||
For JVM target you may use main class by path: `dev.inmo.micro_utils.startup.launcher.MainKt`
|
||||
|
||||
It is expected, that you will pass the main 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](#config) section.
|
||||
|
||||
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`
|
||||
|
||||
## JS
|
||||
|
||||
In JS for starting of your plugins app, you should use `PluginsStarter` in your code:
|
||||
|
||||
```kotlin
|
||||
PluginsStarter.startPlugins(
|
||||
Config(HelloWorldPlugin)
|
||||
)
|
||||
```
|
||||
|
||||
`Config` here is deserialized variant from [Config](#config) section. As was said in [Config](#config) section, in JS
|
||||
there is no way to find classes/objects by their full qualifiers. Because of it you should use some way to register your
|
||||
plugins in `StartPluginSerializer` or use the code like in the snippet above: there plugins will be registered
|
||||
automatically.
|
||||
|
||||
In case you wish to register your plugins manually and run server from config, you should use one of the ways to register
|
||||
plugin on start.
|
||||
|
||||
Sample with `EagerInitialization`: [Kotlin JS doc about lazy initialization](https://kotlinlang.org/docs/js-ir-compiler.html#incremental-compilation-for-development-binaries),
|
||||
[@EagerInitialization docs](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.js/-eager-initialization/):
|
||||
|
||||
```kotlin
|
||||
@ExperimentalStdlibApi
|
||||
@EagerInitialization
|
||||
val plugin = createStartupPluginAndRegister("PluginNameToUseInConfig") {
|
||||
// Your plugin creation. For example:
|
||||
HelloWorldPlugin
|
||||
}
|
||||
```
|
||||
|
||||
So, in that case you will be able to load plugins list as `JsonObject` from anywhere and start plugins app with it:
|
||||
|
||||
```kotlin
|
||||
PluginsStarter.startPlugins(
|
||||
jsonObject
|
||||
)
|
||||
```
|
||||
|
||||
It will load `HelloWorldPlugin` if `jsonObject` have next content:
|
||||
|
||||
```json
|
||||
{
|
||||
"plugins": [
|
||||
"PluginNameToUseInConfig"
|
||||
]
|
||||
}
|
||||
```
|
@ -4,7 +4,7 @@ plugins {
|
||||
id "application"
|
||||
}
|
||||
|
||||
apply from: "$mppJavaProjectPresetPath"
|
||||
apply from: "$mppJsAndJavaProjectPresetPath"
|
||||
|
||||
kotlin {
|
||||
sourceSets {
|
||||
@ -13,7 +13,7 @@ kotlin {
|
||||
api internalProject("micro_utils.startup.plugin")
|
||||
}
|
||||
}
|
||||
jvmTest {
|
||||
commonTest {
|
||||
dependencies {
|
||||
implementation libs.kt.coroutines.test
|
||||
}
|
||||
|
@ -1,4 +1,3 @@
|
||||
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
|
||||
@ -6,16 +5,15 @@ import dev.inmo.micro_utils.startup.launcher.start
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import kotlinx.serialization.json.jsonObject
|
||||
import org.koin.core.context.GlobalContext
|
||||
import kotlin.test.BeforeTest
|
||||
import kotlin.test.Test
|
||||
|
||||
class StartupLaunchingTests {
|
||||
@BeforeTest
|
||||
fun resetGlobalKoinContext() {
|
||||
kotlin.runCatching { GlobalContext.stopKoin() }
|
||||
runCatching { stopKoin() }
|
||||
}
|
||||
@Test(timeout = 60000L)
|
||||
@Test
|
||||
fun CheckThatEmptyPluginsListLeadsToEndOfMain() {
|
||||
val emptyJson = defaultJson.encodeToJsonElement(
|
||||
Config.serializer(),
|
||||
@ -29,7 +27,7 @@ class StartupLaunchingTests {
|
||||
job.join()
|
||||
}
|
||||
}
|
||||
@Test(timeout = 60000L)
|
||||
@Test
|
||||
fun CheckThatHelloWorldPluginsListLeadsToEndOfMain() {
|
||||
val emptyJson = defaultJson.encodeToJsonElement(
|
||||
Config.serializer(),
|
1
startup/launcher/src/commonTest/kotlin/StopKoin.kt
Normal file
1
startup/launcher/src/commonTest/kotlin/StopKoin.kt
Normal file
@ -0,0 +1 @@
|
||||
expect fun stopKoin()
|
@ -0,0 +1,14 @@
|
||||
package dev.inmo.micro_utils.startup.launcher
|
||||
|
||||
import dev.inmo.micro_utils.startup.plugin.StartPlugin
|
||||
import dev.inmo.micro_utils.startup.plugin.StartPluginSerializer
|
||||
|
||||
/**
|
||||
* Creates [T] using [block], register it in [StartPluginSerializer] using its [StartPluginSerializer.registerPlugin]
|
||||
* and returns created by [block] plugin
|
||||
*/
|
||||
inline fun <T : StartPlugin> createStartupPluginAndRegister(name: String, block: () -> T): T {
|
||||
val plugin = block()
|
||||
StartPluginSerializer.registerPlugin(name, plugin)
|
||||
return plugin
|
||||
}
|
33
startup/launcher/src/jsMain/kotlin/PluginsStarter.kt
Normal file
33
startup/launcher/src/jsMain/kotlin/PluginsStarter.kt
Normal file
@ -0,0 +1,33 @@
|
||||
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 kotlinx.serialization.json.jsonObject
|
||||
|
||||
object PluginsStarter {
|
||||
init {
|
||||
KSLog.default = KSLog("Launcher")
|
||||
}
|
||||
|
||||
/**
|
||||
* It is expected that you have registered all the [dev.inmo.micro_utils.startup.plugin.StartPlugin]s of your JS
|
||||
* app inside of [dev.inmo.micro_utils.startup.plugin.StartPluginSerializer] using its
|
||||
* [dev.inmo.micro_utils.startup.plugin.StartPluginSerializer.registerPlugin] method
|
||||
*/
|
||||
suspend fun startPlugins(json: JsonObject) {
|
||||
start(json)
|
||||
}
|
||||
/**
|
||||
* Will convert [config] to [JsonObject] with auto registration of [dev.inmo.micro_utils.startup.plugin.StartPlugin]s
|
||||
* in [dev.inmo.micro_utils.startup.plugin.StartPluginSerializer]
|
||||
*/
|
||||
suspend fun startPlugins(config: Config) {
|
||||
|
||||
KSLog.i("Start convert config to JSON")
|
||||
val json = defaultJson.encodeToJsonElement(Config.serializer(), config).jsonObject
|
||||
KSLog.i("Config has been read")
|
||||
|
||||
start(json)
|
||||
}
|
||||
}
|
3
startup/launcher/src/jsTest/kotlin/stopKoin.kt
Normal file
3
startup/launcher/src/jsTest/kotlin/stopKoin.kt
Normal file
@ -0,0 +1,3 @@
|
||||
import org.koin.core.context.GlobalContext
|
||||
|
||||
actual fun stopKoin() = GlobalContext.stopKoin()
|
@ -26,7 +26,7 @@ import java.io.File
|
||||
*/
|
||||
suspend fun main(args: Array<String>) {
|
||||
|
||||
KSLog.default = KSLog("ServerLauncher")
|
||||
KSLog.default = KSLog("Launcher")
|
||||
val (configPath) = args
|
||||
val file = File(configPath)
|
||||
KSLog.i("Start read config from ${file.absolutePath}")
|
||||
|
3
startup/launcher/src/jvmTest/kotlin/stopKoin.kt
Normal file
3
startup/launcher/src/jvmTest/kotlin/stopKoin.kt
Normal file
@ -0,0 +1,3 @@
|
||||
import org.koin.core.context.GlobalContext
|
||||
|
||||
actual fun stopKoin() = GlobalContext.stopKoin()
|
@ -16,5 +16,10 @@ kotlin {
|
||||
api project(":micro_utils.coroutines")
|
||||
}
|
||||
}
|
||||
jsMain {
|
||||
dependencies {
|
||||
api libs.uuid
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,20 +1,35 @@
|
||||
package dev.inmo.micro_utils.startup.plugin
|
||||
|
||||
import com.benasher44.uuid.uuid4
|
||||
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() = TODO("Not yet implemented")
|
||||
private val registeredPlugins = mutableMapOf<String, StartPlugin>()
|
||||
private val registeredPluginsByPlugin = mutableMapOf<StartPlugin, String>()
|
||||
override val descriptor: SerialDescriptor = String.serializer().descriptor
|
||||
|
||||
override fun deserialize(decoder: Decoder): StartPlugin {
|
||||
TODO("Not yet implemented")
|
||||
val name = decoder.decodeString()
|
||||
return registeredPlugins[name] ?: error("Unable to find startup plugin for $name")
|
||||
}
|
||||
|
||||
override fun serialize(encoder: Encoder, value: StartPlugin) {
|
||||
TODO("Not yet implemented")
|
||||
val name = registeredPluginsByPlugin[value] ?: uuid4().toString().also {
|
||||
registeredPlugins[it] = value
|
||||
registeredPluginsByPlugin[value] = it
|
||||
}
|
||||
encoder.encodeString(name)
|
||||
}
|
||||
|
||||
/**
|
||||
* Register plugin inside of this [KSerializer]. Since plugin has been registered, you may use its [name] in any
|
||||
* serialized [dev.inmo.micro_utils.startup.launcher.Config] to retrieve [plugin] you passed here
|
||||
*/
|
||||
fun registerPlugin(name: String, plugin: StartPlugin) {
|
||||
registeredPlugins[name] = plugin
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user