-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
266 lines (224 loc) · 7.06 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Config client for Spring Cloud Configuration server.
package config
import (
"errors"
"fmt"
"github.com/ContainX/go-utils/encoding"
"github.com/ContainX/go-utils/envsubst"
"github.com/ContainX/go-utils/httpclient"
"os"
"strings"
)
const (
// EnvConfigProfile holds the name of the environment variable (CONFIG_PROFILE)
// which is used during runtime lookups
EnvConfigProfile = "CONFIG_PROFILE"
// The configuration server URI environment variable (CONFIG_SERVER_URI)
// ex. http://host:8888
EnvConfigServerURI = "CONFIG_SERVER_URI"
// UriDefault is the default URI to the configuration server
UriDefault = "http://localhost:8888"
// ProfileDefault is the default profile
ProfileDefault = "default"
// LabelDefault is the initial SCM branch
LabelDefault = "master"
// Format is {uri}/{label}/{name}-{profile}.type
configPathFmt = "%s/%s/%s-%s.%s"
extJSON = "json"
extPROP = "properties"
extYAML = "yml"
)
var (
NameNotDeclaredErr = errors.New("Name must be declared")
FileNotDeclaredErr = errors.New("Filename must have a value")
)
type ConfigClient interface {
// Fetch queries the remote configuration service and populates the
// target value
Fetch(target interface{}) error
// FetchWithSubstitution fetches a remote config, substitutes environment variables
// and writes it to the target
FetchWithSubstitution(target interface{}) error
// Fetch queries the remote configuration service and populates
// a map of kv strings. This call flattens hierarchical values
// into flattened form. Example: datasource.mysql.user
FetchAsMap() (map[string]string, error)
// Fetch queries the remote configuration service and returns
// the result as a JSON string
FetchAsJSON() (string, error)
// Fetch queries the remote configuration service and returns
// the result as a YAML string
FetchAsYAML() (string, error)
// Fetch queries the remote configuration service and returns
// the result as a Properties string
FetchAsProperties() (string, error)
// Bootstrap returns a reference to the current bootstrap settings
Bootstrap() *Bootstrap
}
type client struct {
bootstrap *Bootstrap
}
// Bootstrap is the properties needed to fetch a remote configuration from
// spring cloud configuration server.
type Bootstrap struct {
// The URI of the remote server (default http://localhost:8888).
URI string `json:"uri"`
// Context is used as the base URI an optional /refresh endpoint which can by
// exposed to update an anonynmous function when configuration changes
Context string `json:"context"`
// Profile represents the default to use when fetching remote configuration (comma-separated).
// Default is "default".
//
// Note: During runtime the config client looks for the presence of an environment
// variable called CONFIG_PROFILE. If this is defined it overwrites this value.
Profile string `json:"profile"`
// Name of application used to fetch remote properties.
Name string `json:"name"`
// Label name to use to pull remote configuration properties. The default is set
// on the server (generally "master" for a git based server).
Label string `json:"label"`
// The username to use (HTTP Basic) when contacting the remote server.
Username string `json:"username,omitempty"`
// The password to use (HTTP Basic) when contacting the remote server.
Password string `json:"password,omitempty"`
}
// New creates a new ConfigClient based on b Bootstrap
// Error will be thrown if Name is not set
func New(b Bootstrap) (ConfigClient, error) {
if b.Name == "" {
return nil, NameNotDeclaredErr
}
b.URI = defaultVal(b.URI, UriDefault)
b.Profile = defaultVal(b.Profile, ProfileDefault)
b.Label = defaultVal(b.Label, LabelDefault)
client := &client{
bootstrap: &b,
}
return client, nil
}
func LoadFromFile(filename string) (ConfigClient, error) {
if filename == "" {
return nil, FileNotDeclaredErr
}
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
if encoder, err := encoding.NewEncoderFromFileExt(filename); err == nil {
config := &Bootstrap{}
if e := encoder.UnMarshal(f, config); e != nil {
return nil, e
}
config.PopulateDefaultsIfEmpty()
if config.Name == "" {
return nil, NameNotDeclaredErr
}
client := &client{
bootstrap: config,
}
return client, nil
} else {
return nil, err
}
}
func (b *Bootstrap) PopulateDefaultsIfEmpty() {
if b.URI == "" {
b.URI = UriDefault
}
if b.Profile == "" {
b.Profile = ProfileDefault
}
if b.Label == "" {
b.Label = LabelDefault
}
}
// defaultVal returns "d" if "s" aka source has an empty value
func defaultVal(s, d string) string {
if s == "" {
return d
}
return s
}
// Profile returns the resolved value of the profile. It will
// either use the CLUSTER_PROFILE env variable or fallback to
// the specified default value as a fallback
func (c *client) resolveProfile() string {
if v := os.Getenv(EnvConfigProfile); v != "" {
return v
}
return c.bootstrap.Profile
}
// URI returns the resolved value of the config server URI. It will
// either use the CONFIG_SERVER_URI env variable or fallback to
// the specified default value as a fallback
func (c *client) resolveURI() string {
if v := os.Getenv(EnvConfigServerURI); v != "" {
return v
}
return c.bootstrap.URI
}
// Fetch queries the remote configuration service and populates the
// target value
func (c *client) Fetch(target interface{}) error {
uri := c.buildRequestURI(extJSON)
resp := httpclient.Get(uri, target)
if resp.Error != nil {
return resp.Error
}
return nil
}
func (c *client) FetchWithSubstitution(target interface{}) error {
content, err := c.FetchAsYAML()
if err != nil {
return err
}
enc, _ := encoding.NewEncoder(encoding.YAML)
if err = enc.UnMarshalStr(content, target); err != nil {
return err
}
return nil
}
func (c *client) FetchAsMap() (map[string]string, error) {
uri := c.buildRequestURI(extPROP)
resp := httpclient.Get(uri, nil)
if resp.Error != nil {
return nil, resp.Error
}
m := map[string]string{}
for _, line := range strings.Split(resp.Content, "\n") {
kv := strings.Split(line, ":")
if len(kv) == 2 {
m[kv[0]] = strings.TrimSpace(kv[1])
}
}
return m, nil
}
func (c *client) FetchAsProperties() (string, error) {
return c.fetchAsString(extPROP)
}
func (c *client) FetchAsJSON() (string, error) {
return c.fetchAsString(extJSON)
}
func (c *client) FetchAsYAML() (string, error) {
return c.fetchAsString(extYAML)
}
func (c *client) fetchAsString(extension string) (string, error) {
uri := c.buildRequestURI(extension)
resp := httpclient.Get(uri, nil)
content := resp.Content
if resp.Error == nil {
content = envsubst.Substitute(strings.NewReader(content), false, func(s string) string {
return os.Getenv(s)
})
}
return content, resp.Error
}
func (c *client) Bootstrap() *Bootstrap {
return c.bootstrap
}
// Builds the final request URI for fetching a remote configuration.
// The returned URI is in the format of : {uri}/{label}/{name}-{profile}.json
func (c *client) buildRequestURI(t string) string {
return fmt.Sprintf(configPathFmt, c.resolveURI(), c.bootstrap.Label, c.bootstrap.Name, c.resolveProfile(), t)
}