3
votes

I've had a look around but it's not very clear to me how I can configure a set of environment specific variables for my Play framework application.

As an example, I would like to use an in memory database like h2 for local development but when I move to production or my pre-production environment I would like to be connecting to a postgres database.

How do I configure my app so that it will use the variables relevant to the environment it is being deployed to? This is a Scala Play app.

3

3 Answers

6
votes

One option (as documented in the excellent play docs), is to specify conf files during app startup.

Using -Dconfig.resource will search for an alternative configuration file in the application classpath (you usually provide these alternative configuration files into your application conf/ directory before packaging). Play will look into conf/ so you don’t have to add conf/.

$ /path/to/bin/<project-name> -Dconfig.resource=prod.conf

Using -Dconfig.file you can specify an environment specific configuration file not packaged into the application artifacts:

$ start -Dconfig.file=/opt/conf/prod.conf

Using -Dconfig.url you can also specify a configuration file to be loaded from any URL:

$ start -Dconfig.url=http://conf.mycompany.com/conf/prod.conf

Note that you can always reference the original configuration file in a new prod.conf file using the include directive, such as:

include "application.conf"

key.to.override=blah
0
votes

You can have Puppet or some similar tools to generate the needed parameters in environment.conf and place in a dedicated directory.

Then in application.conf, at the end of the file, have this:

include "file:///[your directory...]/environment.conf"

to override any testing or local values (e.g.DB parameteres) listed above

0
votes

You can use different configuration files by overriding onLoadConfig method of the Global object like:

object Global extends GlobalSettings {
   override def onLoadConfig(config: Configuration, path: File, classloader: ClassLoader, mode: Mode.Mode): Configuration = {
    val fileName = s"application.${mode.toString.toLowerCase}.conf"
    config ++ Configuration(ConfigFactory.load(fileName))
  }
}

This way you have 'application.test.conf' for test mode and 'application.dev.conf' for develop mode whereas you can use another config file in production via '-Dconfig.file' parameter.