diff options
Diffstat (limited to 'kvision-modules')
-rw-r--r-- | kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVModules.kt (renamed from kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt) | 22 | ||||
-rw-r--r-- | kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt | 19 | ||||
-rw-r--r-- | kvision-modules/kvision-server-ktor/src/main/kotlin/pl/treksoft/kvision/remote/KVModules.kt | 22 | ||||
-rw-r--r-- | kvision-modules/kvision-server-ktor/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt | 30 | ||||
-rw-r--r-- | kvision-modules/kvision-server-ktor/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt | 48 |
5 files changed, 52 insertions, 89 deletions
diff --git a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVModules.kt index eab093ec..9ae6d0e7 100644 --- a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt +++ b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVModules.kt @@ -27,21 +27,11 @@ import org.jooby.json.Jackson import pl.treksoft.kvision.types.KV_JSON_DATE_FORMAT import java.text.SimpleDateFormat -/** - * A Jooby based server. - */ -open class KVServer(init: KVServer.() -> Unit) : Kooby() { - init { - @Suppress("LeakingThis") - assets("/", "index.html") - @Suppress("LeakingThis") - assets("/**").onMissing(0) - val mapper = jacksonObjectMapper().apply { - dateFormat = SimpleDateFormat(KV_JSON_DATE_FORMAT) - } - @Suppress("LeakingThis") - use(Jackson(mapper)) - @Suppress("LeakingThis") - init.invoke(this) +fun Kooby.kvisionInit() { + assets("/", "/assets/index.html") + assets("/**", "/assets/{0}").onMissing(0) + val mapper = jacksonObjectMapper().apply { + dateFormat = SimpleDateFormat(KV_JSON_DATE_FORMAT) } + use(Jackson(mapper)) } diff --git a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt index 4e8516d3..bff27798 100644 --- a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt +++ b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt @@ -26,6 +26,7 @@ import com.google.inject.Injector import kotlinx.coroutines.CoroutineStart import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch +import org.jooby.Kooby import org.jooby.Request import org.jooby.Response import org.slf4j.Logger @@ -44,7 +45,7 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: val LOG: Logger = LoggerFactory.getLogger(KVServiceManager::class.java.name) } - protected val routes: MutableList<KVServer.() -> Unit> = mutableListOf() + val routes: MutableList<Kooby.() -> Unit> = mutableListOf() val mapper = jacksonObjectMapper().apply { dateFormat = SimpleDateFormat(KV_JSON_DATE_FORMAT) } @@ -349,7 +350,7 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: method: RpcHttpMethod, path: String, handler: (Request, Response) -> Unit - ): KVServer.() -> Unit { + ): Kooby.() -> Unit { return { when (method) { RpcHttpMethod.POST -> post(path, handler) @@ -370,14 +371,10 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: } ?: null as T } - /** - * Applies all defined routes to the given server. - * @param k a server - */ - fun applyRoutes(k: KVServer) { - routes.forEach { - it.invoke(k) - } - } +} +fun <T : Any> Kooby.applyRoutes(serviceManager: KVServiceManager<T>) { + serviceManager.routes.forEach { + it.invoke(this@applyRoutes) + } } diff --git a/kvision-modules/kvision-server-ktor/src/main/kotlin/pl/treksoft/kvision/remote/KVModules.kt b/kvision-modules/kvision-server-ktor/src/main/kotlin/pl/treksoft/kvision/remote/KVModules.kt index eb57f7d7..06018761 100644 --- a/kvision-modules/kvision-server-ktor/src/main/kotlin/pl/treksoft/kvision/remote/KVModules.kt +++ b/kvision-modules/kvision-server-ktor/src/main/kotlin/pl/treksoft/kvision/remote/KVModules.kt @@ -31,19 +31,16 @@ import io.ktor.application.ApplicationCallPipeline import io.ktor.application.call import io.ktor.application.install import io.ktor.features.ContentNegotiation -import io.ktor.http.HttpMethod import io.ktor.http.content.default import io.ktor.http.content.resources import io.ktor.http.content.static import io.ktor.jackson.jackson -import io.ktor.request.httpMethod -import io.ktor.request.uri import io.ktor.routing.routing import io.ktor.util.AttributeKey import pl.treksoft.kvision.types.KV_JSON_DATE_FORMAT import java.text.SimpleDateFormat -fun Application.kvision(vararg modules: Module) { +fun Application.kvisionInit(vararg modules: Module) { install(ContentNegotiation) { jackson { dateFormat = SimpleDateFormat(KV_JSON_DATE_FORMAT) @@ -56,27 +53,12 @@ fun Application.kvision(vararg modules: Module) { } } + @Suppress("SpreadOperator") val injector = Guice.createInjector(MainModule(this), *modules) - val kvServer = injector.getInstance(KVServer::class.java) intercept(ApplicationCallPipeline.Features) { call.attributes.put(InjectorKey, injector.createChildInjector(CallModule(call))) } - - intercept(ApplicationCallPipeline.Call) { - val routeUri = call.request.uri - if (routeUri.startsWith("/kv/")) { - kvServer.services.mapNotNull { - when (call.request.httpMethod) { - HttpMethod.Post -> it.postRequests[routeUri] - HttpMethod.Put -> it.putRequests[routeUri] - HttpMethod.Delete -> it.deleteRequests[routeUri] - HttpMethod.Options -> it.optionsRequests[routeUri] - else -> null - } - }.firstOrNull()?.invoke(call) - } - } } val InjectorKey = AttributeKey<Injector>("injector") diff --git a/kvision-modules/kvision-server-ktor/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt b/kvision-modules/kvision-server-ktor/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt deleted file mode 100644 index 29084e5d..00000000 --- a/kvision-modules/kvision-server-ktor/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (c) 2017-present Robert Jaros - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package pl.treksoft.kvision.remote - -import org.pac4j.core.profile.CommonProfile -import kotlinx.coroutines.async as coroutinesAsync - -/** - * A Ktor based server. - */ -open class KVServer(val services: List<KVServiceManager<*>>) diff --git a/kvision-modules/kvision-server-ktor/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt b/kvision-modules/kvision-server-ktor/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt index 471eff2a..fbc94f64 100644 --- a/kvision-modules/kvision-server-ktor/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt +++ b/kvision-modules/kvision-server-ktor/src/main/kotlin/pl/treksoft/kvision/remote/KVServiceManager.kt @@ -23,8 +23,15 @@ package pl.treksoft.kvision.remote import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import io.ktor.application.ApplicationCall +import io.ktor.application.call import io.ktor.request.receive import io.ktor.response.respond +import io.ktor.routing.Route +import io.ktor.routing.delete +import io.ktor.routing.options +import io.ktor.routing.post +import io.ktor.routing.put +import io.ktor.util.pipeline.PipelineContext import kotlinx.coroutines.ExperimentalCoroutinesApi import org.slf4j.Logger import org.slf4j.LoggerFactory @@ -42,10 +49,12 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: val LOG: Logger = LoggerFactory.getLogger(KVServiceManager::class.java.name) } - val postRequests: MutableMap<String, suspend (ApplicationCall) -> Unit> = mutableMapOf() - val putRequests: MutableMap<String, suspend (ApplicationCall) -> Unit> = mutableMapOf() - val deleteRequests: MutableMap<String, suspend (ApplicationCall) -> Unit> = mutableMapOf() - val optionsRequests: MutableMap<String, suspend (ApplicationCall) -> Unit> = mutableMapOf() + val postRequests: MutableMap<String, suspend PipelineContext<Unit, ApplicationCall>.(Unit) -> Unit> = mutableMapOf() + val putRequests: MutableMap<String, suspend PipelineContext<Unit, ApplicationCall>.(Unit) -> Unit> = mutableMapOf() + val deleteRequests: MutableMap<String, suspend PipelineContext<Unit, ApplicationCall>.(Unit) -> Unit> = + mutableMapOf() + val optionsRequests: MutableMap<String, suspend PipelineContext<Unit, ApplicationCall>.(Unit) -> Unit> = + mutableMapOf() val mapper = jacksonObjectMapper().apply { dateFormat = SimpleDateFormat(KV_JSON_DATE_FORMAT) @@ -64,7 +73,7 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: route: String?, method: RpcHttpMethod ) { val routeDef = route ?: "route${this::class.simpleName}${counter++}" - addRoute(method, "/kv/$routeDef") { call -> + addRoute(method, "/kv/$routeDef") { val service = call.injector.getInstance(serviceClass.java) val jsonRpcRequest = call.receive<JsonRpcRequest>() try { @@ -99,7 +108,7 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: route: String?, method: RpcHttpMethod ) { val routeDef = route ?: "route${this::class.simpleName}${counter++}" - addRoute(method, "/kv/$routeDef") { call -> + addRoute(method, "/kv/$routeDef") { val service = call.injector.getInstance(serviceClass.java) val jsonRpcRequest = call.receive<JsonRpcRequest>() if (jsonRpcRequest.params.size == 1) { @@ -144,7 +153,7 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: route: String?, method: RpcHttpMethod ) { val routeDef = route ?: "route${this::class.simpleName}${counter++}" - addRoute(method, "/kv/$routeDef") { call -> + addRoute(method, "/kv/$routeDef") { val service = call.injector.getInstance(serviceClass.java) val jsonRpcRequest = call.receive<JsonRpcRequest>() if (jsonRpcRequest.params.size == 2) { @@ -190,7 +199,7 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: route: String?, method: RpcHttpMethod ) { val routeDef = route ?: "route${this::class.simpleName}${counter++}" - addRoute(method, "/kv/$routeDef") { call -> + addRoute(method, "/kv/$routeDef") { val service = call.injector.getInstance(serviceClass.java) val jsonRpcRequest = call.receive<JsonRpcRequest>() @Suppress("MagicNumber") @@ -238,7 +247,7 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: route: String?, method: RpcHttpMethod ) { val routeDef = route ?: "route${this::class.simpleName}${counter++}" - addRoute(method, "/kv/$routeDef") { call -> + addRoute(method, "/kv/$routeDef") { val service = call.injector.getInstance(serviceClass.java) val jsonRpcRequest = call.receive<JsonRpcRequest>() @Suppress("MagicNumber") @@ -289,7 +298,7 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: method: RpcHttpMethod ) { val routeDef = route ?: "route${this::class.simpleName}${counter++}" - addRoute(method, "/kv/$routeDef") { call -> + addRoute(method, "/kv/$routeDef") { val service = call.injector.getInstance(serviceClass.java) val jsonRpcRequest = call.receive<JsonRpcRequest>() @Suppress("MagicNumber") @@ -336,7 +345,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") { call -> + addRoute(RpcHttpMethod.POST, "/kv/$routeDef") { val service = call.injector.getInstance(serviceClass.java) val jsonRpcRequest = call.receive<JsonRpcRequest>() if (jsonRpcRequest.params.size == 2) { @@ -373,7 +382,7 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: fun addRoute( method: RpcHttpMethod, path: String, - handler: suspend (ApplicationCall) -> Unit + handler: suspend PipelineContext<Unit, ApplicationCall>.(Unit) -> Unit ) { when (method) { RpcHttpMethod.POST -> postRequests[path] = handler @@ -393,3 +402,18 @@ actual open class KVServiceManager<T : Any> actual constructor(val serviceClass: } ?: null as T } } + +fun <T : Any> Route.applyRoutes(serviceManager: KVServiceManager<T>) { + serviceManager.postRequests.forEach { (path, handler) -> + post(path, handler) + } + serviceManager.putRequests.forEach { (path, handler) -> + put(path, handler) + } + serviceManager.deleteRequests.forEach { (path, handler) -> + delete(path, handler) + } + serviceManager.optionsRequests.forEach { (path, handler) -> + options(path, handler) + } +} |