-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Implementing clipevery web server (#19)
- Loading branch information
1 parent
35c6bb7
commit d3c8d1d
Showing
4 changed files
with
65 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
composeApp/src/commonMain/kotlin/com/clipevery/net/ClipServer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
56 changes: 56 additions & 0 deletions
56
composeApp/src/desktopMain/kotlin/com/clipevery/net/DesktopClipServer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters