From c0b7927e19bb5ec4cd0c3b973486a85f4a1ff56a Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Mon, 11 Feb 2019 23:02:40 +0100 Subject: Refactoring of server side components. --- .../pl/treksoft/kvision/remote/KVController.kt | 3 +- .../pl/treksoft/kvision/remote/KVServiceManager.kt | 55 ++++++++++++++-------- 2 files changed, 37 insertions(+), 21 deletions(-) (limited to 'kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision') 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 actual constructor(val serviceClass: val LOG: Logger = LoggerFactory.getLogger(KVServiceManager::class.java.name) } + val getRequests: MutableMap Unit> = mutableMapOf() val postRequests: MutableMap Unit> = mutableMapOf() val putRequests: MutableMap Unit> = mutableMapOf() val deleteRequests: MutableMap Unit> = mutableMapOf() @@ -57,18 +58,22 @@ actual open class KVServiceManager 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 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 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 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 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 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 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 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 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 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 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 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 actual constructor(val serviceClass: function: T.(String?, String?) -> List ) { 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 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 } } -- cgit