mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2025-09-17 14:29:24 +00:00
Compare commits
29 Commits
Author | SHA1 | Date | |
---|---|---|---|
592c5f3732 | |||
f44a78a5f5 | |||
e0bdd5dfdc | |||
99c0f06b72 | |||
66fc6df3d7 | |||
a36425a905 | |||
d920fee6d4 | |||
23590be5de | |||
94acc3c93b | |||
5616326a3b | |||
7601860c5c | |||
8b43d785cc | |||
b62d3a0b7d | |||
fad73c7213 | |||
2403c7c2b0 | |||
fa090bf920 | |||
a83ee86340 | |||
204955bcce | |||
ee56e9543a | |||
96fdff6ffd | |||
58b007cbb3 | |||
4f0c139889 | |||
c584c24fce | |||
85e5cee154 | |||
5af91981f1 | |||
2fe4f08059 | |||
83796f345a | |||
c1e21364a6 | |||
067d9d0d3b |
36
CHANGELOG.md
36
CHANGELOG.md
@@ -1,5 +1,41 @@
|
||||
# Changelog
|
||||
|
||||
## 0.16.6
|
||||
|
||||
* `Startup`:
|
||||
* `Launcher`:
|
||||
* Improvements in `StartLauncherPlugin#start` methods
|
||||
* Add opportunity to pass second argument on `JVM` platform as log level
|
||||
* `Repos`:
|
||||
* `Ktor`:
|
||||
* `Client`:
|
||||
* All clients repos got opportunity to customize their flows
|
||||
* `Exposed`:
|
||||
* Extensions `eqOrIsNull` and `neqOrIsNotNull` for `Column`
|
||||
|
||||
## 0.16.5
|
||||
|
||||
* `Versions`:
|
||||
* `Ktor`: `2.2.1` -> `2.2.2`
|
||||
|
||||
## 0.16.4
|
||||
|
||||
* `Coroutines`:
|
||||
* Create `launchInCurrentThread`
|
||||
|
||||
## 0.16.3
|
||||
|
||||
* `Startup`:
|
||||
* `Launcher`:
|
||||
* All starting API have been moved into `StartLauncherPlugin` and do not require serialize/deserialize cycle for now
|
||||
|
||||
## 0.16.2
|
||||
|
||||
* `Versions`:
|
||||
* `Compose`: `1.2.1` -> `1.2.2`
|
||||
* `Startup`:
|
||||
* Module become available on `JS` target
|
||||
|
||||
## 0.16.1
|
||||
|
||||
* `Coroutines`:
|
||||
|
@@ -22,6 +22,7 @@ kotlin {
|
||||
dependencies {
|
||||
api libs.kt.coroutines.android
|
||||
}
|
||||
dependsOn(jvmMain)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,9 @@
|
||||
package dev.inmo.micro_utils.coroutines
|
||||
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
|
||||
fun <T> launchInCurrentThread(block: suspend CoroutineScope.() -> T): T {
|
||||
val scope = CoroutineScope(Dispatchers.Unconfined)
|
||||
return scope.launchSynchronously(block)
|
||||
}
|
@@ -6,7 +6,7 @@ fun <T> CoroutineScope.launchSynchronously(block: suspend CoroutineScope.() -> T
|
||||
var result: Result<T>? = null
|
||||
val objectToSynchronize = Object()
|
||||
synchronized(objectToSynchronize) {
|
||||
launch {
|
||||
launch(start = CoroutineStart.UNDISPATCHED) {
|
||||
result = safelyWithResult(block)
|
||||
}.invokeOnCompletion {
|
||||
synchronized(objectToSynchronize) {
|
||||
|
@@ -0,0 +1,47 @@
|
||||
package dev.inmo.micro_utils.coroutines
|
||||
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class LaunchInCurrentThreadTests {
|
||||
@Test
|
||||
fun simpleTestThatLaunchInCurrentThreadWorks() {
|
||||
val expectedResult = 10
|
||||
val result = launchInCurrentThread {
|
||||
expectedResult
|
||||
}
|
||||
assertEquals(expectedResult, result)
|
||||
}
|
||||
@Test
|
||||
fun simpleTestThatSeveralLaunchInCurrentThreadWorks() {
|
||||
val testData = 0 until 100
|
||||
|
||||
testData.forEach {
|
||||
val result = launchInCurrentThread {
|
||||
it
|
||||
}
|
||||
assertEquals(it, result)
|
||||
}
|
||||
}
|
||||
@Test
|
||||
fun simpleTestThatLaunchInCurrentThreadWillCorrectlyHandleSuspensionsWorks() {
|
||||
val testData = 0 until 100
|
||||
|
||||
suspend fun test(data: Any): Any {
|
||||
return withContext(Dispatchers.Default) {
|
||||
delay(1)
|
||||
data
|
||||
}
|
||||
}
|
||||
|
||||
testData.forEach {
|
||||
val result = launchInCurrentThread {
|
||||
test(it)
|
||||
}
|
||||
assertEquals(it, result)
|
||||
}
|
||||
}
|
||||
}
|
@@ -23,6 +23,7 @@ allprojects {
|
||||
mppProjectWithSerializationPresetPath = "${rootProject.projectDir.absolutePath}/mppProjectWithSerialization.gradle"
|
||||
mppProjectWithSerializationAndComposePresetPath = "${rootProject.projectDir.absolutePath}/mppProjectWithSerializationAndCompose.gradle"
|
||||
mppJavaProjectPresetPath = "${rootProject.projectDir.absolutePath}/mppJavaProject.gradle"
|
||||
mppJsAndJavaProjectPresetPath = "${rootProject.projectDir.absolutePath}/mppJsAndJavaProject.gradle"
|
||||
mppAndroidProjectPresetPath = "${rootProject.projectDir.absolutePath}/mppAndroidProject.gradle"
|
||||
|
||||
defaultAndroidSettingsPresetPath = "${rootProject.projectDir.absolutePath}/defaultAndroidSettings.gradle"
|
||||
|
@@ -14,5 +14,5 @@ crypto_js_version=4.1.1
|
||||
# Project data
|
||||
|
||||
group=dev.inmo
|
||||
version=0.16.1
|
||||
android_code_version=169
|
||||
version=0.16.6
|
||||
android_code_version=174
|
||||
|
@@ -6,14 +6,14 @@ kt-coroutines = "1.6.4"
|
||||
|
||||
kslog = "0.5.4"
|
||||
|
||||
jb-compose = "1.2.1"
|
||||
jb-compose = "1.2.2"
|
||||
jb-exposed = "0.41.1"
|
||||
jb-dokka = "1.7.20"
|
||||
|
||||
klock = "3.4.0"
|
||||
uuid = "0.6.0"
|
||||
|
||||
ktor = "2.2.1"
|
||||
ktor = "2.2.2"
|
||||
|
||||
gh-release = "2.4.1"
|
||||
|
||||
|
@@ -19,7 +19,7 @@ import kotlinx.coroutines.isActive
|
||||
* connection. Must return true in case if must be reconnected. By default always reconnecting
|
||||
*/
|
||||
@Warning("This feature is internal and should not be used directly. It is can be changed without any notification and warranty on compile-time or other guaranties")
|
||||
inline fun <reified T : Any> openBaseWebSocketFlow(
|
||||
inline fun <T : Any> openBaseWebSocketFlow(
|
||||
noinline checkReconnection: suspend (Throwable?) -> Boolean = { true },
|
||||
noinline webSocketSessionRequest: suspend SendChannel<T>.() -> Unit
|
||||
): Flow<T> {
|
||||
@@ -57,7 +57,7 @@ inline fun <reified T : Any> HttpClient.openWebSocketFlow(
|
||||
): Flow<T> {
|
||||
pluginOrNull(WebSockets) ?: error("Plugin $WebSockets must be installed for using createStandardWebsocketFlow")
|
||||
|
||||
return openBaseWebSocketFlow<T>(checkReconnection) {
|
||||
return openBaseWebSocketFlow(checkReconnection) {
|
||||
val block: suspend DefaultClientWebSocketSession.() -> Unit = {
|
||||
while (isActive) {
|
||||
send(receiveDeserialized<T>())
|
||||
|
49
mppJsAndJavaProject.gradle
Normal file
49
mppJsAndJavaProject.gradle
Normal file
@@ -0,0 +1,49 @@
|
||||
project.version = "$version"
|
||||
project.group = "$group"
|
||||
|
||||
apply from: "$publishGradlePath"
|
||||
|
||||
kotlin {
|
||||
jvm {
|
||||
compilations.main {
|
||||
kotlinOptions {
|
||||
jvmTarget = "1.8"
|
||||
}
|
||||
}
|
||||
}
|
||||
js (IR) {
|
||||
browser()
|
||||
nodejs()
|
||||
}
|
||||
|
||||
sourceSets {
|
||||
commonMain {
|
||||
dependencies {
|
||||
implementation kotlin('stdlib')
|
||||
}
|
||||
}
|
||||
commonTest {
|
||||
dependencies {
|
||||
implementation kotlin('test-common')
|
||||
implementation kotlin('test-annotations-common')
|
||||
}
|
||||
}
|
||||
|
||||
jvmTest {
|
||||
dependencies {
|
||||
implementation kotlin('test-junit')
|
||||
}
|
||||
}
|
||||
jsTest {
|
||||
dependencies {
|
||||
implementation kotlin('test-js')
|
||||
implementation kotlin('test-junit')
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
java {
|
||||
sourceCompatibility = JavaVersion.VERSION_1_8
|
||||
targetCompatibility = JavaVersion.VERSION_1_8
|
||||
}
|
@@ -0,0 +1,23 @@
|
||||
package dev.inmo.micro_utils.repos.exposed
|
||||
|
||||
import org.jetbrains.exposed.sql.Column
|
||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
|
||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.isNotNull
|
||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.isNull
|
||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.neq
|
||||
|
||||
fun <T> Column<T?>.eqOrIsNull(
|
||||
value: T?
|
||||
) = if (value == null) {
|
||||
isNull()
|
||||
} else {
|
||||
eq(value)
|
||||
}
|
||||
|
||||
fun <T> Column<T?>.neqOrIsNotNull(
|
||||
value: T?
|
||||
) = if (value == null) {
|
||||
isNotNull()
|
||||
} else {
|
||||
neq(value)
|
||||
}
|
@@ -1,12 +1,17 @@
|
||||
package dev.inmo.micro_utils.repos.ktor.client.crud
|
||||
|
||||
import dev.inmo.micro_utils.ktor.client.createStandardWebsocketFlow
|
||||
import dev.inmo.micro_utils.ktor.common.*
|
||||
import dev.inmo.micro_utils.pagination.PaginationResult
|
||||
import dev.inmo.micro_utils.repos.*
|
||||
import dev.inmo.micro_utils.repos.ktor.common.crud.deletedObjectsIdsFlowRouting
|
||||
import dev.inmo.micro_utils.repos.ktor.common.crud.newObjectsFlowRouting
|
||||
import dev.inmo.micro_utils.repos.ktor.common.crud.updatedObjectsFlowRouting
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.util.reflect.TypeInfo
|
||||
import io.ktor.util.reflect.typeInfo
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.serialization.*
|
||||
|
||||
class KtorCRUDRepoClient<ObjectType, IdType, InputValue> (
|
||||
@@ -21,6 +26,15 @@ class KtorCRUDRepoClient<ObjectType, IdType, InputValue> (
|
||||
baseUrl: String,
|
||||
httpClient: HttpClient,
|
||||
contentType: ContentType,
|
||||
newObjectsFlow: Flow<ObjectType> = httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, newObjectsFlowRouting),
|
||||
),
|
||||
updatedObjectsFlow: Flow<ObjectType> = httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, updatedObjectsFlowRouting),
|
||||
),
|
||||
deletedObjectsIdsFlow: Flow<IdType> = httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, deletedObjectsIdsFlowRouting),
|
||||
),
|
||||
noinline idSerializer: suspend (IdType) -> String
|
||||
) = KtorCRUDRepoClient(
|
||||
KtorReadCRUDRepoClient(
|
||||
@@ -35,7 +49,10 @@ class KtorCRUDRepoClient<ObjectType, IdType, InputValue> (
|
||||
KtorWriteCrudRepoClient<ObjectType, IdType, InputValue>(
|
||||
baseUrl,
|
||||
httpClient,
|
||||
contentType
|
||||
contentType,
|
||||
newObjectsFlow,
|
||||
updatedObjectsFlow,
|
||||
deletedObjectsIdsFlow
|
||||
)
|
||||
)
|
||||
|
||||
@@ -44,11 +61,23 @@ class KtorCRUDRepoClient<ObjectType, IdType, InputValue> (
|
||||
subpart: String,
|
||||
httpClient: HttpClient,
|
||||
contentType: ContentType,
|
||||
newObjectsFlow: Flow<ObjectType> = httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, newObjectsFlowRouting),
|
||||
),
|
||||
updatedObjectsFlow: Flow<ObjectType> = httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, updatedObjectsFlowRouting),
|
||||
),
|
||||
deletedObjectsIdsFlow: Flow<IdType> = httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, deletedObjectsIdsFlowRouting),
|
||||
),
|
||||
noinline idSerializer: suspend (IdType) -> String
|
||||
) = KtorCRUDRepoClient<ObjectType, IdType, InputValue>(
|
||||
buildStandardUrl(baseUrl, subpart),
|
||||
httpClient,
|
||||
contentType,
|
||||
newObjectsFlow,
|
||||
updatedObjectsFlow,
|
||||
deletedObjectsIdsFlow,
|
||||
idSerializer
|
||||
)
|
||||
}
|
||||
@@ -80,11 +109,23 @@ inline fun <reified ObjectType, reified IdType, reified InputValue> KtorCRUDRepo
|
||||
subpart: String,
|
||||
httpClient: HttpClient,
|
||||
contentType: ContentType,
|
||||
newObjectsFlow: Flow<ObjectType> = httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, newObjectsFlowRouting),
|
||||
),
|
||||
updatedObjectsFlow: Flow<ObjectType> = httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, updatedObjectsFlowRouting),
|
||||
),
|
||||
deletedObjectsIdsFlow: Flow<IdType> = httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, deletedObjectsIdsFlowRouting),
|
||||
),
|
||||
noinline idSerializer: suspend (IdType) -> String
|
||||
) = KtorCRUDRepoClient<ObjectType, IdType, InputValue>(
|
||||
buildStandardUrl(baseUrl, subpart),
|
||||
httpClient,
|
||||
contentType,
|
||||
newObjectsFlow,
|
||||
updatedObjectsFlow,
|
||||
deletedObjectsIdsFlow,
|
||||
idSerializer
|
||||
)
|
||||
|
||||
|
@@ -53,19 +53,22 @@ class KtorWriteCrudRepoClient<ObjectType, IdType, InputValue> (
|
||||
inline operator fun <reified ObjectType, reified IdType, reified InputValue> invoke(
|
||||
baseUrl: String,
|
||||
httpClient: HttpClient,
|
||||
contentType: ContentType
|
||||
contentType: ContentType,
|
||||
newObjectsFlow: Flow<ObjectType> = httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, newObjectsFlowRouting),
|
||||
),
|
||||
updatedObjectsFlow: Flow<ObjectType> = httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, updatedObjectsFlowRouting),
|
||||
),
|
||||
deletedObjectsIdsFlow: Flow<IdType> = httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, deletedObjectsIdsFlowRouting),
|
||||
),
|
||||
) = KtorWriteCrudRepoClient<ObjectType, IdType, InputValue>(
|
||||
baseUrl,
|
||||
httpClient,
|
||||
httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, newObjectsFlowRouting),
|
||||
),
|
||||
httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, updatedObjectsFlowRouting),
|
||||
),
|
||||
httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, deletedObjectsIdsFlowRouting),
|
||||
),
|
||||
newObjectsFlow,
|
||||
updatedObjectsFlow,
|
||||
deletedObjectsIdsFlow,
|
||||
{
|
||||
contentType(contentType)
|
||||
setBody(it)
|
||||
|
@@ -1,10 +1,14 @@
|
||||
package dev.inmo.micro_utils.repos.ktor.client.key.value
|
||||
|
||||
import dev.inmo.micro_utils.ktor.client.createStandardWebsocketFlow
|
||||
import dev.inmo.micro_utils.ktor.common.*
|
||||
import dev.inmo.micro_utils.repos.*
|
||||
import dev.inmo.micro_utils.repos.ktor.common.key_value.onNewValueRoute
|
||||
import dev.inmo.micro_utils.repos.ktor.common.key_value.onValueRemovedRoute
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.http.encodeURLQueryComponent
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.serialization.*
|
||||
|
||||
class KtorKeyValueRepoClient<Key, Value> (
|
||||
@@ -20,6 +24,12 @@ class KtorKeyValueRepoClient<Key, Value> (
|
||||
httpClient: HttpClient,
|
||||
contentType: ContentType,
|
||||
noinline idSerializer: suspend (Key) -> String,
|
||||
onNewValue: Flow<Pair<Key, Value>> = httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, onNewValueRoute),
|
||||
),
|
||||
onValueRemoved: Flow<Key> = httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, onValueRemovedRoute),
|
||||
),
|
||||
noinline valueSerializer: suspend (Value) -> String
|
||||
) = KtorKeyValueRepoClient(
|
||||
KtorReadKeyValueRepoClient(
|
||||
@@ -28,7 +38,9 @@ class KtorKeyValueRepoClient<Key, Value> (
|
||||
KtorWriteKeyValueRepoClient(
|
||||
baseUrl,
|
||||
httpClient,
|
||||
contentType
|
||||
contentType,
|
||||
onNewValue,
|
||||
onValueRemoved
|
||||
)
|
||||
)
|
||||
inline operator fun <reified Key, reified Value> invoke(
|
||||
@@ -37,12 +49,20 @@ class KtorKeyValueRepoClient<Key, Value> (
|
||||
httpClient: HttpClient,
|
||||
contentType: ContentType,
|
||||
noinline idSerializer: suspend (Key) -> String,
|
||||
onNewValue: Flow<Pair<Key, Value>> = httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, onNewValueRoute),
|
||||
),
|
||||
onValueRemoved: Flow<Key> = httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, onValueRemovedRoute),
|
||||
),
|
||||
noinline valueSerializer: suspend (Value) -> String
|
||||
) = KtorKeyValueRepoClient(
|
||||
buildStandardUrl(baseUrl, subpart),
|
||||
httpClient,
|
||||
contentType,
|
||||
idSerializer,
|
||||
onNewValue,
|
||||
onValueRemoved,
|
||||
valueSerializer
|
||||
)
|
||||
}
|
||||
|
@@ -60,17 +60,19 @@ class KtorWriteKeyValueRepoClient<Key, Value>(
|
||||
inline operator fun <reified Key, reified Value> invoke(
|
||||
baseUrl: String,
|
||||
httpClient: HttpClient,
|
||||
contentType: ContentType
|
||||
contentType: ContentType,
|
||||
onNewValue: Flow<Pair<Key, Value>> = httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, onNewValueRoute),
|
||||
),
|
||||
onValueRemoved: Flow<Key> = httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, onValueRemovedRoute),
|
||||
),
|
||||
) = KtorWriteKeyValueRepoClient<Key, Value>(
|
||||
baseUrl,
|
||||
httpClient,
|
||||
contentType,
|
||||
httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, onNewValueRoute),
|
||||
),
|
||||
httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, onValueRemovedRoute),
|
||||
),
|
||||
onNewValue,
|
||||
onValueRemoved,
|
||||
typeInfo<List<Key>>(),
|
||||
typeInfo<List<Value>>(),
|
||||
typeInfo<Map<Key, Value>>()
|
||||
|
@@ -1,10 +1,15 @@
|
||||
package dev.inmo.micro_utils.repos.ktor.client.key.values
|
||||
|
||||
import dev.inmo.micro_utils.ktor.client.createStandardWebsocketFlow
|
||||
import dev.inmo.micro_utils.ktor.common.*
|
||||
import dev.inmo.micro_utils.repos.*
|
||||
import dev.inmo.micro_utils.repos.ktor.common.one_to_many.onDataClearedRoute
|
||||
import dev.inmo.micro_utils.repos.ktor.common.one_to_many.onNewValueRoute
|
||||
import dev.inmo.micro_utils.repos.ktor.common.one_to_many.onValueRemovedRoute
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.http.encodeURLQueryComponent
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.serialization.*
|
||||
|
||||
class KtorKeyValuesRepoClient<Key, Value> (
|
||||
@@ -20,6 +25,15 @@ class KtorKeyValuesRepoClient<Key, Value> (
|
||||
httpClient: HttpClient,
|
||||
contentType: ContentType,
|
||||
noinline keySerializer: suspend (Key) -> String,
|
||||
onNewValue: Flow<Pair<Key, Value>> = httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, onNewValueRoute),
|
||||
),
|
||||
onValueRemoved: Flow<Pair<Key, Value>> = httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, onValueRemovedRoute),
|
||||
),
|
||||
onDataCleared: Flow<Key> = httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, onDataClearedRoute),
|
||||
),
|
||||
noinline valueSerializer: suspend (Value) -> String
|
||||
) = KtorKeyValuesRepoClient(
|
||||
KtorReadKeyValuesRepoClient(
|
||||
@@ -32,7 +46,10 @@ class KtorKeyValuesRepoClient<Key, Value> (
|
||||
KtorWriteKeyValuesRepoClient(
|
||||
baseUrl,
|
||||
httpClient,
|
||||
contentType
|
||||
contentType,
|
||||
onNewValue,
|
||||
onValueRemoved,
|
||||
onDataCleared
|
||||
)
|
||||
)
|
||||
inline operator fun <reified Key : Any, reified Value : Any> invoke(
|
||||
@@ -41,12 +58,24 @@ class KtorKeyValuesRepoClient<Key, Value> (
|
||||
httpClient: HttpClient,
|
||||
contentType: ContentType,
|
||||
noinline keySerializer: suspend (Key) -> String,
|
||||
onNewValue: Flow<Pair<Key, Value>> = httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, onNewValueRoute),
|
||||
),
|
||||
onValueRemoved: Flow<Pair<Key, Value>> = httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, onValueRemovedRoute),
|
||||
),
|
||||
onDataCleared: Flow<Key> = httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, onDataClearedRoute),
|
||||
),
|
||||
noinline valueSerializer: suspend (Value) -> String
|
||||
) = KtorKeyValuesRepoClient(
|
||||
buildStandardUrl(baseUrl, subpart),
|
||||
httpClient,
|
||||
contentType,
|
||||
keySerializer,
|
||||
onNewValue,
|
||||
onValueRemoved,
|
||||
onDataCleared,
|
||||
valueSerializer
|
||||
)
|
||||
}
|
||||
@@ -59,13 +88,25 @@ inline fun <reified Key : Any, reified Value : Any> KtorKeyValuesRepoClient(
|
||||
keySerializer: SerializationStrategy<Key>,
|
||||
valueSerializer: SerializationStrategy<Value>,
|
||||
serialFormat: StringFormat,
|
||||
onNewValue: Flow<Pair<Key, Value>> = httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, onNewValueRoute),
|
||||
),
|
||||
onValueRemoved: Flow<Pair<Key, Value>> = httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, onValueRemovedRoute),
|
||||
),
|
||||
onDataCleared: Flow<Key> = httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, onDataClearedRoute),
|
||||
),
|
||||
) = KtorKeyValuesRepoClient<Key, Value>(
|
||||
baseUrl,
|
||||
httpClient,
|
||||
contentType,
|
||||
{
|
||||
serialFormat.encodeToString(keySerializer, it).encodeURLQueryComponent()
|
||||
}
|
||||
},
|
||||
onNewValue,
|
||||
onValueRemoved,
|
||||
onDataCleared
|
||||
) {
|
||||
serialFormat.encodeToString(valueSerializer, it).encodeURLQueryComponent()
|
||||
}
|
||||
@@ -77,13 +118,25 @@ inline fun <reified Key : Any, reified Value : Any> KtorKeyValuesRepoClient(
|
||||
keySerializer: SerializationStrategy<Key>,
|
||||
valueSerializer: SerializationStrategy<Value>,
|
||||
serialFormat: BinaryFormat,
|
||||
onNewValue: Flow<Pair<Key, Value>> = httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, onNewValueRoute),
|
||||
),
|
||||
onValueRemoved: Flow<Pair<Key, Value>> = httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, onValueRemovedRoute),
|
||||
),
|
||||
onDataCleared: Flow<Key> = httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, onDataClearedRoute),
|
||||
),
|
||||
) = KtorKeyValuesRepoClient<Key, Value>(
|
||||
baseUrl,
|
||||
httpClient,
|
||||
contentType,
|
||||
{
|
||||
serialFormat.encodeHex(keySerializer, it)
|
||||
}
|
||||
},
|
||||
onNewValue,
|
||||
onValueRemoved,
|
||||
onDataCleared
|
||||
) {
|
||||
serialFormat.encodeHex(valueSerializer, it)
|
||||
}
|
||||
|
@@ -84,20 +84,23 @@ class KtorWriteKeyValuesRepoClient<Key : Any, Value : Any>(
|
||||
inline operator fun <reified Key : Any, reified Value : Any> invoke(
|
||||
baseUrl: String,
|
||||
httpClient: HttpClient,
|
||||
contentType: ContentType
|
||||
contentType: ContentType,
|
||||
onNewValue: Flow<Pair<Key, Value>> = httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, onNewValueRoute),
|
||||
),
|
||||
onValueRemoved: Flow<Pair<Key, Value>> = httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, onValueRemovedRoute),
|
||||
),
|
||||
onDataCleared: Flow<Key> = httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, onDataClearedRoute),
|
||||
),
|
||||
) = KtorWriteKeyValuesRepoClient<Key, Value>(
|
||||
baseUrl,
|
||||
httpClient,
|
||||
contentType,
|
||||
httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, onNewValueRoute),
|
||||
),
|
||||
httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, onValueRemovedRoute),
|
||||
),
|
||||
httpClient.createStandardWebsocketFlow(
|
||||
buildStandardUrl(baseUrl, onDataClearedRoute),
|
||||
),
|
||||
onNewValue,
|
||||
onValueRemoved,
|
||||
onDataCleared,
|
||||
typeInfo<Key>(),
|
||||
typeInfo<Value>(),
|
||||
typeInfo<Map<Key, List<Value>>>()
|
||||
|
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,10 +1,8 @@
|
||||
package dev.inmo.micro_utils.startup.launcher
|
||||
|
||||
import dev.inmo.kslog.common.i
|
||||
import dev.inmo.micro_utils.startup.launcher.StartLauncherPlugin.setupDI
|
||||
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
|
||||
@@ -13,19 +11,7 @@ import org.koin.dsl.module
|
||||
* @param rawConfig It is expected that this [JsonObject] will contain serialized [Config] ([StartLauncherPlugin] will
|
||||
* deserialize it in its [StartLauncherPlugin.setupDI]
|
||||
*/
|
||||
@Deprecated("Fully replaced with StartLauncherPlugin#start", ReplaceWith("StartLauncherPlugin.start(rawConfig)", "dev.inmo.micro_utils.startup.launcher.StartLauncherPlugin"))
|
||||
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")
|
||||
}
|
||||
StartLauncherPlugin.start(rawConfig)
|
||||
}
|
||||
|
@@ -4,6 +4,8 @@ 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.launcher.StartLauncherPlugin.setupDI
|
||||
import dev.inmo.micro_utils.startup.launcher.StartLauncherPlugin.startPlugin
|
||||
import dev.inmo.micro_utils.startup.plugin.StartPlugin
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
@@ -12,7 +14,11 @@ import kotlinx.coroutines.launch
|
||||
import kotlinx.serialization.SerialFormat
|
||||
import kotlinx.serialization.StringFormat
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
import kotlinx.serialization.json.decodeFromJsonElement
|
||||
import kotlinx.serialization.json.jsonObject
|
||||
import org.koin.core.Koin
|
||||
import org.koin.core.KoinApplication
|
||||
import org.koin.core.context.startKoin
|
||||
import org.koin.core.module.Module
|
||||
import org.koin.dsl.binds
|
||||
import org.koin.dsl.module
|
||||
@@ -23,26 +29,20 @@ import org.koin.dsl.module
|
||||
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)
|
||||
fun Module.setupDI(config: Config, rawJsonObject: JsonObject? = null) {
|
||||
val rawJsonObject = rawJsonObject ?: defaultJson.encodeToJsonElement(Config.serializer(), config).jsonObject
|
||||
|
||||
single { pluginsConfig }
|
||||
single { rawJsonObject }
|
||||
single { config }
|
||||
single { CoroutineScope(Dispatchers.Default) }
|
||||
single { defaultJson } binds arrayOf(StringFormat::class, SerialFormat::class)
|
||||
|
||||
includes(
|
||||
pluginsConfig.plugins.mapNotNull {
|
||||
config.plugins.mapNotNull {
|
||||
runCatching {
|
||||
module {
|
||||
with(it) {
|
||||
setupDI(config)
|
||||
setupDI(rawJsonObject)
|
||||
}
|
||||
}
|
||||
}.onFailure { e ->
|
||||
@@ -52,11 +52,28 @@ object StartLauncherPlugin : StartPlugin {
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
logger.i("Koin for current module has started setup")
|
||||
setupDI(
|
||||
defaultJson.decodeFromJsonElement(Config.serializer(), config),
|
||||
config
|
||||
)
|
||||
logger.i("Koin for current module has been setup")
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
logger.i("Start starting of subplugins")
|
||||
val scope = koin.get<CoroutineScope>()
|
||||
koin.get<Config>().plugins.map { plugin ->
|
||||
scope.launch {
|
||||
@@ -66,11 +83,61 @@ object StartLauncherPlugin : StartPlugin {
|
||||
startPlugin(koin)
|
||||
}
|
||||
}.onFailure { e ->
|
||||
logger.w("Unable to load bot part of $plugin", e)
|
||||
logger.w("Unable to start plugin $plugin", e)
|
||||
}.onSuccess {
|
||||
logger.i("Complete loading of $plugin")
|
||||
}
|
||||
}
|
||||
}.joinAll()
|
||||
logger.i("Complete subplugins start")
|
||||
}
|
||||
|
||||
/**
|
||||
* Will create [KoinApplication], init, load modules using [StartLauncherPlugin] and start plugins using the same base
|
||||
* plugin. It is basic [start] method which accepts both [config] and [rawConfig] which suppose to be the same or
|
||||
* at least [rawConfig] must contain serialized variant of [config]
|
||||
*
|
||||
* @param rawConfig It is expected that this [JsonObject] will contain serialized [Config] ([StartLauncherPlugin] will
|
||||
* deserialize it in its [StartLauncherPlugin.setupDI]
|
||||
*/
|
||||
suspend fun start(config: Config, rawConfig: JsonObject) {
|
||||
|
||||
logger.i("Start initialization")
|
||||
val koinApp = KoinApplication.init()
|
||||
koinApp.modules(
|
||||
module {
|
||||
setupDI(config, rawConfig)
|
||||
}
|
||||
)
|
||||
logger.i("Modules loaded")
|
||||
startKoin(koinApp)
|
||||
logger.i("Koin started")
|
||||
startPlugin(koinApp.koin)
|
||||
logger.i("App has been setup")
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Call [start] with deserialized [Config] as config and [rawConfig] as is
|
||||
*
|
||||
* @param rawConfig It is expected that this [JsonObject] will contain serialized [Config]
|
||||
*/
|
||||
suspend fun start(rawConfig: JsonObject) {
|
||||
|
||||
start(defaultJson.decodeFromJsonElement(Config.serializer(), rawConfig), rawConfig)
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Call [start] with deserialized [Config] as is and serialize it to [JsonObject] to pass as the first parameter
|
||||
* to the basic [start] method
|
||||
*
|
||||
* @param config Will be converted to [JsonObject] as raw config. That means that all plugins from [config] will
|
||||
* receive serialized version of [config] in [StartPlugin.setupDI] method
|
||||
*/
|
||||
suspend fun start(config: Config) {
|
||||
|
||||
start(config, defaultJson.encodeToJsonElement(Config.serializer(), config).jsonObject)
|
||||
|
||||
}
|
||||
}
|
||||
|
@@ -1,21 +1,20 @@
|
||||
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.StartLauncherPlugin
|
||||
import dev.inmo.micro_utils.startup.launcher.defaultJson
|
||||
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 org.koin.core.context.stopKoin
|
||||
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(),
|
||||
@@ -24,12 +23,12 @@ class StartupLaunchingTests {
|
||||
|
||||
runTest {
|
||||
val job = launch {
|
||||
start(emptyJson)
|
||||
StartLauncherPlugin.start(emptyJson)
|
||||
}
|
||||
job.join()
|
||||
}
|
||||
}
|
||||
@Test(timeout = 60000L)
|
||||
@Test
|
||||
fun CheckThatHelloWorldPluginsListLeadsToEndOfMain() {
|
||||
val emptyJson = defaultJson.encodeToJsonElement(
|
||||
Config.serializer(),
|
||||
@@ -38,7 +37,7 @@ class StartupLaunchingTests {
|
||||
|
||||
runTest {
|
||||
val job = launch {
|
||||
start(emptyJson)
|
||||
StartLauncherPlugin.start(emptyJson)
|
||||
}
|
||||
job.join()
|
||||
}
|
25
startup/launcher/src/jsMain/kotlin/PluginsStarter.kt
Normal file
25
startup/launcher/src/jsMain/kotlin/PluginsStarter.kt
Normal file
@@ -0,0 +1,25 @@
|
||||
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
|
||||
|
||||
@Deprecated("Useless due to including of the same functionality in StrtLauncherPlugin")
|
||||
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) = StartLauncherPlugin.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) = StartLauncherPlugin.start(config)
|
||||
}
|
@@ -1,6 +1,7 @@
|
||||
package dev.inmo.micro_utils.startup.launcher
|
||||
|
||||
import dev.inmo.kslog.common.KSLog
|
||||
import dev.inmo.kslog.common.LogLevel
|
||||
import dev.inmo.kslog.common.i
|
||||
import kotlinx.serialization.json.jsonObject
|
||||
import java.io.File
|
||||
@@ -23,15 +24,30 @@ import java.io.File
|
||||
* 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`
|
||||
*
|
||||
* ## Debug mode
|
||||
*
|
||||
* You may pass the second parameter, one of [LogLevel] enum variants to setup [KSLog] minimal logging level. Sample:
|
||||
*
|
||||
* ```bash
|
||||
* ./gradlew run --args="sample.config.json DEBUG" // enable debugging output
|
||||
* ```
|
||||
*
|
||||
* OR
|
||||
* ```bash
|
||||
* ./gradlew run --args="sample.config.json WARNING" // enable logging since WARNING
|
||||
* ```
|
||||
*
|
||||
* **Default level is [LogLevel.INFO]**
|
||||
*/
|
||||
suspend fun main(args: Array<String>) {
|
||||
|
||||
KSLog.default = KSLog("ServerLauncher")
|
||||
KSLog.default = KSLog("Launcher", args.getOrNull(1) ?.let { LogLevel.valueOf(it) } ?: LogLevel.INFO)
|
||||
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)
|
||||
StartLauncherPlugin.start(json)
|
||||
}
|
||||
|
@@ -3,7 +3,7 @@ plugins {
|
||||
id "org.jetbrains.kotlin.plugin.serialization"
|
||||
}
|
||||
|
||||
apply from: "$mppJavaProjectPresetPath"
|
||||
apply from: "$mppJsAndJavaProjectPresetPath"
|
||||
|
||||
kotlin {
|
||||
sourceSets {
|
||||
@@ -16,5 +16,10 @@ kotlin {
|
||||
api project(":micro_utils.coroutines")
|
||||
}
|
||||
}
|
||||
jsMain {
|
||||
dependencies {
|
||||
api libs.uuid
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,38 @@
|
||||
package dev.inmo.micro_utils.startup.plugin
|
||||
|
||||
import com.benasher44.uuid.uuid4
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
/**
|
||||
* Creates [T] using [block], register it in [StartPluginSerializer] using its [StartPluginSerializer.registerPlugin]
|
||||
* and returns created by [block] plugin
|
||||
*
|
||||
* @param name Will be used as a key for registration in [StartPluginSerializer] and will be passed to the [block] as
|
||||
* parameter
|
||||
*/
|
||||
inline fun <T : StartPlugin> createStartupPluginAndRegister(
|
||||
name: String = uuid4().toString(),
|
||||
block: (String) -> T
|
||||
): T {
|
||||
val plugin = block(name)
|
||||
StartPluginSerializer.registerPlugin(name, plugin)
|
||||
return plugin
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates [T] using [block], register it in [StartPluginSerializer] using its [StartPluginSerializer.registerPlugin]
|
||||
* and returns created by [block] plugin
|
||||
*/
|
||||
inline fun <T : StartPlugin> createStartupPluginAndRegister(
|
||||
kClass: KClass<T>,
|
||||
name: String = uuid4().toString(),
|
||||
block: (String) -> T
|
||||
): T = createStartupPluginAndRegister("${kClass.simpleName}_$name", block)
|
||||
|
||||
/**
|
||||
* Creates [T] using [block], register it in [StartPluginSerializer] using its [StartPluginSerializer.registerPlugin]
|
||||
* and returns created by [block] plugin
|
||||
*/
|
||||
inline fun <reified T : StartPlugin> createStartupPluginAndRegister(
|
||||
block: (String) -> T
|
||||
): T = createStartupPluginAndRegister(T::class, uuid4().toString(), block)
|
35
startup/plugin/src/jsMain/kotlin/StartPluginSerializer.kt
Normal file
35
startup/plugin/src/jsMain/kotlin/StartPluginSerializer.kt
Normal file
@@ -0,0 +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> {
|
||||
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 {
|
||||
val name = decoder.decodeString()
|
||||
return registeredPlugins[name] ?: error("Unable to find startup plugin for $name")
|
||||
}
|
||||
|
||||
override fun serialize(encoder: Encoder, value: StartPlugin) {
|
||||
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
|
||||
}
|
||||
}
|
10
startup/template/README.md
Normal file
10
startup/template/README.md
Normal file
@@ -0,0 +1,10 @@
|
||||
# How to use
|
||||
|
||||
In case you have multiplatform project and wish to use startup plugin, this template may help you to create new modules.
|
||||
|
||||
1. Copy-paste whole template folder (you may clone this folder to your project and actualize some data to copy your prepared template)
|
||||
2. Replace `group_name` by your project (or root module) group name
|
||||
3. Replace `module_name` by the name of your new module name
|
||||
|
||||
You may read about the `build.gradle` structure in these templates in project
|
||||
[KotlinMultiplatformProjectTemplate](https://github.com/InsanusMokrassar/KotlinMultiplatformProjectTemplate).
|
18
startup/template/client/build.gradle
Normal file
18
startup/template/client/build.gradle
Normal file
@@ -0,0 +1,18 @@
|
||||
plugins {
|
||||
id "org.jetbrains.kotlin.multiplatform"
|
||||
id "org.jetbrains.kotlin.plugin.serialization"
|
||||
id "com.android.library"
|
||||
alias(libs.plugins.compose)
|
||||
}
|
||||
|
||||
apply from: "$mppProjectWithSerializationPresetPath"
|
||||
|
||||
kotlin {
|
||||
sourceSets {
|
||||
commonMain {
|
||||
dependencies {
|
||||
api project(":${rootProject.name}.module_name.common")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,15 @@
|
||||
package group_name.module_name.client
|
||||
|
||||
import dev.inmo.micro_utils.startup.plugin.StartPlugin
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
import org.koin.core.Koin
|
||||
import org.koin.core.module.Module
|
||||
|
||||
object ClientPlugin : StartPlugin {
|
||||
override fun Module.setupDI(config: JsonObject) {
|
||||
}
|
||||
|
||||
override suspend fun startPlugin(koin: Koin) {
|
||||
super.startPlugin(koin)
|
||||
}
|
||||
}
|
20
startup/template/client/src/jsMain/kotlin/ClientJSPlugin.kt
Normal file
20
startup/template/client/src/jsMain/kotlin/ClientJSPlugin.kt
Normal file
@@ -0,0 +1,20 @@
|
||||
package group_name.module_name.client
|
||||
|
||||
import group_name.module_name.common.CommonJSPlugin
|
||||
import dev.inmo.micro_utils.startup.plugin.StartPlugin
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
import org.koin.core.Koin
|
||||
import org.koin.core.module.Module
|
||||
|
||||
object ClientJSPlugin : StartPlugin {
|
||||
override fun Module.setupDI(config: JsonObject) {
|
||||
with(CommonJSPlugin) { setupDI(config) }
|
||||
with(ClientPlugin) { setupDI(config) }
|
||||
}
|
||||
|
||||
override suspend fun startPlugin(koin: Koin) {
|
||||
super.startPlugin(koin)
|
||||
CommonJSPlugin.startPlugin(koin)
|
||||
ClientPlugin.startPlugin(koin)
|
||||
}
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
package group_name.module_name.client
|
||||
|
||||
import dev.inmo.micro_utils.startup.plugin.createStartupPluginAndRegister
|
||||
|
||||
@ExperimentalStdlibApi
|
||||
@EagerInitialization
|
||||
@JsExport
|
||||
@ExperimentalJsExport
|
||||
private val jsModuleLoader = createStartupPluginAndRegister("template.ClientJSPlugin") { ClientJSPlugin }
|
@@ -0,0 +1,21 @@
|
||||
package group_name.module_name.client
|
||||
|
||||
import group_name.module_name.common.CommonJVMPlugin
|
||||
import group_name.module_name.common.CommonJVMPlugin.setupDI
|
||||
import dev.inmo.micro_utils.startup.plugin.StartPlugin
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
import org.koin.core.Koin
|
||||
import org.koin.core.module.Module
|
||||
|
||||
object ClientJVMPlugin : StartPlugin {
|
||||
override fun Module.setupDI(config: JsonObject) {
|
||||
with(CommonJVMPlugin) { setupDI(config) }
|
||||
with(ClientPlugin) { setupDI(config) }
|
||||
}
|
||||
|
||||
override suspend fun startPlugin(koin: Koin) {
|
||||
super.startPlugin(koin)
|
||||
CommonJVMPlugin.startPlugin(koin)
|
||||
ClientPlugin.startPlugin(koin)
|
||||
}
|
||||
}
|
1
startup/template/client/src/main/AndroidManifest.xml
Normal file
1
startup/template/client/src/main/AndroidManifest.xml
Normal file
@@ -0,0 +1 @@
|
||||
<manifest package="group_name.module_name.client"/>
|
@@ -0,0 +1,21 @@
|
||||
package group_name.module_name.client
|
||||
|
||||
import group_name.module_name.common.CommonAndroidPlugin
|
||||
import group_name.module_name.common.CommonAndroidPlugin.setupDI
|
||||
import dev.inmo.micro_utils.startup.plugin.StartPlugin
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
import org.koin.core.Koin
|
||||
import org.koin.core.module.Module
|
||||
|
||||
object ClientAndroidPlugin : StartPlugin {
|
||||
override fun Module.setupDI(config: JsonObject) {
|
||||
with(CommonAndroidPlugin) { setupDI(config) }
|
||||
with(ClientPlugin) { setupDI(config) }
|
||||
}
|
||||
|
||||
override suspend fun startPlugin(koin: Koin) {
|
||||
super.startPlugin(koin)
|
||||
CommonAndroidPlugin.startPlugin(koin)
|
||||
ClientPlugin.startPlugin(koin)
|
||||
}
|
||||
}
|
7
startup/template/common/build.gradle
Normal file
7
startup/template/common/build.gradle
Normal file
@@ -0,0 +1,7 @@
|
||||
plugins {
|
||||
id "org.jetbrains.kotlin.multiplatform"
|
||||
id "org.jetbrains.kotlin.plugin.serialization"
|
||||
id "com.android.library"
|
||||
}
|
||||
|
||||
apply from: "$mppProjectWithSerializationPresetPath"
|
@@ -0,0 +1,11 @@
|
||||
package group_name.module_name.common
|
||||
|
||||
import dev.inmo.micro_utils.startup.plugin.StartPlugin
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
import org.koin.core.module.Module
|
||||
|
||||
object CommonPlugin : StartPlugin {
|
||||
override fun Module.setupDI(config: JsonObject) {
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1 @@
|
||||
package group_name.module_name.common
|
17
startup/template/common/src/jsMain/kotlin/CommonJSPlugin.kt
Normal file
17
startup/template/common/src/jsMain/kotlin/CommonJSPlugin.kt
Normal file
@@ -0,0 +1,17 @@
|
||||
package group_name.module_name.common
|
||||
|
||||
import dev.inmo.micro_utils.startup.plugin.StartPlugin
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
import org.koin.core.Koin
|
||||
import org.koin.core.module.Module
|
||||
|
||||
object CommonJSPlugin : StartPlugin {
|
||||
override fun Module.setupDI(config: JsonObject) {
|
||||
with (CommonPlugin) { setupDI(config) }
|
||||
}
|
||||
|
||||
override suspend fun startPlugin(koin: Koin) {
|
||||
super.startPlugin(koin)
|
||||
CommonPlugin.startPlugin(koin)
|
||||
}
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
package group_name.module_name.common
|
||||
|
||||
import dev.inmo.micro_utils.startup.plugin.createStartupPluginAndRegister
|
||||
|
||||
@ExperimentalStdlibApi
|
||||
@EagerInitialization
|
||||
@JsExport
|
||||
@ExperimentalJsExport
|
||||
private val jsModuleLoader = createStartupPluginAndRegister("template.CommonJSPlugin") { CommonJSPlugin }
|
@@ -0,0 +1,17 @@
|
||||
package group_name.module_name.common
|
||||
|
||||
import dev.inmo.micro_utils.startup.plugin.StartPlugin
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
import org.koin.core.Koin
|
||||
import org.koin.core.module.Module
|
||||
|
||||
object CommonJVMPlugin : StartPlugin {
|
||||
override fun Module.setupDI(config: JsonObject) {
|
||||
with (CommonPlugin) { setupDI(config) }
|
||||
}
|
||||
|
||||
override suspend fun startPlugin(koin: Koin) {
|
||||
super.startPlugin(koin)
|
||||
CommonPlugin.startPlugin(koin)
|
||||
}
|
||||
}
|
1
startup/template/common/src/main/AndroidManifest.xml
Normal file
1
startup/template/common/src/main/AndroidManifest.xml
Normal file
@@ -0,0 +1 @@
|
||||
<manifest package="group_name.module_name.common"/>
|
@@ -0,0 +1,17 @@
|
||||
package group_name.module_name.common
|
||||
|
||||
import dev.inmo.micro_utils.startup.plugin.StartPlugin
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
import org.koin.core.Koin
|
||||
import org.koin.core.module.Module
|
||||
|
||||
object CommonAndroidPlugin : StartPlugin {
|
||||
override fun Module.setupDI(config: JsonObject) {
|
||||
with (CommonPlugin) { setupDI(config) }
|
||||
}
|
||||
|
||||
override suspend fun startPlugin(koin: Koin) {
|
||||
super.startPlugin(koin)
|
||||
CommonPlugin.startPlugin(koin)
|
||||
}
|
||||
}
|
16
startup/template/server/build.gradle
Normal file
16
startup/template/server/build.gradle
Normal file
@@ -0,0 +1,16 @@
|
||||
plugins {
|
||||
id "org.jetbrains.kotlin.multiplatform"
|
||||
id "org.jetbrains.kotlin.plugin.serialization"
|
||||
}
|
||||
|
||||
apply from: "$mppJavaProjectPresetPath"
|
||||
|
||||
kotlin {
|
||||
sourceSets {
|
||||
commonMain {
|
||||
dependencies {
|
||||
api project(":${rootProject.name}.module_name.common")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,15 @@
|
||||
package group_name.module_name.server
|
||||
|
||||
import dev.inmo.micro_utils.startup.plugin.StartPlugin
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
import org.koin.core.Koin
|
||||
import org.koin.core.module.Module
|
||||
|
||||
object ServerPlugin : StartPlugin {
|
||||
override fun Module.setupDI(config: JsonObject) {
|
||||
}
|
||||
|
||||
override suspend fun startPlugin(koin: Koin) {
|
||||
super.startPlugin(koin)
|
||||
}
|
||||
}
|
@@ -0,0 +1,20 @@
|
||||
package group_name.module_name.server
|
||||
|
||||
import group_name.module_name.common.CommonJVMPlugin
|
||||
import dev.inmo.micro_utils.startup.plugin.StartPlugin
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
import org.koin.core.Koin
|
||||
import org.koin.core.module.Module
|
||||
|
||||
object ServerJVMPlugin : StartPlugin {
|
||||
override fun Module.setupDI(config: JsonObject) {
|
||||
with(CommonJVMPlugin) { setupDI(config) }
|
||||
with(ServerPlugin) { setupDI(config) }
|
||||
}
|
||||
|
||||
override suspend fun startPlugin(koin: Koin) {
|
||||
super.startPlugin(koin)
|
||||
CommonJVMPlugin.startPlugin(koin)
|
||||
ServerPlugin.startPlugin(koin)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user