From 470953c78c2509224bb452c16f8bbea54e53b3d7 Mon Sep 17 00:00:00 2001 From: Robert Jaros <rjaros@finn.pl> Date: Mon, 8 Oct 2018 22:11:21 +0200 Subject: Better server class abstraction. --- .../kotlin/pl/treksoft/kvision/remote/Jooby.kt | 91 ---------------------- .../kotlin/pl/treksoft/kvision/remote/KVServer.kt | 91 ++++++++++++++++++++++ .../pl/treksoft/kvision/remote/ServiceManager.kt | 8 +- 3 files changed, 95 insertions(+), 95 deletions(-) delete mode 100644 kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/Jooby.kt create mode 100644 kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt (limited to 'kvision-modules/kvision-server-jooby') diff --git a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/Jooby.kt b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/Jooby.kt deleted file mode 100644 index 928892a2..00000000 --- a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/Jooby.kt +++ /dev/null @@ -1,91 +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. - */ -@file:Suppress("EXPERIMENTAL_FEATURE_WARNING") - -package pl.treksoft.kvision.remote - -import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper -import kotlinx.coroutines.Deferred -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.GlobalScope -import org.jooby.Kooby -import org.jooby.Session -import org.jooby.json.Jackson -import org.pac4j.core.profile.CommonProfile -import kotlinx.coroutines.async as coroutinesAsync - -/** - * A Jooby based server. - */ -actual open class JoobyServer(init: JoobyServer.() -> Unit) : Kooby() { - init { - @Suppress("LeakingThis") - assets("/", "index.html") - @Suppress("LeakingThis") - assets("/**").onMissing(0) - val mapper = jacksonObjectMapper() - @Suppress("LeakingThis") - use(Jackson(mapper)) - @Suppress("LeakingThis") - init.invoke(this) - } -} - -/** - * A server request. - */ -actual typealias Request = org.jooby.Request - -/** - * A user profile. - */ -actual typealias Profile = CommonProfile - -/** - * A helper extension function for asynchronous request processing. - */ -fun <RESP> Request?.async(block: (Request) -> RESP): Deferred<RESP> = this?.let { req -> - GlobalScope.coroutinesAsync(Dispatchers.Unconfined) { - block(req) - } -} ?: throw IllegalStateException("Request not set!") - -/** - * A helper extension function for asynchronous request processing with session. - */ -fun <RESP> Request?.async(block: (Request, Session) -> RESP): Deferred<RESP> = this?.let { req -> - val session = req.session() - GlobalScope.coroutinesAsync(Dispatchers.Unconfined) { - block(req, session) - } -} ?: throw IllegalStateException("Request not set!") - -/** - * A helper extension function for asynchronous request processing with session and user profile. - */ -fun <RESP> Request?.async(block: (Request, Session, Profile) -> RESP): Deferred<RESP> = this?.let { req -> - val session = req.session() - val profile = req.require(CommonProfile::class.java) - GlobalScope.coroutinesAsync(Dispatchers.Unconfined) { - block(req, session, profile) - } -} ?: throw IllegalStateException("Request not set!") 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 new file mode 100644 index 00000000..76f1ee30 --- /dev/null +++ b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt @@ -0,0 +1,91 @@ +/* + * 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. + */ +@file:Suppress("EXPERIMENTAL_FEATURE_WARNING") + +package pl.treksoft.kvision.remote + +import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import kotlinx.coroutines.Deferred +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.GlobalScope +import org.jooby.Kooby +import org.jooby.Session +import org.jooby.json.Jackson +import org.pac4j.core.profile.CommonProfile +import kotlinx.coroutines.async as coroutinesAsync + +/** + * A Jooby based server. + */ +actual open class KVServer(init: KVServer.() -> Unit) : Kooby() { + init { + @Suppress("LeakingThis") + assets("/", "index.html") + @Suppress("LeakingThis") + assets("/**").onMissing(0) + val mapper = jacksonObjectMapper() + @Suppress("LeakingThis") + use(Jackson(mapper)) + @Suppress("LeakingThis") + init.invoke(this) + } +} + +/** + * A server request. + */ +actual typealias Request = org.jooby.Request + +/** + * A user profile. + */ +actual typealias Profile = CommonProfile + +/** + * A helper extension function for asynchronous request processing. + */ +fun <RESP> Request?.async(block: (Request) -> RESP): Deferred<RESP> = this?.let { req -> + GlobalScope.coroutinesAsync(Dispatchers.Unconfined) { + block(req) + } +} ?: throw IllegalStateException("Request not set!") + +/** + * A helper extension function for asynchronous request processing with session. + */ +fun <RESP> Request?.async(block: (Request, Session) -> RESP): Deferred<RESP> = this?.let { req -> + val session = req.session() + GlobalScope.coroutinesAsync(Dispatchers.Unconfined) { + block(req, session) + } +} ?: throw IllegalStateException("Request not set!") + +/** + * A helper extension function for asynchronous request processing with session and user profile. + */ +fun <RESP> Request?.async(block: (Request, Session, Profile) -> RESP): Deferred<RESP> = this?.let { req -> + val session = req.session() + val profile = req.require(CommonProfile::class.java) + GlobalScope.coroutinesAsync(Dispatchers.Unconfined) { + block(req, session, profile) + } +} ?: throw IllegalStateException("Request not set!") diff --git a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/ServiceManager.kt b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/ServiceManager.kt index edaa9ba3..b37d7319 100644 --- a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/ServiceManager.kt +++ b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/ServiceManager.kt @@ -39,7 +39,7 @@ actual open class ServiceManager<out T> actual constructor(val service: T) { val LOG: Logger = LoggerFactory.getLogger(ServiceManager::class.java.name) } - protected val routes: MutableList<JoobyServer.() -> Unit> = mutableListOf() + protected val routes: MutableList<KVServer.() -> Unit> = mutableListOf() val mapper = jacksonObjectMapper() var counter: Int = 0 @@ -299,7 +299,7 @@ actual open class ServiceManager<out T> actual constructor(val service: T) { method: RpcHttpMethod, path: String, handler: (Request, Response) -> Unit - ): JoobyServer.() -> Unit { + ): KVServer.() -> Unit { return { when (method) { RpcHttpMethod.POST -> post(path, handler) @@ -322,9 +322,9 @@ actual open class ServiceManager<out T> actual constructor(val service: T) { /** * Applies all defined routes to the given server. - * @param k a Jooby server + * @param k a server */ - actual fun applyRoutes(k: JoobyServer) { + actual fun applyRoutes(k: KVServer) { routes.forEach { it.invoke(k) } -- cgit