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

Azolo
Hey everyone, I just released WebSockex which is a Elixir WebSocket client. WebSockex strives to work as a OTP special process, be RFC6...
New
alisinabh
Hey everyone i’ve developed a library for Jalaali calendar for elixir which supports converting Gregorian dates to Jalaali and vice vers...
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
Crowdhailer
The latest release of Ace (0.10.0) includes serving content over HTTP/2. I have started writing a webserver to teach my self more about...
New
archan937
It is a well-know topic within the Elixir community: “To mock or not to mock? :)” Every alchemist probably has his / her own opinion con...
New
woutdp
Hi! I wanted to introduce my latest project LiveSvelte. It allows you to render Svelte inside LiveView with end-to-end reactivity. It’s ...
New
jakub-zawislak
Hi everyone, I’m coming from the Symfony (PHP) framework. I like Phoenix, but it has a one thing that was build much better in the Symfo...
New
mbuhot
EctoJob A transactional job queue built with Ecto, PostgreSQL and GenStage Available on Hex.pm: ecto_job | Hex Docs: API Reference — ec...
New
mischov
import Meeseeks.CSS html = HTTPoison.get!("https://news.ycombinator.com/").body for story <- Meeseeks.all(html, css("tr.athing")) do...
New
New

Other popular topics Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Sub Categories:

We're in Beta

About us Mission Statement