markmark206

markmark206

SimpleFeatureFlags – turn features on in some environments, via configuration

simple_feature_flags is a tiny package that lets you turn features on or off based on which environment (e.g. localhost, staging, production) your app is running in — without needing a database or UI, just using configuration.

See the documentation (Simple Feature Flags v0.1.4 — Documentation) for more details, but here is a summary / example:

1. Install

Add the package to your dependencies:

mix.exs:

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

2. Is the feature enabled?

In your code, check if a feature (e.g. :new_algorithm) is enabled with:

 SimpleFeatureFlags.enabled?(:new_algorithm)

Example:

  def compute_pi() do
    if SimpleFeatureFlags.enabled?(:new_algorithm) do
      # Use the new, exciting algorithm.
      compute_pi_new_algorithm()
    else
      # Use the old, boring algorithm.
      3.14
    end
  end

The function returns true or false, based on whether :new_algorithm is enabled in the current environment.

3. Configuration

In your configuration file (e.g. config/runtime.exs), tell the package:

  • a. in which environment the code is currently running, and
  • b. in which environments each feature (e.g. :new_algorithm) is enabled.

Example:

config/runtime.exs

# Determine the environment in which the code is currently running in (e.g. `:localhost`, `:staging`, `:production`).
# This example uses a dedicated environment variable, set to an appropriate value.
# Note: in a Phoenix service, you might be able deduce the deployment environment from `PHX_HOST` or some such.
current_deployment_environment =
  System.get_env("DEPLOYMENT_ENVIRONMENT")
  |> case do
    nil -> raise "DEPLOYMENT_ENVIRONMENT is not defined"
    env -> String.to_atom(env)
  end

# Tell the package the name of the environment where the code is running,
# and the environments in which the feature is enabled.
config :simple_feature_flags, :flags, %{
  current_deployment_environment: current_deployment_environment,
  features: %{
    new_algorithm: %{enabled_in: [:localhost, :staging]},
    new_ui: %{enabled_in: [:staging]}
  }
}

4. Change where features are enabled

To change where features are enabled, just update your configs (e.g. add :production to the enabled_in: list for :new_algorithm) and redeploy. That’s it – no database, no UI, no runtime toggle logic.

5. This is it!

There are a couple of other (optional) ergonomics features (check the docs for known_deployment_environments and current_configuration_to_string()), but other than that, this is it!

The package is intentionally simple – its interface is just enabled?() and current_configuration_to_string() – but I have been using this package extensively and I found it quite useful for quickly toggling features, with minimal overhead, and so I thought I’d share it here, in case it saves someone time.

If you have questions or requests, please let me know here or via an issue in the github repo.

Thank you!

Here is the package on hexdocs: Simple Feature Flags v0.1.4 — Documentation
And here is the github repo: GitHub - shipworthy/simple_feature_flags: configuration-based feature flags for Elixir applications

Where Next?

Popular in Libraries Top

scohen
Lexical Lexical is a next-generation language server for the Elixir programming language. Features Context aware code completion As-you...
New
tompave
Hello there, I would like to share a feature toggles library (AKA feature flags) I’ve been working on. The main package is FunWithFlags...
New
devonestes
Introducing assertions, the library that helps you write really great test assertions! GitHub: https://github.com/devonestes/assertions ...
New
sasajuric
I’d like to announce a small library called boundaries. This is an experimental project which explores the idea of enforcing boundaries ...
New
mbuhot
Leverage Open Api 3.0 (Swagger) to document, test, validate and explore your Plug and Phoenix APIs. Generate and serve a JSON Open API ...
New
riverrun
I’ve just released version 3 of Comeonin, a password hashing library. The following small changes have been made: changes to the NIF c...
New
asiniy
Hey there! I wrote a download elixir package which does exactly what its name about - an easy way to download files. I saw solutions ...
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
ahamez
Hi everyone, I’ve been working on this protobuf library for 3 years. We use it in the company I work for, EasyMile, to communicate with ...
New
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

Sub Categories:

We're in Beta

About us Mission Statement