Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typesafe Config Environment Variables

Attempting to use ${HOSTNAME} in a config file does not work! According to the documentation, config files should resolve environment variables as mentioned in the docs:

substitutions fall back to environment variables if they don't resolve in the config itself, so ${HOME} would work as you expect. Also, most configs have system properties merged in so you could use ${user.home}.

Is there a way to get hostname into the config file?

Reproduction
Add host.name=${HOSTNAME} to an application.conf file, then try and access it from anywhere. For example try adding

Logger.info(s"Hostname is ${current.configuration.getString("host.name").getOrElse("NOT-FOUND")}")

to the Global.scala.

Environment
This was run on a RHEL6 environment where echo $HOSTNAME produces precise32 so the environment variable exists, this is not the program hostname.

like image 445
tysonjh Avatar asked Sep 12 '13 17:09

tysonjh


People also ask

What is COM Typesafe config?

Typesafe Config allows you to store configuration into separated files and use include keyword to include configuration of those files.

What is ConfigFactory in Scala?

Configurations from a file By default, the ConfigFactory looks for a configuration file called application. conf. If willing to use a different configuration file (e.g.: another. conf), we just need to indicate a different file name and path to load (e.g.: ConfigFactory.

What is a config factory?

class ConfigFactory The configuration object factory instantiates a Config object for each configuration object name that is accessed and returns it to callers. Each configuration object gets a storage object injected, which is used for reading and writing the configuration data.


1 Answers

The solution seems to be passing in the hostname via a system property as -Dhost.name=$HOSTNAME or -Dhost.name=$(hostname). I'd imagine in windows it would be something else, but this works for *NIX environments.

Unless anyone can come up with something cleaner this will be the accepted answer.

like image 145
tysonjh Avatar answered Sep 26 '22 04:09

tysonjh