Skip to content

Commit

Permalink
[#3996] Move threading into Routes (#3998)
Browse files Browse the repository at this point in the history
* [#3996] Move threading into Routes

* [#3996] Move threading into Routes
  • Loading branch information
mustafaozhan authored Sep 29, 2024
1 parent e4e10f7 commit daf57dc
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,30 @@ 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" }

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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit daf57dc

Please sign in to comment.