Skip to content

Commit

Permalink
Merge pull request JetBrains#1 from aleksanderaleksic/ktor-v2-support
Browse files Browse the repository at this point in the history
Upgraded ktor version to 2.0.3
  • Loading branch information
aleksanderaleksic authored Aug 4, 2022
2 parents 596c77f + 798ff58 commit fcee2b3
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 30 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmCompile

group = "io.kotless"
version = "0.2.0"
version = "0.3.0"

plugins {
id("io.gitlab.arturbosch.detekt") version ("1.15.0") apply true
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/io/kotless/buildsrc/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ object Versions {

const val serverlessContainers = "1.5.2"

const val ktor = "1.5.0"
const val ktor = "2.0.3"

const val springBoot = "2.4.2"
const val spring = "5.3.3"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import io.kotless.dsl.ktor.app.KotlessEngine
import io.kotless.dsl.ktor.lang.LambdaWarming
import io.kotless.dsl.model.HttpResponse
import io.kotless.dsl.utils.JSON
import io.ktor.application.*
import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.util.pipeline.*
import kotlinx.coroutines.runBlocking
Expand All @@ -31,7 +31,6 @@ abstract class KotlessAWS : RequestStreamHandler {

private var prepared = false

@EngineAPI
val engine = KotlessEngine(applicationEngineEnvironment {
log = logger
}).also {
Expand All @@ -41,7 +40,7 @@ abstract class KotlessAWS : RequestStreamHandler {

abstract fun prepare(app: Application)

@OptIn(InternalAPI::class, EngineAPI::class)
@OptIn(InternalAPI::class)
override fun handleRequest(input: InputStream, output: OutputStream, @Suppress("UNUSED_PARAMETER") any: Context?) {
if (!prepared) {
prepare(engine.application)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@ package io.kotless.dsl.ktor
import com.microsoft.azure.functions.*
import com.microsoft.azure.functions.annotation.*
import io.kotless.InternalAPI
import io.kotless.ScheduledEventType
import io.kotless.dsl.cloud.azure.AzureRequestHandler
import io.kotless.dsl.cloud.azure.model.toRequest
import io.kotless.dsl.ktor.app.KotlessCall
import io.kotless.dsl.ktor.app.KotlessEngine
import io.kotless.dsl.ktor.lang.LambdaWarming
import io.ktor.application.*
import io.ktor.server.engine.*
import io.ktor.util.pipeline.*
import io.ktor.server.application.Application
import io.ktor.server.engine.applicationEngineEnvironment
import kotlinx.coroutines.runBlocking
import org.slf4j.LoggerFactory
import java.util.*
import java.util.Optional


/**
Expand All @@ -30,7 +28,6 @@ abstract class KotlessAzure : AzureRequestHandler {

private var prepared = false

@EngineAPI
val engine = KotlessEngine(applicationEngineEnvironment {
log = logger
}).also {
Expand All @@ -41,13 +38,13 @@ abstract class KotlessAzure : AzureRequestHandler {
abstract fun prepare(app: Application)

@InternalAPI
@EngineAPI
override fun run(
@HttpTrigger(
name = "req",
methods = [HttpMethod.GET, HttpMethod.POST],
authLevel = AuthorizationLevel.FUNCTION
) request: HttpRequestMessage<Optional<String>>, context: ExecutionContext
) request: HttpRequestMessage<Optional<String>>,
context: ExecutionContext,
): HttpResponseMessage {
if (!prepared) {
prepare(engine.application)
Expand All @@ -63,7 +60,7 @@ abstract class KotlessAzure : AzureRequestHandler {

val call = KotlessCall(engine.application, myRequest)

engine.pipeline.execute(call)
engine.pipeline.execute(call, Unit)

call.response.toHttp()
}
Expand All @@ -83,7 +80,6 @@ abstract class KotlessAzure : AzureRequestHandler {
return outputResponse.build()
}

@OptIn(InternalAPI::class, EngineAPI::class)
override fun timer(@TimerTrigger(name = "timer", schedule = "* * * * * *") timer: String, context: ExecutionContext) {
logger.trace("Executing warmup sequence")
engine.environment.monitor.raise(LambdaWarming, engine.application)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package io.kotless.dsl.ktor.app

import io.kotless.dsl.model.HttpRequest
import io.ktor.application.*
import io.ktor.http.*
import io.ktor.server.engine.*
import io.ktor.http.Parameters
import io.ktor.server.application.Application
import io.ktor.server.engine.BaseApplicationCall

/**
* Ktor Call used by Kotless. It is mapped from APIGateway request and to APIGateway response.
*/
@EngineAPI
class KotlessCall(application: Application, request: HttpRequest) : BaseApplicationCall(application) {
override val request = KotlessRequest(request, this)
override val response = KotlessResponse(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import io.ktor.server.engine.*
* Kotless implementation of Ktor engine.
* Optimized for serverless use-case.
*/
@EngineAPI
class KotlessEngine(environment: ApplicationEngineEnvironment) : BaseApplicationEngine(environment) {
override fun start(wait: Boolean): ApplicationEngine {
environment.start()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package io.kotless.dsl.ktor.app

import io.kotless.dsl.model.HttpRequest
import io.ktor.application.*
import io.ktor.http.*
import io.ktor.request.*
import io.ktor.server.engine.*
import io.ktor.utils.io.*
import io.ktor.http.Headers
import io.ktor.http.HttpMethod
import io.ktor.http.Parameters
import io.ktor.http.RequestConnectionPoint
import io.ktor.server.application.ApplicationCall
import io.ktor.server.engine.BaseApplicationRequest
import io.ktor.server.request.ApplicationReceivePipeline
import io.ktor.server.request.RequestCookies
import io.ktor.utils.io.ByteReadChannel

/**
* Ktor Request used by Kotless. It will be created from APIGateway request.
Expand Down Expand Up @@ -36,6 +40,7 @@ class KotlessRequest(val query: HttpRequest, call: ApplicationCall) : BaseApplic
override val queryParameters: Parameters = Parameters.build {
query.params.forEach { append(it.key, it.value) }
}
override val rawQueryParameters: Parameters = queryParameters

override fun receiveChannel() = ByteReadChannel(query.body?.bytes ?: ByteArray(0))
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package io.kotless.dsl.ktor.app

import io.kotless.MimeType
import io.kotless.dsl.model.HttpResponse
import io.ktor.application.*
import io.ktor.http.*
import io.ktor.http.content.*
import io.ktor.response.*
import io.ktor.server.application.ApplicationCall
import io.ktor.server.engine.*
import io.ktor.server.response.ResponseHeaders
import io.ktor.utils.io.*
import io.ktor.utils.io.CancellationException
import io.ktor.utils.io.core.*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.kotless.dsl.ktor.lang

import io.ktor.application.*
import io.ktor.server.application.Application
import io.ktor.events.EventDefinition

/**
* Event that will be emitted during warming of lambda.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.kotless.dsl.ktor.lang.event

import io.ktor.application.*
import io.ktor.server.application.Application
import io.ktor.events.Events

/**
* DSL Marker for DSL to define events in Ktor
Expand All @@ -13,6 +14,6 @@ annotation class EventsDsl
* Configuration method to define applications events
*/
@EventsDsl
fun Application.events(body: ApplicationEvents.() -> Unit) {
fun Application.events(body: Events.() -> Unit) {
this.environment.monitor.body()
}

0 comments on commit fcee2b3

Please sign in to comment.