From dd3d3351c058bf5c057d3201de4bff3bed900623 Mon Sep 17 00:00:00 2001 From: Mustafa Ozhan Date: Sat, 28 Sep 2024 13:30:21 +0200 Subject: [PATCH 1/2] [Oztechan/CCC#3996] Move threading into Routes --- .../ccc/backend/app/module/APIModule.kt | 8 ++++---- .../ccc/backend/app/routes/CurrencyRoute.kt | 16 ++++++++++++---- .../ccc/backend/app/routes/ErrorRoute.kt | 10 +++++++--- .../ccc/backend/app/routes/RootRoute.kt | 10 +++++++--- .../ccc/backend/app/routes/VersionRoute.kt | 19 +++++++++++++------ 5 files changed, 43 insertions(+), 20 deletions(-) diff --git a/backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/module/APIModule.kt b/backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/module/APIModule.kt index a6d21ffde..75ddf19d7 100644 --- a/backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/module/APIModule.kt +++ b/backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/module/APIModule.kt @@ -32,10 +32,10 @@ internal fun Application.apiModule() { routing { globalScope.launch(ioDispatcher) { - root() - currency(apiController) - version() - error() + root(ioDispatcher) + currency(apiController, ioDispatcher) + version(ioDispatcher) + error(ioDispatcher) } } } diff --git a/backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/CurrencyRoute.kt b/backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/CurrencyRoute.kt index 7a369e616..dd33ddd73 100644 --- a/backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/CurrencyRoute.kt +++ b/backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/CurrencyRoute.kt @@ -11,12 +11,15 @@ import io.ktor.server.application.call import io.ktor.server.response.respond import io.ktor.server.routing.Route import io.ktor.server.routing.get +import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.withContext private const val PATH_BY_BASE = "/currency/byBase/" private const val PARAMETER_BASE = "base" internal fun Route.currency( - apiController: APIController + apiController: APIController, + ioDispatcher: CoroutineDispatcher ) { get(PATH_BY_BASE) { Logger.v { "GET Request $PATH_BY_BASE" } @@ -24,9 +27,14 @@ internal fun Route.currency( call.parameters[PARAMETER_BASE]?.let { base -> Logger.v { "Parameter: $PARAMETER_BASE $base" } - apiController.getExchangeRateByBase(base) - ?.let { call.respond(HttpStatusCode.OK, it) } - ?: call.respond(HttpStatusCode.NotFound) + withContext(ioDispatcher) { + apiController.getExchangeRateByBase(base) + }?.let { + call.respond( + status = HttpStatusCode.OK, + message = it + ) + } ?: call.respond(HttpStatusCode.NotFound) } ?: call.respond(HttpStatusCode.BadRequest) } } diff --git a/backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/ErrorRoute.kt b/backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/ErrorRoute.kt index 0df232a3e..6d772110f 100644 --- a/backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/ErrorRoute.kt +++ b/backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/ErrorRoute.kt @@ -12,17 +12,21 @@ import io.ktor.server.response.respond import io.ktor.server.response.respondText import io.ktor.server.routing.Route import io.ktor.server.routing.get +import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.withContext private const val PATH_ERROR = "/error" private const val ERROR_HTML = "error.html" -internal fun Route.error() { +internal fun Route.error(ioDispatcher: CoroutineDispatcher) { get(PATH_ERROR) { Logger.v { "GET Request $PATH_ERROR" } - javaClass.classLoader?.getResource(ERROR_HTML)?.readText()?.let { resource -> + withContext(ioDispatcher) { + javaClass.classLoader?.getResource(ERROR_HTML)?.readText() + }?.let { call.respondText( - text = resource, + text = it, contentType = ContentType.Text.Html, status = HttpStatusCode.OK ) diff --git a/backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/RootRoute.kt b/backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/RootRoute.kt index 0c7d6673f..554e7f523 100644 --- a/backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/RootRoute.kt +++ b/backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/RootRoute.kt @@ -12,17 +12,21 @@ import io.ktor.server.response.respond import io.ktor.server.response.respondText import io.ktor.server.routing.Route import io.ktor.server.routing.get +import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.withContext private const val PATH_ROOT = "/" private const val INDEX_HTML = "index.html" -internal fun Route.root() { +internal fun Route.root(ioDispatcher: CoroutineDispatcher) { get(PATH_ROOT) { Logger.v { "GET Request $PATH_ROOT" } - javaClass.classLoader?.getResource(INDEX_HTML)?.readText()?.let { resource -> + withContext(ioDispatcher) { + javaClass.classLoader?.getResource(INDEX_HTML)?.readText() + }?.let { call.respondText( - text = resource, + text = it, contentType = ContentType.Text.Html, status = HttpStatusCode.OK ) diff --git a/backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/VersionRoute.kt b/backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/VersionRoute.kt index 6a9fc312a..086e4fdec 100644 --- a/backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/VersionRoute.kt +++ b/backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/VersionRoute.kt @@ -4,20 +4,27 @@ import co.touchlab.kermit.Logger import io.ktor.http.ContentType import io.ktor.http.HttpStatusCode import io.ktor.server.application.call +import io.ktor.server.response.respond import io.ktor.server.response.respondText import io.ktor.server.routing.Route import io.ktor.server.routing.get +import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.withContext private const val PATH_VERSION = "/version" -internal fun Route.version() { +internal fun Route.version(ioDispatcher: CoroutineDispatcher) { get(PATH_VERSION) { Logger.v { "GET Request $PATH_VERSION" } - call.respondText( - text = "Version: ${javaClass.`package`?.implementationVersion}", - contentType = ContentType.Text.Plain, - status = HttpStatusCode.OK - ) + withContext(ioDispatcher) { + javaClass.`package`?.implementationVersion + }?.let { + call.respondText( + text = "Version: $it", + contentType = ContentType.Text.Plain, + status = HttpStatusCode.OK + ) + } ?: call.respond(HttpStatusCode.NotFound) } } From d85a5386d2e18c0534ecfd9f39127e6a0c01cc69 Mon Sep 17 00:00:00 2001 From: Mustafa Ozhan Date: Sat, 28 Sep 2024 13:38:20 +0200 Subject: [PATCH 2/2] [Oztechan/CCC#3996] Move threading into Routes --- .../com/oztechan/ccc/backend/app/routes/CurrencyRoute.kt | 4 ++-- .../kotlin/com/oztechan/ccc/backend/app/routes/ErrorRoute.kt | 4 ++-- .../kotlin/com/oztechan/ccc/backend/app/routes/RootRoute.kt | 4 ++-- .../com/oztechan/ccc/backend/app/routes/VersionRoute.kt | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/CurrencyRoute.kt b/backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/CurrencyRoute.kt index dd33ddd73..94fa68f87 100644 --- a/backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/CurrencyRoute.kt +++ b/backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/CurrencyRoute.kt @@ -29,10 +29,10 @@ internal fun Route.currency( withContext(ioDispatcher) { apiController.getExchangeRateByBase(base) - }?.let { + }?.let { exchangeRate -> call.respond( status = HttpStatusCode.OK, - message = it + message = exchangeRate ) } ?: call.respond(HttpStatusCode.NotFound) } ?: call.respond(HttpStatusCode.BadRequest) diff --git a/backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/ErrorRoute.kt b/backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/ErrorRoute.kt index 6d772110f..81806c7a2 100644 --- a/backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/ErrorRoute.kt +++ b/backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/ErrorRoute.kt @@ -24,9 +24,9 @@ internal fun Route.error(ioDispatcher: CoroutineDispatcher) { withContext(ioDispatcher) { javaClass.classLoader?.getResource(ERROR_HTML)?.readText() - }?.let { + }?.let { errorPage -> call.respondText( - text = it, + text = errorPage, contentType = ContentType.Text.Html, status = HttpStatusCode.OK ) diff --git a/backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/RootRoute.kt b/backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/RootRoute.kt index 554e7f523..282cb0cd4 100644 --- a/backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/RootRoute.kt +++ b/backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/RootRoute.kt @@ -24,9 +24,9 @@ internal fun Route.root(ioDispatcher: CoroutineDispatcher) { withContext(ioDispatcher) { javaClass.classLoader?.getResource(INDEX_HTML)?.readText() - }?.let { + }?.let { rootPage -> call.respondText( - text = it, + text = rootPage, contentType = ContentType.Text.Html, status = HttpStatusCode.OK ) diff --git a/backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/VersionRoute.kt b/backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/VersionRoute.kt index 086e4fdec..49f8a7b39 100644 --- a/backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/VersionRoute.kt +++ b/backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/VersionRoute.kt @@ -19,9 +19,9 @@ internal fun Route.version(ioDispatcher: CoroutineDispatcher) { withContext(ioDispatcher) { javaClass.`package`?.implementationVersion - }?.let { + }?.let { version -> call.respondText( - text = "Version: $it", + text = "Version: $version", contentType = ContentType.Text.Plain, status = HttpStatusCode.OK )