Dynamic configuration in your erlang application


License
BSD-1-Clause

Documentation

Dōteki

Copyright (c) 2015, 2016 Bots Unit

Version: 1.0.0

Authors: Gregoire Lejeune (gregoire.lejeune@botsunit.com).

Hex.pm version Hex.pm downloads License

About

Dōteki allow you to use dynamic configuration in your erlang application.

Using environment variables

In your configuration file, you can declare an explicit usage of en environment variable.

To do so, you can use and atom, prefixed by env. :

Erlang :


[
  {app, [
    {key, 'env.ENV_VAR'}
  ]}
].

Elixir :


use Mix.Config

config :app,
  key: :"env.ENV_VAR"

You can also use a tuple where the first element is the atom system or env, the second element is a string giving the environment variable name and the third element is the default value to use if the environment variable is not set. The third element is optional and if the environment variable is not set, Doteki will return undefined.

Erlang :


[
  {app, [
    {key, {system, "ENV_VAR"}}
  ]}
].

Elixir :


use Mix.Config

config :app,
  key: {:system, "ENV_VAR"}

With those notations, when you get the value for [app, key], Dōteki will return the value of the environment variable ENV_VAR.

Erlang :


export ENV_VAR=123
1> doteki:get_env([app, key]).
% => 123

Elixir :


export ENV_VAR=123
iex(1)> Doteki.get_env([:app, :key])
# => 123

You can also overwrite every value in the configuration, using an environment variable. Example: if you have the following configuration :

Erlang :


[
  {app, [
    {key1, 'env.ENV_VAR'},
    {key2, "hello world"}
  ]}
].

Elixir :


use Mix.Config

config :app,
  key1: :"env.ENV_VAR",
  key2: "hello world"

If you set an environment variable APP_KEY2 and get the value for [app, key2] Dōteki will return defined by APP_KEY2.

WARNING : The environment variables overwriting a key by its path are interpreted before those declares in the configuration. So if you export APP_KEY1, the value returned for [app, key1] will be the value exported for this variable.

When Dōteki find an environment it will try to interpret it.

Type export Erlang Elixir
Atom "hello"
"hello:atom"
"hello:term"
hello :hello
String "\"hello world\""
"hello world:string"
"hello world" 'hello world'
Binary "<<\"hola mundo\">>"
"hola mundo:binary"
<<"hola mundo">> "hola mundo"
Integer "123"
"123:integer"
123 123
Float "123.45"
"123.45:integer"
123.45 123.45
Tuple "{a, \"b\", 123}"
"{a, \"b\", 123}:term"
{a, "b", 123} {:a, 'b', 123}
List "[a, \"b\", 123]"
"[a, \"b\", 123]:term"
[a, "b", 123] [:a, 'b', 123]

In some case, you can want to force Dōteki to interpret a value. For example, if you declare an environment variable MY_NUMBER and want it to be interpreted as a string, you can use the following notation :

Erlang :


{system, "MY_NUMBER", as, string}

Elixir :


{:system, "MY_NUMBER", :as, :string}

You can also add a default value in case where MY_NUMBER is not set :

Erlang :


{system, "MY_NUMBER", as, string, "1234"}

Elixir :


{:system, "MY_NUMBER", :as, :string, '1234'}

Using functions

If you want to use a function, you can use those notations :

  • {fn, Module, Function}

  • {fn, Module, Function, [Args]}

  • {fn, Module, Function, Arity, [Args]}

  • {'fun', Module, Function}

  • {'fun', Module, Function, [Args]}

  • {'fun', Module, Function, Arity, [Args]}

  • 'fun.Module:Function'

  • {'fun.Module:Function', [Args]}

  • {'fun.Module:Function/Arity', [Args]}

  • 'fn.Module:Function'

  • {'fn.Module:Function', [Args]}

  • {'fn.Module:Function/Arity', [Args]}

Erlang :


[
  {app, [
    {key, {fn, module, function, 2, [{system, "ENV_VAR"}, 2]}}
  ]}
].

Elixir :


use Mix.Config

config :app,
  key: {:fn, Module, :function, 2, [{:system, "ENV_VAR"}, 2]}

Licence

Copyright (c) 2015, 2016, Bots Unit
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Modules

doteki