• Stars
    star
    295
  • Rank 135,550 (Top 3 %)
  • Language
    Elixir
  • License
    MIT License
  • Created over 7 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Useful helper to read and use application configuration from environment variables.

Confex

Inline docs Hex.pm Downloads Latest Version License Build Status Coverage Status Ebert

Confex simplifies reading configuration at run-time with adapter-based system for fetch values from any source. It's inspired by Phoenix {:system, value} definition for HTTP port and have no external dependencies.

Installation

It's available on hex.pm and can be installed as project dependency:

  1. Add confex to your list of dependencies in mix.exs:

    def deps do
      [{:confex, "~> 3.5.0"}]
    end
  2. Ensure confex is started before your application either by adding it to applications list as shown below or by making sure you use extra_applications option instead of applications (this feature is available since Elixir 1.4 and enabled by default for new projects):

    def application do
      [applications: [:confex]]
    end

Usage

  1. Replace values with configuration tuples

    Define configuration in your config.exs:

    config :my_app, MyApp.MyQueue,
      queue: [
        name:        {:system, "OUT_QUEUE_NAME", "MyQueueOut"},
        error_name:  {:system, "OUT_ERROR_QUEUE_NAME", "MyQueueOut.Errors"},
        routing_key: {:system, "OUT_ROUTING_KEY", ""},
        durable:     {:system, "OUT_DURABLE", false},
        port:        {:system, :integer, "OUT_PORT", 1234},
      ]

    Configuration tuples examples:

    • var - any bare values will be left as-is.
    • {:system, "ENV_NAME", "default"} - read string from system environment or fallback to "default" if it is not set.
    • {:system, "ENV_NAME"} - same as above, but raise error if ENV_NAME is not set.

    Additionally you can cast string values to common types:

    • {:system, :string, "ENV_NAME", "default"} (string is a default type).
    • {:system, :string, "ENV_NAME"}.
    • {:system, :integer, "ENV_NAME", 123}.
    • {:system, :integer, "ENV_NAME"}.
    • {:system, :float, "ENV_NAME", 123.5}.
    • {:system, :float, "ENV_NAME"}.
    • {:system, :boolean, "ENV_NAME", true}.
    • {:system, :boolean, "ENV_NAME"}.
    • {:system, :atom, "ENV_NAME"}.
    • {:system, :atom, "ENV_NAME", :default}.
    • {:system, :module, "ENV_NAME"}.
    • {:system, :module, "ENV_NAME", MyDefault}.
    • {:system, :list, "ENV_NAME"}.
    • {:system, :list, "ENV_NAME", ["a", "b", "c"]}.
    • {:system, :charlist, "ENV_NAME"}.
    • {:system, :charlist, "ENV_NAME", 'default'}.

    :system can be replaced with a {:via, adapter} tuple, where adapter is a module that implements Confex.Adapter behaviour.

    Type can be replaced with {module, function, arguments} tuple, in this case Confex will use external function to resolve the type. Function must returns either {:ok, value} or {:error, reason :: String.t} tuple.

  2. Read configuration by replacing Application.fetch_env/2, Application.fetch_env!/2 and Application.get_env/3 calls with Confex functions

    Fetch string values:

    iex> Confex.fetch_env(:myapp, MyKey)
    {:ok, "abc"}

    Fetch integer values:

    iex> Confex.fetch_env(:myapp, MyIntKey)
    {:ok, 123}

    Fetch configuration from maps or keywords:

    iex> Confex.fetch_env(:myapp, MyIntKey)
    {:ok, [a: 123, b: "abc"]}

Integrating with Ecto

Ecto.Repo has a init/2 callback, you can use it with Confex to read environment variables. We used to have all our repos to look like this:

defmodule MyApp do
  use Ecto.Repo, otp_app: :my_app

  @doc """
  Dynamically loads the repository configuration from the environment variables.
  """
  def init(_, config) do
    url = System.get_env("DATABASE_URL")
    config = if url, do: [url: url] ++ config, else: Confex.Resolver.resolve!(config)

    unless config[:database] do
      raise "Set DB_NAME environment variable!"
    end

    {:ok, config}
  end
