Skip to content

Commit

Permalink
Update dependencies, use Ktor CIO for server
Browse files Browse the repository at this point in the history
  • Loading branch information
MrPowerGamerBR committed Nov 16, 2024
1 parent 5a1d5a0 commit dfc5fdb
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 10 deletions.
5 changes: 1 addition & 4 deletions backend/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ dependencies {
implementation("ch.qos.logback:logback-classic:1.5.10")

implementation(libs.kotlin.logging)
implementation(libs.ktor.server.netty)
implementation(libs.ktor.server.cio)
implementation(libs.ktor.server.compression)
implementation(libs.ktor.server.caching.headers)
implementation(libs.ktor.server.cors)
Expand All @@ -24,9 +24,6 @@ dependencies {

implementation(project(":common"))

// Sequins
implementation("net.perfectdreams.sequins.ktor:base-route:1.0.4")

testImplementation(kotlin("test"))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import io.ktor.http.*
import io.ktor.http.content.*
import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.ktor.server.cio.*
import io.ktor.server.plugins.cachingheaders.*
import io.ktor.server.plugins.compression.*
import io.ktor.server.plugins.cors.routing.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.sync.Mutex
Expand Down Expand Up @@ -80,7 +81,7 @@ class EtherealGambi(val config: EtherealGambiConfig) {
}
)

val server = embeddedServer(Netty) {
val server = embeddedServer(CIO) {
// Enables gzip and deflate compression
install(Compression)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package net.perfectdreams.sequins.ktor

import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.routing.*

/**
* A [BaseRoute] is used to register Ktor routes on the [routing] section, allowing you to split up routes in different classes.
*
* One of the advantages of using [BaseRoute] is that all routes are in different files, keeping the code base tidy and nice.
*
* Another advantage is that the HTTP Method is inferred from the class name, so, if the class is named `PostUserDataRoute`, the route will be
* registered as a POST instead of an GET.
*
* All HTTP Methods, except GET, needs to be explictly set in the class name.
*
* @param path the path's route
*/
abstract class BaseRoute(val path: String) {
abstract suspend fun onRequest(call: ApplicationCall)

fun register(route: Route) = registerWithPath(route, path) { onRequest(call) }
fun registerWithPath(route: Route, path: String) = registerWithPath(route, path) { onRequest(call) }

fun registerWithPath(route: Route, path: String, callback: RoutingHandler) {
val method = getMethod()
when (method) {
HttpMethod.Get -> route.get(path, callback)
HttpMethod.Post -> route.post(path, callback)
HttpMethod.Patch -> route.patch(path, callback)
HttpMethod.Put -> route.put(path, callback)
HttpMethod.Delete -> route.delete(path, callback)
else -> route.get(path, callback)
}
}

open fun getMethod(): HttpMethod {
val className = this::class.simpleName?.toLowerCase() ?: "Unknown"
return when {
className.startsWith("get") -> HttpMethod.Get
className.startsWith("post") -> HttpMethod.Post
className.startsWith("patch") -> HttpMethod.Patch
className.startsWith("put") -> HttpMethod.Put
className.startsWith("delete") -> HttpMethod.Delete
else -> HttpMethod.Get
}
}
}
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {

allprojects {
group = "net.perfectdreams.etherealgambi"
version = "1.0.3"
version = "1.0.4"

repositories {
mavenCentral()
Expand Down
6 changes: 3 additions & 3 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ include(":client")
dependencyResolutionManagement {
versionCatalogs {
create("libs") {
val kotlin = version("kotlin", "1.9.10")
val kotlin = version("kotlin", "2.0.21")
val kotlinXSerialization = version("kotlinx-serialization", "1.6.0")
val ktor = version("ktor", "2.3.3")
val ktor = version("ktor", "3.0.1")

library("kotlinx-coroutines-core", "org.jetbrains.kotlinx", "kotlinx-coroutines-core").version("1.7.3")
library("kotlin-logging", "io.github.microutils", "kotlin-logging").version("3.0.5")
Expand All @@ -20,7 +20,7 @@ dependencyResolutionManagement {
library("kotlinx-serialization-hocon", "org.jetbrains.kotlinx", "kotlinx-serialization-hocon").versionRef(kotlinXSerialization)
library("ktor-http", "io.ktor", "ktor-http").versionRef(ktor)
library("ktor-client-cio", "io.ktor", "ktor-client-cio").versionRef(ktor)
library("ktor-server-netty", "io.ktor", "ktor-server-netty").versionRef(ktor)
library("ktor-server-cio", "io.ktor", "ktor-server-cio").versionRef(ktor)
library("ktor-server-compression", "io.ktor", "ktor-server-compression").versionRef(ktor)
library("ktor-server-caching-headers", "io.ktor", "ktor-server-caching-headers").versionRef(ktor)
library("ktor-server-cors", "io.ktor", "ktor-server-cors").versionRef(ktor)
Expand Down

0 comments on commit dfc5fdb

Please sign in to comment.