-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
74 lines (67 loc) · 1.78 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
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
const Boom = require('boom');
const str2fn = require('str2fn');
const path = require('path');
const package = require(path.join(process.cwd(), 'package.json'));
const defaults = {
auth: false,
token: false,
endpoint: '/health',
checks: [],
envs: [] // list of environment variables to include in report
};
const register = function(server, options) {
const settings = Object.assign({}, defaults, options);
const handler = async (request, h) => {
if (settings.token && settings.token !== request.query.token) {
throw Boom.unauthorized();
}
const output = {
host: server.info.host,
env: process.env.NODE_ENV,
uptime: Math.floor(process.uptime()),
cpu: process.cpuUsage(),
memory: process.memoryUsage(),
version: package.version
};
if (settings.envs.length) {
output.envs = settings.envs.reduce((memo, varName) => {
memo[varName] = process.env[varName];
return memo;
}, {});
}
await Promise.all(settings.checks.map(async check => {
if (!check.name || !check.method) {
throw new Boom('Invalid check');
}
output[check.name] = await str2fn.execute(
check.method,
server.methods,
{
request,
options: check.options
}
);
}));
return output;
};
const routeConfig = {
method: 'get',
path: settings.endpoint,
handler
};
// if auth is undefined set it to false to avoid the default auth:
if (options.auth === undefined || options.auth === null) {
options.auth = false;
}
if (options.auth || options.auth === false) {
routeConfig.config = {
auth: options.auth
};
}
server.route(routeConfig);
};
exports.plugin = {
once: true,
pkg: require('./package.json'),
register
};