From 93cfdf0dcf6404eb636037fbb59fbff8389a434a Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Sat, 2 Jun 2018 00:07:39 +0200 Subject: Generate automatic routes names. --- .../pl/treksoft/kvision/remote/ServiceManager.kt | 55 ++++++++++++---------- 1 file changed, 31 insertions(+), 24 deletions(-) (limited to 'kvision-server/src/main/kotlin/pl/treksoft/kvision') diff --git a/kvision-server/src/main/kotlin/pl/treksoft/kvision/remote/ServiceManager.kt b/kvision-server/src/main/kotlin/pl/treksoft/kvision/remote/ServiceManager.kt index 6c1b47e7..50ef770f 100644 --- a/kvision-server/src/main/kotlin/pl/treksoft/kvision/remote/ServiceManager.kt +++ b/kvision-server/src/main/kotlin/pl/treksoft/kvision/remote/ServiceManager.kt @@ -33,7 +33,7 @@ import org.slf4j.LoggerFactory * Multiplatform service manager. */ @Suppress("EXPERIMENTAL_FEATURE_WARNING") -actual open class ServiceManager actual constructor(val service: T?) { +actual open class ServiceManager actual constructor(val service: T) { companion object { val LOG: Logger = LoggerFactory.getLogger(ServiceManager::class.java.name) @@ -41,20 +41,22 @@ actual open class ServiceManager actual constructor(val service: T?) { protected val routes: MutableList Unit> = mutableListOf() val mapper = jacksonObjectMapper() + var counter: Int = 0 /** * Binds a given route with a function of the receiver. - * @param route a route * @param function a function of the receiver + * @param route a route * @param method a HTTP method * @param prefix an URL address prefix */ protected actual inline fun bind( - route: String, - noinline function: T.(Request?) -> Deferred, method: RpcHttpMethod, prefix: String + noinline function: T.(Request?) -> Deferred, + route: String?, method: RpcHttpMethod, prefix: String ) { + val routeDef = if (route != null) route else "route${this::class.simpleName}${counter++}" routes.add({ - call(method, "$prefix$route") { req, res -> + call(method, "$prefix$routeDef") { req, res -> if (service != null) { val jsonRpcRequest = req.body(JsonRpcRequest::class.java) try { @@ -78,17 +80,18 @@ actual open class ServiceManager actual constructor(val service: T?) { /** * Binds a given route with a function of the receiver. - * @param route a route * @param function a function of the receiver + * @param route a route * @param method a HTTP method * @param prefix an URL address prefix */ protected actual inline fun bind( - route: String, - noinline function: T.(PAR, Request?) -> Deferred, method: RpcHttpMethod, prefix: String + noinline function: T.(PAR, Request?) -> Deferred, + route: String?, method: RpcHttpMethod, prefix: String ) { + val routeDef = if (route != null) route else "route${this::class.simpleName}${counter++}" routes.add({ - call(method, "$prefix$route") { req, res -> + call(method, "$prefix$routeDef") { req, res -> if (service != null) { val jsonRpcRequest = req.body(JsonRpcRequest::class.java) if (jsonRpcRequest.params.size == 1) { @@ -117,17 +120,18 @@ actual open class ServiceManager actual constructor(val service: T?) { /** * Binds a given route with a function of the receiver. - * @param route a route * @param function a function of the receiver + * @param route a route * @param method a HTTP method * @param prefix an URL address prefix */ protected actual inline fun bind( - route: String, - noinline function: T.(PAR1, PAR2, Request?) -> Deferred, method: RpcHttpMethod, prefix: String + noinline function: T.(PAR1, PAR2, Request?) -> Deferred, + route: String?, method: RpcHttpMethod, prefix: String ) { + val routeDef = if (route != null) route else "route${this::class.simpleName}${counter++}" routes.add({ - call(method, "$prefix$route") { req, res -> + call(method, "$prefix$routeDef") { req, res -> if (service != null) { val jsonRpcRequest = req.body(JsonRpcRequest::class.java) if (jsonRpcRequest.params.size == 2) { @@ -157,17 +161,18 @@ actual open class ServiceManager actual constructor(val service: T?) { /** * Binds a given route with a function of the receiver. - * @param route a route * @param function a function of the receiver + * @param route a route * @param method a HTTP method * @param prefix an URL address prefix */ protected actual inline fun bind( - route: String, - noinline function: T.(PAR1, PAR2, PAR3, Request?) -> Deferred, method: RpcHttpMethod, prefix: String + noinline function: T.(PAR1, PAR2, PAR3, Request?) -> Deferred, + route: String?, method: RpcHttpMethod, prefix: String ) { + val routeDef = if (route != null) route else "route${this::class.simpleName}${counter++}" routes.add({ - call(method, "$prefix$route") { req, res -> + call(method, "$prefix$routeDef") { req, res -> if (service != null) { val jsonRpcRequest = req.body(JsonRpcRequest::class.java) if (jsonRpcRequest.params.size == 3) { @@ -198,17 +203,18 @@ actual open class ServiceManager actual constructor(val service: T?) { /** * Binds a given route with a function of the receiver. - * @param route a route * @param function a function of the receiver + * @param route a route * @param method a HTTP method * @param prefix an URL address prefix */ protected actual inline fun bind( - route: String, - noinline function: T.(PAR1, PAR2, PAR3, PAR4, Request?) -> Deferred, method: RpcHttpMethod, prefix: String + noinline function: T.(PAR1, PAR2, PAR3, PAR4, Request?) -> Deferred, + route: String?, method: RpcHttpMethod, prefix: String ) { + val routeDef = if (route != null) route else "route${this::class.simpleName}${counter++}" routes.add({ - call(method, "$prefix$route") { req, res -> + call(method, "$prefix$routeDef") { req, res -> if (service != null) { val jsonRpcRequest = req.body(JsonRpcRequest::class.java) if (jsonRpcRequest.params.size == 4) { @@ -241,20 +247,21 @@ actual open class ServiceManager actual constructor(val service: T?) { /** * Binds a given route with a function of the receiver. - * @param route a route * @param function a function of the receiver + * @param route a route * @param method a HTTP method * @param prefix an URL address prefix */ protected actual inline fun bind( - route: String, noinline function: T.(PAR1, PAR2, PAR3, PAR4, PAR5, Request?) -> Deferred, + route: String?, method: RpcHttpMethod, prefix: String ) { + val routeDef = if (route != null) route else "route${this::class.simpleName}${counter++}" routes.add({ - call(method, "$prefix$route") { req, res -> + call(method, "$prefix$routeDef") { req, res -> if (service != null) { val jsonRpcRequest = req.body(JsonRpcRequest::class.java) if (jsonRpcRequest.params.size == 5) { -- cgit