Skip to content

Commit e1665d4

Browse files
authored
chore: update to ktor 2.0 (GoogleCloudPlatform#7053)
Fixes #issue > It's a good idea to open an issue first for discussion. - [ ] I have followed [Sample Format Guide](https://github.com/GoogleCloudPlatform/java-docs-samples/blob/main/SAMPLE_FORMAT.md) - [ ] `pom.xml` parent set to latest `shared-configuration` - [ ] Appropriate changes to README are included in PR - [ ] API's need to be enabled to test (tell us) - [ ] Environment Variables need to be set (ask us to set them) - [ ] **Tests** pass: `mvn clean verify` **required** - [ ] **Lint** passes: `mvn -P lint checkstyle:check` **required** - [ ] **Static Analysis**: `mvn -P lint clean compile pmd:cpd-check spotbugs:check` **advisory only** - [ ] Please **merge** this PR for me once it is approved.
1 parent 58de748 commit e1665d4

File tree

6 files changed

+178
-130
lines changed

6 files changed

+178
-130
lines changed

appengine-java11/kotlin-ktor/app.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,3 @@
1313
# limitations under the License.
1414

1515
runtime: java11
16-
entrypoint: 'java -jar target/kotlin-ktor-0.0.1-jar-with-dependencies.jar'

appengine-java11/kotlin-ktor/pom.xml

Lines changed: 97 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -31,151 +31,133 @@ limitations under the License.
3131
</parent>
3232

3333
<properties>
34+
<maven.compiler.target>11</maven.compiler.target>
35+
<maven.compiler.source>11</maven.compiler.source>
36+
<ktor_version>2.0.0</ktor_version>
3437
<kotlin.code.style>official</kotlin.code.style>
38+
<kotlin_version>1.6.21</kotlin_version>
39+
<logback_version>1.2.3</logback_version>
3540
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
3641
<kotlin.compiler.incremental>true</kotlin.compiler.incremental>
37-
<main.class>io.ktor.server.netty.EngineMain</main.class>
38-
<maven.compiler.target>11</maven.compiler.target>
39-
<maven.compiler.source>11</maven.compiler.source>
42+
<main.class>com.example.appengine.ApplicationKt</main.class>
4043
</properties>
4144

4245
<repositories>
4346
<repository>
44-
<id>repo1</id>
45-
<url>https://jcenter.bintray.com</url>
46-
<releases><enabled>true</enabled></releases>
47-
<snapshots><enabled>false</enabled></snapshots>
48-
</repository>
49-
<repository>
50-
<id>repo2</id>
51-
<url>https://kotlin.bintray.com/ktor</url>
52-
<releases><enabled>true</enabled></releases>
53-
<snapshots><enabled>false</enabled></snapshots>
47+
<id>ktor_eap</id>
48+
<url>https://maven.pkg.jetbrains.space/public/p/ktor/eap</url>
49+
<releases>
50+
<enabled>true</enabled>
51+
</releases>
52+
<snapshots>
53+
<enabled>true</enabled>
54+
</snapshots>
5455
</repository>
5556
</repositories>
5657

57-
<dependencyManagement>
58-
<dependencies>
59-
<dependency>
60-
<groupId>io.ktor</groupId>
61-
<artifactId>ktor-bom</artifactId>
62-
<version>1.6.8</version>
63-
<type>pom</type>
64-
<scope>import</scope>
65-
</dependency>
66-
</dependencies>
67-
</dependencyManagement>
68-
6958
<dependencies>
7059
<dependency>
71-
<groupId>org.jetbrains.kotlin</groupId>
72-
<artifactId>kotlin-stdlib-jdk8</artifactId>
73-
<version>1.6.21</version>
74-
</dependency>
75-
<dependency>
76-
<groupId>io.ktor</groupId>
77-
<artifactId>ktor-server-netty</artifactId>
78-
</dependency>
79-
<dependency>
80-
<groupId>ch.qos.logback</groupId>
81-
<artifactId>logback-classic</artifactId>
82-
<version>1.3.0-alpha11</version>
60+
<groupId>io.ktor</groupId>
61+
<artifactId>ktor-server-core-jvm</artifactId>
62+
<version>${ktor_version}</version>
8363
</dependency>
8464
<dependency>
85-
<groupId>io.ktor</groupId>
86-
<artifactId>ktor-server-core</artifactId>
65+
<groupId>io.ktor</groupId>
66+
<artifactId>ktor-server-netty-jvm</artifactId>
67+
<version>${ktor_version}</version>
8768
</dependency>
8869
<dependency>
89-
<groupId>io.ktor</groupId>
90-
<artifactId>ktor-server-host-common</artifactId>
70+
<groupId>ch.qos.logback</groupId>
71+
<artifactId>logback-classic</artifactId>
72+
<version>${logback_version}</version>
9173
</dependency>
9274
<dependency>
93-
<groupId>io.ktor</groupId>
94-
<artifactId>ktor-server-tests</artifactId>
95-
<scope>test</scope>
75+
<groupId>io.ktor</groupId>
76+
<artifactId>ktor-server-tests-jvm</artifactId>
77+
<version>${ktor_version}</version>
78+
<scope>test</scope>
9679
</dependency>
9780
<dependency>
98-
<groupId>org.jetbrains.kotlin</groupId>
99-
<artifactId>kotlin-test-junit</artifactId>
100-
<version>1.6.21</version>
101-
<scope>test</scope>
81+
<groupId>org.jetbrains.kotlin</groupId>
82+
<artifactId>kotlin-test-junit</artifactId>
83+
<version>${kotlin_version}</version>
84+
<scope>test</scope>
10285
</dependency>
103-
104-
</dependencies>
105-
106-
<build>
107-
<sourceDirectory>${project.basedir}/src</sourceDirectory>
108-
<testSourceDirectory>${project.basedir}/test</testSourceDirectory>
86+
</dependencies>
87+
<build>
88+
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
89+
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
10990
<resources>
110-
<resource>
111-
<directory>${project.basedir}/resources</directory>
112-
</resource>
91+
<resource>
92+
<directory>${project.basedir}/src/main/resources</directory>
93+
</resource>
11394
</resources>
95+
11496
<plugins>
115-
<plugin>
116-
<groupId>org.apache.maven.plugins</groupId>
117-
<artifactId>maven-surefire-plugin</artifactId>
118-
</plugin>
119-
<plugin>
120-
<artifactId>maven-compiler-plugin</artifactId>
121-
<configuration><source>1.8</source><target>1.8</target></configuration>
122-
</plugin>
123-
<plugin>
124-
<artifactId>kotlin-maven-plugin</artifactId>
125-
<groupId>org.jetbrains.kotlin</groupId>
126-
<version>1.6.21</version>
127-
<executions>
128-
<execution>
129-
<id>compile</id>
130-
<goals><goal>compile</goal></goals>
97+
<plugin>
98+
<artifactId>kotlin-maven-plugin</artifactId>
99+
<groupId>org.jetbrains.kotlin</groupId>
100+
<version>${kotlin_version}</version>
131101
<configuration>
132-
<experimentalCoroutines>enable</experimentalCoroutines>
102+
<jvmTarget>1.8</jvmTarget>
133103
</configuration>
134-
</execution>
135-
<execution>
136-
<id>test-compile</id>
137-
<goals><goal>test-compile</goal></goals>
104+
<executions>
105+
<execution>
106+
<id>compile</id>
107+
<phase>compile</phase>
108+
<goals>
109+
<goal>compile</goal>
110+
</goals>
111+
</execution>
112+
<execution>
113+
<id>test-compile</id>
114+
<phase>test-compile</phase>
115+
<goals>
116+
<goal>test-compile</goal>
117+
</goals>
118+
</execution>
119+
</executions>
120+
</plugin>
121+
<plugin>
122+
<groupId>org.codehaus.mojo</groupId>
123+
<artifactId>exec-maven-plugin</artifactId>
124+
<version>1.2.1</version>
125+
<executions>
126+
<execution>
127+
<goals>
128+
<goal>java</goal>
129+
</goals>
130+
</execution>
131+
</executions>
138132
<configuration>
139-
<experimentalCoroutines>enable</experimentalCoroutines>
133+
<mainClass>${main.class}</mainClass>
140134
</configuration>
141-
</execution>
142-
</executions>
143-
</plugin>
144-
<plugin>
145-
<groupId>org.apache.maven.plugins</groupId>
146-
<artifactId>maven-jar-plugin</artifactId>
147-
<version>3.2.2</version>
148-
<configuration>
149-
<archive>
150-
<manifest>
151-
<addClasspath>true</addClasspath>
152-
<mainClass>${main.class}</mainClass>
153-
</manifest>
154-
</archive>
155-
</configuration>
156-
</plugin>
157-
<plugin>
158-
<groupId>org.apache.maven.plugins</groupId>
159-
<artifactId>maven-assembly-plugin</artifactId>
160-
<version>3.3.0</version>
161-
<executions>
162-
<execution>
163-
<id>make-assembly</id>
164-
<phase>package</phase>
165-
<goals> <goal>single</goal> </goals>
135+
</plugin>
136+
<plugin>
137+
<groupId>org.apache.maven.plugins</groupId>
138+
<artifactId>maven-assembly-plugin</artifactId>
139+
<version>2.6</version>
166140
<configuration>
167-
<archive>
168-
<manifest>
169-
<mainClass>${main.class}</mainClass>
170-
</manifest>
171-
</archive>
172-
<descriptorRefs>
173-
<descriptorRef>jar-with-dependencies</descriptorRef>
174-
</descriptorRefs>
141+
<descriptorRefs>
142+
<descriptorRef>jar-with-dependencies</descriptorRef>
143+
</descriptorRefs>
144+
<archive>
145+
<manifest>
146+
<addClasspath>true</addClasspath>
147+
<mainClass>${main.class}</mainClass>
148+
</manifest>
149+
</archive>
175150
</configuration>
176-
</execution>
177-
</executions>
178-
</plugin>
151+
<executions>
152+
<execution>
153+
<id>assemble-all</id>
154+
<phase>package</phase>
155+
<goals>
156+
<goal>single</goal>
157+
</goals>
158+
</execution>
159+
</executions>
160+
</plugin>
179161
<!-- Add App Engine plugin -->
180162
<plugin>
181163
<groupId>com.google.cloud.tools</groupId>

appengine-java11/kotlin-ktor/src/Application.kt renamed to appengine-java11/kotlin-ktor/src/main/kotlin/com/example/appengine/Application.kt

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,27 @@
1414

1515
package com.example.appengine
1616

17-
import io.ktor.application.Application
18-
import io.ktor.application.call
19-
import io.ktor.application.install
2017
import io.ktor.http.ContentType
21-
import io.ktor.response.respondText
22-
import io.ktor.routing.get
23-
import io.ktor.routing.routing
24-
import io.ktor.server.engine.ShutDownUrl
25-
import io.ktor.server.netty.EngineMain
18+
import io.ktor.server.application.*
19+
import io.ktor.server.engine.*
20+
import io.ktor.server.response.*
21+
import io.ktor.server.routing.*
2622

27-
fun main(args: Array<String>): Unit = EngineMain.main(args)
23+
fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)
2824

