I am working on a monitoring app and I have to pass in at startup some initial configuration which consists of a couple of lists of IP addresses. What's the OTP way to pass this data to the application - through the .app file or is there any other general accepted way?
-
I guess nobody answered this question yet, which is "Is there an OTP way to pass parameters to an starting Erlang Node?". I'm also fighting with this issue, because I want to have a node that can be programmatically started with the env variables I want. Three approaches come to mind: 1) Modify the ".config" or "*.app" file before starting the node; 2) Pass these parameters as "-Application Key Value" flags during the node creation; 3) Leave the application in a blank state, waiting for configuration messages in order to start its function. I'm still not sure what to use, though... – pedromanoel Jan 26 '12 at 20:07
4 Answers
Use an Erlang configuration file:
A configuration file contains values for configuration parameters for the applications in the system. The erl command line argument -config Name tells the system to use data in the system configuration file Name.config.
Configuration parameter values in the configuration file will override the values in the application resource files (see app(4)). The values in the configuration file can be overridden by command line flags (see erl(1)).
The value of a configuration parameter is retrieved by calling application:get_env/1,2.
If you need to override them at runtime, you can use application:set_env/3
, but with care.

- 30,570
- 21
- 75
- 112
-
3What qualifies as "with care"? Could you elaborate a little, please? – pedromanoel Jan 27 '12 at 17:10
-
1"With care" -> An environment variable set at runtime is reset to its initial value when the node is restarted. It's also hard to track changes made at runtime if they are not reflected in a version-controlled config file. – Roberto Aloi Jan 15 '15 at 20:58
you can handle configuration in several ways. here a link to another stackoverflow topic
IMHO i suggest .app file, or you can use a configuration file (here another link to stackoverflow topic)

- 1
- 1

- 3,215
- 4
- 38
- 48
I would create a name gen_server process that has a list of ip addresses as it's state. In the init of the server a predefined list would be read from a file using file:consult and used as the initial state of the server. To get the list of ip addresses from this named gen_server, a handle_call(get_ip, _From, State) needs to be implemented.
This way you prevent shared global state, which gives you great Erlang karma, and have a better starting point for added functionality like runtime ip address changes.

- 6,316
- 9
- 38
- 61
Use a file in which you have your data as erlang terms. However you need to protect the file. Reading from the file at start up use: file:consult/1
. If modification of the file will occur by the user or system administrator, use the following functions to protect or refuse access to the file:
-include_lib("kernel/include/file.hrl"). protect_file(File)-> {_,File_info} = file:read_file_info(File), file:write_file_info(File,File_info#file_info{access = read,mode = 33060}). unprotect_file(File)-> {_,File_info} = file:read_file_info(File), file:write_file_info(File,File_info#file_info{access = read_write,mode = 33206}).
Use function protect_file/1
to make the file read-only. If you need to make the file writable then modify using unprotect_file/1
. A file with erlang terms is easier because you do not need parsing.You could also write you configurations as JSON
objects or XML
data into a file. In summary, using a file for all you configs will be better managed by your application and those who interact with it.
An example is the ejabberd.cfg file, the config file for ejabberd server. Its easiest with a file with erlang terms because you can comment here and there for the system administrator to see other available options about a certain configuration.

- 7,736
- 3
- 47
- 86
-
After checking the ejabberd source code, I also thought about using this method. Why has this been downvoted? – naiquevin Dec 14 '12 at 05:42
-
i wonder too, i have used this method for long. `Ejabberd` uses this as well, even `Riak` e.t.c Whats wrong with people ? – Muzaaya Joshua Dec 14 '12 at 10:14
-
-1, because protecting the file doesn't actually address the OP's question. – Roger Lipscombe Oct 23 '13 at 07:33