-
Notifications
You must be signed in to change notification settings - Fork 15
/
config.js
41 lines (35 loc) · 971 Bytes
/
config.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
const AWS = require('aws-sdk');
const s3 = new AWS.S3();
const cache = {};
function getConfig(configFileName, callback) {
//configuring parameters
const params = {
Bucket: 'auth-config.experoinc.com',
Key: configFileName
};
s3.getObject(params, function (err, data) {
if (err) {
callback(err, null);
}
else {
const config = data && data.Body && JSON.parse(data.Body.toString());
callback(null, config);
}
});
}
function getConfigCached(request, callback) {
const configFileName = `${request.headers.host[0].value}.json`;
const entry = cache[configFileName];
if (entry && ((Date.now() - entry.time) < (5 * 60 * 1000))) // if entry is less than 5 minutes old
{
return callback(null, entry);
}
getConfig(configFileName, (err, result) => {
if (!err) {
result.time = Date.now();
cache[configFileName] = result;
}
callback(err, result);
});
}
module.exports = getConfigCached;