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..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 @@ -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 { exchangeRate -> + call.respond( + status = HttpStatusCode.OK, + 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 0df232a3e..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 @@ -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 { errorPage -> call.respondText( - text = resource, + 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 0c7d6673f..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 @@ -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 { rootPage -> call.respondText( - text = resource, + 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 6a9fc312a..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 @@ -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 { version -> + call.respondText( + text = "Version: $version", + contentType = ContentType.Text.Plain, + status = HttpStatusCode.OK + ) + } ?: call.respond(HttpStatusCode.NotFound) } }