aboutsummaryrefslogtreecommitdiff
path: root/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft
diff options
context:
space:
mode:
Diffstat (limited to 'kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft')
-rw-r--r--kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt48
1 files changed, 48 insertions, 0 deletions
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 a241842c..162b92dd 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
@@ -373,6 +373,54 @@ actual open class SpringServiceManager<T : Any> actual constructor(val serviceCl
}
}
+ /**
+ * Binds a given function of the receiver as a select options source
+ * @param function a function of the receiver
+ */
+ @Suppress("TooGenericExceptionCaught")
+ protected actual fun bind(
+ function: T.(String) -> List<RemoteSelectOption>
+ ) {
+ val routeDef = "route${this::class.simpleName}${counter++}"
+ addRoute(RpcHttpMethod.POST, "/kv/$routeDef") { req, res ->
+ val service = SpringContext.getBean(serviceClass.java)
+ val jsonRpcRequest = mapper.readValue(req.inputStream, JsonRpcRequest::class.java)
+ if (jsonRpcRequest.params.size == 1) {
+ val param = getParameter<String>(jsonRpcRequest.params[0])
+ 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"
+ )
+ )
+ )
+ }
+ } else {
+ res.writeJSON(
+ mapper.writeValueAsString(
+ JsonRpcResponse(
+ id = jsonRpcRequest.id,
+ error = "Invalid parameters"
+ )
+ )
+ )
+ }
+ }
+ }
+
fun addRoute(
method: RpcHttpMethod,
path: String,