forked from nymag/health-check
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
48 lines (42 loc) · 1.42 KB
/
index.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
'use strict';
const _get = require('lodash/get'),
_isFunction = require('lodash/isFunction'),
_isObject = require('lodash/isObject'),
express = require('express'),
render = require('./lib/render');
/**
*
* @param {object} [options]
* @param {object} [options.info] Hash of extra info to include with their functions or objects
* @param {[string]} [options.env] Environment Variables to include
* @param {[string]} [options.required] Info that will cause a 500 if missing
* @param {[string]} [options.path] Custom route for the health-check
* @returns {*}
*/
function routes(options) {
options = options || {};
const errors = {},
router = express.Router(),
info = _get(options, 'info', {}),
path = options.path || '';
// shortcut to list environment variables
_get(options, 'env', []).forEach(function (value) {
info[value] = function () { return process.env[value]; };
});
// fail when these functions fail
_get(options, 'required', []).forEach(function (value) {
if (_isObject(info[value]) || _isFunction(info[value])) {
info[value].isRequired = true;
} else {
errors[value] = 'Required stat ' + value + ' is not defined.';
}
});
if (path) {
// e.g. /microservice-name/health-check
router.get('/' + path + '/health-check', render(info, errors));
} else {
router.get('/health-check', render(info, errors));
}
return router;
}
module.exports = routes;