aboutsummaryrefslogtreecommitdiff
path: root/kvision-server
diff options
context:
space:
mode:
Diffstat (limited to 'kvision-server')
-rw-r--r--kvision-server/build.gradle29
-rw-r--r--kvision-server/src/main/kotlin/pl/treksoft/kvision/remote/Jooby.kt68
-rw-r--r--kvision-server/src/main/kotlin/pl/treksoft/kvision/remote/ServiceManager.kt230
-rw-r--r--kvision-server/src/main/kotlin/pl/treksoft/kvision/types/KDate.kt42
4 files changed, 369 insertions, 0 deletions
diff --git a/kvision-server/build.gradle b/kvision-server/build.gradle
new file mode 100644
index 00000000..8be34ecf
--- /dev/null
+++ b/kvision-server/build.gradle
@@ -0,0 +1,29 @@
+apply plugin: "io.spring.dependency-management"
+apply plugin: 'kotlin-platform-jvm'
+apply plugin: 'kotlinx-serialization'
+
+dependencyManagement {
+ imports {
+ mavenBom "org.jooby:jooby-bom:${joobyVersion}"
+ }
+}
+
+dependencies {
+ expectedBy project(":kvision-common")
+ compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
+ compile "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$serializationVersion"
+ compile "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion"
+ compile "org.jooby:jooby-lang-kotlin"
+ compile "org.jooby:jooby-jackson"
+ compile "com.fasterxml.jackson.module:jackson-module-kotlin:${jacksonModuleKotlinVersion}"
+ testCompile "org.jetbrains.kotlin:kotlin-test:$kotlinVersion"
+ testCompile project(":kvision-common")
+}
+
+compileKotlin {
+ targetCompatibility = javaVersion
+ sourceCompatibility = javaVersion
+ kotlinOptions {
+ jvmTarget = javaVersion
+ }
+}
diff --git a/kvision-server/src/main/kotlin/pl/treksoft/kvision/remote/Jooby.kt b/kvision-server/src/main/kotlin/pl/treksoft/kvision/remote/Jooby.kt
new file mode 100644
index 00000000..14b2b376
--- /dev/null
+++ b/kvision-server/src/main/kotlin/pl/treksoft/kvision/remote/Jooby.kt
@@ -0,0 +1,68 @@
+/*
+ * 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.experimental.Deferred
+import org.jooby.Kooby
+import org.jooby.Session
+import org.jooby.json.Jackson
+import kotlinx.coroutines.experimental.async as coroutinesAsync
+
+/**
+ * A Jooby based server.
+ */
+actual open class JoobyServer(init: JoobyServer.() -> Unit) : Kooby() {
+ init {
+ val mapper = jacksonObjectMapper()
+ @Suppress("LeakingThis")
+ use(Jackson(mapper))
+ @Suppress("LeakingThis")
+ init.invoke(this)
+ }
+}
+
+/**
+ * A server request.
+ */
+actual typealias Request = org.jooby.Request
+
+/**
+ * A helper extension function for asynchronous request processing.
+ */
+fun <RESP> Request?.async(block: (Request) -> RESP): Deferred<RESP> = this?.let { req ->
+ coroutinesAsync {
+ block(req)
+ }
+} ?: throw IllegalStateException("Request not set!")
+
+/**
+ * A helper extension function for asynchronous request processing with session.
+ */
+fun <RESP> Request?.asyncSession(block: (Request, Session) -> RESP): Deferred<RESP> = this?.let { req ->
+ val session = req.session()
+ coroutinesAsync {
+ block(req, session)
+ }
+} ?: throw IllegalStateException("Request not set!")
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
new file mode 100644
index 00000000..45a995ac
--- /dev/null
+++ b/kvision-server/src/main/kotlin/pl/treksoft/kvision/remote/ServiceManager.kt
@@ -0,0 +1,230 @@
+/*
+ * 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 kotlinx.coroutines.experimental.Deferred
+import kotlinx.coroutines.experimental.runBlocking
+import org.jooby.Status
+
+/**
+ * Multiplatform service manager.
+ */
+@Suppress("EXPERIMENTAL_FEATURE_WARNING")
+actual open class ServiceManager<out T> actual constructor(val service: T?) {
+
+ protected val routes: MutableList<JoobyServer.() -> Unit> = mutableListOf()
+ val mapper = jacksonObjectMapper()
+
+ /**
+ * Binds a given route with a function of the receiver.
+ * @param route a route
+ * @param function a function of the receiver
+ */
+ protected actual inline fun <reified RET> bind(
+ route: String,
+ noinline function: T.(Request?) -> Deferred<RET>
+ ) {
+ routes.add({
+ post("$SERVICE_PREFIX/$route") { req, res ->
+ if (service != null) {
+ try {
+ res.send(runBlocking { function.invoke(service, req).await() })
+ } catch (e: Exception) {
+ e.printStackTrace()
+ res.status(500).send(e.message ?: "Error")
+ }
+ } else {
+ res.status(Status.BAD_REQUEST)
+ }
+ }
+ })
+ }
+
+ /**
+ * Binds a given route with a function of the receiver.
+ * @param route a route
+ * @param function a function of the receiver
+ */
+ protected actual inline fun <reified PAR, reified RET> bind(
+ route: String,
+ noinline function: T.(PAR, Request?) -> Deferred<RET>
+ ) {
+ routes.add({
+ post("$SERVICE_PREFIX/$route") { req, res ->
+ val param = try {
+ req.body(PAR::class.java)
+ } catch (e: Exception) {
+ null as PAR
+ }
+ if (service != null) {
+ try {
+ res.send(runBlocking { function.invoke(service, param, req).await() })
+ } catch (e: Exception) {
+ e.printStackTrace()
+ res.status(500).send(e.message ?: "Error")
+ }
+ } else {
+ res.status(Status.BAD_REQUEST)
+ }
+ }
+ })
+ }
+
+ /**
+ * Binds a given route with a function of the receiver.
+ * @param route a route
+ * @param function a function of the receiver
+ */
+ protected actual inline fun <reified PAR1, reified PAR2, reified RET> bind(
+ route: String,
+ noinline function: T.(PAR1, PAR2, Request?) -> Deferred<RET>
+ ) {
+ routes.add({
+ post("$SERVICE_PREFIX/$route") { req, res ->
+ val str = req.body(String::class.java)
+ val tree = mapper.readTree(str)
+ if (tree.size() == 2 && service != null) {
+ val p1 = mapper.treeToValue(tree[0], PAR1::class.java)
+ val p2 = mapper.treeToValue(tree[1], PAR2::class.java)
+ try {
+ res.send(runBlocking { function.invoke(service, p1, p2, req).await() })
+ } catch (e: Exception) {
+ e.printStackTrace()
+ res.status(500).send(e.message ?: "Error")
+ }
+ } else {
+ res.status(Status.BAD_REQUEST)
+ }
+ }
+ })
+ }
+
+ /**
+ * Binds a given route with a function of the receiver.
+ * @param route a route
+ * @param function a function of the receiver
+ */
+ protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, reified RET> bind(
+ route: String,
+ noinline function: T.(PAR1, PAR2, PAR3, Request?) -> Deferred<RET>
+ ) {
+ routes.add({
+ post("$SERVICE_PREFIX/$route") { req, res ->
+ val str = req.body(String::class.java)
+ val tree = mapper.readTree(str)
+ if (tree.size() == 3 && service != null) {
+ val p1 = mapper.treeToValue(tree[0], PAR1::class.java)
+ val p2 = mapper.treeToValue(tree[1], PAR2::class.java)
+ val p3 = mapper.treeToValue(tree[2], PAR3::class.java)
+ try {
+ res.send(runBlocking { function.invoke(service, p1, p2, p3, req).await() })
+ } catch (e: Exception) {
+ e.printStackTrace()
+ res.status(500).send(e.message ?: "Error")
+ }
+ } else {
+ res.status(Status.BAD_REQUEST)
+ }
+ }
+ })
+ }
+
+ /**
+ * Binds a given route with a function of the receiver.
+ * @param route a route
+ * @param function a function of the receiver
+ */
+ protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, reified PAR4, reified RET> bind(
+ route: String,
+ noinline function: T.(PAR1, PAR2, PAR3, PAR4, Request?) -> Deferred<RET>
+ ) {
+ routes.add({
+ post("$SERVICE_PREFIX/$route") { req, res ->
+ val str = req.body(String::class.java)
+ val tree = mapper.readTree(str)
+ if (tree.size() == 4 && service != null) {
+ val p1 = mapper.treeToValue(tree[0], PAR1::class.java)
+ val p2 = mapper.treeToValue(tree[1], PAR2::class.java)
+ val p3 = mapper.treeToValue(tree[2], PAR3::class.java)
+ val p4 = mapper.treeToValue(tree[3], PAR4::class.java)
+ try {
+ res.send(runBlocking { function.invoke(service, p1, p2, p3, p4, req).await() })
+ } catch (e: Exception) {
+ e.printStackTrace()
+ res.status(500).send(e.message ?: "Error")
+ }
+ } else {
+ res.status(Status.BAD_REQUEST)
+ }
+ }
+ })
+ }
+
+ /**
+ * Binds a given route with a function of the receiver.
+ * @param route a route
+ * @param function a function of the receiver
+ */
+ protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, reified PAR4, reified PAR5, reified RET> bind(
+ route: String,
+ noinline function: T.(PAR1, PAR2, PAR3, PAR4, PAR5, Request?) -> Deferred<RET>
+ ) {
+ routes.add({
+ post("$SERVICE_PREFIX/$route") { req, res ->
+ val str = req.body(String::class.java)
+ val tree = mapper.readTree(str)
+ if (tree.size() == 5 && service != null) {
+ val p1 = mapper.treeToValue(tree[0], PAR1::class.java)
+ val p2 = mapper.treeToValue(tree[1], PAR2::class.java)
+ val p3 = mapper.treeToValue(tree[2], PAR3::class.java)
+ val p4 = mapper.treeToValue(tree[3], PAR4::class.java)
+ val p5 = mapper.treeToValue(tree[4], PAR5::class.java)
+ try {
+ res.send(runBlocking { function.invoke(service, p1, p2, p3, p4, p5, req).await() })
+ } catch (e: Exception) {
+ e.printStackTrace()
+ res.status(500).send(e.message ?: "Error")
+ }
+ } else {
+ res.status(Status.BAD_REQUEST)
+ }
+ }
+ })
+ }
+
+ /**
+ * Applies all defined routes to the given server.
+ * @param k a Jooby server
+ */
+ actual fun applyRoutes(k: JoobyServer) {
+ routes.forEach {
+ it.invoke(k)
+ }
+ }
+
+ /**
+ * Returns the list of defined bindings.
+ * Not used on the jvm platform.
+ */
+ actual fun getCalls(): Map<String, String> = mapOf()
+}
diff --git a/kvision-server/src/main/kotlin/pl/treksoft/kvision/types/KDate.kt b/kvision-server/src/main/kotlin/pl/treksoft/kvision/types/KDate.kt
new file mode 100644
index 00000000..1d174f88
--- /dev/null
+++ b/kvision-server/src/main/kotlin/pl/treksoft/kvision/types/KDate.kt
@@ -0,0 +1,42 @@
+/*
+ * 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.types
+
+import java.text.SimpleDateFormat
+import java.util.*
+
+/**
+ * A serializable wrapper for a multiplatform Date type.
+ */
+@Suppress("MayBeConstant")
+actual val KDATE_FORMAT = "yyyy-MM-dd HH:mm:ss"
+
+internal actual fun nowDate(): KDate =
+ KDate(Date().time)
+
+internal actual fun String.toKDateF(format: String): KDate =
+ KDate(SimpleDateFormat(format).parse(this).time)
+
+internal actual fun KDate.toStringF(format: String) =
+ SimpleDateFormat(format).format(this.toJava())
+
+fun KDate.toJava(): java.util.Date = java.util.Date(this.time)