aboutsummaryrefslogtreecommitdiff
path: root/kvision-modules/kvision-server-spring-boot/src
diff options
context:
space:
mode:
authorRobert Jaros <rjaros@finn.pl>2018-12-13 12:40:40 +0100
committerRobert Jaros <rjaros@finn.pl>2018-12-13 12:40:40 +0100
commitc3b1a2312f4110fcb344d71e83ee3f952534cdcf (patch)
tree7c491bf4b45a43386b520313cbb62f84dae6a9ce /kvision-modules/kvision-server-spring-boot/src
parent6370bb46a7e47f308ee24f1a32ab8d8cf309dedf (diff)
downloadkvision-c3b1a2312f4110fcb344d71e83ee3f952534cdcf.tar.gz
kvision-c3b1a2312f4110fcb344d71e83ee3f952534cdcf.tar.bz2
kvision-c3b1a2312f4110fcb344d71e83ee3f952534cdcf.zip
Major refactor of server side modules architecture.
Diffstat (limited to 'kvision-modules/kvision-server-spring-boot/src')
-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
3 files changed, 131 insertions, 132 deletions
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(