diff options
author | Robert Jaros <rjaros@finn.pl> | 2019-02-01 15:30:28 +0100 |
---|---|---|
committer | Robert Jaros <rjaros@finn.pl> | 2019-02-01 15:30:28 +0100 |
commit | 48d0bddd8f27c7165f7dfce3abe1072b6eb4f9f1 (patch) | |
tree | 628b9df75c3f361238edb297cec7123c2679974e /kvision-modules/kvision-server-jooby/src | |
parent | 110ba60c6557bf16a3bfc5f7ff5af46a98164c75 (diff) | |
download | kvision-48d0bddd8f27c7165f7dfce3abe1072b6eb4f9f1.tar.gz kvision-48d0bddd8f27c7165f7dfce3abe1072b6eb4f9f1.tar.bz2 kvision-48d0bddd8f27c7165f7dfce3abe1072b6eb4f9f1.zip |
Refactor server-side interfaces.
Diffstat (limited to 'kvision-modules/kvision-server-jooby/src')
-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 |
2 files changed, 14 insertions, 27 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) + } } |