From d3c8d1d0f2a1c3a6df5619b93fc4854fc8197240 Mon Sep 17 00:00:00 2001 From: Yiqun Zhang Date: Mon, 20 Nov 2023 22:10:14 +0800 Subject: [PATCH] :sparkles: Implementing clipevery web server (#19) --- composeApp/build.gradle.kts | 1 + .../kotlin/com/clipevery/net/ClipServer.kt | 5 ++ .../com/clipevery/net/DesktopClipServer.kt | 56 +++++++++++++++++++ composeApp/src/desktopMain/kotlin/main.kt | 14 +---- 4 files changed, 65 insertions(+), 11 deletions(-) create mode 100644 composeApp/src/desktopMain/kotlin/com/clipevery/net/DesktopClipServer.kt diff --git a/composeApp/build.gradle.kts b/composeApp/build.gradle.kts index 2904907f8..7b8f22507 100644 --- a/composeApp/build.gradle.kts +++ b/composeApp/build.gradle.kts @@ -20,6 +20,7 @@ kotlin { implementation("com.google.zxing:core:3.5.2") implementation("com.google.zxing:javase:3.5.2") implementation("ch.qos.logback:logback-classic:1.4.11") + implementation("io.javalin:javalin:5.6.3") } commonMain.dependencies { implementation(compose.runtime) diff --git a/composeApp/src/commonMain/kotlin/com/clipevery/net/ClipServer.kt b/composeApp/src/commonMain/kotlin/com/clipevery/net/ClipServer.kt index bbc1b5fdd..035dc4e1a 100644 --- a/composeApp/src/commonMain/kotlin/com/clipevery/net/ClipServer.kt +++ b/composeApp/src/commonMain/kotlin/com/clipevery/net/ClipServer.kt @@ -1,5 +1,10 @@ package com.clipevery.net interface ClipServer { + fun start(): ClipServer + + fun stop() + + fun port(): Int } diff --git a/composeApp/src/desktopMain/kotlin/com/clipevery/net/DesktopClipServer.kt b/composeApp/src/desktopMain/kotlin/com/clipevery/net/DesktopClipServer.kt new file mode 100644 index 000000000..07eb3793b --- /dev/null +++ b/composeApp/src/desktopMain/kotlin/com/clipevery/net/DesktopClipServer.kt @@ -0,0 +1,56 @@ +package com.clipevery.net + +import io.github.oshai.kotlinlogging.KotlinLogging +import io.javalin.Javalin +import io.javalin.apibuilder.EndpointGroup +import io.javalin.compression.CompressionStrategy +import io.javalin.config.JavalinConfig +import io.javalin.http.ExceptionHandler +import org.eclipse.jetty.server.Server +import org.eclipse.jetty.util.thread.QueuedThreadPool +import java.util.function.Consumer + + +class DesktopClipServer: ClipServer { + + private val logger = KotlinLogging.logger {} + + + private var server: Javalin? = null + + private fun config(): Consumer { + return Consumer { + it.jetty.server { Server(QueuedThreadPool(10)) } + it.http.maxRequestSize = Long.MAX_VALUE + it.http.defaultContentType = "application/json" + it.http.generateEtags = true + it.compression.custom(CompressionStrategy.GZIP) + } + } + + private fun endpoints(): EndpointGroup { + return EndpointGroup { + } + } + + private fun exceptionHandler(): ExceptionHandler { + return ExceptionHandler { e, ctx -> logger.error(e) { "${ctx.path()} exception" } } + } + + override fun start(): ClipServer { + server = Javalin.create(config()) + .routes(endpoints()) + .exception(Exception::class.java, exceptionHandler()) + .start("0.0.0.0", 0) + logger.info { "start server ${port()}" } + return this + } + + override fun stop() { + server?.stop() + } + + override fun port(): Int { + return server?.port() ?: 0 + } +} \ No newline at end of file diff --git a/composeApp/src/desktopMain/kotlin/main.kt b/composeApp/src/desktopMain/kotlin/main.kt index 8e01db37e..12b41395f 100644 --- a/composeApp/src/desktopMain/kotlin/main.kt +++ b/composeApp/src/desktopMain/kotlin/main.kt @@ -11,6 +11,7 @@ import com.clipevery.config.ConfigManager import com.clipevery.config.FileType import com.clipevery.log.initLogger import com.clipevery.net.ClipServer +import com.clipevery.net.DesktopClipServer import com.clipevery.path.PathProvider import com.clipevery.path.getPathProvider import com.clipevery.presist.DesktopOneFilePersist @@ -59,19 +60,10 @@ private fun getDependencies( ioScope: CoroutineScope ) = object : Dependencies() { - val logger = KotlinLogging.logger {} - - override val clipServer: ClipServer = object : ClipServer { - } + override val clipServer: ClipServer = DesktopClipServer().start() override val filePersist: FilePersist = object : FilePersist { - override val pathProvider: PathProvider = run { - val innerPathProvider = getPathProvider() - logger.info { "userPath ${innerPathProvider.resolveUser(null)}" } - logger.info { "appPath ${innerPathProvider.resolveApp(null)}" } - logger.info { "logPath ${innerPathProvider.resolveLog(null)}" } - getPathProvider() - } + override val pathProvider: PathProvider = getPathProvider() override fun createOneFilePersist(path: Path): OneFilePersist { return DesktopOneFilePersist(path)