Add kodein separations
This commit is contained in:
parent
eea0151ff0
commit
913e3dfc93
21
api/src/Dependencies.kt
Normal file
21
api/src/Dependencies.kt
Normal file
@ -0,0 +1,21 @@
|
||||
package be.vandewalleh
|
||||
|
||||
import be.vandewalleh.features.configurationModule
|
||||
import be.vandewalleh.migrations.Migration
|
||||
import be.vandewalleh.services.serviceModule
|
||||
import me.liuwj.ktorm.database.*
|
||||
import org.kodein.di.Kodein
|
||||
import org.kodein.di.generic.bind
|
||||
import org.kodein.di.generic.instance
|
||||
import org.kodein.di.generic.singleton
|
||||
import org.slf4j.Logger
|
||||
import org.slf4j.LoggerFactory
|
||||
import javax.sql.DataSource
|
||||
|
||||
val mainModule = Kodein.Module("main") {
|
||||
import(serviceModule)
|
||||
import(configurationModule)
|
||||
bind<Logger>() with singleton { LoggerFactory.getLogger("Application") }
|
||||
bind<Migration>() with singleton { Migration(this.kodein) }
|
||||
bind<Database>() with singleton { Database.connect(this.instance<DataSource>()) }
|
||||
}
|
||||
@ -20,15 +20,13 @@ import org.slf4j.Logger
|
||||
import org.slf4j.LoggerFactory
|
||||
import javax.sql.DataSource
|
||||
|
||||
val kodein = Kodein {
|
||||
import(serviceModule)
|
||||
import(configurationModule)
|
||||
bind<Logger>() with singleton { LoggerFactory.getLogger("Application") }
|
||||
bind<Migration>() with singleton { Migration(this.kodein) }
|
||||
bind<Database>() with singleton { Database.connect(this.instance<DataSource>()) }
|
||||
}
|
||||
|
||||
fun main() {
|
||||
|
||||
val kodein = Kodein{
|
||||
import(mainModule)
|
||||
}
|
||||
|
||||
val config by kodein.instance<Config>()
|
||||
val logger by kodein.instance<Logger>()
|
||||
logger.info("Running application with configuration $config")
|
||||
@ -43,7 +41,7 @@ fun serve(kodein: Kodein) {
|
||||
val logger by kodein.instance<Logger>()
|
||||
val env = applicationEngineEnvironment {
|
||||
module {
|
||||
module()
|
||||
module(kodein)
|
||||
}
|
||||
log = logger
|
||||
connector {
|
||||
@ -55,7 +53,7 @@ fun serve(kodein: Kodein) {
|
||||
}
|
||||
|
||||
|
||||
fun Application.module() {
|
||||
fun Application.module(kodein: Kodein) {
|
||||
loadFeatures(kodein)
|
||||
|
||||
routing {
|
||||
|
||||
@ -1,12 +1,12 @@
|
||||
package be.vandewalleh.auth
|
||||
|
||||
import be.vandewalleh.kodein
|
||||
import io.ktor.application.*
|
||||
import io.ktor.auth.*
|
||||
import io.ktor.auth.jwt.*
|
||||
import org.kodein.di.Kodein
|
||||
import org.kodein.di.generic.instance
|
||||
|
||||
fun Application.authenticationModule() {
|
||||
fun Application.authenticationModule(kodein: Kodein) {
|
||||
install(Authentication) {
|
||||
jwt {
|
||||
val simpleJwt by kodein.instance<SimpleJWT>(tag = "auth")
|
||||
|
||||
@ -11,6 +11,6 @@ fun Application.loadFeatures(kodein: Kodein) {
|
||||
corsFeature()
|
||||
}
|
||||
contentNegotiationFeature()
|
||||
authenticationModule()
|
||||
authenticationModule(kodein)
|
||||
handleErrors()
|
||||
}
|
||||
|
||||
@ -1,18 +1,16 @@
|
||||
package services
|
||||
|
||||
import be.vandewalleh.mainModule
|
||||
import be.vandewalleh.migrations.Migration
|
||||
import be.vandewalleh.services.NotesService
|
||||
import be.vandewalleh.services.UserService
|
||||
import com.zaxxer.hikari.HikariConfig
|
||||
import com.zaxxer.hikari.HikariDataSource
|
||||
import me.liuwj.ktorm.database.*
|
||||
import org.amshove.kluent.*
|
||||
import org.junit.jupiter.api.*
|
||||
import org.kodein.di.Kodein
|
||||
import org.kodein.di.generic.bind
|
||||
import org.kodein.di.generic.instance
|
||||
import org.kodein.di.generic.singleton
|
||||
import org.testcontainers.containers.MariaDBContainer
|
||||
import utils.KMariadbContainer
|
||||
import utils.testContainerDataSource
|
||||
import javax.sql.DataSource
|
||||
|
||||
|
||||
@ -20,28 +18,11 @@ import javax.sql.DataSource
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation::class)
|
||||
class UserServiceTest {
|
||||
|
||||
class KMariadbContainer : MariaDBContainer<KMariadbContainer>()
|
||||
|
||||
private val mariadb = KMariadbContainer().apply {
|
||||
this.start()
|
||||
}
|
||||
|
||||
private val hikariConfig = HikariConfig().apply {
|
||||
jdbcUrl = mariadb.jdbcUrl
|
||||
username = mariadb.username
|
||||
password = mariadb.password
|
||||
}
|
||||
|
||||
private val dataSource = HikariDataSource(hikariConfig)
|
||||
|
||||
private val db = Database.Companion.connect(dataSource)
|
||||
private val mariadb = KMariadbContainer().apply { start() }
|
||||
|
||||
private val kodein = Kodein {
|
||||
bind<DataSource>() with singleton { dataSource }
|
||||
bind<Database>() with singleton { db }
|
||||
bind<Migration>() with singleton { Migration(this.kodein) }
|
||||
bind<UserService>() with singleton { UserService(this.kodein) }
|
||||
bind<NotesService>() with singleton { NotesService(this.kodein) }
|
||||
import(mainModule, allowOverride = true)
|
||||
bind<DataSource>(overrides = true) with singleton { testContainerDataSource(mariadb) }
|
||||
}
|
||||
|
||||
private val migration by kodein.instance<Migration>()
|
||||
|
||||
18
api/test/utils/TestContainers.kt
Normal file
18
api/test/utils/TestContainers.kt
Normal file
@ -0,0 +1,18 @@
|
||||
package utils
|
||||
|
||||
import com.zaxxer.hikari.HikariConfig
|
||||
import com.zaxxer.hikari.HikariDataSource
|
||||
import org.testcontainers.containers.MariaDBContainer
|
||||
import javax.sql.DataSource
|
||||
|
||||
class KMariadbContainer : MariaDBContainer<KMariadbContainer>()
|
||||
|
||||
fun testContainerDataSource(container: KMariadbContainer): DataSource {
|
||||
val hikariConfig = HikariConfig().apply {
|
||||
jdbcUrl = container.jdbcUrl
|
||||
username = container.username
|
||||
password = container.password
|
||||
}
|
||||
|
||||
return HikariDataSource(hikariConfig)
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user