Skip to content
This repository has been archived by the owner on Aug 14, 2022. It is now read-only.

update dependencies, migrate to Ktor 2 #7

Merged
merged 6 commits into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[versions]
kotlin = "1.6.10"
ktor = "1.6.7"
kotlin = "1.6.20"
ktor = "2.0.3"
detekt = "1.18.1"
junit = "5.7.2"
mockk = "1.12.0"
dokka = "1.6.10"
junit = "5.8.2"
mockk = "1.12.4"
dokka = "1.6.21"
logback = "1.2.11"
swaggerCore = "2.1.12"
swaggerUi = "4.5.2"
swaggerCore = "2.2.1"
swaggerUi = "4.8.0"

[libraries]

Expand All @@ -17,12 +17,13 @@ kotlin-stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib-jdk8", version.re

ktor-server-core = { module = "io.ktor:ktor-server-core", version.ref = "ktor" }
ktor-server-netty = { module = "io.ktor:ktor-server-netty", version.ref = "ktor" }
ktor-server-jackson = { module = "io.ktor:ktor-jackson", version.ref = "ktor" }
ktor-server-content-negotiation = { module = "io.ktor:ktor-server-content-negotiation", version.ref = "ktor" }
ktor-server-jackson = { module = "io.ktor:ktor-serialization-jackson", version.ref = "ktor" }

swaggerCore-models = { module = "io.swagger.core.v3:swagger-models", version.ref = "swaggerCore" }
swaggerCore-core = { module = "io.swagger.core.v3:swagger-core", version.ref = "swaggerCore" }

swaggerUi = { module = "org.webjars:swagger-ui", version.ref = "swaggerUi"}
swaggerUi = { module = "org.webjars:swagger-ui", version.ref = "swaggerUi" }

logback = { module = "ch.qos.logback:logback-classic", version.ref = "logback" }

