[erlang-questions] How do I move this into a config file?

Garrett Smith g@REDACTED
Sat Aug 27 18:31:36 CEST 2011


On Thu, Aug 25, 2011 at 10:02 PM, Matthew Hillsborough
<matthew.hillsborough@REDACTED> wrote:
> Hi everyone,
>
> I have some hard coded defines in one of my modules that looks like the
> following:
>
> -ifdef(debug).
>
> -define(FOOBAR, "DEBUG_FOOBAR").
>
> -else.
>
> -define(FOOBAR, "PROD_FOOBAR").
>
> -endif.
>
> I want to be able to deploy this to multiple servers with a config file
> using rebar. So I suppose this is a two part question:
>
> How does one move the above into an external configuration file and then
> read it back into the application?

There's a canonical way to do this in Erlang that you can (should) use.

It will help to do a little reading on "applications":

http://www.erlang.org/doc/design_principles/applications.html

In particular, look at "Configuring an Application".

Also, check out the get_env and get_all_env functions in the application module:

http://www.erlang.org/doc/man/application.html

The gist:

- Define you release specific configuration in a *.config file
- When running erl, specify "-config FILE" as an option (note, do not
include the .config suffix, but path is okay)
- From your OTP application, use application:get_env/1 to read your config

The only catch here that can be confusing (IMO) is
application:get_env/1, which looks up values associated with the
application that the process is running under. If process wasn't
started by an application (application:start/1) you'll get undefined.
Just an FYI that can throw you.

Garrett



More information about the erlang-questions mailing list