Migrate to Ktor 2
This commit is contained in:
parent
d07bd589ed
commit
6a5523c998
@ -4,7 +4,7 @@ object Versions {
|
||||
const val kotlinBenchmark = "0.3.1"
|
||||
const val kotlinCoroutines = "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 versionsPlugin = "0.39.0"
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ plugins {
|
||||
}
|
||||
|
||||
application {
|
||||
mainClass.set("io.ktor.server.netty.EngineMain")
|
||||
mainClass.set("com.github.michaelbull.result.example.ApplicationKt")
|
||||
}
|
||||
|
||||
dependencies {
|
||||
@ -14,8 +14,9 @@ dependencies {
|
||||
implementation(kotlin("stdlib-jdk8"))
|
||||
implementation("ch.qos.logback:logback-classic:${Versions.logback}")
|
||||
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-gson:${Versions.ktor}")
|
||||
}
|
||||
|
||||
tasks.withType(KotlinCompile::class.java).all {
|
||||
|
@ -1,5 +1,6 @@
|
||||
package com.github.michaelbull.result.example
|
||||
|
||||
import com.fasterxml.jackson.databind.SerializationFeature
|
||||
import com.github.michaelbull.result.Result
|
||||
import com.github.michaelbull.result.andThen
|
||||
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.mapBoth
|
||||
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.Parameters
|
||||
import io.ktor.request.receive
|
||||
import io.ktor.response.respond
|
||||
import io.ktor.routing.get
|
||||
import io.ktor.routing.post
|
||||
import io.ktor.routing.routing
|
||||
import io.ktor.serialization.jackson.jackson
|
||||
import io.ktor.server.application.Application
|
||||
import io.ktor.server.application.call
|
||||
import io.ktor.server.application.install
|
||||
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() {
|
||||
install(DefaultHeaders)
|
||||
install(Compression)
|
||||
install(CallLogging)
|
||||
fun main() {
|
||||
embeddedServer(Netty, port = 8080, host = "0.0.0.0") {
|
||||
configureRouting()
|
||||
configureSerialization()
|
||||
}.start(wait = true)
|
||||
}
|
||||
|
||||
fun Application.configureSerialization() {
|
||||
install(ContentNegotiation) {
|
||||
gson {
|
||||
setPrettyPrinting()
|
||||
jackson {
|
||||
enable(SerializationFeature.INDENT_OUTPUT)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun Application.configureRouting() {
|
||||
val customers = setOf(
|
||||
CustomerEntity(CustomerId(1L), "Michael", "Bull", "michael@example.com"),
|
||||
CustomerEntity(CustomerId(2L), "Kevin", "Herron", "kevin@example.com"),
|
||||
@ -86,7 +92,6 @@ fun Application.main() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private fun Parameters.readId(): Result<Long, DomainMessage> {
|
||||
|
@ -1,24 +1,6 @@
|
||||
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(
|
||||
val first: 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))
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user