Expand All @@ -46,4 +47,4 @@ detekt-formatting = { module = "io.gitlab.arturbosch.detekt:detekt-formatting",
kotlin = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
dokka = { id = "org.jetbrains.dokka", version.ref = "dokka" }

detekt = { id = "io.gitlab.arturbosch.detekt", version.ref = "detekt" }
detekt = { id = "io.gitlab.arturbosch.detekt", version.ref = "detekt" }
28 changes: 14 additions & 14 deletions koa-ktor-ui/src/main/kotlin/KoaSwaggerUi.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package guru.zoroark.koa.ktor.ui

import io.ktor.application.ApplicationCallPipeline
import io.ktor.application.ApplicationFeature
import io.ktor.application.application
import io.ktor.application.call
import io.ktor.application.feature
import io.ktor.http.ContentType
import io.ktor.http.HttpStatusCode
import io.ktor.http.content.OutgoingContent
import io.ktor.http.content.resourceClasspathResource
import io.ktor.http.defaultForFileExtension
import io.ktor.response.respond
import io.ktor.response.respondText
import io.ktor.routing.Route
import io.ktor.routing.application
import io.ktor.routing.get
import io.ktor.server.application.ApplicationCallPipeline
import io.ktor.server.application.BaseApplicationPlugin
import io.ktor.server.application.application
import io.ktor.server.application.call
import io.ktor.server.application.plugin
import io.ktor.server.http.content.resourceClasspathResource
import io.ktor.server.response.respond
import io.ktor.server.response.respondText
import io.ktor.server.routing.Route
import io.ktor.server.routing.application
import io.ktor.server.routing.get
import io.ktor.util.AttributeKey
import io.ktor.util.InternalAPI
import java.util.Properties
Expand All @@ -24,7 +24,7 @@ private const val SWAGGER_UI_POM_LOCATION: String = "META-INF/maven/org.webjars/
class KoaSwaggerUi(private val classpathPath: String) {
class Configuration

companion object Feature : ApplicationFeature<ApplicationCallPipeline, Configuration, KoaSwaggerUi> {
companion object Plugin : BaseApplicationPlugin<ApplicationCallPipeline, Configuration, KoaSwaggerUi> {
override val key = AttributeKey<KoaSwaggerUi>("KoaSwaggerUi")

private fun computeSwaggerUiPath(): String {
Expand Down Expand Up @@ -57,7 +57,7 @@ class KoaSwaggerUi(private val classpathPath: String) {
fun Route.swaggerUi(path: String, openApiPath: String) {
val indexContent by lazy {
val originalIndex = KoaSwaggerUi::class.java.classLoader
.getResourceAsStream(application.feature(KoaSwaggerUi).getPathFor("index.html"))
.getResourceAsStream(application.plugin(KoaSwaggerUi).getPathFor("index.html"))
.use { it!!.bufferedReader().readText() }
originalIndex.replace("url: \"https://petstore.swagger.io/v2/swagger.json\"", "url: \"$openApiPath\"")
}
Expand All @@ -66,7 +66,7 @@ fun Route.swaggerUi(path: String, openApiPath: String) {
if (fileName == "index.html") {
call.respondText(ContentType.Text.Html) { indexContent }
} else {
val result = application.feature(KoaSwaggerUi).getContentFor(fileName)
val result = application.plugin(KoaSwaggerUi).getContentFor(fileName)
if (result != null) call.respond(result)
else call.respond(HttpStatusCode.NotFound)
}
Expand Down
22 changes: 11 additions & 11 deletions koa-ktor/src/main/kotlin/DescribeDsl.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package guru.zoroark.koa.ktor

import guru.zoroark.koa.dsl.OperationBuilder
import guru.zoroark.koa.dsl.KoaDsl
import io.ktor.routing.HttpMethodRouteSelector
import io.ktor.routing.Route
import io.ktor.routing.RouteSelector
import io.ktor.routing.RouteSelectorEvaluation
import io.ktor.routing.RoutingResolveContext
import io.ktor.routing.application
import io.ktor.util.pipeline.ContextDsl
import guru.zoroark.koa.dsl.OperationBuilder
import io.ktor.server.routing.HttpMethodRouteSelector
import io.ktor.server.routing.Route
import io.ktor.server.routing.RouteSelector
import io.ktor.server.routing.RouteSelectorEvaluation
import io.ktor.server.routing.RoutingResolveContext
import io.ktor.server.routing.application
import io.ktor.util.KtorDsl
import io.swagger.v3.oas.models.PathItem

@ContextDsl
@KtorDsl
@KoaDsl
infix fun Route.describe(descriptionBlock: OperationBuilder.() -> Unit) {
val metadata = parseMetadataFromRoute(this)
Expand All @@ -36,13 +36,13 @@ infix fun Route.describe(descriptionBlock: OperationBuilder.() -> Unit) {
)
}

@ContextDsl
@KtorDsl
@KoaDsl
fun Route.describeSubroutes(descriptionBlock: DescriptionHook) {
application.koa.registerDescriptionHookForSubroutesOf(this, descriptionBlock)
}

@ContextDsl
@KtorDsl
fun Route.routeWithDescription(descriptionHook: DescriptionHook, routeBuilder: Route.() -> Unit): Route {
val subroute = createChild(object : RouteSelector() {
override fun evaluate(context: RoutingResolveContext, segmentIndex: Int) = RouteSelectorEvaluation.Transparent
Expand Down
17 changes: 8 additions & 9 deletions koa-ktor/src/main/kotlin/Koa.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package guru.zoroark.koa.ktor
import guru.zoroark.koa.dsl.KoaDslContext
import guru.zoroark.koa.dsl.RootBuilder
import guru.zoroark.koa.dsl.RootDsl
import io.ktor.application.Application
import io.ktor.application.ApplicationCallPipeline
import io.ktor.application.ApplicationFeature
import io.ktor.application.feature
import io.ktor.routing.Route
import io.ktor.server.application.Application
import io.ktor.server.application.ApplicationCallPipeline
import io.ktor.server.application.BaseApplicationPlugin
import io.ktor.server.application.plugin
import io.ktor.server.routing.Route
import io.ktor.util.AttributeKey
import io.swagger.v3.core.util.Json
import io.swagger.v3.oas.models.Components
Expand All @@ -25,11 +25,11 @@ class Koa(config: Configuration) {

class Configuration(internal val builder: RootBuilder = RootBuilder()) : RootDsl by builder

companion object Feature : ApplicationFeature<ApplicationCallPipeline, Configuration, Koa> {
companion object Plugin : BaseApplicationPlugin<ApplicationCallPipeline, Configuration, Koa> {
override val key = AttributeKey<Koa>("Koa")

override fun install(pipeline: ApplicationCallPipeline, configure: Configuration.() -> Unit): Koa =
Koa(Configuration().apply(configure))
Koa(Configuration().apply(configure))
}

fun addOperation(path: String, method: PathItem.HttpMethod, operation: Operation) {
Expand Down Expand Up @@ -76,8 +76,7 @@ class Koa(config: Configuration) {
}
}

val Application.koa: Koa
get() = feature(Koa)
val Application.koa: Koa get() = plugin(Koa)

private inline fun Route.traverseParents(elementHandler: (Route) -> Unit) {
var current: Route? = this
Expand Down
8 changes: 4 additions & 4 deletions koa-ktor/src/main/kotlin/KtorEndpointMetadataParser.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package guru.zoroark.koa.ktor

import io.ktor.routing.HttpMethodRouteSelector
import io.ktor.routing.PathSegmentConstantRouteSelector
import io.ktor.routing.PathSegmentParameterRouteSelector
import io.ktor.routing.Route
import io.ktor.server.routing.HttpMethodRouteSelector
import io.ktor.server.routing.PathSegmentConstantRouteSelector
import io.ktor.server.routing.PathSegmentParameterRouteSelector
import io.ktor.server.routing.Route
import java.util.LinkedList

data class EndpointMetadata(
Expand Down
4 changes: 2 additions & 2 deletions koa-ktor/src/main/kotlin/RespondOpenApiDocument.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package guru.zoroark.koa.ktor

import io.ktor.application.ApplicationCall
import io.ktor.http.ContentType
import io.ktor.http.HttpStatusCode
import io.ktor.response.respondText
import io.ktor.server.application.ApplicationCall
import io.ktor.server.response.respondText

suspend fun ApplicationCall.respondOpenApiDocument() {
respondText(ContentType.Application.Json, HttpStatusCode.OK) {
Expand Down
14 changes: 7 additions & 7 deletions koa-ktor/src/test/kotlin/FullChainTest.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package guru.zoroark.koa.ktor

import guru.zoroark.koa.dsl.schema
import io.ktor.application.call
import io.ktor.application.install
import io.ktor.response.respond
import io.ktor.response.respondText
import io.ktor.routing.get
import io.ktor.routing.route
import io.ktor.routing.routing
import io.ktor.server.application.call
import io.ktor.server.application.install
import io.ktor.server.response.respond
import io.ktor.server.response.respondText
import io.ktor.server.routing.get
import io.ktor.server.routing.route
import io.ktor.server.routing.routing
import io.ktor.server.testing.withTestApplication
import kotlin.test.Test
import kotlin.test.assertEquals
Expand Down
1 change: 1 addition & 0 deletions samples/ktor/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ dependencies {
implementation project(":koa-ktor-ui")

implementation libs.ktor.server.netty
implementation libs.ktor.server.content.negotiation
implementation libs.ktor.server.jackson
implementation libs.logback
}
Expand Down
22 changes: 11 additions & 11 deletions samples/ktor/src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ import guru.zoroark.koa.ktor.describe
import guru.zoroark.koa.ktor.respondOpenApiDocument
import guru.zoroark.koa.ktor.ui.KoaSwaggerUi
import guru.zoroark.koa.ktor.ui.swaggerUi
import io.ktor.application.call
import io.ktor.application.install
import io.ktor.features.ContentNegotiation
import io.ktor.http.HttpStatusCode
import io.ktor.http.HttpStatusCode.Companion.NoContent
import io.ktor.http.HttpStatusCode.Companion.OK
import io.ktor.jackson.jackson
import io.ktor.request.receive
import io.ktor.response.respond
import io.ktor.routing.get
import io.ktor.routing.patch
import io.ktor.routing.put
import io.ktor.routing.route
import io.ktor.routing.routing
import io.ktor.serialization.jackson.jackson
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.ContentNegotiation
import io.ktor.server.request.receive
import io.ktor.server.response.respond
import io.ktor.server.routing.get
import io.ktor.server.routing.patch
import io.ktor.server.routing.put
import io.ktor.server.routing.route
import io.ktor.server.routing.routing
import io.swagger.v3.oas.models.media.StringSchema

data class ServerState(var myString: String, var myNumber: Int)
Expand Down