|
|
|
@@ -1,42 +1,61 @@
|
|
|
|
|
package be.vandewalleh.features
|
|
|
|
|
|
|
|
|
|
import be.vandewalleh.auth.SimpleJWT
|
|
|
|
|
import com.sksamuel.hoplite.ConfigLoader
|
|
|
|
|
import com.sksamuel.hoplite.Masked
|
|
|
|
|
import com.zaxxer.hikari.HikariConfig
|
|
|
|
|
import com.zaxxer.hikari.HikariDataSource
|
|
|
|
|
import io.ktor.application.*
|
|
|
|
|
import org.kodein.di.Kodein
|
|
|
|
|
import org.kodein.di.generic.bind
|
|
|
|
|
import org.kodein.di.generic.instance
|
|
|
|
|
import org.kodein.di.generic.singleton
|
|
|
|
|
import java.util.concurrent.TimeUnit
|
|
|
|
|
import javax.sql.DataSource
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* [Kodein] configuration module
|
|
|
|
|
* [Kodein] controller module containing the app configuration
|
|
|
|
|
*/
|
|
|
|
|
lateinit var configurationModule: Kodein.Module
|
|
|
|
|
val configurationModule = Kodein.Module(name = "Configuration") {
|
|
|
|
|
bind() from singleton { ConfigLoader().loadConfigOrThrow<Config>("/application.yaml") }
|
|
|
|
|
bind<SimpleJWT>(tag = "auth") with singleton { configureAuthJwt(this.kodein) }
|
|
|
|
|
bind<SimpleJWT>(tag = "refresh") with singleton { configureRefreshJwt(this.kodein) }
|
|
|
|
|
bind<DataSource>() with singleton { configureDatasource(this.kodein) }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
data class DatabaseConfig(val host: String, val port: Int, val name: String, val username: String, val password: Masked)
|
|
|
|
|
data class ServerConfig(val host: String, val port: Int)
|
|
|
|
|
data class JwtConfig(val secret: Masked, val auth: JwtValidity, val refresh: JwtValidity)
|
|
|
|
|
data class JwtValidity(val validity: Long, val unit: TimeUnit)
|
|
|
|
|
data class Config(val database: DatabaseConfig, val server: ServerConfig, val jwt: JwtConfig)
|
|
|
|
|
|
|
|
|
|
val hikariConfig = HikariConfig().apply {
|
|
|
|
|
jdbcUrl = "jdbc:mariadb://$host:$port/$name"
|
|
|
|
|
username = this@with.property("database.user").getString()
|
|
|
|
|
password = this@with.property("database.password").getString()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HikariDataSource(hikariConfig)
|
|
|
|
|
private fun configureAuthJwt(kodein: Kodein): SimpleJWT {
|
|
|
|
|
val config by kodein.instance<Config>()
|
|
|
|
|
val jwtSecret = config.jwt.secret
|
|
|
|
|
val authConfig = config.jwt.auth
|
|
|
|
|
return SimpleJWT(jwtSecret.value, authConfig.validity, authConfig.unit)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun configureRefreshJwt(kodein: Kodein): SimpleJWT {
|
|
|
|
|
val config by kodein.instance<Config>()
|
|
|
|
|
val jwtSecret = config.jwt.secret
|
|
|
|
|
val refreshConfig = config.jwt.auth
|
|
|
|
|
return SimpleJWT(jwtSecret.value, refreshConfig.validity, refreshConfig.unit)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun configureDatasource(kodein: Kodein): DataSource {
|
|
|
|
|
val config by kodein.instance<Config>()
|
|
|
|
|
val dbConfig = config.database
|
|
|
|
|
|
|
|
|
|
val host = dbConfig.host
|
|
|
|
|
val port = dbConfig.port
|
|
|
|
|
val name = dbConfig.name
|
|
|
|
|
|
|
|
|
|
val hikariConfig = HikariConfig().apply {
|
|
|
|
|
jdbcUrl = "jdbc:mariadb://$host:$port/$name"
|
|
|
|
|
username = dbConfig.username
|
|
|
|
|
password = dbConfig.password.value
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val jwtSecret = environment.config.property("jwt.secret").getString()
|
|
|
|
|
val authSimpleJwt = SimpleJWT(jwtSecret, 1, TimeUnit.HOURS)
|
|
|
|
|
val refreshSimpleJwt = SimpleJWT(jwtSecret, 7, TimeUnit.DAYS)
|
|
|
|
|
|
|
|
|
|
configurationModule = Kodein.Module("Configuration") {
|
|
|
|
|
bind<DataSource>() with instance(dataSource)
|
|
|
|
|
bind<SimpleJWT>(tag = "auth") with instance(authSimpleJwt)
|
|
|
|
|
bind<SimpleJWT>(tag = "refresh") with instance(refreshSimpleJwt)
|
|
|
|
|
}
|
|
|
|
|
return HikariDataSource(hikariConfig)
|
|
|
|
|
}
|