Skip to content

Commit

Permalink
make database caching configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
sschaeffner committed Oct 16, 2023
1 parent 96e73a4 commit a8cddda
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 11 deletions.
7 changes: 6 additions & 1 deletion app/src/main/kotlin/cloud/fabX/fabXaccess/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,14 @@ val config = Config.fromEnv()
val app = DI {
import(domainModule)
import(webModule)
import(persistenceModule)
import(loggingModule)

if (config.dbCaching) {
import(cachedPersistenceModule)
} else {
import(persistenceModule)
}

bindConstant(tag = "port") { config.port }

bindConstant(tag = "dburl") { config.dbUrl }
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/kotlin/cloud/fabX/fabXaccess/common/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ data class Config(
val dbUrl: String,
val dbUser: String,
val dbPassword: String,
val dbCaching: Boolean,
val jwtIssuer: String,
val jwtAudience: String,
val jwtHMAC256Secret: String,
Expand All @@ -22,6 +23,7 @@ data class Config(
companion object {
fun fromEnv(): Config {
val port = readEnvInt("PORT", 80)
val dbCaching = readEnvBoolean("DATABASE_CACHING", false)
val jwtIssuer = readEnvString("JWT_ISSUER", "localhost")
val jwtAudience = readEnvString("JWT_AUDIENCE", "localhost")
val jwtHMAC256Secret = readEnvString("JWT_HMAC256_SECRET", Random.nextBytes(32).decodeToString())
Expand All @@ -46,6 +48,7 @@ data class Config(
dbUrl,
dbUser,
dbPassword,
dbCaching,
jwtIssuer,
jwtAudience,
jwtHMAC256Secret,
Expand All @@ -68,6 +71,7 @@ data class Config(
dbUrl,
dbUser,
dbPassword,
dbCaching,
jwtIssuer,
jwtAudience,
jwtHMAC256Secret,
Expand Down
4 changes: 2 additions & 2 deletions app/src/test/kotlin/cloud/fabX/fabXaccess/common/TestApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import cloud.fabX.fabXaccess.common.model.UserId
import cloud.fabX.fabXaccess.common.model.newCorrelationId
import cloud.fabX.fabXaccess.domainModule
import cloud.fabX.fabXaccess.loggingModule
import cloud.fabX.fabXaccess.persistenceModule
import cloud.fabX.fabXaccess.cachedPersistenceModule
import cloud.fabX.fabXaccess.user.model.IsAdminChanged
import cloud.fabX.fabXaccess.user.model.UserCreated
import cloud.fabX.fabXaccess.user.model.UserRepository
Expand Down Expand Up @@ -76,7 +76,7 @@ private fun testSetup(): WebApp {
val testApp = DI {
import(domainModule)
import(webModule)
import(persistenceModule)
import(cachedPersistenceModule)
import(loggingModule)

bindInstance(overrides = true) { dbPool }
Expand Down
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ services:
FABX_DB_URL: jdbc:postgresql://postgres/postgres
FABX_DB_USER: postgres
FABX_DB_PASSWORD: postgrespassword
# must be false when running multiple instances
DATABASE_CACHING: true

postgres:
image: postgres:16
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ package cloud.fabX.fabXaccess

import cloud.fabX.fabXaccess.common.LiquibaseMigrationHandler
import cloud.fabX.fabXaccess.device.infrastructure.CachedDeviceDatabaseRepository
import cloud.fabX.fabXaccess.device.infrastructure.DeviceDatabaseRepository
import cloud.fabX.fabXaccess.qualification.infrastructure.CachedQualificationDatabaseRepository
import cloud.fabX.fabXaccess.qualification.infrastructure.QualificationDatabaseRepository
import cloud.fabX.fabXaccess.tool.infrastructure.CachedToolDatabaseRepository
import cloud.fabX.fabXaccess.tool.infrastructure.ToolDatabaseRepository
import cloud.fabX.fabXaccess.user.infrastructure.CachedUserDatabaseRepository
import cloud.fabX.fabXaccess.user.infrastructure.UserDatabaseRepository
import cloud.fabX.fabXaccess.user.infrastructure.WebauthnInMemoryRepository
import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource
Expand All @@ -13,11 +17,22 @@ import org.kodein.di.DI
import org.kodein.di.bindSingleton
import org.kodein.di.instance

val persistenceModule = DI.Module("persistence") {
bindSingleton { CachedDeviceDatabaseRepository(instance()) }
bindSingleton { CachedQualificationDatabaseRepository(instance()) }
bindSingleton { CachedToolDatabaseRepository(instance(), instance()) }
bindSingleton { CachedUserDatabaseRepository(instance()) }
val persistenceModule = makePersistenceModule(cached = false)
val cachedPersistenceModule = makePersistenceModule(cached = true)

fun makePersistenceModule(cached: Boolean): DI.Module = DI.Module("persistence") {
if (cached) {
bindSingleton { CachedDeviceDatabaseRepository(instance()) }
bindSingleton { CachedQualificationDatabaseRepository(instance()) }
bindSingleton { CachedToolDatabaseRepository(instance(), instance()) }
bindSingleton { CachedUserDatabaseRepository(instance()) }
} else {
bindSingleton { DeviceDatabaseRepository(instance()) }
bindSingleton { QualificationDatabaseRepository(instance()) }
bindSingleton { ToolDatabaseRepository(instance(), instance()) }
bindSingleton { UserDatabaseRepository(instance()) }
}

bindSingleton { WebauthnInMemoryRepository() }

bindSingleton {
Expand Down Expand Up @@ -48,4 +63,4 @@ val persistenceModule = DI.Module("persistence") {
bindSingleton {
Database.connect(instance<HikariDataSource>())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cloud.fabX.fabXaccess.common.infrastructure
import cloud.fabX.fabXaccess.PersistenceApp
import cloud.fabX.fabXaccess.device.infrastructure.DeviceDatabaseRepository
import cloud.fabX.fabXaccess.loggingModule
import cloud.fabX.fabXaccess.persistenceModule
import cloud.fabX.fabXaccess.cachedPersistenceModule
import cloud.fabX.fabXaccess.qualification.infrastructure.QualificationDatabaseRepository
import cloud.fabX.fabXaccess.tool.infrastructure.ToolDatabaseRepository
import cloud.fabX.fabXaccess.user.infrastructure.UserDatabaseRepository
Expand Down Expand Up @@ -52,7 +52,7 @@ internal fun withTestApp(
}

val testApp = DI {
import(persistenceModule)
import(cachedPersistenceModule)
import(loggingModule)

bindInstance(overrides = true) { dbPool }
Expand Down

0 comments on commit a8cddda

Please sign in to comment.