Add kodein separations
This commit is contained in:
@@ -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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user