I have set up a server in Ktor with a Postgres database in Docker, but figured it would be useful to be able to develop the server locally without rebuilding the docker container each time.
In application.conf
I have
// ...
db {
jdbcUrl = ${DATABASE_URL}
dbDriver = "org.postgresql.Driver"
dbDriver = ${?DATABASE_DRIVER}
dbUser = ${DATABASE_USER}
dbPassword = ${DATABASE_PASSWORD}
}
and in my DatabaseFactory I have
object DatabaseFactory {
private val appConfig = HoconApplicationConfig(ConfigFactory.load())
private val dbUrl = appConfig.property("db.jdbcUrl").getString()
private val dbDriver = appConfig.property("db.dbDriver").getString()
private val dbUser = appConfig.property("db.dbUser").getString()
private val dbPassword = appConfig.property("db.dbPassword").getString()
fun init() {
Database.connect(hikari())
transaction {
val flyway = Flyway.configure().dataSource(dbUrl, dbUser, dbPassword).load()
flyway.migrate()
}
}
private fun hikari(): HikariDataSource {
val config = HikariConfig()
config.driverClassName = dbDriver
config.jdbcUrl = dbUrl
config.username = dbUser
config.password = dbPassword
config.maximumPoolSize = 3
config.isAutoCommit = false
config.transactionIsolation = "TRANSACTION_REPEATABLE_READ"
config.validate()
return HikariDataSource(config)
}
suspend fun <T> dbQuery(block: () -> T): T =
withContext(Dispatchers.IO) {
transaction { block() }
}
}
I have edited the Gradle run configuration with the following environment config:
DATABASE_URL=jdbc:h2:mem:default;DATABASE_DRIVER=org.h2.Driver;DATABASE_USER=test;DATABASE_PASSWORD=password
When I run the task I get this error: Could not resolve substitution to a value: ${DATABASE_URL}
, but if I set a breakpoint on the first line (private val appConfig
) and evaluate System.getenv("DATABASE_URL")
it is resolved to the correct value.
My questions are:
- Why does this not work?
- What is the best (or: a good) setup for developing the server without packing it in a container? Preferably without running the database in another container.