aboutsummaryrefslogtreecommitdiff
path: root/kvision-modules/kvision-server-jooby/src/main/kotlin
diff options
context:
space:
mode:
Diffstat (limited to 'kvision-modules/kvision-server-jooby/src/main/kotlin')
-rw-r--r--kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt313
-rw-r--r--kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt14
-rw-r--r--kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/ServiceManager.kt350
-rw-r--r--kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt116
4 files changed, 437 insertions, 356 deletions
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
new file mode 100644
index 00000000..1d5cd926
--- /dev/null
+++ b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt
@@ -0,0 +1,313 @@
+/*
+ * Copyright (c) 2017-present Robert Jaros
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+package pl.treksoft.kvision.remote
+
+import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
+import kotlinx.coroutines.Deferred
+import kotlinx.coroutines.runBlocking
+import org.jooby.Response
+import org.slf4j.Logger
+import org.slf4j.LoggerFactory
+import java.text.SimpleDateFormat
+
+/**
+ * Multiplatform service manager for Jooby.
+ */
+actual open class JoobyServiceManager<T : Any> actual constructor(val service: T) : ServiceManager {
+
+ companion object {
+ val LOG: Logger = LoggerFactory.getLogger(JoobyServiceManager::class.java.name)
+ }
+
+ protected val routes: MutableList<KVServer.() -> Unit> = mutableListOf()
+ val mapper = jacksonObjectMapper().apply {
+ dateFormat = SimpleDateFormat("YYYY-MM-DD HH:mm:ss")
+ }
+ var counter: Int = 0
+
+ /**
+ * Binds a given route with a function of the receiver.
+ * @param function a function of the receiver
+ * @param route a route
+ * @param method a HTTP method
+ */
+ @Suppress("TooGenericExceptionCaught")
+ protected actual inline fun <reified RET> bind(
+ noinline function: T.(Request?) -> Deferred<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)
+ )
+ )
+ } catch (e: Exception) {
+ LOG.error(e.message, e)
+ res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error"))
+ }
+ }.invoke(this)
+ }
+ }
+
+ /**
+ * Binds a given route with a function of the receiver.
+ * @param function a function of the receiver
+ * @param route a route
+ * @param method a HTTP method
+ */
+ @Suppress("TooGenericExceptionCaught")
+ protected actual inline fun <reified PAR, reified RET> bind(
+ noinline function: T.(PAR, Request?) -> Deferred<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)
+ 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)
+ )
+ )
+ } 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"))
+ }
+ }.invoke(this)
+ }
+ }
+
+ /**
+ * Binds a given route with a function of the receiver.
+ * @param function a function of the receiver
+ * @param route a route
+ * @param method a HTTP method
+ */
+ @Suppress("TooGenericExceptionCaught")
+ protected actual inline fun <reified PAR1, reified PAR2, reified RET> bind(
+ noinline function: T.(PAR1, PAR2, Request?) -> Deferred<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)
+ 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)
+ )
+ )
+ } 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"))
+ }
+ }.invoke(this)
+ }
+ }
+
+ /**
+ * Binds a given route with a function of the receiver.
+ * @param function a function of the receiver
+ * @param route a route
+ * @param method a HTTP method
+ */
+ @Suppress("TooGenericExceptionCaught")
+ protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, reified RET> bind(
+ noinline function: T.(PAR1, PAR2, PAR3, Request?) -> Deferred<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)
+ @Suppress("MagicNumber")
+ if (jsonRpcRequest.params.size == 3) {
+ 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)
+ )
+ )
+ } 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"))
+ }
+ }.invoke(this)
+ }
+ }
+
+ /**
+ * Binds a given route with a function of the receiver.
+ * @param function a function of the receiver
+ * @param route a route
+ * @param method a HTTP method
+ */
+ @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>,
+ 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)
+ @Suppress("MagicNumber")
+ if (jsonRpcRequest.params.size == 4) {
+ val param1 = getParameter<PAR1>(jsonRpcRequest.params[0])
+ 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)
+ )
+ )
+ } 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"))
+ }
+ }.invoke(this)
+ }
+ }
+
+ /**
+ * Binds a given route with a function of the receiver.
+ * @param function a function of the receiver
+ * @param route a route
+ * @param method a HTTP method
+ */
+ @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>,
+ 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)
+ @Suppress("MagicNumber")
+ if (jsonRpcRequest.params.size == 5) {
+ val param1 = getParameter<PAR1>(jsonRpcRequest.params[0])
+ val param2 = getParameter<PAR2>(jsonRpcRequest.params[1])
+ 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)
+ )
+ )
+ } 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"))
+ }
+ }.invoke(this)
+ }
+ }
+
+ fun call(
+ method: RpcHttpMethod,
+ path: String,
+ handler: (Request, Response) -> Unit
+ ): KVServer.() -> Unit {
+ return {
+ when (method) {
+ RpcHttpMethod.POST -> post(path, handler)
+ RpcHttpMethod.PUT -> put(path, handler)
+ RpcHttpMethod.DELETE -> delete(path, handler)
+ RpcHttpMethod.OPTIONS -> options(path, handler)
+ }
+ }
+ }
+
+ @Suppress("TooGenericExceptionCaught")
+ protected inline fun <reified T> getParameter(str: String?): T {
+ return str?.let {
+ if (T::class == String::class) {
+ str as T
+ } else {
+ mapper.readValue(str, T::class.java)
+ }
+ } ?: null as T
+ }
+
+ /**
+ * Applies all defined routes to the given server.
+ * @param k a server
+ */
+ actual fun applyRoutes(k: KVServer) {
+ routes.forEach {
+ it.invoke(k)
+ }
+ }
+
+}
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 38edc1b8..1e19f394 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
@@ -73,7 +73,7 @@ fun <RESP> Request?.async(block: (Request) -> RESP): Deferred<RESP> = this?.let
/**
* A helper extension function for asynchronous request processing with session.
*/
-fun <RESP> Request?.async(block: (Request, Session) -> RESP): Deferred<RESP> = this?.let { req ->
+fun <RESP> Request?.asyncSession(block: (Request, Session) -> RESP): Deferred<RESP> = this?.let { req ->
val session = req.session()
GlobalScope.coroutinesAsync(Dispatchers.Unconfined) {
block(req, session)
@@ -83,10 +83,12 @@ fun <RESP> Request?.async(block: (Request, Session) -> RESP): Deferred<RESP> = t
/**
* A helper extension function for asynchronous request processing with session and user profile.
*/
-fun <RESP> Request?.async(block: (Request, Session, Profile) -> RESP): Deferred<RESP> = this?.let { req ->
+fun <RESP> Request?.asyncAuth(block: (Request, Session, Profile) -> RESP): Deferred<RESP> = this?.let { req ->
val session = req.session()
- val profile = req.require(CommonProfile::class.java)
- GlobalScope.coroutinesAsync(Dispatchers.Unconfined) {
- block(req, session, profile)
+ val profile = req.require(CommonProfile::class.java) as CommonProfile?
+ profile?.let {
+ GlobalScope.coroutinesAsync(Dispatchers.Unconfined) {
+ block(req, session, profile)
+ }
}
-} ?: throw IllegalStateException("Request not set!")
+} ?: throw IllegalStateException("Request or profile not set!")
diff --git a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/ServiceManager.kt b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/ServiceManager.kt
deleted file mode 100644
index 3091ce26..00000000
--- a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/ServiceManager.kt
+++ /dev/null
@@ -1,350 +0,0 @@
-/*
- * Copyright (c) 2017-present Robert Jaros
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-package pl.treksoft.kvision.remote
-
-import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
-import kotlinx.coroutines.Deferred
-import kotlinx.coroutines.runBlocking
-import org.jooby.Response
-import org.jooby.Status
-import org.slf4j.Logger
-import org.slf4j.LoggerFactory
-import java.text.SimpleDateFormat
-
-/**
- * Multiplatform service manager.
- */
-actual open class ServiceManager<out T> actual constructor(val service: T) {
-
- companion object {
- val LOG: Logger = LoggerFactory.getLogger(ServiceManager::class.java.name)
- }
-
- protected val routes: MutableList<KVServer.() -> Unit> = mutableListOf()
- val mapper = jacksonObjectMapper().apply {
- dateFormat = SimpleDateFormat("YYYY-MM-DD HH:mm:ss")
- }
- var counter: Int = 0
-
- /**
- * Binds a given route with a function of the receiver.
- * @param function a function of the receiver
- * @param route a route
- * @param method a HTTP method
- * @param prefix an URL address prefix
- */
- @Suppress("TooGenericExceptionCaught")
- protected actual inline fun <reified RET> bind(
- noinline function: T.(Request?) -> Deferred<RET>,
- route: String?, method: RpcHttpMethod, prefix: String
- ) {
- val routeDef = route ?: "route${this::class.simpleName}${counter++}"
- routes.add {
- call(method, "$prefix$routeDef") { req, res ->
- if (service != null) {
- 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)
- )
- )
- } catch (e: Exception) {
- LOG.error(e.message, e)
- res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error"))
- }
- } else {
- res.status(Status.SERVER_ERROR)
- }
- }.invoke(this)
- }
- }
-
- /**
- * Binds a given route with a function of the receiver.
- * @param function a function of the receiver
- * @param route a route
- * @param method a HTTP method
- * @param prefix an URL address prefix
- */
- @Suppress("TooGenericExceptionCaught")
- protected actual inline fun <reified PAR, reified RET> bind(
- noinline function: T.(PAR, Request?) -> Deferred<RET>,
- route: String?, method: RpcHttpMethod, prefix: String
- ) {
- val routeDef = route ?: "route${this::class.simpleName}${counter++}"
- routes.add {
- call(method, "$prefix$routeDef") { req, res ->
- if (service != null) {
- 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)
- )
- )
- } 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"))
- }
- } else {
- res.status(Status.SERVER_ERROR)
- }
- }.invoke(this)
- }
- }
-
- /**
- * Binds a given route with a function of the receiver.
- * @param function a function of the receiver
- * @param route a route
- * @param method a HTTP method
- * @param prefix an URL address prefix
- */
- @Suppress("TooGenericExceptionCaught")
- protected actual inline fun <reified PAR1, reified PAR2, reified RET> bind(
- noinline function: T.(PAR1, PAR2, Request?) -> Deferred<RET>,
- route: String?, method: RpcHttpMethod, prefix: String
- ) {
- val routeDef = route ?: "route${this::class.simpleName}${counter++}"
- routes.add {
- call(method, "$prefix$routeDef") { req, res ->
- if (service != null) {
- val jsonRpcRequest = req.body(JsonRpcRequest::class.java)
- 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)
- )
- )
- } 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"))
- }
- } else {
- res.status(Status.SERVER_ERROR)
- }
- }.invoke(this)
- }
- }
-
- /**
- * Binds a given route with a function of the receiver.
- * @param function a function of the receiver
- * @param route a route
- * @param method a HTTP method
- * @param prefix an URL address prefix
- */
- @Suppress("TooGenericExceptionCaught")
- protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, reified RET> bind(
- noinline function: T.(PAR1, PAR2, PAR3, Request?) -> Deferred<RET>,
- route: String?, method: RpcHttpMethod, prefix: String
- ) {
- val routeDef = route ?: "route${this::class.simpleName}${counter++}"
- routes.add {
- call(method, "$prefix$routeDef") { req, res ->
- if (service != null) {
- val jsonRpcRequest = req.body(JsonRpcRequest::class.java)
- @Suppress("MagicNumber")
- if (jsonRpcRequest.params.size == 3) {
- 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)
- )
- )
- } 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"))
- }
- } else {
- res.status(Status.SERVER_ERROR)
- }
- }.invoke(this)
- }
- }
-
- /**
- * Binds a given route with a function of the receiver.
- * @param function a function of the receiver
- * @param route a route
- * @param method a HTTP method
- * @param prefix an URL address prefix
- */
- @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>,
- route: String?, method: RpcHttpMethod, prefix: String
- ) {
- val routeDef = route ?: "route${this::class.simpleName}${counter++}"
- routes.add {
- call(method, "$prefix$routeDef") { req, res ->
- if (service != null) {
- val jsonRpcRequest = req.body(JsonRpcRequest::class.java)
- @Suppress("MagicNumber")
- if (jsonRpcRequest.params.size == 4) {
- val param1 = getParameter<PAR1>(jsonRpcRequest.params[0])
- 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)
- )
- )
- } 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"))
- }
- } else {
- res.status(Status.SERVER_ERROR)
- }
- }.invoke(this)
- }
- }
-
- /**
- * Binds a given route with a function of the receiver.
- * @param function a function of the receiver
- * @param route a route
- * @param method a HTTP method
- * @param prefix an URL address prefix
- */
- @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>,
- route: String?,
- method: RpcHttpMethod,
- prefix: String
- ) {
- val routeDef = route ?: "route${this::class.simpleName}${counter++}"
- routes.add {
- call(method, "$prefix$routeDef") { req, res ->
- if (service != null) {
- val jsonRpcRequest = req.body(JsonRpcRequest::class.java)
- @Suppress("MagicNumber")
- if (jsonRpcRequest.params.size == 5) {
- val param1 = getParameter<PAR1>(jsonRpcRequest.params[0])
- val param2 = getParameter<PAR2>(jsonRpcRequest.params[1])
- 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)
- )
- )
- } 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"))
- }
- } else {
- res.status(Status.SERVER_ERROR)
- }
- }.invoke(this)
- }
- }
-
- fun call(
- method: RpcHttpMethod,
- path: String,
- handler: (Request, Response) -> Unit
- ): KVServer.() -> Unit {
- return {
- when (method) {
- RpcHttpMethod.POST -> post(path, handler)
- RpcHttpMethod.PUT -> put(path, handler)
- RpcHttpMethod.DELETE -> delete(path, handler)
- RpcHttpMethod.OPTIONS -> options(path, handler)
- }
- }
- }
-
- @Suppress("TooGenericExceptionCaught")
- protected inline fun <reified T> getParameter(str: String?): T {
- return str?.let {
- if (T::class == String::class) {
- str as T
- } else {
- mapper.readValue(str, T::class.java)
- }
- } ?: null as T
- }
-
- /**
- * Applies all defined routes to the given server.
- * @param k a server
- */
- actual fun applyRoutes(k: KVServer) {
- routes.forEach {
- it.invoke(k)
- }
- }
-
- /**
- * Returns the list of defined bindings.
- * Not used on the jvm platform.
- */
- actual fun getCalls(): Map<String, Pair<String, RpcHttpMethod>> = mapOf()
-}
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
new file mode 100644
index 00000000..462b3777
--- /dev/null
+++ b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt
@@ -0,0 +1,116 @@
+/*
+ * Copyright (c) 2017-present Robert Jaros
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+package pl.treksoft.kvision.remote
+
+import kotlinx.coroutines.Deferred
+import kotlin.reflect.KClass
+
+/**
+ * Multiplatform service manager for Spring Boot.
+ * Not to be used in this module.
+ */
+actual open class SpringServiceManager<T : Any> actual constructor(val serviceClass: KClass<T>) : ServiceManager {
+ /**
+ * Binds a given route with a function of the receiver.
+ * @param function a function of the receiver
+ * @param route a route
+ * @param method a HTTP method
+ */
+ protected actual inline fun <reified RET> bind(
+ noinline function: T.() -> Deferred<RET>,
+ route: String?,
+ method: RpcHttpMethod
+ ) {
+ throw IllegalStateException("This class is for Spring Boot integration.")
+ }
+
+ /**
+ * Binds a given route with a function of the receiver.
+ * @param function a function of the receiver
+ * @param route a route
+ * @param method a HTTP method
+ */
+ protected actual inline fun <reified PAR, reified RET> bind(
+ noinline function: T.(PAR) -> Deferred<RET>,
+ route: String?,
+ method: RpcHttpMethod
+ ) {
+ throw IllegalStateException("This class is for Spring Boot integration.")
+ }
+
+ /**
+ * Binds a given route with a function of the receiver.
+ * @param function a function of the receiver
+ * @param route a route
+ * @param method a HTTP method
+ */
+ protected actual inline fun <reified PAR1, reified PAR2, reified RET> bind(
+ noinline function: T.(PAR1, PAR2) -> Deferred<RET>,
+ route: String?,
+ method: RpcHttpMethod
+ ) {
+ throw IllegalStateException("This class is for Spring Boot integration.")
+ }
+
+ /**
+ * Binds a given route with a function of the receiver.
+ * @param function a function of the receiver
+ * @param route a route
+ * @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>,
+ route: String?,
+ method: RpcHttpMethod
+ ) {
+ throw IllegalStateException("This class is for Spring Boot integration.")
+ }
+
+ /**
+ * Binds a given route with a function of the receiver.
+ * @param function a function of the receiver
+ * @param route a route
+ * @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>,
+ route: String?,
+ method: RpcHttpMethod
+ ) {
+ throw IllegalStateException("This class is for Spring Boot integration.")
+ }
+
+ /**
+ * Binds a given route with a function of the receiver.
+ * @param function a function of the receiver
+ * @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>,
+ route: String?,
+ method: RpcHttpMethod
+ ) {
+ throw IllegalStateException("This class is for Spring Boot integration.")
+ }
+
+}