forked from electerious/Ackee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhealthcheck.js
executable file
·52 lines (40 loc) · 1.19 KB
/
healthcheck.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
#!/usr/bin/env node
'use strict'
require('dotenv').config()
const fetch = require('node-fetch')
const signale = require('./utils/signale')
const config = require('./utils/config')
const checkMongoDB = require('./utils/connect')
if (config.dbUrl == null) {
signale.fatal('MongoDB connection URI missing in environment')
process.exit(1)
}
const checkServer = async (url) => {
const response = await fetch(url)
if (response.ok === false) {
throw new Error(`Server is unhealthy and returned with the status '${ response.status }'`)
}
}
const checkApi = async (url) => {
const response = await fetch(url)
if (response.ok === false) {
throw new Error(`API is unhealthy and returned with the status '${ response.status }'`)
}
}
const exit = (healthy) => process.exit(healthy === true ? 0 : 1)
const check = () => Promise.all([
checkMongoDB(config.dbUrl),
checkServer(`http://localhost:${ config.port }`),
checkApi(`http://localhost:${ config.port }/.well-known/apollo/server-health`),
])
const handleSuccess = () => {
signale.success('Ackee is up and running')
exit(true)
}
const handleFailure = (error) => {
signale.fatal(error)
exit(false)
}
check()
.then(handleSuccess)
.catch(handleFailure)