Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a319870

Browse files
committedFeb 14, 2025·
Migrerer til ktor 3.
1 parent a465709 commit a319870

File tree

15 files changed

+76
-49
lines changed

15 files changed

+76
-49
lines changed
 

‎dusseldorf-ktor-core/src/main/kotlin/no/nav/helse/dusseldorf/ktor/core/CallIdExt.kt

+6-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import io.ktor.server.plugins.callid.callId
77
import io.ktor.server.plugins.callid.CallIdConfig
88
import io.ktor.http.HttpHeaders
99
import io.ktor.http.encodeURLParameter
10+
import io.ktor.server.application.PipelineCall
1011
import io.ktor.server.request.header
12+
import io.ktor.server.routing.Route
13+
import io.ktor.server.routing.application
1114
import io.ktor.util.AttributeKey
1215
import io.ktor.util.pipeline.PipelineContext
1316
import org.slf4j.Logger
@@ -71,13 +74,13 @@ class CallIdRequired(private val configure: Configuration) {
7174
))
7275
)
7376

74-
fun interceptPipeline(pipeline: ApplicationCallPipeline) {
75-
pipeline.intercept(ApplicationCallPipeline.Monitoring) {
77+
fun interceptPipeline(route: Route) {
78+
route.application.intercept(ApplicationCallPipeline.Monitoring) {
7679
require(this)
7780
}
7881
}
7982

80-
private suspend fun require(context: PipelineContext<Unit, ApplicationCall>) {
83+
private suspend fun require(context: PipelineContext<Unit, PipelineCall>) {
8184
val callId = context.context.callId
8285
if (callId == null) {
8386
context.context.respondProblemDetails(problemDetails, logger)

‎dusseldorf-ktor-core/src/main/kotlin/no/nav/helse/dusseldorf/ktor/core/CallLoggingExt.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package no.nav.helse.dusseldorf.ktor.core
22

33
import io.ktor.http.*
4-
import io.ktor.server.plugins.callloging.CallLoggingConfig
4+
import io.ktor.server.plugins.calllogging.CallLoggingConfig
55
import io.ktor.server.plugins.callid.*
66
import io.ktor.server.request.*
77
import no.nav.helse.dusseldorf.ktor.core.IdVerifier.trimId

‎dusseldorf-ktor-core/src/main/kotlin/no/nav/helse/dusseldorf/ktor/core/PreStop.kt

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
package no.nav.helse.dusseldorf.ktor.core
22

3-
import io.ktor.server.application.*
4-
import io.ktor.server.request.*
5-
import io.ktor.server.response.*
6-
import io.ktor.server.routing.*
3+
import io.ktor.server.application.Application
4+
import io.ktor.server.application.ApplicationStopPreparing
5+
import io.ktor.server.request.ApplicationRequest
6+
import io.ktor.server.request.path
7+
import io.ktor.server.response.respondText
8+
import io.ktor.server.routing.Route
9+
import io.ktor.server.routing.Routing
10+
import io.ktor.server.routing.RoutingRoot.Plugin.RoutingCallFinished
11+
import io.ktor.server.routing.RoutingRoot.Plugin.RoutingCallStarted
12+
import io.ktor.server.routing.get
713
import kotlinx.coroutines.delay
814
import kotlinx.coroutines.runBlocking
915
import kotlinx.coroutines.withTimeout
@@ -58,12 +64,12 @@ class FullførAktiveRequester(
5864
}
5965

6066
private fun Application.tellAntallAktiveRequester() {
61-
environment.monitor.subscribe(Routing.RoutingCallStarted) {
67+
monitor.subscribe(RoutingCallStarted) {
6268
if (it.request.skalTelles()) {
6369
antallAktiveRequester.incrementAndGet()
6470
}
6571
}
66-
environment.monitor.subscribe(Routing.RoutingCallFinished) {
72+
monitor.subscribe(RoutingCallFinished) {
6773
if (it.request.skalTelles()) {
6874
antallAktiveRequester.decrementAndGet()
6975
}

‎dusseldorf-ktor-core/src/main/kotlin/no/nav/helse/dusseldorf/ktor/core/RouteExt.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import io.ktor.server.routing.*
66
fun Route.requiresCallId(
77
build: Route.() -> Unit
88
): Route {
9-
val requiresCallIdRoutes = createChild(RequiresCallIdRouteSelector())
9+
val requiresCallIdRoutes: Route = createChild(RequiresCallIdRouteSelector())
1010
application.plugin(CallIdRequired).interceptPipeline(requiresCallIdRoutes)
1111
requiresCallIdRoutes.build()
1212
return requiresCallIdRoutes
1313
}
1414

1515
private class RequiresCallIdRouteSelector : RouteSelector() {
16-
override fun evaluate(context: RoutingResolveContext, segmentIndex: Int): RouteSelectorEvaluation {
16+
override suspend fun evaluate(context: RoutingResolveContext, segmentIndex: Int): RouteSelectorEvaluation {
1717
return RouteSelectorEvaluation.Constant
1818
}
1919

‎dusseldorf-ktor-jackson/src/main/kotlin/no/nav/helse/dusseldorf/ktor/jackson/JacksonStatusPages.kt

+10-5
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ import io.ktor.server.application.ApplicationCall
77
import io.ktor.server.plugins.BadRequestException
88
import io.ktor.server.plugins.statuspages.StatusPagesConfig
99
import io.ktor.server.request.path
10-
import io.ktor.util.InternalAPI
1110
import io.ktor.util.rootCause
12-
import no.nav.helse.dusseldorf.ktor.core.*
11+
import io.ktor.utils.io.InternalAPI
12+
import no.nav.helse.dusseldorf.ktor.core.DefaultProblemDetails
13+
import no.nav.helse.dusseldorf.ktor.core.ParameterType
14+
import no.nav.helse.dusseldorf.ktor.core.ValidationProblemDetails
15+
import no.nav.helse.dusseldorf.ktor.core.Violation
16+
import no.nav.helse.dusseldorf.ktor.core.respondProblemDetails
1317
import org.slf4j.Logger
1418
import org.slf4j.LoggerFactory
1519
import java.net.URI
@@ -53,9 +57,9 @@ fun StatusPagesConfig.JacksonStatusPages() {
5357
exception<JsonProcessingException> { call, cause ->
5458

5559
val problemDetails = DefaultProblemDetails(
56-
title = "invalid-json-entity",
57-
status = 400,
58-
detail = "Request entityen inneholder ugyldig JSON."
60+
title = "invalid-json-entity",
61+
status = 400,
62+
detail = "Request entityen inneholder ugyldig JSON."
5963
)
6064
logger.debug("Feil ved prosessering av JSON", cause)
6165
call.respondProblemDetails(problemDetails, logger)
@@ -76,6 +80,7 @@ fun StatusPagesConfig.JacksonStatusPages() {
7680
)
7781
)
7882
}
83+
7984
else -> {
8085
DefaultProblemDetails(
8186
title = "invalid-request-parameters",

‎dusseldorf-ktor-streams/src/main/kotlin/no/nav/helse/dusseldorf/ktor/streams/KafkaConfig.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ fun ApplicationConfig.kafkaConfig() : KafkaConfig {
118118
getRequiredString("nav.kafka.password", secret = true)),
119119
trustStore = trustStore,
120120
unreadyAfterStreamStoppedIn = unreadyAfterStreamStoppedIn,
121-
autoOffsetReset = getRequiredString("nav.kafka.auto_offset_reset", false).toLowerCase().also { autoOffsetReset ->
121+
autoOffsetReset = getRequiredString("nav.kafka.auto_offset_reset", false).lowercase(Locale.getDefault())
122+
.also { autoOffsetReset ->
122123
if (autoOffsetReset != "none") {
123124
logger.warn("'nav.kafka.auto_offset_reset' bør alltid være 'none' så fremt det ikke er første gang appen deployes.")
124125
}

‎dusseldorf-ktor-streams/src/main/kotlin/no/nav/helse/dusseldorf/ktor/streams/ManagedStream.kt

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import no.nav.helse.dusseldorf.ktor.health.Healthy
77
import no.nav.helse.dusseldorf.ktor.health.UnHealthy
88
import org.apache.kafka.streams.KafkaStreams
99
import org.apache.kafka.streams.Topology
10+
import org.apache.kafka.streams.errors.StreamsUncaughtExceptionHandler
1011
import org.slf4j.LoggerFactory
1112
import java.lang.System.currentTimeMillis
1213
import java.lang.Thread.sleep

‎dusseldorf-ktor-testapp/build.gradle.kts

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ repositories {
3535

3636
tasks {
3737
withType<KotlinCompile> {
38-
kotlinOptions.jvmTarget = "17"
38+
kotlinOptions.jvmTarget = "21"
3939
}
4040

4141
named<KotlinCompile>("compileTestKotlin") {
42-
kotlinOptions.jvmTarget = "17"
42+
kotlinOptions.jvmTarget = "21"
4343
}
4444
withType<ShadowJar> {
4545
archiveBaseName.set("app")
@@ -61,6 +61,6 @@ tasks {
6161
}
6262

6363
withType<Wrapper> {
64-
gradleVersion = "8.2.1"
64+
gradleVersion = "8.12.1"
6565
}
6666
}
Binary file not shown.

‎dusseldorf-ktor-testapp/gradle/wrapper/gradle-wrapper.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2.1-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.12.1-bin.zip
44
networkTimeout=10000
55
validateDistributionUrl=true
66
zipStoreBase=GRADLE_USER_HOME

‎dusseldorf-ktor-testapp/gradlew

+12-9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
# See the License for the specific language governing permissions and
1616
# limitations under the License.
1717
#
18+
# SPDX-License-Identifier: Apache-2.0
19+
#
1820

1921
##############################################################################
2022
#
@@ -55,7 +57,7 @@
5557
# Darwin, MinGW, and NonStop.
5658
#
5759
# (3) This script is generated from the Groovy template
58-
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
60+
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
5961
# within the Gradle project.
6062
#
6163
# You can find Gradle at https://github.com/gradle/gradle/.
@@ -83,7 +85,8 @@ done
8385
# This is normally unused
8486
# shellcheck disable=SC2034
8587
APP_BASE_NAME=${0##*/}
86-
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
88+
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
89+
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit
8790

8891
# Use the maximum available, or set MAX_FD != -1 to use that value.
8992
MAX_FD=maximum
@@ -144,15 +147,15 @@ if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
144147
case $MAX_FD in #(
145148
max*)
146149
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
147-
# shellcheck disable=SC3045
150+
# shellcheck disable=SC2039,SC3045
148151
MAX_FD=$( ulimit -H -n ) ||
149152
warn "Could not query maximum file descriptor limit"
150153
esac
151154
case $MAX_FD in #(
152155
'' | soft) :;; #(
153156
*)
154157
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
155-
# shellcheck disable=SC3045
158+
# shellcheck disable=SC2039,SC3045
156159
ulimit -n "$MAX_FD" ||
157160
warn "Could not set maximum file descriptor limit to $MAX_FD"
158161
esac
@@ -201,11 +204,11 @@ fi
201204
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
202205
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
203206

204-
# Collect all arguments for the java command;
205-
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
206-
# shell script including quotes and variable substitutions, so put them in
207-
# double quotes to make sure that they get re-expanded; and
208-
# * put everything else in single quotes, so that it's not re-expanded.
207+
# Collect all arguments for the java command:
208+
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
209+
# and any embedded shellness will be escaped.
210+
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
211+
# treated as '${Hostname}' itself on the command line.
209212

210213
set -- \
211214
"-Dorg.gradle.appname=$APP_BASE_NAME" \

‎dusseldorf-ktor-testapp/gradlew.bat

+12-10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
@rem See the License for the specific language governing permissions and
1414
@rem limitations under the License.
1515
@rem
16+
@rem SPDX-License-Identifier: Apache-2.0
17+
@rem
1618

1719
@if "%DEBUG%"=="" @echo off
1820
@rem ##########################################################################
@@ -43,11 +45,11 @@ set JAVA_EXE=java.exe
4345
%JAVA_EXE% -version >NUL 2>&1
4446
if %ERRORLEVEL% equ 0 goto execute
4547

46-
echo.
47-
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
48-
echo.
49-
echo Please set the JAVA_HOME variable in your environment to match the
50-
echo location of your Java installation.
48+
echo. 1>&2
49+
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
50+
echo. 1>&2
51+
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
52+
echo location of your Java installation. 1>&2
5153

5254
goto fail
5355

@@ -57,11 +59,11 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe
5759

5860
if exist "%JAVA_EXE%" goto execute
5961

60-
echo.
61-
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
62-
echo.
63-
echo Please set the JAVA_HOME variable in your environment to match the
64-
echo location of your Java installation.
62+
echo. 1>&2
63+
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
64+
echo. 1>&2
65+
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
66+
echo location of your Java installation. 1>&2
6567

6668
goto fail
6769

‎dusseldorf-ktor-testapp/src/test/kotlin/no/nav/AppTest.kt

+10-4
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,16 @@ internal class AppTest {
3535
}
3636

3737
private fun withNettyEngine(appPort: Int, block: suspend () -> Unit) {
38-
val server = embeddedServer(Netty, applicationEngineEnvironment {
39-
module { app() }
40-
connector { port = appPort }
41-
})
38+
val server = embeddedServer(
39+
factory = Netty,
40+
environment = applicationEnvironment {},
41+
configure = {
42+
connector { port = appPort }
43+
},
44+
module = {
45+
app()
46+
}
47+
)
4248
val job = GlobalScope.launch {
4349
server.start(wait = true)
4450
}

‎gradle/dusseldorf-ktor.gradle.kts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
val ktorVersion by extra("2.3.13")
2-
val kotlinVersion by extra("2.0.21")
1+
val ktorVersion by extra("3.1.0")
2+
val kotlinVersion by extra("2.1.10")
33
val logbackVersion by extra("1.5.16")
44
val logstashLogbackVersion by extra("8.0")
55
val prometheusVersion by extra("0.16.0")
66
val jacksonVersion by extra("2.18.2")
7-
val caffeineVersion by extra("3.1.8")
7+
val caffeineVersion by extra("3.2.0")
88
val slf4jVersion by extra("2.0.16")
99
val kotlinxCoroutinesVersion by extra("1.10.1")
1010
val micrometerVersion by extra("1.12.5")

‎pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
<!-- kan ikke gå for 2.1 fordi ktor.versjon 2.3 importerer kotlin-stdlib-common, og den er ikke tilgjengelig i nyere kotlin-stdlib-versjoner -->
3838
<kotlin.version>2.1.10</kotlin.version>
3939

40-
<ktor.version>2.3.13</ktor.version>
40+
<ktor.version>3.1.0</ktor.version>
4141
<jackson.version>2.18.2</jackson.version>
4242
<micrometer.version>1.12.5</micrometer.version>
4343
<logback.version>1.5.16</logback.version>

0 commit comments

Comments
 (0)
Please sign in to comment.