aboutsummaryrefslogtreecommitdiff
path: root/kvision-modules/kvision-server-javalin/src/jvmMain/kotlin/pl/treksoft
diff options
context:
space:
mode:
Diffstat (limited to 'kvision-modules/kvision-server-javalin/src/jvmMain/kotlin/pl/treksoft')
-rw-r--r--kvision-modules/kvision-server-javalin/src/jvmMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt50
1 files changed, 50 insertions, 0 deletions
diff --git a/kvision-modules/kvision-server-javalin/src/jvmMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt b/kvision-modules/kvision-server-javalin/src/jvmMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt
index e3189a74..33c858f4 100644
--- a/kvision-modules/kvision-server-javalin/src/jvmMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt
+++ b/kvision-modules/kvision-server-javalin/src/jvmMain/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt
@@ -384,6 +384,56 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass:
}
/**
+ * Binds a given route with a function of the receiver.
+ * @param function a function of the receiver
+ * @param method a HTTP method
+ * @param route a route
+ */
+ @Suppress("TooGenericExceptionCaught")
+ protected actual inline fun <reified PAR1, reified PAR2, reified PAR3,
+ reified PAR4, reified PAR5, reified PAR6, reified RET> bind(
+ noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5, PAR6) -> RET,
+ method: HttpMethod, route: String?
+ ) {
+ if (method == HttpMethod.GET)
+ throw UnsupportedOperationException("GET method is only supported for methods without parameters")
+ val routeDef = route ?: "route${this::class.simpleName}${counter++}"
+ addRoute(method, "/kv/$routeDef") { ctx ->
+ val jsonRpcRequest = ctx.body<JsonRpcRequest>()
+ @Suppress("MagicNumber")
+ if (jsonRpcRequest.params.size == 6) {
+ 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])
+ val param6 = getParameter<PAR6>(jsonRpcRequest.params[5])
+ val injector = ctx.attribute<Injector>(KV_INJECTOR_KEY)!!
+ val service = injector.getInstance(serviceClass.java)
+ initializeService(service, ctx)
+ val future = GlobalScope.future {
+ try {
+ val result = function.invoke(service, param1, param2, param3, param4, param5, param6)
+ JsonRpcResponse(
+ id = jsonRpcRequest.id,
+ result = mapper.writeValueAsString(result)
+ )
+ } catch (e: Exception) {
+ if (e !is ServiceException) LOG.error(e.message, e)
+ JsonRpcResponse(
+ id = jsonRpcRequest.id, error = e.message ?: "Error",
+ exceptionType = e.javaClass.canonicalName
+ )
+ }
+ }
+ ctx.json(future)
+ } else {
+ ctx.json(JsonRpcResponse(id = jsonRpcRequest.id, error = "Invalid parameters"))
+ }
+ }
+ }
+
+ /**
* Binds a given web socket connection with a function of the receiver.
* @param function a function of the receiver
* @param route a route