Skip to content

Commit

Permalink
Enabled support for Cbor
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanOltmann committed Oct 29, 2024
1 parent cfe0fa7 commit 396bc89
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ dependencies {
implementation("io.ktor:ktor-server-content-negotiation:$ktorVersion")
implementation("io.ktor:ktor-server-compression:$ktorVersion")
implementation("io.ktor:ktor-serialization-kotlinx-json:$ktorVersion")
implementation("io.ktor:ktor-serialization-kotlinx-cbor:$ktorVersion")

implementation("io.ktor:ktor-server-cors:$ktorVersion")

Expand Down
17 changes: 16 additions & 1 deletion src/main/kotlin/Routings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import io.ktor.http.ContentType
import io.ktor.http.HttpHeaders
import io.ktor.http.HttpMethod
import io.ktor.http.HttpStatusCode
import io.ktor.serialization.kotlinx.cbor.cbor
import io.ktor.serialization.kotlinx.json.json
import io.ktor.server.application.Application
import io.ktor.server.application.ApplicationCall
Expand All @@ -53,6 +54,8 @@ import io.ktor.server.routing.routing
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.toList
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.cbor.Cbor
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import model.Cluster
Expand Down Expand Up @@ -83,11 +86,18 @@ private val strictAllFieldsJson = Json {
encodeDefaults = true
}

@OptIn(ExperimentalSerializationApi::class)
private val strictAllFieldsCbor = Cbor {
ignoreUnknownKeys = false
encodeDefaults = true
}

private val logger = LoggerFactory.getLogger("Routings")

/* Limit the results to avoid memory issues */
const val RESULT_LIMIT = 100

@OptIn(ExperimentalSerializationApi::class)
fun Application.configureRouting() {

val startTime = System.currentTimeMillis()
Expand All @@ -96,6 +106,7 @@ fun Application.configureRouting() {

install(ContentNegotiation) {
json(strictAllFieldsJson)
cbor(strictAllFieldsCbor)
}

install(Compression) {
Expand All @@ -107,7 +118,11 @@ fun Application.configureRouting() {
/* Only compress responses larger than 1 KB (for efficiency) */
minimumSize(1024)

matchContentType(ContentType.Application.Json, ContentType.Application.Zip)
matchContentType(
ContentType.Application.Json,
ContentType.Application.Cbor,
ContentType.Application.Zip
)
}
}

Expand Down

0 comments on commit 396bc89

Please sign in to comment.