5

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?

6
  • Are you sure this is the conf you tried? The exception seems to indicate you have ${?REMOTE_TEST_DB_USERNAME}"l" somewhere Commented Mar 29, 2017 at 7:34
  • ${?REMOTE_TEST_DB_URL} simply vanishes if there's no value for REMOTE_TEST_DB_URL. Have you the default value somewhere? If you don't have use ${REMOTE_TEST_DB_USERNAME} or define default value before like this: github.com/typesafehub/… Commented Mar 29, 2017 at 7:37
  • @Leonard I didn't get your solution. Commented Mar 29, 2017 at 7:41
  • @Cyrille That was from another run, the error is still there, made the edit. Commented Mar 29, 2017 at 7:41
  • 1
    @PrabhjotRai Could you remove ? from ${?REMOTE_TEST_DB_USERNAME}? Is error message changed? Commented Mar 29, 2017 at 7:46

1 Answer 1

8

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.