-
-
Notifications
You must be signed in to change notification settings - Fork 481
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(pact-jvm-server): Converted Scala Netty to Kotlin KTor server
- Loading branch information
1 parent
31287a1
commit 9fe90db
Showing
9 changed files
with
95 additions
and
256 deletions.
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
pact-jvm-server/src/main/kotlin/au/com/dius/pact/server/MainServer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package au.com.dius.pact.server | ||
|
||
import au.com.dius.pact.core.model.ContentType | ||
import au.com.dius.pact.core.model.IResponse | ||
import au.com.dius.pact.core.model.OptionalBody | ||
import au.com.dius.pact.core.model.Request | ||
import io.ktor.http.HttpStatusCode | ||
import io.ktor.server.application.ApplicationCall | ||
import io.ktor.server.application.ApplicationCallPipeline | ||
import io.ktor.server.application.install | ||
import io.ktor.server.engine.applicationEngineEnvironment | ||
import io.ktor.server.engine.connector | ||
import io.ktor.server.engine.embeddedServer | ||
import io.ktor.server.netty.Netty | ||
import io.ktor.server.plugins.callloging.CallLogging | ||
import io.ktor.server.request.httpMethod | ||
import io.ktor.server.request.path | ||
import io.ktor.server.request.receiveStream | ||
import io.ktor.server.response.respond | ||
import io.ktor.server.response.respondBytes | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import java.util.zip.DeflaterInputStream | ||
import java.util.zip.GZIPInputStream | ||
|
||
data class ServerStateStore(var state: ServerState = ServerState()) | ||
|
||
class MainServer(val store: ServerStateStore, val serverConfig: Config) { | ||
private val env = applicationEngineEnvironment { | ||
connector { | ||
host = serverConfig.host | ||
port = serverConfig.port | ||
} | ||
|
||
module { | ||
install(CallLogging) | ||
intercept(ApplicationCallPipeline.Call) { | ||
val request = toPactRequest(context) | ||
val result = RequestRouter.dispatch(request, store.state, serverConfig) | ||
store.state = result.newState | ||
pactResponseToKTorResponse(result.response, context) | ||
} | ||
} | ||
} | ||
|
||
val server = embeddedServer(Netty, environment = env, configure = {}) | ||
|
||
suspend fun toPactRequest(call: ApplicationCall): Request { | ||
val request = call.request | ||
val headers = request.headers | ||
val bodyContents = withContext(Dispatchers.IO) { | ||
val stream = call.receiveStream() | ||
when (bodyIsCompressed(headers["Content-Encoding"])) { | ||
"gzip" -> GZIPInputStream(stream).readBytes() | ||
"deflate" -> DeflaterInputStream(stream).readBytes() | ||
else -> stream.readBytes() | ||
} | ||
} | ||
val body = if (bodyContents.isEmpty()) { | ||
OptionalBody.empty() | ||
} else { | ||
OptionalBody.body(bodyContents, ContentType.fromString(headers["Content-Type"]).or(ContentType.JSON)) | ||
} | ||
return Request(request.httpMethod.value, request.path(), | ||
request.queryParameters.entries().associate { it.toPair() }.toMutableMap(), | ||
headers.entries().associate { it.toPair() }.toMutableMap(), body) | ||
} | ||
|
||
private fun bodyIsCompressed(encoding: String?): String? { | ||
return if (COMPRESSED_ENCODINGS.contains(encoding)) encoding else null | ||
} | ||
|
||
suspend fun pactResponseToKTorResponse(response: IResponse, call: ApplicationCall) { | ||
response.headers.forEach { entry -> | ||
entry.value.forEach { | ||
call.response.headers.append(entry.key, it, safeOnly = false) | ||
} | ||
} | ||
|
||
val body = response.body | ||
if (body.isPresent()) { | ||
call.respondBytes(status = HttpStatusCode.fromValue(response.status), bytes = body.unwrap()) | ||
} else { | ||
call.respond(HttpStatusCode.fromValue(response.status)) | ||
} | ||
} | ||
|
||
companion object { | ||
private val COMPRESSED_ENCODINGS = setOf("gzip", "deflate") | ||
} | ||
} |
40 changes: 0 additions & 40 deletions
40
pact-jvm-server/src/main/scala/au/com/dius/pact/server/CollectionUtils.scala
This file was deleted.
Oops, something went wrong.
65 changes: 0 additions & 65 deletions
65
pact-jvm-server/src/main/scala/au/com/dius/pact/server/Conversions.scala
This file was deleted.
Oops, something went wrong.
35 changes: 0 additions & 35 deletions
35
pact-jvm-server/src/main/scala/au/com/dius/pact/server/JsonUtils.scala
This file was deleted.
Oops, something went wrong.
28 changes: 0 additions & 28 deletions
28
pact-jvm-server/src/main/scala/au/com/dius/pact/server/RequestHandler.scala
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
pact-jvm-server/src/main/scala/au/com/dius/pact/server/ResponseUtils.scala
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 0 additions & 30 deletions
30
pact-jvm-server/src/test/groovy/au/com/dius/pact/server/ConversionsSpec.groovy
This file was deleted.
Oops, something went wrong.
46 changes: 0 additions & 46 deletions
46
pact-jvm-server/src/test/groovy/au/com/dius/pact/server/JsonUtilsSpec.groovy
This file was deleted.
Oops, something went wrong.