This repository has been archived by the owner on Dec 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
conqueso.js
168 lines (140 loc) · 4.08 KB
/
conqueso.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
'use strict';
const flatten = require('flat');
const HTTP_OK = 200;
const HTTP_METHOD_NOT_ALLOWED = 405;
/**
* Deep copy an object
* https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm
*
* @param {Object} object The object to be copied
* @return {Object} A deep copy of the provided object
*/
function clone(object) {
if (!(object instanceof Object)) {
return object;
}
const Constructor = object.constructor;
let clonedObject = null;
switch (Constructor) {
case RegExp:
clonedObject = new Constructor(object);
break;
case Date:
clonedObject = new Constructor(object.getTime());
break;
default:
clonedObject = new Constructor();
}
for (const key in object) { // eslint-disable-line guard-for-in
clonedObject[key] = clone(object[key]);
}
return clonedObject;
}
/**
* Format the given data as Java properties
*
* @param {Object} data
* @return {String}
*/
function makeJavaProperties(data) {
const results = [];
for (const key in data) {
if (data.hasOwnProperty(key)) {
results.push(key + '=' + data[key]);
}
}
return results.join('\n');
}
/**
* Converts Consul addresess to Conqueso addresses
* Warning! Original Consul properties are removed
*
* Addresses returned by the Consul plugin are formatted as
* {
* "consul.service.addresses": ["x.x.x.x", "y.y.y.y"]
* }
*
* Addresses returned by Conqueso are formatted as
* {
* "conqueso.service.ips": "x.x.x.x, y.y.y.y"
* }
*
* @param {Object} properties Properties as returned from Storage#properties
* @return {Object} Properties with Consul address converted to Conqueso ones
*/
function translateConquesoAddresses(properties) {
if (properties.consul) {
Object.keys(properties.consul).forEach((service) => {
const cluster = properties.consul[service].cluster;
/* eslint-disable no-param-reassign */
properties[`conqueso.${cluster}.ips`] = properties.consul[service].addresses.join(',');
});
delete properties.consul;
}
/* eslint-enable no-param-reassign */
return properties;
}
/**
* Format the given properties as Conqueso properties
*
* @param {Object} properties Properties as returned from Storage#properties
* @return {String} Flattened Java properties as returned by Conqueso
*/
function makeConquesoProperties(properties) {
let results = clone(properties);
results = translateConquesoAddresses(results);
results = flatten(results);
return results;
}
/**
* Conqueso compatible API
*
* @param {Express.App} app
* @param {Storage} storage
*/
function Conqueso(app, storage) {
// Conqueso compatible APIs are defined before the generic catch all route.
app.get('/v1/conqueso/api/roles/:role/properties/:property', (req, res) => {
const property = req.params.property;
const properties = makeConquesoProperties(storage.properties);
res.set('Content-Type', 'text/plain');
res.status(HTTP_OK);
if (property && (properties.hasOwnProperty(property))) {
res.end(String(properties[property]));
} else {
res.end();
}
});
// Handle any other requests by returning all properites.
const route = app.route('/v1/conqueso*');
const allowedMethods = 'GET,POST,PUT,OPTIONS';
function methodNotAllowed(req, res) {
res.set('Allow', allowedMethods);
res.status(HTTP_METHOD_NOT_ALLOWED);
res.end();
}
route.get((req, res) => {
res.set('Content-Type', 'text/plain');
res.status(HTTP_OK);
res.end(makeJavaProperties(makeConquesoProperties(storage.properties)));
});
// Express defaults to using the GET route for HEAD requests.
// So we need to explicitly reject HEAD request.
route.head(methodNotAllowed);
route.post((req, res) => {
res.status(HTTP_OK);
res.end();
});
route.put((req, res) => {
res.status(HTTP_OK);
res.end();
});
route.options((req, res) => {
res.set('Allow', allowedMethods);
res.status(HTTP_OK);
res.end();
});
// Reject anything else e.g. DELETE, TRACE, etc.
route.all(methodNotAllowed);
}
exports.attach = Conqueso;