From dfc5fdb35ee7e95b5b1bd7977cf6c7be911c8c3e Mon Sep 17 00:00:00 2001 From: MrPowerGamerBR Date: Sat, 16 Nov 2024 00:48:51 -0300 Subject: [PATCH] Update dependencies, use Ktor CIO for server --- backend/build.gradle.kts | 5 +- .../etherealgambi/backend/EtherealGambi.kt | 5 +- .../perfectdreams/sequins/ktor/BaseRoute.kt | 48 +++++++++++++++++++ build.gradle.kts | 2 +- settings.gradle.kts | 6 +-- 5 files changed, 56 insertions(+), 10 deletions(-) create mode 100644 backend/src/main/kotlin/net/perfectdreams/sequins/ktor/BaseRoute.kt diff --git a/backend/build.gradle.kts b/backend/build.gradle.kts index dc696b7..2b3d84d 100644 --- a/backend/build.gradle.kts +++ b/backend/build.gradle.kts @@ -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) @@ -24,9 +24,6 @@ dependencies { implementation(project(":common")) - // Sequins - implementation("net.perfectdreams.sequins.ktor:base-route:1.0.4") - testImplementation(kotlin("test")) } diff --git a/backend/src/main/kotlin/net/perfectdreams/etherealgambi/backend/EtherealGambi.kt b/backend/src/main/kotlin/net/perfectdreams/etherealgambi/backend/EtherealGambi.kt index 3ea1835..9051d3e 100644 --- a/backend/src/main/kotlin/net/perfectdreams/etherealgambi/backend/EtherealGambi.kt +++ b/backend/src/main/kotlin/net/perfectdreams/etherealgambi/backend/EtherealGambi.kt @@ -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 @@ -80,7 +81,7 @@ class EtherealGambi(val config: EtherealGambiConfig) { } ) - val server = embeddedServer(Netty) { + val server = embeddedServer(CIO) { // Enables gzip and deflate compression install(Compression) diff --git a/backend/src/main/kotlin/net/perfectdreams/sequins/ktor/BaseRoute.kt b/backend/src/main/kotlin/net/perfectdreams/sequins/ktor/BaseRoute.kt new file mode 100644 index 0000000..22647e6 --- /dev/null +++ b/backend/src/main/kotlin/net/perfectdreams/sequins/ktor/BaseRoute.kt @@ -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 + } + } +} \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index 68cde90..7df0972 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -5,7 +5,7 @@ plugins { allprojects { group = "net.perfectdreams.etherealgambi" - version = "1.0.3" + version = "1.0.4" repositories { mavenCentral() diff --git a/settings.gradle.kts b/settings.gradle.kts index 3ba2667..87cc475 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -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") @@ -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)