-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
132 lines (115 loc) · 2.94 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const express = require('express');
const hbs = require('hbs');
const vote = require('./handlers/vote.js')
var app = express();
var listenOn = 8090;
var healthCheck = false;
var livenessProbe = false;
var answers = {};
hbs.registerPartials(__dirname+'/views/partials');
app.set('view engine', 'hbs');
app.use('/public', express.static(__dirname+'/public'));
app.get('/', (req, res) => {
res.render('home.hbs', {
pageTitle: 'Home Page',
welcomeMessage: "The app works correctly"
});
});
app.get('/isHealthy', (req,res) => {
res.render('healthCheck.hbs', {
pageTitle: 'Check health of application',
healthCheck: healthCheck
})
});
app.get('/isAlive', (req,res) => {
res.render('livenessProve.hbs', {
pageTitle: 'Check readiness of application',
livenessProbe: livenessProbe
})
});
app.get('/healthCheck', (req, res) => {
if (healthCheck) {
res.setHeader('READINESS-PROBE',"READY");
res.writeHead(200);
res.send();
}
else {
res.setHeader('READINESS-PROBE',"NOT_READY");
res.writeHead(404);
res.send();
}
});
app.get('/livenessProbe', (req, res) => {
if (livenessProbe) {
res.setHeader('LIVENESS-PROBE',"READY");
res.writeHead(200);
res.send();
}
else {
res.setHeader('LIVENESS-PROBE',"NOT_READY");
res.writeHead(404);
res.send();
}
});
app.get('/setAsNotHealthy', (req, res) => {
healthCheck = false;
setTimeout(() => {
healthCheck = true;
}, 10000);
res.writeHead(200);
res.send();
});
app.get('/setAsHealty', (req, res) => {
healthCheck = true;
res.writeHead(200);
res.send();
});
app.get('/setAlive', (req, res) => {
livenessProbe = true;
res.writeHead(200);
res.send()
});
app.get('/setDead', (req, res) => {
livenessProbe = false;
res.writeHead(200);
res.send()
});
app.get('/vote', (req, res) => {
vote.vote(req.query.contest, req.query.preference)
res.writeHead(200);
res.send()
});
app.get('/getResults', (req, res) => {
var results = vote.getContestVotesAsArrays(req.query.contest);
if (req.query.contest) {
res.send({
data: `[${results[0]}]`,
labels: `[${results[1]}]`
})
}
else {
res.send();
}
});
app.get('/display', (req, res) => {
var results = vote.getContestVotesAsArrays(req.query.contest);
if (req.query.contest) {
res.render('displayVotes.hbs', {
contest: `'${req.query.contest}'`,
data: `[${results[0]}]`,
labels: `[${results[1]}]`
});
}
else {
res.writeHead(404);
res.send()
}
});
app.listen(listenOn, () => {
console.log(`Server is now listening on ${listenOn}`);
healthCheck = true;
livenessProbe = true;
// setInterval(() => {
// console.log('can this be a way to offload the file system');
// }, 1000);
});