FeatureFlag is project to abstract the communication with Launch Darkly SDK.
Add git repository feature_flag
in your mix.exs
:
defp deps do
[
...,
# To use newest version
{:feature_flag, git: "https://github.com/solfacil/feature-flag.git", branch: "main"}
# To use a specific git tag version
{:feature_flag, git: "https://github.com/solfacil/feature-flag.git", tag: "x.x.x"}
]
end
For details about git repository in deps
, read Git options.
A client instance must be started for feature flag evaluation to work. We recommend use FeatureFlag.Server
to start a client instance on application startup and then feel free to use FeatureFlag.check/3
function to get feature flag value.
Below is a suggestion how to use the lib
Use config/<env>.exs
to set the Launch Darkly secret key.
config :my_app, :feature_flag, secret_key: "ld-secret-key"
Add FeatureFlag.Server
to your MyApp.Application
to start a client instance.
defmodule MyApp.Application do
@moduledoc false
use Application
def start(_type, _args) do
children = [
{FeatureFlag.Server, [secret_key: secret_key()]}
]
opts = [strategy: :one_for_one, name: MyApp.Supervisor]
Supervisor.start_link(children, opts)
end
defp secret_key, do: Application.get_env(:my_app, :feature_flag)[:secret_key]
end
If no
:secret_key
option are given, an error will be thrown.
Now you are ready to go. Use FeatureFlag.check/3
to evaluate given flag value for given user.
iex> user = %FeatureFlag.User{key: "123abc", name: "John", email: "[email protected]"}
iex> FeatureFlag.check("admin-report", user)
true