From 48d0bddd8f27c7165f7dfce3abe1072b6eb4f9f1 Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Fri, 1 Feb 2019 15:30:28 +0100 Subject: Refactor server-side interfaces. --- .../kotlin/pl/treksoft/kvision/remote/KVModules.kt | 37 +++++++++++++++++ .../kotlin/pl/treksoft/kvision/remote/KVServer.kt | 47 ---------------------- .../pl/treksoft/kvision/remote/KVServiceManager.kt | 19 ++++----- 3 files changed, 45 insertions(+), 58 deletions(-) create mode 100644 kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVModules.kt delete mode 100644 kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt (limited to 'kvision-modules/kvision-server-jooby/src') diff --git a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVModules.kt b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVModules.kt new file mode 100644 index 00000000..9ae6d0e7 --- /dev/null +++ b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVModules.kt @@ -0,0 +1,37 @@ +/* + * 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 com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import org.jooby.Kooby +import org.jooby.json.Jackson +import pl.treksoft.kvision.types.KV_JSON_DATE_FORMAT +import java.text.SimpleDateFormat + +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/KVServer.kt b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt deleted file mode 100644 index eab093ec..00000000 --- a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt +++ /dev/null @@ -1,47 +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 com.fasterxml.jackson.module.kotlin.jacksonObjectMapper -import org.jooby.Kooby -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) - } -} 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 actual constructor(val serviceClass: val LOG: Logger = LoggerFactory.getLogger(KVServiceManager::class.java.name) } - protected val routes: MutableList Unit> = mutableListOf() + val routes: MutableList Unit> = mutableListOf() val mapper = jacksonObjectMapper().apply { dateFormat = SimpleDateFormat(KV_JSON_DATE_FORMAT) } @@ -349,7 +350,7 @@ actual open class KVServiceManager 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 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 Kooby.applyRoutes(serviceManager: KVServiceManager) { + serviceManager.routes.forEach { + it.invoke(this@applyRoutes) + } } -- cgit