From e90513aa3637152c49da8f091c103fc1723f3c5e Mon Sep 17 00:00:00 2001 From: Graham Date: Mon, 18 Jul 2022 20:01:36 +0100 Subject: [PATCH] Update to Ktor 2 Signed-off-by: Graham --- .../openrs2/archive/web/CachesController.kt | 17 ++++--- .../org/openrs2/archive/web/KeysController.kt | 8 ++-- .../org/openrs2/archive/web/WebServer.kt | 47 +++++++++++-------- gradle/libs.versions.toml | 29 ++++++++++-- 4 files changed, 65 insertions(+), 36 deletions(-) diff --git a/archive/src/main/kotlin/org/openrs2/archive/web/CachesController.kt b/archive/src/main/kotlin/org/openrs2/archive/web/CachesController.kt index a604c190..b95971f4 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/web/CachesController.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/web/CachesController.kt @@ -1,20 +1,20 @@ package org.openrs2.archive.web -import io.ktor.application.ApplicationCall import io.ktor.http.CacheControl import io.ktor.http.ContentDisposition import io.ktor.http.ContentType import io.ktor.http.HttpHeaders import io.ktor.http.HttpStatusCode -import io.ktor.http.content.CachingOptions import io.ktor.http.content.EntityTagVersion import io.ktor.http.content.caching import io.ktor.http.content.versions -import io.ktor.response.header -import io.ktor.response.respond -import io.ktor.response.respondBytes -import io.ktor.response.respondOutputStream -import io.ktor.thymeleaf.ThymeleafContent +import io.ktor.server.application.ApplicationCall +import io.ktor.server.http.content.CachingOptions +import io.ktor.server.response.header +import io.ktor.server.response.respond +import io.ktor.server.response.respondBytes +import io.ktor.server.response.respondOutputStream +import io.ktor.server.thymeleaf.ThymeleafContent import io.netty.buffer.ByteBufAllocator import io.netty.buffer.ByteBufUtil import kotlinx.coroutines.sync.Semaphore @@ -29,6 +29,8 @@ import org.openrs2.compress.gzip.GzipLevelOutputStream import org.openrs2.crypto.whirlpool import java.nio.file.attribute.FileTime import java.time.Instant +import java.time.ZoneOffset +import java.time.ZonedDateTime import java.util.Base64 import java.util.zip.Deflater import java.util.zip.ZipEntry @@ -105,6 +107,7 @@ public class CachesController @Inject constructor( maxAgeSeconds = 86400, visibility = CacheControl.Visibility.Public, ), + expires = ZonedDateTime.now(ZoneOffset.UTC).plusSeconds(86400), ) versions = listOf( EntityTagVersion(etag, weak = false), diff --git a/archive/src/main/kotlin/org/openrs2/archive/web/KeysController.kt b/archive/src/main/kotlin/org/openrs2/archive/web/KeysController.kt index 07e8f94c..9b1b324d 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/web/KeysController.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/web/KeysController.kt @@ -1,10 +1,10 @@ package org.openrs2.archive.web -import io.ktor.application.ApplicationCall import io.ktor.http.HttpStatusCode -import io.ktor.request.receive -import io.ktor.response.respond -import io.ktor.thymeleaf.ThymeleafContent +import io.ktor.server.application.ApplicationCall +import io.ktor.server.request.receive +import io.ktor.server.response.respond +import io.ktor.server.thymeleaf.ThymeleafContent import org.openrs2.archive.key.KeyExporter import org.openrs2.archive.key.KeyImporter import org.openrs2.archive.key.KeySource diff --git a/archive/src/main/kotlin/org/openrs2/archive/web/WebServer.kt b/archive/src/main/kotlin/org/openrs2/archive/web/WebServer.kt index 90aae90d..4aa64ac0 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/web/WebServer.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/web/WebServer.kt @@ -1,28 +1,30 @@ package org.openrs2.archive.web import com.fasterxml.jackson.databind.ObjectMapper -import io.ktor.application.ApplicationCall -import io.ktor.application.call -import io.ktor.application.install -import io.ktor.features.CORS -import io.ktor.features.CachingHeaders -import io.ktor.features.ConditionalHeaders -import io.ktor.features.ContentNegotiation -import io.ktor.features.XForwardedHeaderSupport import io.ktor.http.ContentType -import io.ktor.http.content.resources -import io.ktor.http.content.static -import io.ktor.jackson.JacksonConverter -import io.ktor.response.respond -import io.ktor.response.respondRedirect -import io.ktor.routing.get -import io.ktor.routing.post -import io.ktor.routing.routing +import io.ktor.serialization.jackson.JacksonConverter +import io.ktor.server.application.ApplicationCall +import io.ktor.server.application.call +import io.ktor.server.application.install import io.ktor.server.cio.CIO import io.ktor.server.engine.embeddedServer -import io.ktor.thymeleaf.Thymeleaf -import io.ktor.thymeleaf.ThymeleafContent -import io.ktor.webjars.Webjars +import io.ktor.server.http.content.resources +import io.ktor.server.http.content.static +import io.ktor.server.plugins.autohead.AutoHeadResponse +import io.ktor.server.plugins.cachingheaders.CachingHeaders +import io.ktor.server.plugins.conditionalheaders.ConditionalHeaders +import io.ktor.server.plugins.contentnegotiation.ContentNegotiation +import io.ktor.server.plugins.cors.routing.CORS +import io.ktor.server.plugins.defaultheaders.DefaultHeaders +import io.ktor.server.plugins.forwardedheaders.XForwardedHeaders +import io.ktor.server.response.respond +import io.ktor.server.response.respondRedirect +import io.ktor.server.routing.get +import io.ktor.server.routing.post +import io.ktor.server.routing.routing +import io.ktor.server.thymeleaf.Thymeleaf +import io.ktor.server.thymeleaf.ThymeleafContent +import io.ktor.server.webjars.Webjars import org.openrs2.json.Json import org.thymeleaf.extras.java8time.dialect.Java8TimeDialect import org.thymeleaf.templatemode.TemplateMode @@ -38,6 +40,7 @@ public class WebServer @Inject constructor( ) { public fun start(address: String, port: Int) { embeddedServer(CIO, host = address, port = port) { + install(AutoHeadResponse) install(CachingHeaders) install(ConditionalHeaders) @@ -46,9 +49,13 @@ public class WebServer @Inject constructor( } install(ContentNegotiation) { + ignoreType() + register(ContentType.Application.Json, JacksonConverter(mapper)) } + install(DefaultHeaders) + install(Thymeleaf) { addDialect(ByteUnitsDialect) addDialect(Java8TimeDialect()) @@ -59,7 +66,7 @@ public class WebServer @Inject constructor( }) } - install(XForwardedHeaderSupport) + install(XForwardedHeaders) install(Webjars) routing { diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 5c6b2bb6..308b50d9 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -8,7 +8,7 @@ jimfs = "1.2" junit = "5.8.2" kotlin = "1.7.10" kotlinCoroutines = "1.6.4" -ktor = "1.6.8" +ktor = "2.0.3" netty = "4.1.79.Final" nettyIoUring = "0.0.14.Final" @@ -56,10 +56,17 @@ junit-launcher = { module = "org.junit.platform:junit-platform-launcher", versio kotlin-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlinCoroutines" } kotlin-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "kotlinCoroutines" } kotlinPoet = { module = "com.squareup:kotlinpoet", version = "1.12.0" } -ktor-jackson = { module = "io.ktor:ktor-jackson", version.ref = "ktor" } +ktor-serialization-jackson = { module = "io.ktor:ktor-serialization-jackson", version.ref = "ktor" } +ktor-server-auto-head-response = { module = "io.ktor:ktor-server-auto-head-response", version.ref = "ktor" } +ktor-server-caching-headers = { module = "io.ktor:ktor-server-caching-headers", version.ref = "ktor" } ktor-server-cio = { module = "io.ktor:ktor-server-cio", version.ref = "ktor" } -ktor-thymeleaf = { module = "io.ktor:ktor-thymeleaf", version.ref = "ktor" } -ktor-webjars = { module = "io.ktor:ktor-webjars", version.ref = "ktor" } +ktor-server-conditional-headers = { module = "io.ktor:ktor-server-conditional-headers", version.ref = "ktor" } +ktor-server-content-negotiation = { module = "io.ktor:ktor-server-content-negotiation", version.ref = "ktor" } +ktor-server-cors = { module = "io.ktor:ktor-server-cors", version.ref = "ktor" } +ktor-server-default-headers = { module = "io.ktor:ktor-server-default-headers", version.ref = "ktor" } +ktor-server-forwarded-header = { module = "io.ktor:ktor-server-forwarded-header", version.ref = "ktor" } +ktor-server-thymeleaf = { module = "io.ktor:ktor-server-thymeleaf", version.ref = "ktor" } +ktor-server-webjars = { module = "io.ktor:ktor-server-webjars", version.ref = "ktor" } logback = { module = "ch.qos.logback:logback-classic", version = "1.2.11" } netty-buffer = { module = "io.netty:netty-buffer", version.ref = "netty" } netty-codec-core = { module = "io.netty:netty-codec", version.ref = "netty" } @@ -77,5 +84,17 @@ xz = { module = "org.tukaani:xz", version = "1.9" } [bundles] asm = ["asm-core", "asm-commons", "asm-tree", "asm-util"] bouncyCastle = ["bouncyCastle-pkix", "bouncyCastle-provider"] -ktor = ["ktor-jackson", "ktor-server-cio", "ktor-thymeleaf", "ktor-webjars"] +ktor = [ + "ktor-serialization-jackson", + "ktor-server-auto-head-response", + "ktor-server-caching-headers", + "ktor-server-cio", + "ktor-server-conditional-headers", + "ktor-server-content-negotiation", + "ktor-server-cors", + "ktor-server-default-headers", + "ktor-server-forwarded-header", + "ktor-server-thymeleaf", + "ktor-server-webjars", +] thymeleaf = ["thymeleaf-core", "thymeleaf-java8time"]