openhim-core
Version:
The OpenHIM core application that provides logging and routing of http requests
110 lines (92 loc) • 2.91 kB
JavaScript
var Channel, Keystore, cacheValueStore, config, getCachedValues, logger, momentTZ, refreshMillis;
logger = require('winston');
config = require('./config/config');
config.caching = config.get('caching');
Channel = require("./model/channels").Channel;
Keystore = require("./model/keystore").Keystore;
momentTZ = require('moment-timezone');
exports.logAndSetResponse = function(ctx, status, msg, logLevel) {
logger[logLevel](msg);
ctx.body = msg;
return ctx.status = status;
};
cacheValueStore = {};
refreshMillis = config.caching.refreshMillis;
getCachedValues = function(store, callback) {
var handler, lastCheck, ref;
lastCheck = (ref = cacheValueStore["" + store]) != null ? ref.lastCheck : void 0;
if (!config.caching.enabled || (lastCheck == null) || ((new Date) - lastCheck) > refreshMillis) {
handler = function(err, results) {
if (err) {
return callback(err);
}
if (config.caching.enabled) {
if (!lastCheck) {
cacheValueStore["" + store] = {};
}
cacheValueStore["" + store].value = results;
cacheValueStore["" + store].lastCheck = new Date;
}
return callback(null, results);
};
if (store === 'channels') {
return Channel.find({}).sort({
priority: 1
}).exec(function(err, channels) {
var noPriorityChannels, sortedChannels;
if (err) {
return handler(err);
}
noPriorityChannels = [];
sortedChannels = [];
channels.forEach(function(channel) {
if (channel.priority == null) {
return noPriorityChannels.push(channel);
} else {
return sortedChannels.push(channel);
}
});
return handler(null, sortedChannels.concat(noPriorityChannels));
});
} else if (store === 'keystore') {
return Keystore.findOne({}, handler);
} else {
return callback("Internal error: Invalid store " + store);
}
} else {
return callback(null, cacheValueStore["" + store].value);
}
};
exports.getAllChannelsInPriorityOrder = function(callback) {
return getCachedValues('channels', callback);
};
exports.getKeystore = function(callback) {
return getCachedValues('keystore', callback);
};
exports.statusCodePatternMatch = function(string, callback) {
return /\dxx/.test(string);
};
exports.uniqArray = function(arr) {
var dict, i, k, len, v;
dict = {};
for (i = 0, len = arr.length; i < len; i++) {
k = arr[i];
dict[k] = k;
}
return (function() {
var results1;
results1 = [];
for (k in dict) {
v = dict[k];
results1.push(v);
}
return results1;
})();
};
exports.typeIsArray = Array.isArray || function(value) {
return {}.toString.call(value) === '[object Array]';
};
exports.serverTimezone = function() {
return momentTZ.tz.guess();
};
//# sourceMappingURL=utils.js.map