29-
@Suppress("unused") // Referenced in application.conf
30-
@kotlin.jvm.JvmOverloads
31-
fun Application.module(testing: Boolean = false) {
32-
install(ShutDownUrl.ApplicationCallFeature) {
25+
fun Application.module() {
26+
install(ShutDownUrl.ApplicationCallPlugin) {
3327
// The URL that will be intercepted. You can also use the
3428
// application.conf's ktor.deployment.shutdown.url key.
3529
shutDownUrl = "/_ah/stop"
3630

3731
// A function that will be executed to get the exit code of the process
3832
exitCodeSupplier = { 0 } // ApplicationCall.() -> Int
3933
}
40-
4134
routing {
4235
get("/") {
4336
call.respondText("Hello World!", contentType = ContentType.Text.Plain)
4437
}
4538
}
4639
}
40+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
ktor {
16+
deployment {
17+
port = 8080
18+
port = ${?PORT}
19+
20+
shutdown.url = "/_ah/stop"
21+
}
22+
application {
23+
modules = [ com.example.appengine.ApplicationKt.module ]
24+
}
25+
}
26+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<configuration>
2+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
3+
<encoder>
4+
<pattern>%d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
5+
</encoder>
6+
</appender>
7+
<root level="trace">
8+
<appender-ref ref="STDOUT"/>
9+
</root>
10+
<logger name="org.eclipse.jetty" level="INFO"/>
11+
<logger name="io.netty" level="INFO"/>
12+
</configuration>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.example.appengine
16+
17+
import io.ktor.server.http.HttpMethod
18+
import io.ktor.server.http.HttpStatusCode
19+
import io.ktor.server.testing.handleRequest
20+
import io.ktor.server.testing.withTestApplication
21+
import kotlin.test.Test
22+
import kotlin.test.assertEquals
23+
24+
class ApplicationTest {
25+
@Test
26+
fun testRoot() {
27+
withTestApplication({ module(testing = true) }) {
28+
handleRequest(HttpMethod.Get, "/").apply {
29+
assertEquals(HttpStatusCode.OK, response.status())
30+
assertEquals("Hello World!", response.content)
31+
}
32+
}
33+
}
34+
}
35+

0 commit comments

Comments
 (0)