-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
39 lines (31 loc) · 995 Bytes
/
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
let express = require('express'),
bodyParser = require('body-parser'),
morgan = require('morgan'),
request = require('request')
let app = express(),
port = process.env.PORT || 1337
let nodes = require('./config.json')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(morgan('tiny'))
app.use(express.static('public'))
const Request = function (url) {
return new Promise((resolve, reject) => {
request.get(url, (err, response, body) => {
resolve(response && response.statusCode)
})
})
}
app.get('/api/heartbeat', async (req, res) => {
let resps = []
for (let i = 0; i < nodes.length; i++) {
let resp = await Request('http://' + nodes[i].address)
resps.push({
name: nodes[i].name,
server: nodes[i].address,
status: resp == 200 ? 'healthy' : (resp != undefined ? 'unhealthy' : 'dead')
})
}
res.json(resps)
})
app.listen(port)