Skip to content

configuration-based feature flags for Elixir applications

License

Notifications You must be signed in to change notification settings

shipworthy/simple_feature_flags

Repository files navigation

README

SimpleFeatureFlags provides a simple way to enable or disable features, per-environment, using application configuration.

Code Example

Put your feature behind the "enabled?"

def compute_pi() do
  if SimpleFeatureFlags.enabled?(:new_algorithm) do
    compute_pi_new_algorithm()
  else
    3.14
  end
end

Enable the feature in some environments

config/runtime.exs

...
config :simple_feature_flags, :flags, %{
  ...
  features: %{
    new_algorithm: %{enabled_in: [:localhost, :staging]}
    ...
  }
  ...
}
...

Optionally, include configuration information in application log

lib/myapp/application.ex

defmodule MyApp.Application do

  require Logger
  use Application

  @impl true
  def start(_type, _args) do
    Logger.info(SimpleFeatureFlags.current_configuration_to_string())
    children =
    ...

Here is an example of the output:

  Current Deployment Environment: :test
  Features:
  - new_algorithm, enabled in [:localhost, :staging]
  - new_ui, enabled in [:localhost]

A complete configuration example

config/runtime.exs

...
config :simple_feature_flags, :flags, %{
  current_deployment_environment: :test,
  features: %{
    new_algorithm: %{enabled_in: [:localhost, :staging]},
    new_ui: %{enabled_in: [:localhost]}
  }
}
...

Enabling or disabling a feature

To turn a feature or or off, update configuration and restart your application. For example, to enable the new_algorithm feature in production, add :production to the list of enabled_in:

    ...
        new_algorithm: %{enabled_in: [:localhost, :staging, :production]}
    ...

Installation

The package can be installed by adding simple_feature_flags to your list of dependencies in mix.exs:

def deps do
  [
    {:simple_feature_flags, "~> 0.1"}
  ]
end

The docs can be found at https://hexdocs.pm/simple_feature_flags.

About

configuration-based feature flags for Elixir applications

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published