Skip to content

Commit

Permalink
Update to Ktor 3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
MrPowerGamerBR committed Oct 21, 2024
1 parent a4e8d02 commit 4acd391
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 9 deletions.
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ object Versions {
const val LORITTA = "2023-SNAPSHOT"
const val PUDDING = "0.0.3-SNAPSHOT"
const val KOTLIN = "2.0.20"
const val KTOR = "2.3.6"
const val KTOR = "3.0.0"
const val KOTLIN_SERIALIZATION = "1.6.0"
const val KOTLIN_COROUTINES = "1.6.0"
const val KOTLIN_LOGGING = "2.1.16"
Expand Down
1 change: 0 additions & 1 deletion loritta-bot-discord/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ dependencies {

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

implementation("net.perfectdreams.randomroleplaypictures:client:1.0.1")
implementation("org.gagravarr:vorbis-java-core:0.8")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class LorittaWebsite(
val pathCache = ConcurrentHashMap<File, Any>()
var config = WebsiteConfig(loritta)
val svgIconManager = SVGIconManager(this)
lateinit var server: CIOApplicationEngine
lateinit var server: EmbeddedServer<CIOApplicationEngine, *>
private val typesToCache = listOf(
ContentType.Text.CSS,
ContentType.Text.JavaScript,
Expand Down Expand Up @@ -422,7 +422,7 @@ class LorittaWebsite(
}
}

this.environment.monitor.subscribe(Routing.RoutingCallStarted) { call: RoutingApplicationCall ->
this.monitor.subscribe(RoutingRoot.RoutingCallStarted) { call: RoutingCall ->
call.attributes.put(TimeToProcess, System.currentTimeMillis())
val userAgent = call.request.userAgent()
val trueIp = call.request.trueIp
Expand All @@ -432,7 +432,7 @@ class LorittaWebsite(
logger.info("${trueIp} (${userAgent}): ${httpMethod} ${call.request.path()}${queryString}")
}

this.environment.monitor.subscribe(Routing.RoutingCallFinished) { call: RoutingApplicationCall ->
this.monitor.subscribe(RoutingRoot.RoutingCallFinished) { call: RoutingCall ->
val originalStartTime = call.attributes[TimeToProcess]

val queryString = call.request.urlQueryString
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 settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ dependencyResolutionManagement {
create("libs") {
val kotlin = version("kotlin", "1.7.10")
val kotlinXSerialization = version("kotlinx-serialization", "1.7.1")
val ktor = version("ktor", "2.3.6")
val ktor = version("ktor", "3.0.0")
val jib = version("jib", "3.4.3")
// We can't use 0.50.0 yet because they broke the inList and notInList signature for ID fields
val exposed = version("exposed", "0.49.0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import mu.KotlinLogging
import net.perfectdreams.temmiediscordauth.TemmieDiscordAuth
import java.util.*

@Serializable
data class LorittaJsonWebSession(
val base64CachedIdentification: String?,
val base64StoredDiscordAuthTokens: String?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package net.perfectdreams.loritta.cinnamon.dashboard.backend.utils
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.routing.*
import io.ktor.util.pipeline.*

/**
* A [BaseRoute] is used to register Ktor routes on the [routing] section, allowing you to split up routes in different classes.
Expand All @@ -23,7 +22,7 @@ abstract class BaseRoute(val path: String) {
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: PipelineInterceptor<Unit, ApplicationCall>) {
fun registerWithPath(route: Route, path: String, callback: RoutingHandler) {
val method = getMethod()
when (method) {
HttpMethod.Get -> route.get(path, callback)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class BlueskyFirehoseClient {
private val client = HttpClient(CIO) {
install(WebSockets) {
// We DON'T WANT to use the pingInterval, because we already do our own "ping at home" that automatically restarts the session if we haven't received an event for a looong time
pingInterval = -1L
pingInterval = null
}
}
private val logger = KotlinLogging.logger {}
Expand Down

0 comments on commit 4acd391

Please sign in to comment.