An OTP application typically gets its configuration using the
operating system environment (os:getenv/1) and/or application
environment (application:get_env/2). Both are used to allow
configuration to be easily overridden by the OS environment, falling
back to defaults provided by the application environment.
The result of calling os:getenv/1 is always a string() (or false
if not present in the environment). For example, running the following:
KERNEL_HTTP_PORT=8080 erl1> os:getenv("KERNEL_HTTP_PORT").
"8080"Whereas using the application environment:
erl -kernel http_port 80802> application:get_env(kernel, http_port).
{ok,8080}Envy is a simple wrapper over both os:getenv and
application:get_env which does a couple of things:
-
Convert the different types naturally returned by
os:getenvandapplication:get_envto the type that you are expecting. -
Prefix any
os_envwith the name of the application, preventing any environment clashes.
As an example, starting erlang with envy running:
cd envy
KERNEL_HTTP_PORT=8080 make shellThe following calls to to_integer/3 demonstrate obtaining
configuration either from the OS or application environment:
1> application:set_env(kernel, http_port, 1080).
2> envy:to_integer(kernel, http_port, [app_env, os_env, {default, 80}]).
1080
3> envy:to_integer(kernel, http_port, [os_env, app_env, {default, 80}]).
8080When using the os_env strategy envy automatically prefixes the
application name to the environment variable lookup, so that
KERNEL_HTTP_PORT is used when http_port is the key.
Envy has helper wrappers for to_integer/3, to_float/3,
to_atom/3, to_boolean/3, to_binary/3 and to_list/3.