Configure database inside api
This commit is contained in:
parent
49a3d47653
commit
37fa8cd17e
@ -7,3 +7,11 @@ ktor {
|
||||
modules = [ be.vandewalleh.NotesApplicationKt.module ]
|
||||
}
|
||||
}
|
||||
|
||||
database {
|
||||
host = "127.0.0.1"
|
||||
port = "3306"
|
||||
name = "Notes"
|
||||
user = "test"
|
||||
password = "test"
|
||||
}
|
||||
@ -2,17 +2,22 @@ package be.vandewalleh
|
||||
|
||||
import be.vandewalleh.controllers.KodeinController
|
||||
import be.vandewalleh.controllers.controllerModule
|
||||
import be.vandewalleh.features.configurationFeature
|
||||
import be.vandewalleh.features.configurationModule
|
||||
import be.vandewalleh.features.features
|
||||
import com.fasterxml.jackson.databind.SerializationFeature
|
||||
import io.ktor.application.Application
|
||||
import io.ktor.application.install
|
||||
import io.ktor.application.log
|
||||
import io.ktor.features.CORS
|
||||
import io.ktor.features.ContentNegotiation
|
||||
import io.ktor.jackson.jackson
|
||||
import io.ktor.locations.Locations
|
||||
import io.ktor.routing.routing
|
||||
import org.kodein.di.Kodein
|
||||
import org.kodein.di.description
|
||||
import org.kodein.di.generic.instance
|
||||
import javax.sql.DataSource
|
||||
|
||||
lateinit var kodein: Kodein
|
||||
|
||||
@ -22,8 +27,11 @@ fun Application.module() {
|
||||
|
||||
kodein = Kodein {
|
||||
import(controllerModule)
|
||||
import(configurationModule)
|
||||
}
|
||||
|
||||
log.debug(kodein.container.tree.bindings.description())
|
||||
|
||||
val controllers: Set<KodeinController> by kodein.instance()
|
||||
|
||||
routing {
|
||||
|
||||
33
api/src/features/ConfigurationFeature.kt
Normal file
33
api/src/features/ConfigurationFeature.kt
Normal file
@ -0,0 +1,33 @@
|
||||
package be.vandewalleh.features
|
||||
|
||||
import io.ktor.application.Application
|
||||
import org.kodein.di.Kodein
|
||||
import org.kodein.di.generic.bind
|
||||
import org.kodein.di.generic.instance
|
||||
import org.mariadb.jdbc.MariaDbDataSource
|
||||
import javax.sql.DataSource
|
||||
|
||||
/**
|
||||
* [Kodein] configuration module
|
||||
*/
|
||||
lateinit var configurationModule: Kodein.Module
|
||||
|
||||
fun Application.configurationFeature() {
|
||||
val dataSource: DataSource = with(environment.config) {
|
||||
val host = property("database.host").getString()
|
||||
val port = property("database.port").getString()
|
||||
val name = property("database.name").getString()
|
||||
val user = property("database.user").getString()
|
||||
val password = property("database.password").getString()
|
||||
|
||||
val url = "jdbc:mariadb://$host:$port/$name"
|
||||
|
||||
MariaDbDataSource(url).apply {
|
||||
this.user = user
|
||||
setPassword(password)
|
||||
}
|
||||
}
|
||||
configurationModule = Kodein.Module("Configuration") {
|
||||
bind<DataSource>() with instance(dataSource)
|
||||
}
|
||||
}
|
||||
@ -6,4 +6,5 @@ fun Application.features() {
|
||||
locationFeature()
|
||||
corsFeature()
|
||||
contentNegotiationFeature()
|
||||
configurationFeature()
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user