end

Integrating with Phoenix

Same for Phoenix, use init/2 callback of Phoenix.Endpoint:

defmodule MyApp.Web.Endpoint do

  # Some code here

  @doc """
  Dynamically loads configuration from the system environment
  on startup.

  It receives the endpoint configuration from the config files
  and must return the updated configuration.
  """
  def init(_type, config) do
    {:ok, config} = Confex.Resolver.resolve(config)

    unless config[:secret_key_base] do
      raise "Set SECRET_KEY environment variable!"
    end

    {:ok, config}
  end
end

Populating configuration at start-time

In case you want to keep using Application.get_env/2 and other methods to keep accessing configuration, you can resolve it one-time when application is started:

defmodule MyApp do
  use Application

  def start(_type, _args) do
    # Replace Application environment with resolved values
    Confex.resolve_env!(:my_app)

    # ...
  end
end

However, don't drink too much Kool-Aid. Direct calls to the Confex are more explicit and should be default way to go, you don't want your colleagues to waste their time finding out how that resolved value got into the configuration, right?

Using Confex macros

Confex is supplied with helper macros that allow to attach configuration to specific modules of your application.

defmodule Connection do
  use Confex, otp_app: :myapp
end

It will add config/0 function to Connection module that reads configuration at run-time for :myapp OTP application with key Connection.

You can add defaults by extending macro options:

defmodule Connection do
  use Confex,
    otp_app: :myapp,
    some_value: {:system, "ENV_NAME", "this_will_be_default value"}
end

If application environment contains values in Keyword or Map structs, default values will be recursively merged with application configuration.

We recommend to avoid using tuples without default values in this case, since config/0 calls will raise exceptions if they are not resolved.

You can validate configuration by overriding validate_config!/1 function, which will receive configuration and must return it back to caller function. It will be evaluated each time config/1 is called.

defmodule Connection do
  use Confex, otp_app: :myapp

  def validate_config!(config) do
    unless config[:password] do
      raise "Password is not set!"
    end

    config
  end
end

Adapters

Currently Confex supports two embedded adapters:

  • :system - read configuration from system environment;
  • :system_file - read file path from system environment and read configuration from this file. Useful when you want to resolve Docker, Swarm or Kubernetes secrets that are stored in files.

You can create adapter by implementing Confex.Adapter behaviour with your own logic.

Helpful links

More Repositories

1

sage

A dependency-free tool to run distributed transactions in Elixir, inspired by Sagas pattern.
Elixir
900
star
2

annon.api

Configurable API gateway that acts as a reverse proxy with a plugin system.
Elixir
329
star
3

ecto_mnesia

Ecto adapter for Mnesia Erlang term database.
Elixir
240
star
4

logger_json

JSON console backend for Elixir Logger.
Elixir
212
star
5

gandalf.api

Open-Source Decision Engine and Scoring Table for Big-Data.
PHP
98
star
6

multiverse

Elixir package that allows to add compatibility layers via API gateways.
Elixir
93
star
7

ecto_trail

EctoTrail allows to store Ecto changeset changes in a separate audit_log table.
Elixir
54
star
8

gandalf.web

Open-Source Decision Engine and Scoring Table for Big-Data.
JavaScript
45
star
9

renew

Mix task to create mix projects that builds into Docker containers.
Elixir
33
star
10

mouth

Simple adapter based SMS sending library
Elixir
29
star
11

rbmq

Simple API for spawning RabbitMQ Producers and Consumers.
Elixir
22
star
12

gen_task

Generic Task behavior that helps encapsulate errors and recover from them in classic GenStage workers.
Elixir
22
star
13

k8s-utils

Kubernetes utils for debugging our development or production environments
Shell
20
star
14

ael.api

Media content storage access control system based on Signed URL's.
Elixir
18
star
15

