Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Oztechan/CCC#3996] Move threading into Routes #3998

Merged
merged 2 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@

routing {
globalScope.launch(ioDispatcher) {
root()
currency(apiController)
version()
error()
root(ioDispatcher)
currency(apiController, ioDispatcher)
version(ioDispatcher)
error(ioDispatcher)

Check warning on line 38 in backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/module/APIModule.kt

View check run for this annotation

Codecov / codecov/patch

backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/module/APIModule.kt#L35-L38

Added lines #L35 - L38 were not covered by tests
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,30 @@
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)

Check warning on line 31 in backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/CurrencyRoute.kt

View check run for this annotation

Codecov / codecov/patch

backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/CurrencyRoute.kt#L30-L31

Added lines #L30 - L31 were not covered by tests
}?.let { exchangeRate ->
call.respond(
status = HttpStatusCode.OK,

Check warning on line 34 in backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/CurrencyRoute.kt

View check run for this annotation

Codecov / codecov/patch

backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/CurrencyRoute.kt#L33-L34

Added lines #L33 - L34 were not covered by tests
message = exchangeRate
)
} ?: call.respond(HttpStatusCode.NotFound)

Check warning on line 37 in backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/CurrencyRoute.kt

View check run for this annotation

Codecov / codecov/patch

backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/CurrencyRoute.kt#L37

Added line #L37 was not covered by tests
} ?: 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.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) {

Check warning on line 25 in backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/ErrorRoute.kt

View check run for this annotation

Codecov / codecov/patch

backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/ErrorRoute.kt#L25

Added line #L25 was not covered by tests
javaClass.classLoader?.getResource(ERROR_HTML)?.readText()
}?.let { errorPage ->
call.respondText(
text = resource,
text = errorPage,

Check warning on line 29 in backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/ErrorRoute.kt

View check run for this annotation

Codecov / codecov/patch

backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/ErrorRoute.kt#L29

Added line #L29 was not covered by tests
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.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) {

Check warning on line 25 in backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/RootRoute.kt

View check run for this annotation

Codecov / codecov/patch

backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/RootRoute.kt#L25

Added line #L25 was not covered by tests
javaClass.classLoader?.getResource(INDEX_HTML)?.readText()
}?.let { rootPage ->
call.respondText(
text = resource,
text = rootPage,

Check warning on line 29 in backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/RootRoute.kt

View check run for this annotation

Codecov / codecov/patch

backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/RootRoute.kt#L29

Added line #L29 was not covered by tests
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 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) {

Check warning on line 20 in backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/VersionRoute.kt

View check run for this annotation

Codecov / codecov/patch

backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/VersionRoute.kt#L20

Added line #L20 was not covered by tests
javaClass.`package`?.implementationVersion
}?.let { version ->
call.respondText(
text = "Version: $version",
contentType = ContentType.Text.Plain,
status = HttpStatusCode.OK

Check warning on line 26 in backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/VersionRoute.kt

View check run for this annotation

Codecov / codecov/patch

backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/VersionRoute.kt#L23-L26

Added lines #L23 - L26 were not covered by tests
)
} ?: call.respond(HttpStatusCode.NotFound)

Check warning on line 28 in backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/VersionRoute.kt

View check run for this annotation

Codecov / codecov/patch

backend/app/src/main/kotlin/com/oztechan/ccc/backend/app/routes/VersionRoute.kt#L28

Added line #L28 was not covered by tests
}
}