Skip to content

Commit

Permalink
Merge pull request #33 from crowdproj/wip-4
Browse files Browse the repository at this point in the history
cleanup: remove AppMode, AppStub, etc
  • Loading branch information
sszuev authored Apr 13, 2024
2 parents b527735 + 453318d commit 0bce150
Show file tree
Hide file tree
Showing 44 changed files with 152 additions and 1,413 deletions.
1 change: 0 additions & 1 deletion app-ktor/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ dependencies {
implementation(project(":common"))
implementation(project(":tts-lib"))
implementation(project(":tts-client"))
implementation(project(":specs"))
implementation(project(":core"))
implementation(project(":db-common"))
implementation(project(":db-pg"))
Expand Down
10 changes: 4 additions & 6 deletions app-ktor/src/main/kotlin/AppMain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ fun main(args: Array<String>) = io.ktor.server.jetty.EngineMain.main(args)
* - To disable authentication for debugging use `-DKEYCLOAK_DEBUG_AUTH=auth-uuid`
* - For run mode (prod, test, stub) use `-DRUN_MODE=mode`
*
* Example: `-DBOOTSTRAP_SERVERS=LOGS_KAFKA_HOSTS_IS_UNDEFINED -DKEYCLOAK_DEBUG_AUTH=c9a414f5-3f75-4494-b664-f4c8b33ff4e6 -DRUN_MODE=test`
* Example: `-DBOOTSTRAP_SERVERS=LOGS_KAFKA_HOSTS_IS_UNDEFINED -DKEYCLOAK_DEBUG_AUTH=c9a414f5-3f75-4494-b664-f4c8b33ff4e6 -DRUN_MODE=test -DDATA_DIRECTORY=/local/data`
*/
@KtorExperimentalLocationsAPI
@Suppress("unused")
Expand All @@ -86,7 +86,7 @@ fun Application.module(
) {
logger.info(printGeneralSettings(runConfig, keycloakConfig, tutorConfig))

val repositories = appRepositories()
val repositories = appRepositories(runConfig)

val port = environment.config.property("ktor.deployment.port").getString()

Expand Down Expand Up @@ -195,7 +195,7 @@ fun Application.module(
if (principal == null) {
call.respond(HttpStatusCode.Unauthorized)
} else {
call.respond(thymeleafContent(runConfig, tutorConfig, keycloakConfig, principal))
call.respond(thymeleafContent(tutorConfig, keycloakConfig, principal))
}
}
}
Expand All @@ -212,14 +212,13 @@ fun Application.module(
contextConfig = contextConfig,
)
get("/") {
call.respond(thymeleafContent(runConfig, tutorConfig, keycloakConfig, null))
call.respond(thymeleafContent(tutorConfig, keycloakConfig, null))
}
}
}
}

