Migrate to Ktor 2

This commit is contained in:
Michael Bull 2021-12-30 11:42:07 +00:00
parent d07bd589ed
commit 6a5523c998
4 changed files with 29 additions and 41 deletions

View File

@ -4,7 +4,7 @@ object Versions {
const val kotlinBenchmark = "0.3.1" const val kotlinBenchmark = "0.3.1"
const val kotlinCoroutines = "1.5.2" const val kotlinCoroutines = "1.5.2"
const val kotlinCoroutinesTest = "1.5.2" const val kotlinCoroutinesTest = "1.5.2"
const val ktor = "1.5.4" const val ktor = "2.0.0-beta-1"
const val logback = "1.2.3" const val logback = "1.2.3"
const val versionsPlugin = "0.39.0" const val versionsPlugin = "0.39.0"
} }

View File

@ -6,7 +6,7 @@ plugins {
} }
application { application {
mainClass.set("io.ktor.server.netty.EngineMain") mainClass.set("com.github.michaelbull.result.example.ApplicationKt")
} }
dependencies { dependencies {
@ -14,8 +14,9 @@ dependencies {
implementation(kotlin("stdlib-jdk8")) implementation(kotlin("stdlib-jdk8"))
implementation("ch.qos.logback:logback-classic:${Versions.logback}") implementation("ch.qos.logback:logback-classic:${Versions.logback}")
implementation("io.ktor:ktor-server-core:${Versions.ktor}") implementation("io.ktor:ktor-server-core:${Versions.ktor}")
implementation("io.ktor:ktor-server-content-negotiation:${Versions.ktor}")
implementation("io.ktor:ktor-serialization-jackson:${Versions.ktor}")
implementation("io.ktor:ktor-server-netty:${Versions.ktor}") implementation("io.ktor:ktor-server-netty:${Versions.ktor}")
implementation("io.ktor:ktor-gson:${Versions.ktor}")
} }
tasks.withType(KotlinCompile::class.java).all { tasks.withType(KotlinCompile::class.java).all {

View File

@ -1,5 +1,6 @@
package com.github.michaelbull.result.example package com.github.michaelbull.result.example
import com.fasterxml.jackson.databind.SerializationFeature
import com.github.michaelbull.result.Result import com.github.michaelbull.result.Result
import com.github.michaelbull.result.andThen import com.github.michaelbull.result.andThen
import com.github.michaelbull.result.example.model.domain.Created import com.github.michaelbull.result.example.model.domain.Created
@ -28,32 +29,37 @@ import com.github.michaelbull.result.example.repository.InMemoryCustomerReposito
import com.github.michaelbull.result.example.service.CustomerService import com.github.michaelbull.result.example.service.CustomerService
import com.github.michaelbull.result.mapBoth import com.github.michaelbull.result.mapBoth
import com.github.michaelbull.result.toResultOr import com.github.michaelbull.result.toResultOr
import io.ktor.application.Application
import io.ktor.application.call
import io.ktor.application.install
import io.ktor.features.CallLogging
import io.ktor.features.Compression
import io.ktor.features.ContentNegotiation
import io.ktor.features.DefaultHeaders
import io.ktor.gson.gson
import io.ktor.http.HttpStatusCode import io.ktor.http.HttpStatusCode
import io.ktor.http.Parameters import io.ktor.http.Parameters
import io.ktor.request.receive import io.ktor.serialization.jackson.jackson
import io.ktor.response.respond import io.ktor.server.application.Application
import io.ktor.routing.get import io.ktor.server.application.call
import io.ktor.routing.post import io.ktor.server.application.install
import io.ktor.routing.routing import io.ktor.server.engine.embeddedServer
import io.ktor.server.netty.Netty
import io.ktor.server.plugins.ContentNegotiation
import io.ktor.server.request.receive
import io.ktor.server.response.respond
import io.ktor.server.routing.get
import io.ktor.server.routing.post
import io.ktor.server.routing.routing
fun Application.main() { fun main() {
install(DefaultHeaders) embeddedServer(Netty, port = 8080, host = "0.0.0.0") {
install(Compression) configureRouting()
install(CallLogging) configureSerialization()
}.start(wait = true)
}
fun Application.configureSerialization() {
install(ContentNegotiation) { install(ContentNegotiation) {
gson { jackson {
setPrettyPrinting() enable(SerializationFeature.INDENT_OUTPUT)
} }
} }
}
fun Application.configureRouting() {
val customers = setOf( val customers = setOf(
CustomerEntity(CustomerId(1L), "Michael", "Bull", "michael@example.com"), CustomerEntity(CustomerId(1L), "Michael", "Bull", "michael@example.com"),
CustomerEntity(CustomerId(2L), "Kevin", "Herron", "kevin@example.com"), CustomerEntity(CustomerId(2L), "Kevin", "Herron", "kevin@example.com"),
@ -86,7 +92,6 @@ fun Application.main() {
} }
} }
} }
} }
private fun Parameters.readId(): Result<Long, DomainMessage> { private fun Parameters.readId(): Result<Long, DomainMessage> {

View File

@ -1,24 +1,6 @@
package com.github.michaelbull.result.example.model.domain package com.github.michaelbull.result.example.model.domain
import com.github.michaelbull.result.Err
import com.github.michaelbull.result.Ok
import com.github.michaelbull.result.Result
data class PersonalName( data class PersonalName(
val first: String, val first: String,
val last: String val last: String
) )
private const val MAX_LENGTH = 10
fun Pair<String?, String?>.toPersonalName(): Result<PersonalName, DomainMessage> {
val (first, last) = this
return when {
first.isNullOrBlank() -> Err(FirstNameRequired)
last.isNullOrBlank() -> Err(LastNameRequired)
first.length > MAX_LENGTH -> Err(FirstNameTooLong)
last.length > MAX_LENGTH -> Err(LastNameTooLong)
else -> Ok(PersonalName(first, last))
}
}