diff options
author | Robert Jaros <rjaros@finn.pl> | 2018-12-19 12:14:03 +0100 |
---|---|---|
committer | Robert Jaros <rjaros@finn.pl> | 2018-12-19 12:14:03 +0100 |
commit | 474195d3aa862686712cfe6c800dc43f8fee8ec5 (patch) | |
tree | 2c89307d8740b3efa7648061dbeb57a822c40e6d /kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote | |
parent | 161264957dc1b41cd6716ee7777139c5e29589f5 (diff) | |
download | kvision-474195d3aa862686712cfe6c800dc43f8fee8ec5.tar.gz kvision-474195d3aa862686712cfe6c800dc43f8fee8ec5.tar.bz2 kvision-474195d3aa862686712cfe6c800dc43f8fee8ec5.zip |
An addon remote module for select component.
Diffstat (limited to 'kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote')
-rw-r--r-- | kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt | 48 |
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, |