Surgex v2.8.0 Surgex.Config

Application config getter, aka. Application.get_env on steroids.

This getter embodies the usage of {:system, "ENV_VAR_NAME"} convention for managing configs via system env vars on environments like Heroku. This convention is further extended here to allow type casting and falling back to defaults in such tuples.

Configs are assumed to live in app-specific parent scope, which is by default set to :surgex, but should be set to actual application name. Then, all Mix configs that will be fetched via Surgex.Config should be nested in that scope.

Check out Surgex.Config.Patch for an automated solution that allows to patch any Mix config with the capabilities of this module.

Usage

Here’s how your prod.exs may look:

config :surgex,
  config_app_name: :my_project

config :my_project,
  pool_size: {:system, "POOL_SIZE", :integer, 5},
  feature_x_enabled: {:system, "FEATURE_X_ENABLED", :boolean, false}

config :my_project, :api,
  url: {:system, "API_URL"},
  enabled: {:system, "API_ENABLED", :boolean, true}

In other env configs, like config.exs and dev.exs, you’ll usually follow the Elixir convention to simply fill the relevant keys with hard-coded values, optionally extracting them to *.secret.exs gitignored files to hold out-of-repo settings.

Having that, you can use either get/1 or get/2 to get specific config values in all envs.

Summary

Functions

Gets the config value for specified key

Gets the config value for specified scope and key

Parses a config value which may or may not be a system tuple

Functions

get(key)

Gets the config value for specified key.

Examples

iex> Mix.Config.persist(surgex: [feature_enabled: true])
[:surgex]
iex> Surgex.Config.get(:feature_enabled)
true
get(scope, key)

Gets the config value for specified scope and key.

Examples

iex> Mix.Config.persist(surgex: [api: [url: "example.com"]])
[:surgex]
iex> Surgex.Config.get(:api, :url)
"example.com"
parse(value)

Parses a config value which may or may not be a system tuple.

Examples

iex> Surgex.Config.parse("value")
"value"
iex> Surgex.Config.parse({:system, "NON_EXISTING_ENV_VAR"})
nil
iex> Surgex.Config.parse({:system, "NON_EXISTING_ENV_VAR", "default value"})
"default value"