-
Notifications
You must be signed in to change notification settings - Fork 0
/
consul.js
64 lines (52 loc) · 1.29 KB
/
consul.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
const https = require('https');
const axios = require('axios');
class Consul {
constructor(opts) {
this.config = Object.assign({
port: '8500',
protocol: 'https',
}, opts);
}
async get(key, opts) {
const resp = await this.request(Object.assign({
key: key
}, opts));
return {
responseStatus: resp.status,
responseBody: resp.data,
value: new Buffer.from(resp.data[0].Value, 'base64').toString('utf-8')
};
}
async set(key, value) {
const resp = await this.request({
key: key,
body: value,
method: 'put'
});
return resp.data;
}
async delete(key) {
const resp = await this.request({
key: key,
method: 'delete'
});
return resp.data;
}
request(opts) {
const config = this.config;
const requestOptions = {
url: `${config.protocol}://${config.host}:${config.port}/v1/kv/${opts.key}?token=${config.token}${opts.recurse ? '&recurse' : ''}${opts.dc ? '&dc=' + opts.dc : ''}`,
method: opts.method || 'get',
data: opts.body
};
if (config.tlsCert) {
requestOptions.httpsAgent = new https.Agent({
cert: config.tlsCert,
key: config.tlsKey,
ca: config.ca
});
}
return axios(requestOptions);
}
}
module.exports = Consul;