pik4
pik4

Reputation: 1373

How to use different application.conf per environment in Akka-Http?

I am new in Akka and want to set up different application.conf files per environment (test, staging, prod, etc.).

I read the documentation from Akka but I couldn't find a way to do it.

May you know any proper way to have multiple configuration files?

Upvotes: 0

Views: 2578

Answers (4)

Pankaj Gupta
Pankaj Gupta

Reputation: 94

Akka uses the Typesafe Config Library.

The convenience method ConfigFactory.load() loads the following (first-listed are higher priority):

  1. system properties
  2. application.conf (all resources on classpath with this name)
  3. application.json (all resources on classpath with this name)
  4. application.properties (all resources on classpath with this name)
  5. reference.conf (all resources on classpath with this name)

So we usually create an application.conf file in /resources folder.

For different environments, we can create environment specific files like

  1. development.conf
  2. production.conf

Using includes, include application.conf at the beginning and specify environment specific configurations which will override the application.conf.

To use a specific environment force a different config source (e.g. from command line -Dconfig.resource=environment.conf)

Start your application using below command:

$ sbt run -Dconfig.resource=development.conf

Upvotes: 3

johanandren
johanandren

Reputation: 11479

The options for this aren't so much related to Akka as it is to the config library. Akka uses the default config resolution unless you give it a Config instance.

With no specific user code to manually select a specific file you have a few options. I think the most useful are the system properties config.resource to choose a file on the class path (inside the app jar for example), config.file to use a file from the file system.

Of course you may still have reasons to write your own code to select files as the other answers suggested.

More details in the config library docs: https://github.com/lightbend/config#standard-behavior

Upvotes: 0

C4stor
C4stor

Reputation: 8036

A possibility is have a single variable in application.conf which would be "confFile", based on an actual env like

confFile=${?CONF_FILE_NAME}

Then in your code, you load the correspondind file like this

val configFile = ConfigFactory.load().getString("confFile")
val appConf = ConfigFactory.load(configFile)

Upvotes: 0

Puneeth Reddy V
Puneeth Reddy V

Reputation: 1578

Here are couple ways to do it.

  1. Add tags with in a file itself like

prod-config : { YOUR_CONFIGURATION_FOR_PROD }

test-config : { YOUR_CONFIGURATION_TEST_ENV }

and so-on

  1. Make separate files for the each environment and rename it once after deploying them to specific environment. Say from application-prod.conf to application.conf

Upvotes: 0

Related Questions