-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
247 lines (210 loc) · 7.71 KB
/
index.js
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
var consul = require ('consul');
var events = require('events');
var _defaultLogger = {
debug: function(m) { console.log("12factor@debug: " +m); },
info: function(m) { console.log("12factor@info: " +m); },
warn: function(m) { console.log("12factor@warn: " +m); },
error: function(m) { console.log("12factor@error: " +m); },
}
function id (x) { return x }
function readBool (x) {
if(typeof x == "string" && x.toLowerCase() == "false") {
return false;
}
return !!x;
}
var readerOf = function (value) {
switch(typeof value) {
case 'number':
return parseInt;
case 'boolean':
return readBool
}
return id
}
function _Value(opts) {
this.opts = opts;
opts.reader = opts.reader || readerOf(this.opts.default)
this.apply = function (client, path, key, target, builderOpts) {
var varname = path.concat([key]).join('.');
if (! this.opts.optional) {
builderOpts.required[varname] = true;
}
var prefixed = path.concat([key]);
if (builderOpts.envPrefix) {
prefixed.unshift(builderOpts.envPrefix);
}
prefixed = prefixed.join('_').toUpperCase();
var envValue = process.env[prefixed];
if (envValue !== undefined) {
if (this.opts.sensitive) {
builderOpts.log.info("Setting " + varname + " to env var " + prefixed);
} else {
builderOpts.log.info("Setting " + varname + " to " +envValue + " from env var " + prefixed);
}
target[key] = opts.reader(envValue);
builderOpts.emitter.emit('change', varname, envValue, undefined);
} else if (builderOpts.consul.prefix && builderOpts.enableconsul) {
this.applyConsulWatch(client, path, key, target, builderOpts);
} else {
if (this.opts.sensitive) {
builderOpts.log.info("Setting " + varname + " to default value");
} else {
builderOpts.log.info("Setting " + varname + " to default value '" + opts.default+"'");
}
target[key] = this.opts.default;
builderOpts.emitter.emit('change', varname, opts.default, undefined);
}
}
this.applyConsulWatch = function (client, path, key, target, builderOpts) {
var varname = path.concat([key]).join('.');
var keyPath = [builderOpts.consul.prefix].concat(path).concat([key]).join('/');
var watch = client.watch({ method: client.kv.get, options: { key: keyPath }});
var _default = this.opts.default;
var _sensitive = this.opts.sensitive;
var _reader = this.opts.reader;
watch.on('change', function(data, res) {
var prev = target[key];
if (res.statusCode == 200 && data && data.Value) {
if (_sensitive) {
builderOpts.log.info("Setting " + varname + " to consul kv " + keyPath + "@"+data.ModifyIndex);
} else {
builderOpts.log.info("Setting " + varname + " to " + data.Value + " from consul kv " + keyPath + '@'+data.ModifyIndex);
}
target[key] = _reader(data.Value);
builderOpts.emitter.emit('change', varname, data.Value, prev);
} else {
builderOpts.log.warn("Received status code " + res.statusCode +", data= " + data + " for kv " +keyPath+ ". Falling back to default value '"+_default+"'");
target[key] = _default;
builderOpts.emitter.emit('change', varname, _default, prev);
}
})
}
}
function _Service (name, opts) {
this.opts = opts;
this.service = name;
this.extend = function(o) {
this.extensions = o;
return this;
}
this.apply = function (client, path, key, target, builderOpts) {
var addrVarname = path.concat([key, 'ADDRESS'])
var portVarname = path.concat([key, 'PORT'])
var varname = path.concat([key]).join('.');
if (! this.opts.optional) {
builderOpts.required[varname] = true;
}
if (builderOpts.envPrefix) {
addrVarname.unshift(builderOpts.envPrefix)
portVarname.unshift(builderOpts.envPrefix)
}
addrVarname = addrVarname.join('_').toUpperCase();
portVarname = portVarname.join('_').toUpperCase();
addrVar = process.env[addrVarname]
portVar = parseInt(process.env[portVarname])
if (addrVar !== undefined && portVar !== undefined) {
builderOpts.log.info("Setting service " + this.service + " to "+ addrVar +":" + portVar + " from env vars " + addrVarname +", "+portVarname);
target[key] = new _ServiceValue(addrVar, portVar)
builderOpts.emitter.emit('change', varname, target[key], undefined);
} else if(builderOpts.enableconsul) {
this.applyConsulWatch(client, path, key, target, builderOpts)
}
}
this.applyConsulWatch = function (client, path, key, target, builderOpts) {
var varname = path.concat([key]).join('.');
var watch = client.watch({ method: client.catalog.service.nodes, options: { service: this.service }});
var _default = this.opts.default;
var _service = this.service;
watch.on('change', function(data, res) {
var prev = target[key];
if (res.statusCode == 200 && data.length) {
var item = data[Math.floor(Math.random()*data.length)]
Object.assign(target[key], new _ServiceValue(item.ServiceAddress || item.Address, item.ServicePort));
builderOpts.emitter.emit('change', varname, item, prev);
} else {
builderOpts.log.warn('No data returned from consul for service '+_service);
target[key] = _default;
if (_default !== undefined)
builderOpts.emitter.emit('change', varname, _default, prev);
}
})
}
}
function _ServiceValue (address, port) {
this.port = port
this.address = address
this.getAddress = function () {
return this.address + ':' + this.port
}
this.buildUri = function (path) {
return this.getAddress() + '/' + path
}
}
function _isObject(o) {
if(!o) return false;
if(Array.isArray(o)) return false;
if(o.constructor != Object) return false;
return true;
}
function _buildRecursive (client, target, spec, opts, path) {
for(var key in spec){
var val = spec[key]
if (val instanceof _Value) {
val.apply(client, path, key, target, opts);
} else if (val instanceof _Service) {
target[key] = {};
val.apply(client, path, key, target, opts);
if (val.extensions) {
_buildRecursive(client, target[key], val.extensions, opts, path.concat([key]));
}
} else if (_isObject(val)) {
target[key] = _buildRecursive(client, {}, spec[key], opts, path.concat([key]));
} else {
target[key] = val;
}
}
return target;
}
module.exports.build = function (spec, opts){
if (opts === undefined) { opts = {}; }
if (!opts.consul) { opts.consul = {}; }
if (!opts.log) { opts.log = _defaultLogger; }
opts.consul.promisify = true;
opts.emitter = new events.EventEmitter();
opts.required = {};
function changeHandler (target, resolve, reject) {
var _handle = function (name , v, old) {
opts.required[name] = false;
var hasMissing = false;
for (var val in opts.required) {
hasMissing |= opts.required[val];
}
if(! hasMissing) {
resolve(target)
}
}
return _handle
}
var client = consul(opts.consul);
return client.agent.self()
.then(function (data) {
opts.log.info("Connected to consul node "+data.Config.NodeName);
opts.enableconsul = true;
}).catch(function (e) {
opts.log.warn("Failed to connect to consul at "+opts.consul.host, e);
opts.enableconsul = false;
}).then(function() {
return new Promise(function(resolve, reject) {
var target = {};
opts.emitter.on('change', changeHandler(target, resolve, reject));
_buildRecursive(client, target, spec, opts || {}, []);
});
});
}
module.exports.value = function (opts){
return new _Value(opts || {});
}
module.exports.service = function (name, opts) {
return new _Service(name, (opts || {}));
}