aboutsummaryrefslogtreecommitdiff
path: root/kvision-modules
diff options
context:
space:
mode:
Diffstat (limited to 'kvision-modules')
-rw-r--r--kvision-modules/kvision-common/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt14
-rw-r--r--kvision-modules/kvision-common/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt13
-rw-r--r--kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt155
-rw-r--r--kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt28
-rw-r--r--kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt16
-rw-r--r--kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt17
-rw-r--r--kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt19
-rw-r--r--kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt227
8 files changed, 238 insertions, 251 deletions
diff --git a/kvision-modules/kvision-common/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt b/kvision-modules/kvision-common/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt
index dceae898..cb1b44d0 100644
--- a/kvision-modules/kvision-common/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt
+++ b/kvision-modules/kvision-common/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt
@@ -21,8 +21,6 @@
*/
package pl.treksoft.kvision.remote
-import kotlinx.coroutines.Deferred
-
/**
* Multiplatform service manager for Jooby.
*/
@@ -35,7 +33,7 @@ expect open class JoobyServiceManager<T : Any>(service: T) : ServiceManager {
* @param method a HTTP method
*/
protected inline fun <reified RET> bind(
- noinline function: T.(Request?) -> Deferred<RET>,
+ noinline function: suspend T.(Request?) -> RET,
route: String? = null,
method: RpcHttpMethod = RpcHttpMethod.POST
)
@@ -47,7 +45,7 @@ expect open class JoobyServiceManager<T : Any>(service: T) : ServiceManager {
* @param method a HTTP method
*/
protected inline fun <reified PAR, reified RET> bind(
- noinline function: T.(PAR, Request?) -> Deferred<RET>,
+ noinline function: suspend T.(PAR, Request?) -> RET,
route: String? = null,
method: RpcHttpMethod = RpcHttpMethod.POST
)
@@ -59,7 +57,7 @@ expect open class JoobyServiceManager<T : Any>(service: T) : ServiceManager {
* @param method a HTTP method
*/
protected inline fun <reified PAR1, reified PAR2, reified RET> bind(
- noinline function: T.(PAR1, PAR2, Request?) -> Deferred<RET>,
+ noinline function: suspend T.(PAR1, PAR2, Request?) -> RET,
route: String? = null,
method: RpcHttpMethod = RpcHttpMethod.POST
)
@@ -71,7 +69,7 @@ expect open class JoobyServiceManager<T : Any>(service: T) : ServiceManager {
* @param method a HTTP method
*/
protected inline fun <reified PAR1, reified PAR2, reified PAR3, reified RET> bind(
- noinline function: T.(PAR1, PAR2, PAR3, Request?) -> Deferred<RET>,
+ noinline function: suspend T.(PAR1, PAR2, PAR3, Request?) -> RET,
route: String? = null,
method: RpcHttpMethod = RpcHttpMethod.POST
)
@@ -83,7 +81,7 @@ expect open class JoobyServiceManager<T : Any>(service: T) : ServiceManager {
* @param method a HTTP method
*/
protected inline fun <reified PAR1, reified PAR2, reified PAR3, reified PAR4, reified RET> bind(
- noinline function: T.(PAR1, PAR2, PAR3, PAR4, Request?) -> Deferred<RET>,
+ noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, Request?) -> RET,
route: String? = null,
method: RpcHttpMethod = RpcHttpMethod.POST
)
@@ -95,7 +93,7 @@ expect open class JoobyServiceManager<T : Any>(service: T) : ServiceManager {
* @param method a HTTP method
*/
protected inline fun <reified PAR1, reified PAR2, reified PAR3, reified PAR4, reified PAR5, reified RET> bind(
- noinline function: T.(PAR1, PAR2, PAR3, PAR4, PAR5, Request?) -> Deferred<RET>,
+ noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5, Request?) -> RET,
route: String? = null,
method: RpcHttpMethod = RpcHttpMethod.POST
)
diff --git a/kvision-modules/kvision-common/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt b/kvision-modules/kvision-common/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt
index 0e17d4e7..90926656 100644
--- a/kvision-modules/kvision-common/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt
+++ b/kvision-modules/kvision-common/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt
@@ -21,7 +21,6 @@
*/
package pl.treksoft.kvision.remote
-import kotlinx.coroutines.Deferred
import kotlin.reflect.KClass
/**
@@ -36,7 +35,7 @@ expect open class SpringServiceManager<T : Any>(serviceClass: KClass<T>) : Servi
* @param method a HTTP method
*/
protected inline fun <reified RET> bind(
- noinline function: T.() -> Deferred<RET>,
+ noinline function: suspend T.() -> RET,
route: String? = null,
method: RpcHttpMethod = RpcHttpMethod.POST
)
@@ -48,7 +47,7 @@ expect open class SpringServiceManager<T : Any>(serviceClass: KClass<T>) : Servi
* @param method a HTTP method
*/
protected inline fun <reified PAR, reified RET> bind(
- noinline function: T.(PAR) -> Deferred<RET>,
+ noinline function: suspend T.(PAR) -> RET,
route: String? = null,
method: RpcHttpMethod = RpcHttpMethod.POST
)
@@ -60,7 +59,7 @@ expect open class SpringServiceManager<T : Any>(serviceClass: KClass<T>) : Servi
* @param method a HTTP method
*/
protected inline fun <reified PAR1, reified PAR2, reified RET> bind(
- noinline function: T.(PAR1, PAR2) -> Deferred<RET>,
+ noinline function: suspend T.(PAR1, PAR2) -> RET,
route: String? = null,
method: RpcHttpMethod = RpcHttpMethod.POST
)
@@ -72,7 +71,7 @@ expect open class SpringServiceManager<T : Any>(serviceClass: KClass<T>) : Servi
* @param method a HTTP method
*/
protected inline fun <reified PAR1, reified PAR2, reified PAR3, reified RET> bind(
- noinline function: T.(PAR1, PAR2, PAR3) -> Deferred<RET>,
+ noinline function: suspend T.(PAR1, PAR2, PAR3) -> RET,
route: String? = null,
method: RpcHttpMethod = RpcHttpMethod.POST
)
@@ -84,7 +83,7 @@ expect open class SpringServiceManager<T : Any>(serviceClass: KClass<T>) : Servi
* @param method a HTTP method
*/
protected inline fun <reified PAR1, reified PAR2, reified PAR3, reified PAR4, reified RET> bind(
- noinline function: T.(PAR1, PAR2, PAR3, PAR4) -> Deferred<RET>,
+ noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4) -> RET,
route: String? = null,
method: RpcHttpMethod = RpcHttpMethod.POST
)
@@ -96,7 +95,7 @@ expect open class SpringServiceManager<T : Any>(serviceClass: KClass<T>) : Servi
* @param method a HTTP method
*/
protected inline fun <reified PAR1, reified PAR2, reified PAR3, reified PAR4, reified PAR5, reified RET> bind(
- noinline function: T.(PAR1, PAR2, PAR3, PAR4, PAR5) -> Deferred<RET>,
+ noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5) -> RET,
route: String? = null,
method: RpcHttpMethod = RpcHttpMethod.POST
)
diff --git a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt
index 1d5cd926..17f51443 100644
--- a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt
+++ b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt
@@ -22,8 +22,10 @@
package pl.treksoft.kvision.remote
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
-import kotlinx.coroutines.Deferred
-import kotlinx.coroutines.runBlocking
+import kotlinx.coroutines.CoroutineStart
+import kotlinx.coroutines.ExperimentalCoroutinesApi
+import kotlinx.coroutines.GlobalScope
+import kotlinx.coroutines.launch
import org.jooby.Response
import org.slf4j.Logger
import org.slf4j.LoggerFactory
@@ -32,6 +34,7 @@ import java.text.SimpleDateFormat
/**
* Multiplatform service manager for Jooby.
*/
+@UseExperimental(ExperimentalCoroutinesApi::class)
actual open class JoobyServiceManager<T : Any> actual constructor(val service: T) : ServiceManager {
companion object {
@@ -52,24 +55,26 @@ actual open class JoobyServiceManager<T : Any> actual constructor(val service: T
*/
@Suppress("TooGenericExceptionCaught")
protected actual inline fun <reified RET> bind(
- noinline function: T.(Request?) -> Deferred<RET>,
+ noinline function: suspend T.(Request?) -> RET,
route: String?, method: RpcHttpMethod
) {
val routeDef = route ?: "route${this::class.simpleName}${counter++}"
routes.add {
call(method, "/kv/$routeDef") { req, res ->
val jsonRpcRequest = req.body(JsonRpcRequest::class.java)
- try {
- val result = runBlocking { function.invoke(service, req).await() }
- res.send(
- JsonRpcResponse(
- id = jsonRpcRequest.id,
- result = mapper.writeValueAsString(result)
+ GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) {
+ try {
+ val result = function.invoke(service, req)
+ res.send(
+ JsonRpcResponse(
+ id = jsonRpcRequest.id,
+ result = mapper.writeValueAsString(result)
+ )
)
- )
- } catch (e: Exception) {
- LOG.error(e.message, e)
- res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error"))
+ } catch (e: Exception) {
+ LOG.error(e.message, e)
+ res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error"))
+ }
}
}.invoke(this)
}
@@ -83,7 +88,7 @@ actual open class JoobyServiceManager<T : Any> actual constructor(val service: T
*/
@Suppress("TooGenericExceptionCaught")
protected actual inline fun <reified PAR, reified RET> bind(
- noinline function: T.(PAR, Request?) -> Deferred<RET>,
+ noinline function: suspend T.(PAR, Request?) -> RET,
route: String?, method: RpcHttpMethod
) {
val routeDef = route ?: "route${this::class.simpleName}${counter++}"
@@ -92,17 +97,19 @@ actual open class JoobyServiceManager<T : Any> actual constructor(val service: T
val jsonRpcRequest = req.body(JsonRpcRequest::class.java)
if (jsonRpcRequest.params.size == 1) {
val param = getParameter<PAR>(jsonRpcRequest.params[0])
- try {
- val result = runBlocking { function.invoke(service, param, req).await() }
- res.send(
- JsonRpcResponse(
- id = jsonRpcRequest.id,
- result = mapper.writeValueAsString(result)
+ GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) {
+ try {
+ val result = function.invoke(service, param, req)
+ res.send(
+ JsonRpcResponse(
+ id = jsonRpcRequest.id,
+ result = mapper.writeValueAsString(result)
+ )
)
- )
- } catch (e: Exception) {
- LOG.error(e.message, e)
- res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error"))
+ } catch (e: Exception) {
+ LOG.error(e.message, e)
+ res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error"))
+ }
}
} else {
res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = "Invalid parameters"))
@@ -119,7 +126,7 @@ actual open class JoobyServiceManager<T : Any> actual constructor(val service: T
*/
@Suppress("TooGenericExceptionCaught")
protected actual inline fun <reified PAR1, reified PAR2, reified RET> bind(
- noinline function: T.(PAR1, PAR2, Request?) -> Deferred<RET>,
+ noinline function: suspend T.(PAR1, PAR2, Request?) -> RET,
route: String?, method: RpcHttpMethod
) {
val routeDef = route ?: "route${this::class.simpleName}${counter++}"
@@ -129,17 +136,19 @@ actual open class JoobyServiceManager<T : Any> actual constructor(val service: T
if (jsonRpcRequest.params.size == 2) {
val param1 = getParameter<PAR1>(jsonRpcRequest.params[0])
val param2 = getParameter<PAR2>(jsonRpcRequest.params[1])
- try {
- val result = runBlocking { function.invoke(service, param1, param2, req).await() }
- res.send(
- JsonRpcResponse(
- id = jsonRpcRequest.id,
- result = mapper.writeValueAsString(result)
+ GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) {
+ try {
+ val result = function.invoke(service, param1, param2, req)
+ res.send(
+ JsonRpcResponse(
+ id = jsonRpcRequest.id,
+ result = mapper.writeValueAsString(result)
+ )
)
- )
- } catch (e: Exception) {
- LOG.error(e.message, e)
- res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error"))
+ } catch (e: Exception) {
+ LOG.error(e.message, e)
+ res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error"))
+ }
}
} else {
res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = "Invalid parameters"))
@@ -156,7 +165,7 @@ actual open class JoobyServiceManager<T : Any> actual constructor(val service: T
*/
@Suppress("TooGenericExceptionCaught")
protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, reified RET> bind(
- noinline function: T.(PAR1, PAR2, PAR3, Request?) -> Deferred<RET>,
+ noinline function: suspend T.(PAR1, PAR2, PAR3, Request?) -> RET,
route: String?, method: RpcHttpMethod
) {
val routeDef = route ?: "route${this::class.simpleName}${counter++}"
@@ -168,17 +177,19 @@ actual open class JoobyServiceManager<T : Any> actual constructor(val service: T
val param1 = getParameter<PAR1>(jsonRpcRequest.params[0])
val param2 = getParameter<PAR2>(jsonRpcRequest.params[1])
val param3 = getParameter<PAR3>(jsonRpcRequest.params[2])
- try {
- val result = runBlocking { function.invoke(service, param1, param2, param3, req).await() }
- res.send(
- JsonRpcResponse(
- id = jsonRpcRequest.id,
- result = mapper.writeValueAsString(result)
+ GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) {
+ try {
+ val result = function.invoke(service, param1, param2, param3, req)
+ res.send(
+ JsonRpcResponse(
+ id = jsonRpcRequest.id,
+ result = mapper.writeValueAsString(result)
+ )
)
- )
- } catch (e: Exception) {
- LOG.error(e.message, e)
- res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error"))
+ } catch (e: Exception) {
+ LOG.error(e.message, e)
+ res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error"))
+ }
}
} else {
res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = "Invalid parameters"))
@@ -195,7 +206,7 @@ actual open class JoobyServiceManager<T : Any> actual constructor(val service: T
*/
@Suppress("TooGenericExceptionCaught")
protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, reified PAR4, reified RET> bind(
- noinline function: T.(PAR1, PAR2, PAR3, PAR4, Request?) -> Deferred<RET>,
+ noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, Request?) -> RET,
route: String?, method: RpcHttpMethod
) {
val routeDef = route ?: "route${this::class.simpleName}${counter++}"
@@ -208,18 +219,19 @@ actual open class JoobyServiceManager<T : Any> actual constructor(val service: T
val param2 = getParameter<PAR2>(jsonRpcRequest.params[1])
val param3 = getParameter<PAR3>(jsonRpcRequest.params[2])
val param4 = getParameter<PAR4>(jsonRpcRequest.params[3])
- try {
- val result =
- runBlocking { function.invoke(service, param1, param2, param3, param4, req).await() }
- res.send(
- JsonRpcResponse(
- id = jsonRpcRequest.id,
- result = mapper.writeValueAsString(result)
+ GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) {
+ try {
+ val result = function.invoke(service, param1, param2, param3, param4, req)
+ res.send(
+ JsonRpcResponse(
+ id = jsonRpcRequest.id,
+ result = mapper.writeValueAsString(result)
+ )
)
- )
- } catch (e: Exception) {
- LOG.error(e.message, e)
- res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error"))
+ } catch (e: Exception) {
+ LOG.error(e.message, e)
+ res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error"))
+ }
}
} else {
res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = "Invalid parameters"))
@@ -237,7 +249,7 @@ actual open class JoobyServiceManager<T : Any> actual constructor(val service: T
@Suppress("TooGenericExceptionCaught")
protected actual inline fun <reified PAR1, reified PAR2, reified PAR3,
reified PAR4, reified PAR5, reified RET> bind(
- noinline function: T.(PAR1, PAR2, PAR3, PAR4, PAR5, Request?) -> Deferred<RET>,
+ noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5, Request?) -> RET,
route: String?,
method: RpcHttpMethod
) {
@@ -252,20 +264,19 @@ actual open class JoobyServiceManager<T : Any> actual constructor(val service: T
val param3 = getParameter<PAR3>(jsonRpcRequest.params[2])
val param4 = getParameter<PAR4>(jsonRpcRequest.params[3])
val param5 = getParameter<PAR5>(jsonRpcRequest.params[4])
- try {
- val result =
- runBlocking {
- function.invoke(service, param1, param2, param3, param4, param5, req).await()
- }
- res.send(
- JsonRpcResponse(
- id = jsonRpcRequest.id,
- result = mapper.writeValueAsString(result)
+ GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) {
+ try {
+ val result = function.invoke(service, param1, param2, param3, param4, param5, req)
+ res.send(
+ JsonRpcResponse(
+ id = jsonRpcRequest.id,
+ result = mapper.writeValueAsString(result)
+ )
)
- )
- } catch (e: Exception) {
- LOG.error(e.message, e)
- res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error"))
+ } catch (e: Exception) {
+ LOG.error(e.message, e)
+ res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error"))
+ }
}
} else {
res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = "Invalid parameters"))
diff --git a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt
index bc182467..c2321911 100644
--- a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt
+++ b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt
@@ -22,10 +22,6 @@
package pl.treksoft.kvision.remote
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
-import kotlinx.coroutines.CoroutineStart
-import kotlinx.coroutines.Deferred
-import kotlinx.coroutines.ExperimentalCoroutinesApi
-import kotlinx.coroutines.GlobalScope
import org.jooby.Kooby
import org.jooby.Session
import org.jooby.json.Jackson
@@ -63,36 +59,20 @@ actual typealias Request = org.jooby.Request
actual typealias Profile = CommonProfile
/**
- * A helper extension function for asynchronous request processing.
- */
-@UseExperimental(ExperimentalCoroutinesApi::class)
-fun <RESP> Request?.async(block: (Request) -> RESP): Deferred<RESP> = this?.let { req ->
- GlobalScope.coroutinesAsync(start = CoroutineStart.UNDISPATCHED) {
- block(req)
- }
-} ?: throw IllegalStateException("Request not set!")
-
-/**
* A helper extension function for asynchronous request processing with session.
*/
-@UseExperimental(ExperimentalCoroutinesApi::class)
-fun <RESP> Request?.asyncSession(block: (Request, Session) -> RESP): Deferred<RESP> = this?.let { req ->
+fun <RESP> Request?.withSession(block: (Request, Session) -> RESP): RESP = this?.let { req ->
val session = req.session()
- GlobalScope.coroutinesAsync(start = CoroutineStart.UNDISPATCHED) {
- block(req, session)
- }
+ block(req, session)
} ?: throw IllegalStateException("Request not set!")
/**
* A helper extension function for asynchronous request processing with session and user profile.
*/
-@UseExperimental(ExperimentalCoroutinesApi::class)
-fun <RESP> Request?.asyncAuth(block: (Request, Session, Profile) -> RESP): Deferred<RESP> = this?.let { req ->
+fun <RESP> Request?.withProfile(block: (Request, Session, Profile) -> RESP): RESP = this?.let { req ->
val session = req.session()
val profile = req.require(CommonProfile::class.java) as CommonProfile?
profile?.let {
- GlobalScope.coroutinesAsync(start = CoroutineStart.UNDISPATCHED) {
- block(req, session, profile)
- }
+ block(req, session, profile)
}
} ?: throw IllegalStateException("Request or profile not set!")
diff --git a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt
index 462b3777..5b566423 100644
--- a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt
+++ b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt
@@ -21,7 +21,6 @@
*/
package pl.treksoft.kvision.remote
-import kotlinx.coroutines.Deferred
import kotlin.reflect.KClass
/**
@@ -36,7 +35,7 @@ actual open class SpringServiceManager<T : Any> actual constructor(val serviceCl
* @param method a HTTP method
*/
protected actual inline fun <reified RET> bind(
- noinline function: T.() -> Deferred<RET>,
+ noinline function: suspend T.() -> RET,
route: String?,
method: RpcHttpMethod
) {
@@ -50,7 +49,7 @@ actual open class SpringServiceManager<T : Any> actual constructor(val serviceCl
* @param method a HTTP method
*/
protected actual inline fun <reified PAR, reified RET> bind(
- noinline function: T.(PAR) -> Deferred<RET>,
+ noinline function: suspend T.(PAR) -> RET,
route: String?,
method: RpcHttpMethod
) {
@@ -64,7 +63,7 @@ actual open class SpringServiceManager<T : Any> actual constructor(val serviceCl
* @param method a HTTP method
*/
protected actual inline fun <reified PAR1, reified PAR2, reified RET> bind(
- noinline function: T.(PAR1, PAR2) -> Deferred<RET>,
+ noinline function: suspend T.(PAR1, PAR2) -> RET,
route: String?,
method: RpcHttpMethod
) {
@@ -78,7 +77,7 @@ actual open class SpringServiceManager<T : Any> actual constructor(val serviceCl
* @param method a HTTP method
*/
protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, reified RET> bind(
- noinline function: T.(PAR1, PAR2, PAR3) -> Deferred<RET>,
+ noinline function: suspend T.(PAR1, PAR2, PAR3) -> RET,
route: String?,
method: RpcHttpMethod
) {
@@ -92,7 +91,7 @@ actual open class SpringServiceManager<T : Any> actual constructor(val serviceCl
* @param method a HTTP method
*/
protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, reified PAR4, reified RET> bind(
- noinline function: T.(PAR1, PAR2, PAR3, PAR4) -> Deferred<RET>,
+ noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4) -> RET,
route: String?,
method: RpcHttpMethod
) {
@@ -105,8 +104,9 @@ actual open class SpringServiceManager<T : Any> actual constructor(val serviceCl
* @param route a route
* @param method a HTTP method
*/
- protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, reified PAR4, reified PAR5, reified RET> bind(
- noinline function: T.(PAR1, PAR2, PAR3, PAR4, PAR5) -> Deferred<RET>,
+ protected actual inline fun <reified PAR1, reified PAR2, reified PAR3,
+ reified PAR4, reified PAR5, reified RET> bind(
+ noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5) -> RET,
route: String?,
method: RpcHttpMethod
) {
diff --git a/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt b/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt
index 75410e07..422c97ba 100644
--- a/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt
+++ b/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt
@@ -21,8 +21,6 @@
*/
package pl.treksoft.kvision.remote
-import kotlinx.coroutines.Deferred
-
/**
* Multiplatform service manager for Jooby.
* Not to be used in this module.
@@ -35,7 +33,7 @@ actual open class JoobyServiceManager<T : Any> actual constructor(val service: T
* @param method a HTTP method
*/
protected actual inline fun <reified RET> bind(
- noinline function: T.(Request?) -> Deferred<RET>,
+ noinline function: suspend T.(Request?) -> RET,
route: String?,
method: RpcHttpMethod
) {
@@ -49,7 +47,7 @@ actual open class JoobyServiceManager<T : Any> actual constructor(val service: T
* @param method a HTTP method
*/
protected actual inline fun <reified PAR, reified RET> bind(
- noinline function: T.(PAR, Request?) -> Deferred<RET>,
+ noinline function: suspend T.(PAR, Request?) -> RET,
route: String?,
method: RpcHttpMethod
) {
@@ -63,7 +61,7 @@ actual open class JoobyServiceManager<T : Any> actual constructor(val service: T
* @param method a HTTP method
*/
protected actual inline fun <reified PAR1, reified PAR2, reified RET> bind(
- noinline function: T.(PAR1, PAR2, Request?) -> Deferred<RET>,
+ noinline function: suspend T.(PAR1, PAR2, Request?) -> RET,
route: String?,
method: RpcHttpMethod
) {
@@ -77,7 +75,7 @@ actual open class JoobyServiceManager<T : Any> actual constructor(val service: T
* @param method a HTTP method
*/
protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, reified RET> bind(
- noinline function: T.(PAR1, PAR2, PAR3, Request?) -> Deferred<RET>,
+ noinline function: suspend T.(PAR1, PAR2, PAR3, Request?) -> RET,
route: String?,
method: RpcHttpMethod
) {
@@ -91,7 +89,7 @@ actual open class JoobyServiceManager<T : Any> actual constructor(val service: T
* @param method a HTTP method
*/
protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, reified PAR4, reified RET> bind(
- noinline function: T.(PAR1, PAR2, PAR3, PAR4, Request?) -> Deferred<RET>,
+ noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, Request?) -> RET,
route: String?,
method: RpcHttpMethod
) {
@@ -104,8 +102,9 @@ actual open class JoobyServiceManager<T : Any> actual constructor(val service: T
* @param route a route
* @param method a HTTP method
*/
- protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, reified PAR4, reified PAR5, reified RET> bind(
- noinline function: T.(PAR1, PAR2, PAR3, PAR4, PAR5, Request?) -> Deferred<RET>,
+ protected actual inline fun <reified PAR1, reified PAR2, reified PAR3,
+ reified PAR4, reified PAR5, reified RET> bind(
+ noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5, Request?) -> RET,
route: String?,
method: RpcHttpMethod
) {
diff --git a/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt b/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt
index 11b848bf..f2e3e951 100644
--- a/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt
+++ b/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt
@@ -21,8 +21,6 @@
*/
package pl.treksoft.kvision.remote
-import kotlinx.coroutines.Deferred
-import kotlinx.coroutines.GlobalScope
import org.pac4j.core.context.J2EContext
import org.pac4j.core.context.session.J2ESessionStore
import org.pac4j.core.profile.CommonProfile
@@ -48,17 +46,10 @@ actual typealias Request = HttpServletRequest
actual typealias Profile = CommonProfile
/**
- * A helper extension function for asynchronous processing.
+ * A helper extension function for processing with authenticated user profile.
*/
-fun <RESP> async(block: () -> RESP): Deferred<RESP> =
- GlobalScope.coroutinesAsync {
- block()
- }
-
-/**
- * A helper extension function for asynchronous processing with user profile.
- */
-fun <RESP> asyncAuth(block: (Profile) -> RESP): Deferred<RESP> {
+@Suppress("TooGenericExceptionCaught")
+fun <RESP> withProfile(block: (Profile) -> RESP): RESP {
val profile = try {
val requestAttributes = (RequestContextHolder.getRequestAttributes() as ServletRequestAttributes)
val req = requestAttributes.request
@@ -68,8 +59,6 @@ fun <RESP> asyncAuth(block: (Profile) -> RESP): Deferred<RESP> {
null
}
return profile?.let {
- GlobalScope.coroutinesAsync {
- block(it)
- }
+ block(it)
} ?: throw IllegalStateException("Profile not set!")
}
diff --git a/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt b/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt
index 1baa856e..a241842c 100644
--- a/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt
+++ b/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt
@@ -22,8 +22,10 @@
package pl.treksoft.kvision.remote
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
-import kotlinx.coroutines.Deferred
-import kotlinx.coroutines.runBlocking
+import kotlinx.coroutines.CoroutineStart
+import kotlinx.coroutines.ExperimentalCoroutinesApi
+import kotlinx.coroutines.GlobalScope
+import kotlinx.coroutines.launch
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.text.SimpleDateFormat
@@ -33,6 +35,7 @@ import kotlin.reflect.KClass
/**
* Multiplatform service manager for Spring Boot.
*/
+@UseExperimental(ExperimentalCoroutinesApi::class)
actual open class SpringServiceManager<T : Any> actual constructor(val serviceClass: KClass<T>) : ServiceManager {
companion object {
@@ -57,33 +60,35 @@ actual open class SpringServiceManager<T : Any> actual constructor(val serviceCl
*/
@Suppress("TooGenericExceptionCaught")
protected actual inline fun <reified RET> bind(
- noinline function: T.() -> Deferred<RET>,
+ noinline function: suspend T.() -> RET,
route: String?, method: RpcHttpMethod
) {
val routeDef = route ?: "route${this::class.simpleName}${counter++}"
addRoute(method, "/kv/$routeDef") { req, res ->
val service = SpringContext.getBean(serviceClass.java)
val jsonRpcRequest = mapper.readValue(req.inputStream, JsonRpcRequest::class.java)
- try {
- val result = runBlocking { function.invoke(service).await() }
- res.writeJSON(
- mapper.writeValueAsString(
- JsonRpcResponse(
- id = jsonRpcRequest.id,
- result = mapper.writeValueAsString(result)
+ GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) {
+ try {
+ val result = function.invoke(service)
+ res.writeJSON(
+ mapper.writeValueAsString(
+ JsonRpcResponse(
+ id = jsonRpcRequest.id,
+ result = mapper.writeValueAsString(result)
+ )
)
)
- )
- } catch (e: Exception) {
- LOG.error(e.message, e)
- res.writeJSON(
- mapper.writeValueAsString(
- JsonRpcResponse(
- id = jsonRpcRequest.id,
- error = e.message ?: "Error"
+ } catch (e: Exception) {
+ LOG.error(e.message, e)
+ res.writeJSON(
+ mapper.writeValueAsString(
+ JsonRpcResponse(
+ id = jsonRpcRequest.id,
+ error = e.message ?: "Error"
+ )
)
)
- )
+ }
}
}
}
@@ -96,7 +101,7 @@ actual open class SpringServiceManager<T : Any> actual constructor(val serviceCl
*/
@Suppress("TooGenericExceptionCaught")
protected actual inline fun <reified PAR, reified RET> bind(
- noinline function: T.(PAR) -> Deferred<RET>,
+ noinline function: suspend T.(PAR) -> RET,
route: String?, method: RpcHttpMethod
) {
val routeDef = route ?: "route${this::class.simpleName}${counter++}"
@@ -105,26 +110,28 @@ actual open class SpringServiceManager<T : Any> actual constructor(val serviceCl
val jsonRpcRequest = mapper.readValue(req.inputStream, JsonRpcRequest::class.java)
if (jsonRpcRequest.params.size == 1) {
val param = getParameter<PAR>(jsonRpcRequest.params[0])
- try {
- val result = runBlocking { function.invoke(service, param).await() }
- res.writeJSON(
- mapper.writeValueAsString(
- JsonRpcResponse(
- id = jsonRpcRequest.id,
- result = mapper.writeValueAsString(result)
+ GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) {
+ try {
+ val result = function.invoke(service, param)
+ res.writeJSON(
+ mapper.writeValueAsString(
+ JsonRpcResponse(
+ id = jsonRpcRequest.id,
+ result = mapper.writeValueAsString(result)
+ )
)
)
- )
- } catch (e: Exception) {
- LOG.error(e.message, e)
- res.writeJSON(
- mapper.writeValueAsString(
- JsonRpcResponse(
- id = jsonRpcRequest.id,
- error = e.message ?: "Error"
+ } catch (e: Exception) {
+ LOG.error(e.message, e)
+ res.writeJSON(
+ mapper.writeValueAsString(
+ JsonRpcResponse(
+ id = jsonRpcRequest.id,
+ error = e.message ?: "Error"
+ )
)
)
- )
+ }
}
} else {
res.writeJSON(
@@ -147,7 +154,7 @@ actual open class SpringServiceManager<T : Any> actual constructor(val serviceCl
*/
@Suppress("TooGenericExceptionCaught")
protected actual inline fun <reified PAR1, reified PAR2, reified RET> bind(
- noinline function: T.(PAR1, PAR2) -> Deferred<RET>,
+ noinline function: suspend T.(PAR1, PAR2) -> RET,
route: String?, method: RpcHttpMethod
) {
val routeDef = route ?: "route${this::class.simpleName}${counter++}"
@@ -157,26 +164,28 @@ actual open class SpringServiceManager<T : Any> actual constructor(val serviceCl
if (jsonRpcRequest.params.size == 2) {
val param1 = getParameter<PAR1>(jsonRpcRequest.params[0])
val param2 = getParameter<PAR2>(jsonRpcRequest.params[1])
- try {
- val result = runBlocking { function.invoke(service, param1, param2).await() }
- res.writeJSON(
- mapper.writeValueAsString(
- JsonRpcResponse(
- id = jsonRpcRequest.id,
- result = mapper.writeValueAsString(result)
+ GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) {
+ try {
+ val result = function.invoke(service, param1, param2)
+ res.writeJSON(
+ mapper.writeValueAsString(
+ JsonRpcResponse(
+ id = jsonRpcRequest.id,
+ result = mapper.writeValueAsString(result)
+ )
)
)
- )
- } catch (e: Exception) {
- LOG.error(e.message, e)
- res.writeJSON(
- mapper.writeValueAsString(
- JsonRpcResponse(
- id = jsonRpcRequest.id,
- error = e.message ?: "Error"
+ } catch (e: Exception) {
+ LOG.error(e.message, e)
+ res.writeJSON(
+ mapper.writeValueAsString(
+ JsonRpcResponse(
+ id = jsonRpcRequest.id,
+ error = e.message ?: "Error"
+ )
)
)
- )
+ }
}
} else {
res.writeJSON(
@@ -199,7 +208,7 @@ actual open class SpringServiceManager<T : Any> actual constructor(val serviceCl
*/
@Suppress("TooGenericExceptionCaught")
protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, reified RET> bind(
- noinline function: T.(PAR1, PAR2, PAR3) -> Deferred<RET>,
+ noinline function: suspend T.(PAR1, PAR2, PAR3) -> RET,
route: String?, method: RpcHttpMethod
) {
val routeDef = route ?: "route${this::class.simpleName}${counter++}"
@@ -211,26 +220,28 @@ actual open class SpringServiceManager<T : Any> actual constructor(val serviceCl
val param1 = getParameter<PAR1>(jsonRpcRequest.params[0])
val param2 = getParameter<PAR2>(jsonRpcRequest.params[1])
val param3 = getParameter<PAR3>(jsonRpcRequest.params[2])
- try {
- val result = runBlocking { function.invoke(service, param1, param2, param3).await() }
- res.writeJSON(
- mapper.writeValueAsString(
- JsonRpcResponse(
- id = jsonRpcRequest.id,
- result = mapper.writeValueAsString(result)
+ GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) {
+ try {
+ val result = function.invoke(service, param1, param2, param3)
+ res.writeJSON(
+ mapper.writeValueAsString(
+ JsonRpcResponse(
+ id = jsonRpcRequest.id,
+ result = mapper.writeValueAsString(result)
+ )
)
)
- )
- } catch (e: Exception) {
- LOG.error(e.message, e)
- res.writeJSON(
- mapper.writeValueAsString(
- JsonRpcResponse(
- id = jsonRpcRequest.id,
- error = e.message ?: "Error"
+ } catch (e: Exception) {
+ LOG.error(e.message, e)
+ res.writeJSON(
+ mapper.writeValueAsString(
+ JsonRpcResponse(
+ id = jsonRpcRequest.id,
+ error = e.message ?: "Error"
+ )
)
)
- )
+ }
}
} else {
res.writeJSON(
@@ -253,7 +264,7 @@ actual open class SpringServiceManager<T : Any> actual constructor(val serviceCl
*/
@Suppress("TooGenericExceptionCaught")
protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, reified PAR4, reified RET> bind(
- noinline function: T.(PAR1, PAR2, PAR3, PAR4) -> Deferred<RET>,
+ noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4) -> RET,
route: String?, method: RpcHttpMethod
) {
val routeDef = route ?: "route${this::class.simpleName}${counter++}"
@@ -266,27 +277,28 @@ actual open class SpringServiceManager<T : Any> actual constructor(val serviceCl
val param2 = getParameter<PAR2>(jsonRpcRequest.params[1])
val param3 = getParameter<PAR3>(jsonRpcRequest.params[2])
val param4 = getParameter<PAR4>(jsonRpcRequest.params[3])
- try {
- val result =
- runBlocking { function.invoke(service, param1, param2, param3, param4).await() }
- res.writeJSON(
- mapper.writeValueAsString(
- JsonRpcResponse(
- id = jsonRpcRequest.id,
- result = mapper.writeValueAsString(result)
+ GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) {
+ try {
+ val result = function.invoke(service, param1, param2, param3, param4)
+ res.writeJSON(
+ mapper.writeValueAsString(
+ JsonRpcResponse(
+ id = jsonRpcRequest.id,
+ result = mapper.writeValueAsString(result)
+ )
)
)
- )
- } catch (e: Exception) {
- LOG.error(e.message, e)
- res.writeJSON(
- mapper.writeValueAsString(
- JsonRpcResponse(
- id = jsonRpcRequest.id,
- error = e.message ?: "Error"
+ } catch (e: Exception) {
+ LOG.error(e.message, e)
+ res.writeJSON(
+ mapper.writeValueAsString(
+ JsonRpcResponse(
+ id = jsonRpcRequest.id,
+ error = e.message ?: "Error"
+ )
)
)
- )
+ }
}
} else {
res.writeJSON(
@@ -310,7 +322,7 @@ actual open class SpringServiceManager<T : Any> actual constructor(val serviceCl
@Suppress("TooGenericExceptionCaught")
protected actual inline fun <reified PAR1, reified PAR2, reified PAR3,
reified PAR4, reified PAR5, reified RET> bind(
- noinline function: T.(PAR1, PAR2, PAR3, PAR4, PAR5) -> Deferred<RET>,
+ noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5) -> RET,
route: String?,
method: RpcHttpMethod
) {
@@ -325,29 +337,28 @@ actual open class SpringServiceManager<T : Any> actual constructor(val serviceCl
val param3 = getParameter<PAR3>(jsonRpcRequest.params[2])
val param4 = getParameter<PAR4>(jsonRpcRequest.params[3])
val param5 = getParameter<PAR5>(jsonRpcRequest.params[4])
- try {
- val result =
- runBlocking {
- function.invoke(service, param1, param2, param3, param4, param5).await()
- }
- res.writeJSON(
- mapper.writeValueAsString(
- JsonRpcResponse(
- id = jsonRpcRequest.id,
- result = mapper.writeValueAsString(result)
+ GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) {
+ try {
+ val result = function.invoke(service, param1, param2, param3, param4, param5)
+ res.writeJSON(
+ mapper.writeValueAsString(
+ JsonRpcResponse(
+ id = jsonRpcRequest.id,
+ result = mapper.writeValueAsString(result)
+ )
)
)
- )
- } catch (e: Exception) {
- LOG.error(e.message, e)
- res.writeJSON(
- mapper.writeValueAsString(
- JsonRpcResponse(
- id = jsonRpcRequest.id,
- error = e.message ?: "Error"
+ } catch (e: Exception) {
+ LOG.error(e.message, e)
+ res.writeJSON(
+ mapper.writeValueAsString(
+ JsonRpcResponse(
+ id = jsonRpcRequest.id,
+ error = e.message ?: "Error"
+ )
)
)
- )
+ }
}
} else {
res.writeJSON(