cover/Elixir.DaProductApp.FeatureFlags.html

1 defmodule DaProductApp.FeatureFlags do
2 @moduledoc """
3 Feature flag system for gradual migration and A/B testing.
4
5 This module provides a centralized way to enable/disable features
6 during development and production deployments, supporting the
7 gradual migration strategy.
8 """
9
10 @doc """
11 Check if a feature flag is enabled.
12
13 Priority order:
14 1. Environment variables (FEATURE_<FLAG_NAME>=true/false)
15 2. Application config
16 3. Database settings (when implemented)
17 4. Default value
18 """
19
:-(
def enabled?(flag_name, default \\ false) do
20 flag_name
21
:-(
|> to_string()
22 |> String.upcase()
23
:-(
|> check_feature_flag(default)
24 end
25
26 @doc """
27 Enable a feature flag programmatically.
28 Useful for testing and dynamic feature control.
29 """
30 def enable(flag_name) do
31
:-(
Application.put_env(:da_product_app, :feature_flags,
32 Keyword.put(get_feature_flags(), flag_name, true)
33 )
34 end
35
36 @doc """
37 Disable a feature flag programmatically.
38 """
39 def disable(flag_name) do
40
:-(
Application.put_env(:da_product_app, :feature_flags,
41 Keyword.put(get_feature_flags(), flag_name, false)
42 )
43 end
44
45 @doc """
46 Get all currently configured feature flags.
47 """
48 def list_flags do
49
:-(
get_feature_flags()
50 end
51
52 # Available feature flags for our gradual migration
53 @available_flags %{
54 # Data source flags
55 use_database_settings: false,
56 use_database_international_payments: false,
57 use_database_settlements: false,
58
59 # Performance flags
60 use_redis_caching: false,
61 enable_transaction_type_filtering: true,
62
63 # Audit and logging flags
64 enable_api_request_logging: true,
65 enable_response_logging: true,
66 enable_log_viewer: false,
67
68 # Fallback and reliability flags
69 enable_database_fallback: true,
70 enable_file_logging_on_db_failure: true,
71
72 # UI and monitoring flags
73 show_real_time_metrics: false,
74 enable_advanced_monitoring: false
75 }
76
77 @doc """
78 Get default configuration for all available flags.
79 """
80
:-(
def default_flags, do: @available_flags
81
82 # Private functions
83
84 defp check_feature_flag(flag_name, default) do
85 # 1. Check environment variable first
86
:-(
case System.get_env("FEATURE_#{flag_name}") do
87
:-(
"true" -> true
88
:-(
"false" -> false
89 nil ->
90 # 2. Check application config
91
:-(
case get_feature_flags()[String.to_atom(String.downcase(flag_name))] do
92
:-(
nil -> default
93
:-(
value -> value
94 end
95
:-(
_ -> default
96 end
97 end
98
99 defp get_feature_flags do
100
:-(
Application.get_env(:da_product_app, :feature_flags, [])
101 end
102 end
Line Hits Source