ecto_paging

Cursor-based pagination for Ecto.
Elixir
14
star
16

annon.web

Annon API Gateway Dashboard - manage API Gateway settings, review and replay requests from history.
JavaScript
11
star
17

bsoneach

Elixir package that applies a function to each document in a BSON file.
Elixir
9
star
18

jvalid

Json Scheme validation helper, that allows to store schemes in a separate files.
Elixir
8
star
19

man.api

Template Rendering Engine as a Service
Elixir
6
star
20

annon.infra

Infrastructure helpers for Annon API Gateway.
Shell
6
star
21

postboy.api

Asynchronous delivery service for Email or SMS messages.
Elixir
4
star
22

alpine-cassandra

Cassandra in Alpine Linux box.
Shell
4
star
23

eview

Nebo #15 Views for our Elixir API applications.
Elixir
4
star
24

mithril.api

Authentication and role management service.
Elixir
4
star
25

react-nebo15-events

Event Manager for application on React JS
JavaScript
4
star
26

lumen-intercom

Intercom service for lumen
PHP
3
star
27

annon.ktl

`annonktl` is a Annon API Gateway management CLI.
Elixir
3
star
28

man.web

Mรกn Templates Rendering Service
JavaScript
3
star
29

react-nebo15-validate

Validation module for React application by Nebo15
JavaScript
3
star
30

alpine-php

Linux Alpine container with PHP 5.6/7 and Mongo driver.
Shell
2
star
31

lumen-mandrill

PHP
2
star
32

alpine-erlang

Erlang container based on Alpine Linux with Kubernetes support.
Dockerfile
2
star
33

alpine-elixir

Alpine Box with Elixir and Erlang installed.
Dockerfile
2
star
34

react-nebo15-components

React components
JavaScript
2
star
35

alpine-postgre

PostgreSQL Docker Images based on Alpine Linux and with the same API as official repo has.
Shell
2
star
36

annon.status.web

Annon API Gateway Status Page - see statuses of the APIs from Annon API Gateway.
JavaScript
1
star
37

egndf

gndf.io client for Elixir
Elixir
1
star
38

d3-structure

๐Ÿจ Build your d3-graphics with data structures and one entrypoint!
JavaScript
1
star
39

mbank.lib.angular-compiled

Compiled version for Angular MBank libruary
JavaScript
1
star
40

nebo-error-messages

JavaScript
1
star
41

alpine-mongodb

Alpine container with MongoDB
Shell
1
star
42

react-boilerplate

Boilerplate for React Univeral application, that is using Redux, React-router and Express
JavaScript
1
star
43

d3-builder

JavaScript
1
star
44

gulp-by-path

Transform files grouped by file.relative
JavaScript
1
star
45

lumen-rest

PHP
1
star
46

nebo-localize

JavaScript
1
star
47

drunken-russian

Tasks manager for PHP and MongoDB. 100% alcohol free.
PHP
1
star
48

oxygen

Deprecated. Use Elixir Registry instead of this package.
Elixir
1
star
49

url-parser

URL Parser module
JavaScript
1
star
50

alpine-fluentd-elasticsearch

Alpine Box with Fluentd and ElasticSearch plugin
1
star
51

nebo-validate

JavaScript
1
star
52

onedayofmine.web

OneDayOfMine Project - it's our old and first unsupported startup, which we want to re-implement in future
PHP
1
star
53

javascript-library-boilerplate

Javascript Library Boilerplate
1
star
54

react-nebo15-currency-input

React currency input by Nebo15
JavaScript
1
star
55

credits4all.web.admin

Playground based on Parse.com
CSS
1
star
56

tokenizer.api

Card Tokenization Service
Elixir
1
star
57

dogstat

Elixir client for StatsD servers.
Elixir
1
star
58

alpine-postgre-backup

Backup system for PostgreSQL containers.
Shell
1
star
59

ci-utils

Continuous Integration scripts for different languages
1
star