-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfeatureflags.go
46 lines (39 loc) · 974 Bytes
/
featureflags.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package featureflags
import (
"context"
"errors"
ffclient "github.com/thomaspoignant/go-feature-flag"
"github.com/thomaspoignant/go-feature-flag/ffcontext"
"go.uber.org/fx"
)
var Module = fx.Module(
"featureflags",
fx.Invoke(HookClient),
)
func HookClient(lifecycle fx.Lifecycle, config Config) {
lifecycle.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
return ffclient.Init(ffclient.Config{
PollingInterval: config.PollingInterval,
Retriever: config,
FileFormat: "json",
})
},
OnStop: func(ctx context.Context) error {
ffclient.Close()
return nil
},
})
}
func forKey(key string) (map[string]any, error) {
flags := ffclient.AllFlagsState(ffcontext.NewEvaluationContext(key))
if !flags.IsValid() {
return nil, errors.New("invalid flags state")
}
allFlags := flags.GetFlags()
m := make(map[string]any, len(allFlags))
for name, state := range allFlags {
m[name] = state.Value
}
return m, nil
}