Skip to content

Commit

Permalink
Migrate to Ktor 2
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbull committed Jan 8, 2022
1 parent d07bd58 commit 6a5523c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 42 deletions.
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
5 changes: 3 additions & 2 deletions example/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ plugins {
}

application {
mainClass.set("io.ktor.server.netty.EngineMain")
mainClass.set("com.github.michaelbull.result.example.ApplicationKt")
}

dependencies {
implementation(project(":kotlin-result"))
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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

fun Application.main() {
install(DefaultHeaders)
install(Compression)
install(CallLogging)
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 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"),
Expand Down Expand Up @@ -86,7 +92,6 @@ fun Application.main() {
}
}
}

}

private fun Parameters.readId(): Result<Long, DomainMessage> {
Expand Down
Original file line number Diff line number Diff line change
@@ -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))
}
}

0 comments on commit 6a5523c

Please sign in to comment.