diff options
author | Robert Jaros <rjaros@finn.pl> | 2019-02-11 23:02:40 +0100 |
---|---|---|
committer | Robert Jaros <rjaros@finn.pl> | 2019-02-11 23:02:40 +0100 |
commit | c0b7927e19bb5ec4cd0c3b973486a85f4a1ff56a (patch) | |
tree | ec0ea1304f242de3ecc123e55979b03325ab41be /kvision-modules/kvision-server-spring-boot/src | |
parent | 0d32732cfa8e0b359e1fd13523a6eb4356c97441 (diff) | |
download | kvision-c0b7927e19bb5ec4cd0c3b973486a85f4a1ff56a.tar.gz kvision-c0b7927e19bb5ec4cd0c3b973486a85f4a1ff56a.tar.bz2 kvision-c0b7927e19bb5ec4cd0c3b973486a85f4a1ff56a.zip |
Refactoring of server side components.
Diffstat (limited to 'kvision-modules/kvision-server-spring-boot/src')
2 files changed, 37 insertions, 21 deletions
diff --git a/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVController.kt b/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVController.kt index 50544dc1..86789705 100644 --- a/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVController.kt +++ b/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVController.kt @@ -36,12 +36,13 @@ open class KVController { @RequestMapping( "/kv/**", - method = [RequestMethod.POST, RequestMethod.PUT, RequestMethod.DELETE, RequestMethod.OPTIONS] + method = [RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT, RequestMethod.DELETE, RequestMethod.OPTIONS] ) open fun kVMapping(req: HttpServletRequest, res: HttpServletResponse) { val routeUrl = req.requestURI val route = kvServer.services.mapNotNull { when (req.method) { + "GET" -> it.getRequests[routeUrl] "POST" -> it.postRequests[routeUrl] "PUT" -> it.putRequests[routeUrl] "DELETE" -> it.deleteRequests[routeUrl] diff --git a/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt b/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt index 4820fb2e..4705b0b1 100644 --- a/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt +++ b/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt @@ -44,6 +44,7 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: val LOG: Logger = LoggerFactory.getLogger(KVServiceManager::class.java.name) } + val getRequests: MutableMap<String, (HttpServletRequest, HttpServletResponse) -> Unit> = mutableMapOf() val postRequests: MutableMap<String, (HttpServletRequest, HttpServletResponse) -> Unit> = mutableMapOf() val putRequests: MutableMap<String, (HttpServletRequest, HttpServletResponse) -> Unit> = mutableMapOf() val deleteRequests: MutableMap<String, (HttpServletRequest, HttpServletResponse) -> Unit> = mutableMapOf() @@ -57,18 +58,22 @@ 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 route a route * @param method a HTTP method + * @param route a route */ @Suppress("TooGenericExceptionCaught") protected actual inline fun <reified RET> bind( noinline function: suspend T.() -> RET, - route: String?, method: RpcHttpMethod + method: HttpMethod, route: String? ) { 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) + val jsonRpcRequest = if (method == HttpMethod.GET) { + JsonRpcRequest(req.getParameter("id")?.toInt() ?: 0, "", listOf()) + } else { + mapper.readValue(req.inputStream, JsonRpcRequest::class.java) + } GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) { try { val result = function.invoke(service) @@ -98,14 +103,16 @@ 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 route a route * @param method a HTTP method + * @param route a route */ @Suppress("TooGenericExceptionCaught") protected actual inline fun <reified PAR, reified RET> bind( noinline function: suspend T.(PAR) -> RET, - route: String?, method: RpcHttpMethod + 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") { req, res -> val service = SpringContext.getBean(serviceClass.java) @@ -151,14 +158,16 @@ 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 route a route * @param method a HTTP method + * @param route a route */ @Suppress("TooGenericExceptionCaught") protected actual inline fun <reified PAR1, reified PAR2, reified RET> bind( noinline function: suspend T.(PAR1, PAR2) -> RET, - route: String?, method: RpcHttpMethod + 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") { req, res -> val service = SpringContext.getBean(serviceClass.java) @@ -205,14 +214,16 @@ 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 route a route * @param method a HTTP method + * @param route a route */ @Suppress("TooGenericExceptionCaught") protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, reified RET> bind( noinline function: suspend T.(PAR1, PAR2, PAR3) -> RET, - route: String?, method: RpcHttpMethod + 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") { req, res -> val service = SpringContext.getBean(serviceClass.java) @@ -261,14 +272,16 @@ 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 route a route * @param method a HTTP method + * @param route a route */ @Suppress("TooGenericExceptionCaught") protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, reified PAR4, reified RET> bind( noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4) -> RET, - route: String?, method: RpcHttpMethod + 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") { req, res -> val service = SpringContext.getBean(serviceClass.java) @@ -318,16 +331,17 @@ 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 route a route * @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 RET> bind( noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5) -> RET, - route: String?, - method: RpcHttpMethod + 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") { req, res -> val service = SpringContext.getBean(serviceClass.java) @@ -384,7 +398,7 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: function: T.(String?, String?) -> List<RemoteSelectOption> ) { val routeDef = "route${this::class.simpleName}${counter++}" - addRoute(RpcHttpMethod.POST, "/kv/$routeDef") { req, res -> + addRoute(HttpMethod.POST, "/kv/$routeDef") { req, res -> val service = SpringContext.getBean(serviceClass.java) val jsonRpcRequest = mapper.readValue(req.inputStream, JsonRpcRequest::class.java) if (jsonRpcRequest.params.size == 2) { @@ -425,15 +439,16 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: } fun addRoute( - method: RpcHttpMethod, + method: HttpMethod, path: String, handler: (HttpServletRequest, HttpServletResponse) -> Unit ) { when (method) { - RpcHttpMethod.POST -> postRequests[path] = handler - RpcHttpMethod.PUT -> putRequests[path] = handler - RpcHttpMethod.DELETE -> deleteRequests[path] = handler - RpcHttpMethod.OPTIONS -> optionsRequests[path] = handler + HttpMethod.GET -> getRequests[path] = handler + HttpMethod.POST -> postRequests[path] = handler + HttpMethod.PUT -> putRequests[path] = handler + HttpMethod.DELETE -> deleteRequests[path] = handler + HttpMethod.OPTIONS -> optionsRequests[path] = handler } } |