Dan K
2019-08-04 ac6169db7b90e950156a87d9784308429ce3bee2
commit | author | age
009903 1 var express = require('express'),
IC 2     app     = express();
3
4 var port = process.env.PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080,
5     ip   = process.env.IP   || process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0';
6
7 var route = express.Router();
8
9 // global var to track app health
10 var healthy = true;
11
12 app.use('/', route);
13
14 // A route that says hello
15 route.get('/', function(req, res) {
16     res.send('Hello! This is the index page for the app.\n');
17 });
18
19 // A route that returns readiness status
20 // simulates readiness 30 seconds after start up
21 route.get('/ready', function(req, res) {
22       var now = Math.floor(Date.now() / 1000);
23       var lapsed = now - started;
24       if (lapsed > 30) {
25         console.log('ping /ready => pong [ready]');
26         res.send('Ready for service requests...\n');
27       }
28       else {
29     console.log('ping /ready => pong [notready]');
30     res.status(503);
31         res.send('Error! Service not ready for requests...\n');
32       }
33 });
34
35 // A route that returns health status
36 route.get('/healthz', function(req, res) {
37     if (healthy) {
38       console.log('ping /healthz => pong [healthy]');
39       res.send('OK\n');
40     }
41     else {
42       console.log('ping /healthz => pong [unhealthy]');
43       res.status(503);
44       res.send('Error!. App not healthy!\n');
45     }
46 });
47
48 // This route handles switching the state of the app
49 route.route('/flip').get(function(req, res) {
50
51         var flag = req.query.op;
52         if (flag == "kill") {
53       console.log('Received kill request. Changing app state to unhealthy...');
54       healthy = false;
55       res.send('Switched app state to unhealthy...\n');
56     }
57     else if (flag == "awaken") {
58       console.log('Received awaken request. Changing app state to healthy...');
59       healthy = true;
60       res.send('Switched app state to healthy...\n');
61     }
62     else {
63       res.send('Error! unknown flag...\n');
64     }
65     });
66
67 app.listen(port, ip);
68 console.log('nodejs server running on http://%s:%s', ip, port);
69 var started = Math.floor(Date.now() / 1000);
70
71 module.exports = app;