This repository was archived by the owner on Feb 25, 2025. It is now read-only.
forked from thomaspoignant/go-feature-flag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
165 lines (143 loc) · 4.96 KB
/
config.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package ffclient
import (
"context"
"errors"
"log"
"time"
"github.com/thomaspoignant/go-feature-flag/ffnotifier"
"github.com/thomaspoignant/go-feature-flag/internal"
"github.com/thomaspoignant/go-feature-flag/internal/notifier"
)
// Config is the configuration of go-feature-flag.
// You should also have a retriever to specify where to read the flags file.
type Config struct {
// PollingInterval (optional) Poll every X time
// The minimum possible is 1 second
// Default: 60 seconds
PollingInterval time.Duration
// Logger (optional) logger use by the library
// Default: No log
Logger *log.Logger
// Context (optional) used to call other services (HTTP, S3 ...)
// Default: context.Background()
Context context.Context
// Environment (optional), can be checked in feature flag rules
// Default: ""
Environment string
// Retriever is the component in charge to retrieve your flag file
Retriever Retriever
// Notifiers (optional) is the list of notifiers called when a flag change
Notifiers []NotifierConfig
// FileFormat (optional) is the format of the file to retrieve (available YAML, TOML and JSON)
// Default: YAML
FileFormat string
// DataExporter (optional) is the configuration where we store how we should output the flags variations results
DataExporter DataExporter
// StartWithRetrieverError (optional) If true, the SDK will start even if we did not get any flags from the retriever.
// It will serve only default values until the retriever returns the flags.
// The init method will not return any error if the flag file is unreachable.
// Default: false
StartWithRetrieverError bool
// Offline (optional) If true, the SDK will not try to retrieve the flag file and will not export any data.
// No notification will be send neither.
// Default: false
Offline bool
}
// GetRetriever returns a retriever.FlagRetriever configure with the retriever available in the config.
func (c *Config) GetRetriever() (Retriever, error) {
if c.Retriever == nil {
return nil, errors.New("no retriever in the configuration, impossible to get the flags")
}
return c.Retriever, nil
}
// NotifierConfig is the interface for your notifiers.
// You can use as notifier a WebhookConfig
//
// Notifiers: []ffclient.NotifierConfig{
// &ffclient.WebhookConfig{
// EndpointURL: " https://example.com/hook",
// Secret: "Secret",
// Meta: map[string]string{
// "app.name": "my app",
// },
// },
// // ...
// }
type NotifierConfig interface {
GetNotifier(config Config) (ffnotifier.Notifier, error)
}
// WebhookConfig is the configuration of your webhook.
// we will call this URL with a POST request with the following format
//
// {
// "meta":{
// "hostname": "server01"
// },
// "flags":{
// "deleted": {
// "test-flag": {
// "rule": "key eq \"random-key\"",
// "percentage": 100,
// "true": true,
// "false": false,
// "default": false
// }
// },
// "added": {
// "test-flag3": {
// "percentage": 5,
// "true": "test",
// "false": "false",
// "default": "default"
// }
// },
// "updated": {
// "test-flag2": {
// "old_value": {
// "rule": "key eq \"not-a-key\"",
// "percentage": 100,
// "true": true,
// "false": false,
// "default": false
// },
// "new_value": {
// "disable": true,
// "rule": "key eq \"not-a-key\"",
// "percentage": 100,
// "true": true,
// "false": false,
// "default": false
// }
// }
// }
// }
// }
type WebhookConfig struct {
// Deprecated: use EndpointURL instead
PayloadURL string
// EndpointURL is the URL where we gonna do the POST Request.
EndpointURL string
Secret string // Secret used to sign your request body.
Meta map[string]string // Meta information that you want to send to your webhook (not mandatory)
}
// GetNotifier convert the configuration in a Notifier struct
func (w *WebhookConfig) GetNotifier(config Config) (ffnotifier.Notifier, error) {
url := w.EndpointURL
// remove this if when EndpointURL will be removed
if url == "" {
url = w.PayloadURL
}
notifier, err := notifier.NewWebhookNotifier(
config.Logger,
internal.DefaultHTTPClient(),
url, w.Secret, w.Meta)
return ¬ifier, err
}
type SlackNotifier struct {
SlackWebhookURL string
}
// GetNotifier convert the configuration in a Notifier struct
func (w *SlackNotifier) GetNotifier(config Config) (ffnotifier.Notifier, error) {
notifier := notifier.NewSlackNotifier(config.Logger, internal.DefaultHTTPClient(), w.SlackWebhookURL)
return ¬ifier, nil
}