Skip to content

Commit c4db124

Browse files
author
nphau
committed
feat: update io.ktor version
1 parent a67a1e0 commit c4db124

File tree

5 files changed

+30
-45
lines changed

5 files changed

+30
-45
lines changed

.idea/gradle.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,21 @@ android {
4242
dependencies {
4343

4444
implementation 'androidx.core:core-ktx:1.9.0'
45-
implementation 'androidx.appcompat:appcompat:1.5.1'
46-
implementation 'com.google.android.material:material:1.7.0'
45+
implementation 'androidx.appcompat:appcompat:1.6.1'
46+
implementation 'com.google.android.material:material:1.8.0'
4747
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
48-
androidTestImplementation 'androidx.test.ext:junit:1.1.4'
49-
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
48+
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
49+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
5050

5151
// Coroutines
5252
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4"
5353

5454
// Embedded Server
55-
implementation 'io.ktor:ktor-server-core:1.6.4'
56-
implementation 'io.ktor:ktor-gson:1.6.4'
57-
implementation 'io.ktor:ktor-server-netty:1.6.2'
58-
implementation 'io.ktor:ktor-websockets:1.6.2'
55+
implementation 'io.ktor:ktor-server-core:2.2.4'
56+
implementation 'io.ktor:ktor-server-netty:2.2.4'
57+
implementation 'io.ktor:ktor-serialization-kotlinx-json:2.2.4'
58+
implementation 'io.ktor:ktor-client-content-negotiation:2.2.4'
59+
implementation 'io.ktor:ktor-server-content-negotiation:2.2.4'
60+
implementation 'io.ktor:ktor-server-cors:2.2.4'
61+
5962
}

app/src/main/java/com/nphausg/app/embeddedserver/activities/MainActivity.kt

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,60 +17,42 @@ import com.nphausg.app.embeddedserver.data.Database
1717
import com.nphausg.app.embeddedserver.data.models.Cart
1818
import com.nphausg.app.embeddedserver.extensions.animateFlash
1919
import com.nphausg.app.embeddedserver.utils.NetworkUtils
20-
import io.ktor.application.call
21-
import io.ktor.application.install
22-
import io.ktor.features.CORS
23-
import io.ktor.features.CallLogging
24-
import io.ktor.features.Compression
25-
import io.ktor.features.ContentNegotiation
26-
import io.ktor.features.gzip
27-
import io.ktor.gson.gson
20+
import io.ktor.server.plugins.contentnegotiation.ContentNegotiation
2821
import io.ktor.http.ContentType
29-
import io.ktor.http.HttpMethod
3022
import io.ktor.http.HttpStatusCode
31-
import io.ktor.response.respond
32-
import io.ktor.response.respondText
33-
import io.ktor.routing.get
34-
import io.ktor.routing.routing
23+
import io.ktor.serialization.kotlinx.json.json
24+
import io.ktor.server.application.call
25+
import io.ktor.server.application.install
3526
import io.ktor.server.engine.embeddedServer
3627
import io.ktor.server.netty.Netty
37-
import io.ktor.websocket.WebSockets
28+
import io.ktor.server.plugins.cors.routing.CORS
29+
import io.ktor.server.response.respond
30+
import io.ktor.server.response.respondText
31+
import io.ktor.server.routing.get
32+
import io.ktor.server.routing.routing
3833
import kotlinx.coroutines.CoroutineScope
3934
import kotlinx.coroutines.Dispatchers
4035
import kotlinx.coroutines.launch
36+
import kotlinx.serialization.json.Json
4137

4238
class MainActivity : AppCompatActivity() {
4339

4440
companion object {
45-
private const val PORT = 5001
41+
private const val PORT = 6868
4642
}
4743

4844
private val server by lazy {
4945
embeddedServer(Netty, PORT, watchPaths = emptyList()) {
50-
install(WebSockets)
51-
install(CallLogging)
52-
// provides the automatic content conversion of requests based on theirContent-Type
53-
// and Accept headers. Together with the json() setting, this enables automatic
54-
// serialization and deserialization to and from JSON – allowing
55-
// us to delegate this tedious task to the framework.
56-
install(ContentNegotiation) {
57-
gson {
58-
setPrettyPrinting()
59-
disableHtmlEscaping()
60-
}
61-
}
6246
// configures Cross-Origin Resource Sharing. CORS is needed to make calls from arbitrary
6347
// JavaScript clients, and helps us prevent issues down the line.
6448
install(CORS) {
65-
method(HttpMethod.Get)
66-
method(HttpMethod.Post)
67-
method(HttpMethod.Delete)
6849
anyHost()
6950
}
70-
// Greatly reduces the amount of data that's needed to be sent to the client by
71-
// gzipping outgoing content when applicable.
72-
install(Compression) {
73-
gzip()
51+
install(ContentNegotiation) {
52+
json(Json {
53+
prettyPrint = true
54+
isLenient = true
55+
})
7456
}
7557
routing {
7658
get("/") {

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ buildscript {
66
}
77
dependencies {
88
classpath 'com.android.tools.build:gradle:7.3.1'
9-
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.20"
9+
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.0'
1010
}
1111
}
1212

0 commit comments

Comments
 (0)