-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
65 lines (57 loc) · 1.48 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
'use strict'
const DEFAULT_PATH = '/healthcheck'
module.exports = function(options) {
options = options || {}
// set path
options.path = options.path || DEFAULT_PATH
// set healthy function
options.healthy = options.healthy || function() {
return { uptime: process.uptime() }
}
if (typeof options.healthy !== 'function') {
throw new Error('koa-simple-healthcheck `healthy` method must be a function')
}
// set test function
options.test = options.test || function() {}
if (typeof options.test !== 'function') {
throw new Error('koa-simple-healthcheck `test` method must be a function')
}
if (options.test.length === 0) {
const test = options.test;
options.test = function (callback) {
callback(test());
};
}
return async function healthcheck(ctx, next) {
if (options.path != ctx.path) return await next()
try {
options.test(function(err) {
var status, response
if (err) {
status = 500
response = err
} else {
status = 200
response = options.healthy()
}
jsonlize(ctx, status, response)
})
} catch (err) {
jsonlize(ctx, 500, err)
}
}
}
/**
* Stringify JSON, and set status code, body, type to context
*
* @param {Object} ctx
* @param {number} status
* @param {*} value
* @returns {void}
* @private
*/
function jsonlize(ctx, status, val) {
ctx.status = status
ctx.type = 'application/json'
ctx.body = JSON.stringify(val)
}