Skip to content

Commit

Permalink
Migrate to Ktor 3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Stexxe committed Oct 9, 2024
1 parent d12954e commit 089600c
Show file tree
Hide file tree
Showing 44 changed files with 139 additions and 466 deletions.
3 changes: 3 additions & 0 deletions chat/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ buildscript {
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:2.0.20"
classpath "org.jetbrains.kotlin:kotlin-serialization:2.0.20"
}
}

apply plugin: 'kotlin-multiplatform'
apply plugin: 'org.jetbrains.kotlin.plugin.serialization'

kotlin {
targets {
Expand Down Expand Up @@ -70,6 +72,7 @@ repositories {

tasks.named("backendProcessResources").configure {
dependsOn("frontendBrowserProductionWebpack")
dependsOn("frontendBrowserDistribution")
}

tasks.register("run", JavaExec) {
Expand Down
22 changes: 13 additions & 9 deletions chat/src/backendMain/kotlin/ChatApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.http.content.*
import io.ktor.server.netty.*
import io.ktor.server.plugins.callloging.*
import io.ktor.server.plugins.calllogging.CallLogging
import io.ktor.server.plugins.defaultheaders.*
import io.ktor.server.routing.*
import io.ktor.server.sessions.*
import io.ktor.server.websocket.*
import io.ktor.util.*
import io.ktor.websocket.*
import kotlinx.coroutines.channels.*
import java.time.*
import kotlinx.serialization.Serializable
import kotlin.time.Duration
import kotlin.time.Duration.Companion.minutes

/**
* An entry point of the application.
Expand Down Expand Up @@ -61,7 +63,7 @@ class ChatApplication {
// This installs the WebSockets plugin to be able to establish a bidirectional configuration
// between the server and the client
install(WebSockets) {
pingPeriod = Duration.ofMinutes(1)
pingPeriod = 1.minutes
}
// This enables the use of sessions to keep information between requests/refreshes of the browser.
install(Sessions) {
Expand Down Expand Up @@ -121,19 +123,21 @@ class ChatApplication {
}

// This defines a block of static resources for the '/' path (since no path is specified and we start at '/')
static {
// This marks index.html from the 'web' folder in resources as the default file to serve.
defaultResource("index.html", "web")
// This serves files from the 'web' folder in the application resources.
resources("web")
}
staticResources("", "web")
// static {
// // This marks index.html from the 'web' folder in resources as the default file to serve.
// defaultResource("index.html", "web")
// // This serves files from the 'web' folder in the application resources.
// resources("web")
// }

}
}

/**
* A chat session is identified by a unique nonce ID. This nonce comes from a secure random source.
*/
@Serializable
data class ChatSession(val id: String)

/**
Expand Down
1 change: 1 addition & 0 deletions chat/src/frontendMain/kotlin/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ fun main() {
})
}

@OptIn(DelicateCoroutinesApi::class)
suspend fun initConnection(wsClient: WsClient) {
try {
wsClient.connect()
Expand Down
2 changes: 1 addition & 1 deletion di-kodein/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ dependencies {
implementation("io.ktor:ktor-server-html-builder")
implementation("org.kodein.di:kodein-di-jvm:7.17.0")
implementation("ch.qos.logback:logback-classic:$logback_version")
testImplementation("io.ktor:ktor-server-tests-jvm")
testImplementation("io.ktor:ktor-server-test-host-jvm")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version")
}
2 changes: 1 addition & 1 deletion di-kodein/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
kotlin_version=2.0.20
logback_version=1.2.11
logback_version=1.5.8
kotlin.code.style=official
2 changes: 1 addition & 1 deletion filelisting/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ dependencies {
implementation("io.ktor:ktor-server-default-headers")
implementation("io.ktor:ktor-server-html-builder")
implementation("io.ktor:ktor-server-call-logging")
testImplementation("io.ktor:ktor-server-tests-jvm")
testImplementation("io.ktor:ktor-server-test-host-jvm")
implementation("ch.qos.logback:logback-classic:$logback_version")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version")
}
2 changes: 1 addition & 1 deletion filelisting/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
kotlin_version=2.0.20
logback_version=1.2.11
logback_version=1.5.8
kotlin.code.style=official
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import io.ktor.server.html.*
import io.ktor.server.http.content.*
import io.ktor.server.netty.*
import io.ktor.server.plugins.*
import io.ktor.server.plugins.callloging.*
import io.ktor.server.plugins.calllogging.CallLogging
import io.ktor.server.plugins.defaultheaders.*
import io.ktor.server.request.*
import io.ktor.server.response.*
Expand Down Expand Up @@ -35,7 +35,7 @@ fun main() {
call.respondInfo()
}
route("/myfiles") {
files(root)
staticFiles("", root)
listing(root)
}
}
Expand All @@ -53,7 +53,8 @@ suspend fun ApplicationCall.respondInfo() {
respondHtml {
body {
style {
+"""
unsafe {
"""
table {
font: 1em Arial;
border: 1px solid black;
Expand All @@ -71,6 +72,7 @@ suspend fun ApplicationCall.respondInfo() {
padding: 0.5em 1em;
}
""".trimIndent()
}
}
h1 {
+"Ktor info"
Expand Down Expand Up @@ -109,32 +111,28 @@ suspend fun ApplicationCall.respondInfo() {
row("request.ranges()", request.ranges())
}

for (
(name, value) in listOf(
"request.local" to request.local,
"request.origin" to request.origin
)
) {
for ((name, value) in listOf(
"request.local" to request.local,
"request.origin" to request.origin
)) {
h2 {
+name
}
table {
row("$name.version", value.version)
row("$name.method", value.method)
row("$name.scheme", value.scheme)
row("$name.host", value.host)
row("$name.port", value.port)
row("$name.host", value.localHost)
row("$name.port", value.localPort)
row("$name.remoteHost", value.remoteHost)
row("$name.uri", value.uri)
}
}

for (
(name, parameters) in listOf(
"Query parameters" to request.queryParameters,
"Headers" to request.headers
)
) {
for ((name, parameters) in listOf(
"Query parameters" to request.queryParameters,
"Headers" to request.headers
)) {
h2 {
+name
}
Expand Down
2 changes: 1 addition & 1 deletion h2/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ dependencies {
implementation("io.ktor:ktor-server-netty-jvm")
implementation("ch.qos.logback:logback-classic:$logback_version")
implementation("io.ktor:ktor-server-html-builder")
testImplementation("io.ktor:ktor-server-tests-jvm")
testImplementation("io.ktor:ktor-server-test-host-jvm")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version")
}
2 changes: 1 addition & 1 deletion h2/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
kotlin_version=2.0.20
logback_version=1.2.11
logback_version=1.5.8
kotlin.code.style=official
2 changes: 1 addition & 1 deletion httpbin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ dependencies {
implementation("io.ktor:ktor-server-html-builder")
implementation("io.ktor:ktor-server-partial-content")
implementation("ch.qos.logback:logback-classic:$logback_version")
testImplementation("io.ktor:ktor-server-tests-jvm")
testImplementation("io.ktor:ktor-server-test-host-jvm")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version")
}

Expand Down
2 changes: 1 addition & 1 deletion httpbin/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
kotlin_version=2.0.20
logback_version=1.2.11
logback_version=1.5.8
kotlin.code.style=official

org.gradle.jvmargs=-Xmx2g -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package io.ktor.samples.httpbin

import com.google.gson.*
import com.google.gson.reflect.*
import io.ktor.content.TextContent
import io.ktor.http.*
import io.ktor.http.content.*
import io.ktor.serialization.gson.*
Expand All @@ -11,8 +10,8 @@ import io.ktor.server.auth.*
import io.ktor.server.html.*
import io.ktor.server.http.content.*
import io.ktor.server.plugins.*
import io.ktor.server.plugins.autohead.*
import io.ktor.server.plugins.callloging.*
import io.ktor.server.plugins.autohead.AutoHeadResponse
import io.ktor.server.plugins.calllogging.CallLogging
import io.ktor.server.plugins.compression.*
import io.ktor.server.plugins.conditionalheaders.*
import io.ktor.server.plugins.contentnegotiation.*
Expand Down Expand Up @@ -446,7 +445,13 @@ fun Route.handleRequestWithBodyFor(method: HttpMethod) {
contentType(ContentType.MultiPart.FormData) {
method(method) {
handle {
val listFiles = call.receive<MultiPartData>().readAllParts().filterIsInstance<PartData.FileItem>()
val listFiles = mutableListOf<PartData.FileItem>()
call.receive<MultiPartData>().forEachPart {
if (it is PartData.FileItem) {
listFiles.add(it)
}
}

call.sendHttpBinResponse {
form = call.receive<Parameters>()
files = listFiles.associateBy { part -> part.name ?: "a" }
Expand Down
2 changes: 1 addition & 1 deletion ktor-client-wasm/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ compose = "1.6.2"
compose-plugin = "1.6.0"
junit = "4.13.2"
kotlin = "2.0.20"
ktor = "3.0.0-beta-2-eap-920"
ktor = "3.0.0"

[libraries]
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
Expand Down
2 changes: 1 addition & 1 deletion kweet/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
kotlin_version=2.0.20
logback_version=1.2.11
logback_version=1.5.8
exposed_version=0.40.1
kotlin.code.style=official
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import io.ktor.samples.kweet.model.*
import io.ktor.server.application.*
import io.ktor.server.freemarker.*
import io.ktor.server.plugins.*
import io.ktor.server.plugins.callloging.*
import io.ktor.server.plugins.calllogging.CallLogging
import io.ktor.server.plugins.conditionalheaders.*
import io.ktor.server.plugins.defaultheaders.*
import io.ktor.server.plugins.partialcontent.*
Expand Down Expand Up @@ -65,6 +65,7 @@ class Logout()
/**
* Represents a session in this site containing the user ID.
*/
@Serializable
data class KweetSession(val userId: String)

/**
Expand Down Expand Up @@ -108,7 +109,7 @@ fun Application.main() {
// First, we initialize the database.
dao.init()
// Then, we subscribe to the stop event of the application, so we can also close the [ComboPooledDataSource] [pool].
environment.monitor.subscribe(ApplicationStopped) { pool.close() }
monitor.subscribe(ApplicationStopped) { pool.close() }
// Now we call to a main with the dependencies as arguments.
// Separating this function with its dependencies allows us to provide several modules with
// the same code and different datasources living in the same application,
Expand Down
Loading

0 comments on commit 089600c

Please sign in to comment.