private fun thymeleafContent(
runConfig: RunConfig,
tutorConfig: TutorConfig,
keycloakConfig: KeycloakConfig,
principal: OAuthAccessTokenResponse.OAuth2?
Expand All @@ -239,7 +238,6 @@ private fun thymeleafContent(
)
}
val commonConfig = mapOf(
"runMode" to runConfig.modeString(),
"numberOfWordsToShow" to tutorConfig.numberOfWordsToShow.toString(),
"numberOfWordsPerStage" to tutorConfig.numberOfWordsPerStage.toString(),
"numberOfRightAnswers" to tutorConfig.numberOfRightAnswers.toString(),
Expand Down
23 changes: 15 additions & 8 deletions app-ktor/src/main/kotlin/Repositories.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
package com.gitlab.sszuev.flashcards

import com.gitlab.sszuev.flashcards.config.RunConfig
import com.gitlab.sszuev.flashcards.dbmem.MemDbCardRepository
import com.gitlab.sszuev.flashcards.dbmem.MemDbDictionaryRepository
import com.gitlab.sszuev.flashcards.dbpg.PgDbCardRepository
import com.gitlab.sszuev.flashcards.dbpg.PgDbDictionaryRepository
import com.gitlab.sszuev.flashcards.speaker.createDirectTTSResourceRepository
import com.gitlab.sszuev.flashcards.speaker.rabbitmq.RMQTTSResourceRepository

fun appRepositories() = AppRepositories(
prodTTSClientRepository = RMQTTSResourceRepository(),
testTTSClientRepository = createDirectTTSResourceRepository(),
prodCardRepository = PgDbCardRepository(),
testCardRepository = MemDbCardRepository(),
prodDictionaryRepository = PgDbDictionaryRepository(),
testDictionaryRepository = MemDbDictionaryRepository(),
)
fun appRepositories(config: RunConfig) = if (config.mode == RunConfig.Mode.TEST) {
AppRepositories(
cardRepository = MemDbCardRepository(),
dictionaryRepository = MemDbDictionaryRepository(),
ttsClientRepository = createDirectTTSResourceRepository(),
)
} else {
AppRepositories(
PgDbCardRepository(),
dictionaryRepository =
PgDbDictionaryRepository(),
ttsClientRepository = RMQTTSResourceRepository(),
)
}
9 changes: 4 additions & 5 deletions app-ktor/src/main/kotlin/config/RunConfig.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package com.gitlab.sszuev.flashcards.config

import com.gitlab.sszuev.flashcards.model.common.AppMode
import io.ktor.server.config.ApplicationConfig

data class RunConfig(
val auth: String,
val mode: AppMode,
val mode: Mode,
) {

constructor(config: ApplicationConfig) : this(
auth = config.property("run-config.debug-auth").getString(),
mode = AppMode.valueOf(config.property("run-config.mode").getString().uppercase()),
mode = Mode.valueOf(config.property("run-config.mode").getString().uppercase()),
)

fun modeString(): String {
return mode.name.lowercase()
enum class Mode {
PROD, TEST
}
}
7 changes: 5 additions & 2 deletions app-ktor/src/test/kotlin/TestUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import com.gitlab.sszuev.flashcards.config.KeycloakConfig
import com.gitlab.sszuev.flashcards.config.RunConfig
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.client.request.HttpRequestBuilder
import io.ktor.client.request.header
Expand All @@ -21,7 +22,7 @@ import io.ktor.server.testing.ApplicationTestBuilder
import io.ktor.server.testing.testApplication
import java.util.Base64

val testKeycloakConfig = KeycloakConfig(
private val testKeycloakConfig = KeycloakConfig(
authorizeAddress = "http://test-keycloak-server.ex",
accessTokenAddress = "http://test-keycloak-server.ex",
clientId = "test-client",
Expand All @@ -31,12 +32,14 @@ val testKeycloakConfig = KeycloakConfig(
realm = "test-realm",
)

private val testRunConfig = RunConfig(auth = "", mode = RunConfig.Mode.TEST)

@OptIn(KtorExperimentalLocationsAPI::class)
fun testSecuredApp(
block: suspend ApplicationTestBuilder.() -> Unit
) {
testApplication {
application { module(keycloakConfig = testKeycloakConfig) }
application { module(keycloakConfig = testKeycloakConfig, runConfig = testRunConfig) }
block()
}
}
Expand Down
11 changes: 0 additions & 11 deletions app-ktor/src/test/kotlin/api/controllers/CardControllerRunTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import com.gitlab.sszuev.flashcards.api.v1.models.CardWordExampleResource
import com.gitlab.sszuev.flashcards.api.v1.models.CardWordResource
import com.gitlab.sszuev.flashcards.api.v1.models.CreateCardRequest
import com.gitlab.sszuev.flashcards.api.v1.models.CreateCardResponse
import com.gitlab.sszuev.flashcards.api.v1.models.DebugResource
import com.gitlab.sszuev.flashcards.api.v1.models.DeleteCardRequest
import com.gitlab.sszuev.flashcards.api.v1.models.DeleteCardResponse
import com.gitlab.sszuev.flashcards.api.v1.models.GetAllCardsRequest
Expand All @@ -20,7 +19,6 @@ import com.gitlab.sszuev.flashcards.api.v1.models.LearnResource
import com.gitlab.sszuev.flashcards.api.v1.models.ResetCardRequest
import com.gitlab.sszuev.flashcards.api.v1.models.ResetCardResponse
import com.gitlab.sszuev.flashcards.api.v1.models.Result
import com.gitlab.sszuev.flashcards.api.v1.models.RunMode
import com.gitlab.sszuev.flashcards.api.v1.models.SearchCardsRequest
import com.gitlab.sszuev.flashcards.api.v1.models.SearchCardsResponse
import com.gitlab.sszuev.flashcards.api.v1.models.UpdateCardRequest
Expand Down Expand Up @@ -56,7 +54,6 @@ internal class CardControllerRunTest {
fun `test get-audio-resource`() = testSecuredApp {
val requestBody = GetAudioRequest(
requestId = "resource-request",
debug = DebugResource(mode = RunMode.TEST),
word = "xxx",
lang = "xx",
)
Expand All @@ -77,7 +74,6 @@ internal class CardControllerRunTest {
fun `test get-all-cards success`() = testSecuredApp {
val requestBody = GetAllCardsRequest(
requestId = "success-request",
debug = DebugResource(mode = RunMode.TEST),
dictionaryId = "2",
)
val response = testPost("/v1/api/cards/get-all", requestBody)
Expand All @@ -95,7 +91,6 @@ internal class CardControllerRunTest {
// deterministic dictionary,we know exactly what entity is coming back:
val requestBody = GetCardRequest(
requestId = "success-request",
debug = DebugResource(mode = RunMode.TEST),
cardId = "246",
)
val response = testPost("/v1/api/cards/get", requestBody)
Expand Down Expand Up @@ -137,7 +132,6 @@ internal class CardControllerRunTest {
)
val requestBody = CreateCardRequest(
requestId = "success-request",
debug = DebugResource(mode = RunMode.TEST),
card = CardResource(
dictionaryId = expectedCard.dictionaryId.asString(),
words = expectedCard.words.map {
Expand Down Expand Up @@ -170,7 +164,6 @@ internal class CardControllerRunTest {
val start = OffsetDateTime.now(ZoneOffset.UTC).minusMinutes(1)
val requestBody = UpdateCardRequest(
requestId = "success-request",
debug = DebugResource(mode = RunMode.TEST),
card = CardResource(
cardId = "1",
dictionaryId = "1",
Expand Down Expand Up @@ -211,7 +204,6 @@ internal class CardControllerRunTest {
fun `test search-cards success`() = testSecuredApp {
val requestBody = SearchCardsRequest(
requestId = "success-request",
debug = DebugResource(mode = RunMode.TEST),
dictionaryIds = listOf("1", "2"),
random = false,
length = 2,
Expand All @@ -230,7 +222,6 @@ internal class CardControllerRunTest {
fun `test learn-card success`() = testSecuredApp {
val requestBody = LearnCardsRequest(
requestId = "success-request",
debug = DebugResource(mode = RunMode.TEST),
cards = listOf(LearnResource(cardId = "246", details = mapOf("mosaic" to 42L)))
)
val response = testPost("/v1/api/cards/learn", requestBody)
Expand All @@ -247,7 +238,6 @@ internal class CardControllerRunTest {
fun `test reset-card success`() = testSecuredApp {
val requestBody = ResetCardRequest(
requestId = "success-request",
debug = DebugResource(mode = RunMode.TEST),
cardId = "246",
)
val response = testPost("/v1/api/cards/reset", requestBody)
Expand All @@ -264,7 +254,6 @@ internal class CardControllerRunTest {
fun `test delete-card success`() = testSecuredApp {
val requestBody = DeleteCardRequest(
requestId = "success-request",
debug = DebugResource(mode = RunMode.TEST),
cardId = "2",
)
val response = testPost("/v1/api/cards/delete", requestBody)
Expand Down
Loading

0 comments on commit 0bce150

Please sign in to comment.