Tuesday, May 19, 2015

Separate Configuration for Play Framework / Play2 production, testing and development environment

Working on Play Framework configuration is easy. In this article, I'll walk you through simple implementation where you are able to create a generic application.conf and a dedicated application.conf for each environment.

Objective: To be able to easily manage your Play2 application configuration separately for production, testing and development.

Solution: Override GlobalSettings onLoadConfig to point to a commong configuration and a fallback configuration specific for your current environment. Your code should look like below.

    public Configuration onLoadConfig(Configuration configuration, File file, ClassLoader classLoader, Mode mode) {

        String path = String.format("%s/conf/%s/application.conf", file.getPath(), mode.name().toLowerCase());

        Logger.info("Fallback configuration: {}", path);

        Config envConfig = ConfigFactory.parseFile(new File(path));
        Config baseConfig = configuration.getWrappedConfiguration().underlying();

        return new Configuration(baseConfig.withFallback(envConfig));

And finally, you should have setup your configurations under conf/ on your project.

You should have something like below.

conf/application.conf --> configurations common for all environment
conf/dev/application.conf --> configurations for development environment
conf/test/application.conf --> configurations for testing environment
conf/prod/application.conf --> configurations for production environment

That is all there is to do. This may not be perfect solution but hopefully you'll find this useful.

Leave a comment below for your questions or thoughts.


No comments:

Post a Comment