-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
60 lines (44 loc) · 1.4 KB
/
server.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
const express = require('express');
const morgan = require('morgan');
const path = require('path');
const index = require('./routes/index');
const app = express();
app.use(morgan('dev'));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
app.get('/', index);
const startupTimestamp = new Date();
// Health check and readiness check endpoints
const wait = ms => new Promise(res => setTimeout(res, ms));
app.get('/healthy', async (req, res, next) => {
wait(process.env.HEALTHY_SLEEP).then(() => res.sendStatus(200))
});
app.get('/healthz', async (req, res, next) => {
const current = new Date();
if (current - startupTimestamp < 10000) {
res.sendStatus(200);
} else {
res.sendStatus(500);
}
});
app.get('/readyz', async (req, res, next) => {
const current = new Date();
if (current - startupTimestamp < 10000) {
res.sendStatus(500);
} else {
res.sendStatus(200);
}
});
app.get('/ready', async (req, res, next) => {
wait(process.env.READY_SLEEP).then(() => res.sendStatus(200))
});
app.get('/fail', async (req, res, next) => {
wait(process.env.FAIL_SLEEP).then(() => res.sendStatus(500))
});
app.use((req, res, next) => {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`hello-kube running on port ${port}.`));