forked from splitio/split-evaluator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
235 lines (197 loc) · 6.47 KB
/
server.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
'use strict';
const express = require('express');
const path = require('path');
const app = express();
const config = require('config');
const reduce = require('lodash/reduce');
const map = require('lodash/map');
const os = require('os');
const ip = require('ip');
const thenable = require('@splitsoftware/splitio/lib/utils/promise/thenable');
const utils = require('./utils');
const api = require('./sdk');
const client = api.client();
const manager = api.manager();
const port = process.env.SPLITIO_SERVER_PORT || 7548;
const EXT_API_KEY = process.env.SPLITIO_EXT_API_KEY;
if (!EXT_API_KEY) {
console[console.warn ? 'warn' : 'log']('External API key not provided. If you want a security filter use the EXT_API_KEY environment variable as explained on the README file.');
}
// Healthcheck used by kubernetes does not check api key
app.get('/admin/ping', (req, res) => {
res.send("pong");
});
app.use((req, res, next) => {
if (!EXT_API_KEY || req.headers.authorization == EXT_API_KEY) {
next();
} else {
console.log('Returning 401 Unauthorized.');
res.status(401).send('Unauthorized');
}
});
app.get('/describe/get-treatment', (req, res) => {
res.type('text').send(`
GET
/get-treatment
QUERY PARAMS
key:
This is the key used in the getTreatment call.
bucketing-key:
(Optional) This is the bucketing key used in the getTreatment call.
split-name:
This should be the name of the split you want to include in the getTreatment call.
attributes:
(Optional) This should be a json string of the attributes you want to include in the getTreatment call.
EXAMPLE
curl 'http://localhost:4444/get-treatment?key=my-customer-key&split-name=my-experiment
&attributes=\{"attribute1":"one","attribute2":2,"attribute3":true\}' -H 'Authorization={SPLITIO_EXT_API_KEY}'
`);
});
app.get('/get-treatment', (req, res) => {
console.log('Getting a treatment.');
const state = req.query;
const key = utils.parseKey(state.key, state['bucketing-key']);
const split = state['split-name'];
let attributes = null;
try {
if (state['attributes']) {
attributes = JSON.parse(state['attributes']);
}
} catch (e) {
res.status(400).send('There was an error parsing the provided attributes. Check the format.');
return;
}
function asyncResult(treatment) {
console.log('Returning the treatment.');
res.set('Cache-Control', config.get('cacheControl'))
.send({
splitName: split,
treatment
});
}
const eventuallyAvailableValue = client.getTreatment(key, split, attributes);
if (thenable(eventuallyAvailableValue)) eventuallyAvailableValue.then(asyncResult);
else asyncResult(eventuallyAvailableValue);
});
app.get('/describe/get-treatments', (req, res) => {
res.type('text').send(`
GET
/get-treatments
QUERY PARAMS
keys:
This is the array of keys to be used in the getTreatments call. Each key should specify a "matchingKey"
and a "trafficType". You can also specify a "bucketingKey".
attributes:
(Optional) This should be a json string of the attributes you want to include in the getTreatments call.
EXAMPLE
curl 'http://localhost:4444/get-treatments?keys=\[\{"matchingKey":"my-first-key","trafficType":"account"\},
\{"matchingKey":"my-second-key","bucketingKey":"my-bucketing-key","trafficType":"user"\}\]
&attributes=\{"attribute1":"one","attribute2":2,"attribute3":true\}' -H 'Authorization={SPLITIO_EXT_API_KEY}'
`);
});
// Returns the list of split names of a given traffic type
function filterSplitsByTT(splitViews, trafficType) {
return reduce(splitViews, (acc, view) => {
if (view.trafficType === trafficType) {
acc.push(view.name);
}
return acc;
}, []);
}
app.get('/get-treatments', (req, res) => {
console.log('Getting treatments.');
const state = req.query;
let keys = [];
try {
// Keys are required.
keys = JSON.parse(state.keys);
} catch (e) {
res.status(400).send('There was an error parsing the provided keys. Check that the format is correct.');
return;
}
let attributes;
try {
if (state['attributes']) {
attributes = JSON.parse(state['attributes']);
}
} catch (e) {
res.status(400).send('There was an error parsing the provided attributes. Check the format.');
return;
}
const splitsPromise = Promise.resolve(manager.splits()).then(views => {
return map(keys, key => {
return {
trafficType: key.trafficType,
key: utils.parseKey(key.matchingKey, key.bucketingKey),
splits: filterSplitsByTT(views, key.trafficType)
};
});
});
Promise.resolve(splitsPromise)
// Call getTreatments
.then(splitsByTT => {
return reduce(splitsByTT, (acc, group) => {
// @TODO: Support thenables here when necessary.
const partial = client.getTreatments(group.key, group.splits, attributes);
const results = reduce(partial, (acc, treatment, feature) => {
acc.push({
splitName: feature,
treatment
});
return acc;
}, [])
return acc.concat(results);
}, []);
})
// Send the response to the client
.then(treatments => {
console.log('Returning treatments.');
return res.set('Cache-Control', config.get('cacheControl')).type('json').send(treatments);
})
// 500 on error
.catch(() => res.sendStatus(500));
});
app.get('/version', (req, res) => {
console.log('Getting version.');
const version = utils.getVersion();
const parts = api.settings.version.split('-');
const sdkLanguage = parts[0];
const sdkVersion = parts.slice(1).join('-');
res.send({
version,
sdk: sdkLanguage,
sdkVersion
});
});
app.get('/machine', (req, res) => {
console.log('Getting machine information.');
let address; let hostname;
try {
address = ip.address();
hostname = os.hostname();
} catch(e) {
address = hostname = 'unavailable';
}
res.send({
ip: address,
name: hostname
});
});
//Route not found -- Set 404
app.get('*', function (req, res) {
console.log('Wrong endpoint called.');
res.json({
'route': 'Sorry this page does not exist!'
});
});
function spinUpServer() {
app.listen(port, '0.0.0.0', function () {
console.log('Server is Up and Running at Port : ' + port);
});
}
// Only available for in memory settings.
if (config.get('blockUntilReady')) {
client.ready().then(spinUpServer);
} else {
spinUpServer();
}