I am trying to read the environment variables in typesafe config in scala, configuring the slick database. Here is what I tried
remote_test_db = {
dataSourceClass = "slick.jdbc.DatabaseUrlDataSource"
properties = {
driver = "org.postgresql.Driver"
url = ${?REMOTE_TEST_DB_URL}
user = ${?REMOTE_TEST_DB_USERNAME}
password = ${?REMOTE_TEST_DB_PASSWORD}
}
connectionPool = disabled
keepAliveConnection = true
}
But I get this error:
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.flyhomes.mls_pull.dump.MlsProvider.main(MlsProvider.scala)
Caused by: com.typesafe.config.ConfigException$NotResolved: need to Config#resolve(), see the API docs for Config#resolve(); substitution not resolved: ConfigConcatenation(${?REMOTE_TEST_DB_USERNAME})
at com.typesafe.config.impl.ConfigConcatenation.notResolved(ConfigConcatenation.java:51)
at com.typesafe.config.impl.ConfigConcatenation.valueType(ConfigConcatenation.java:58)
at slick.util.ConfigExtensionMethods$$anonfun$slick$util$ConfigExtensionMethods$$toProps$1$1.apply(GlobalConfig.scala:71)
at slick.util.ConfigExtensionMethods$$anonfun$slick$util$ConfigExtensionMethods$$toProps$1$1.apply(GlobalConfig.scala:69)
at scala.collection.Iterator$class.foreach(Iterator.scala:893)
at scala.collection.AbstractIterator.foreach(Iterator.scala:1336)
at scala.collection.IterableLike$class.foreach(IterableLike.scala:72)
at scala.collection.AbstractIterable.foreach(Iterable.scala:54)
at slick.util.ConfigExtensionMethods$.slick$util$ConfigExtensionMethods$$toProps$1(GlobalConfig.scala:69)
at slick.util.ConfigExtensionMethods$.toProperties$extension(GlobalConfig.scala:78)
at slick.util.ConfigExtensionMethods$.getPropertiesOr$extension(GlobalConfig.scala:64)
at slick.util.ConfigExtensionMethods$.getPropertiesOpt$extension(GlobalConfig.scala:84)
at slick.jdbc.DataSourceJdbcDataSource$.forConfig(JdbcDataSource.scala:90)
at slick.jdbc.DataSourceJdbcDataSource$.forConfig(JdbcDataSource.scala:86)
at slick.jdbc.JdbcDataSource$.forConfig(JdbcDataSource.scala:48)
at slick.jdbc.JdbcBackend$DatabaseFactoryDef$class.forConfig(JdbcBackend.scala:288)
at slick.jdbc.JdbcBackend$$anon$3.forConfig(JdbcBackend.scala:33)
at com.flyhomes.mls_pull.Databases$.remoteTestDb$lzycompute(Databases.scala:21)
at com.flyhomes.mls_pull.Databases$.remoteTestDb(Databases.scala:21)
at com.flyhomes.mls_pull.dump.MlsProvider$.<init>(MlsProvider.scala:18)
at com.flyhomes.mls_pull.dump.MlsProvider$.<clinit>(MlsProvider.scala)
... 1 more
How do I access the environment variables here?
From the exception it seems that you need to call resolve - line 3: need to Config#resolve()
. This will actually substitute every variable. Maybe this will help:
val config = ??? // load config here
config.resolve() // force the substitution of variables
Quoted from the docs of the class:
Resolving substitutions
Substitutions are the ${foo.bar} syntax in config files, described in the specification. Resolving substitutions replaces these references with real values.
Before using a Config it's necessary to call resolve() to handle substitutions (though ConfigFactory.load() and similar methods will do the resolve for you already).