Configure database inside api
This commit is contained in:
parent
49a3d47653
commit
37fa8cd17e
@ -7,3 +7,11 @@ ktor {
|
|||||||
modules = [ be.vandewalleh.NotesApplicationKt.module ]
|
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.KodeinController
|
||||||
import be.vandewalleh.controllers.controllerModule
|
import be.vandewalleh.controllers.controllerModule
|
||||||
|
import be.vandewalleh.features.configurationFeature
|
||||||
|
import be.vandewalleh.features.configurationModule
|
||||||
import be.vandewalleh.features.features
|
import be.vandewalleh.features.features
|
||||||
import com.fasterxml.jackson.databind.SerializationFeature
|
import com.fasterxml.jackson.databind.SerializationFeature
|
||||||
import io.ktor.application.Application
|
import io.ktor.application.Application
|
||||||
import io.ktor.application.install
|
import io.ktor.application.install
|
||||||
|
import io.ktor.application.log
|
||||||
import io.ktor.features.CORS
|
import io.ktor.features.CORS
|
||||||
import io.ktor.features.ContentNegotiation
|
import io.ktor.features.ContentNegotiation
|
||||||
import io.ktor.jackson.jackson
|
import io.ktor.jackson.jackson
|
||||||
import io.ktor.locations.Locations
|
import io.ktor.locations.Locations
|
||||||
import io.ktor.routing.routing
|
import io.ktor.routing.routing
|
||||||
import org.kodein.di.Kodein
|
import org.kodein.di.Kodein
|
||||||
|
import org.kodein.di.description
|
||||||
import org.kodein.di.generic.instance
|
import org.kodein.di.generic.instance
|
||||||
|
import javax.sql.DataSource
|
||||||
|
|
||||||
lateinit var kodein: Kodein
|
lateinit var kodein: Kodein
|
||||||
|
|
||||||
@ -22,8 +27,11 @@ fun Application.module() {
|
|||||||
|
|
||||||
kodein = Kodein {
|
kodein = Kodein {
|
||||||
import(controllerModule)
|
import(controllerModule)
|
||||||
|
import(configurationModule)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.debug(kodein.container.tree.bindings.description())
|
||||||
|
|
||||||
val controllers: Set<KodeinController> by kodein.instance()
|
val controllers: Set<KodeinController> by kodein.instance()
|
||||||
|
|
||||||
routing {
|
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()
|
locationFeature()
|
||||||
corsFeature()
|
corsFeature()
|
||||||
contentNegotiationFeature()
|
contentNegotiationFeature()
|
||||||
|
configurationFeature()
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user