Skip to content

Commit

Permalink
✨ Implementing clipevery web server
Browse files Browse the repository at this point in the history
  • Loading branch information
guiyanakuang committed Nov 20, 2023
1 parent 35c6bb7 commit 95d36f4
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 11 deletions.
1 change: 1 addition & 0 deletions composeApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.clipevery.net

interface ClipServer {
fun start(): ClipServer

fun stop()

fun port(): Int
}

Original file line number Diff line number Diff line change
@@ -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<JavalinConfig> {
return Consumer<JavalinConfig> {
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<in java.lang.Exception?> {
return ExceptionHandler<java.lang.Exception?> { 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
}
}
14 changes: 3 additions & 11 deletions composeApp/src/desktopMain/kotlin/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 95d36f4

Please sign in to comment.