From 724b165e2303301191d854d76665b756d4d3ae1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Mass=C3=A9?= Date: Thu, 12 Jul 2018 19:41:44 +0200 Subject: [PATCH] initial release --- Custom-HTTP-Probe/package.json | 13 ++++ Custom-HTTP-Probe/server.js | 110 +++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 Custom-HTTP-Probe/package.json create mode 100644 Custom-HTTP-Probe/server.js diff --git a/Custom-HTTP-Probe/package.json b/Custom-HTTP-Probe/package.json new file mode 100644 index 0000000..462735d --- /dev/null +++ b/Custom-HTTP-Probe/package.json @@ -0,0 +1,13 @@ +{ + "name": "sample-app", + "version": "0.0.1", + "description": "An sample app that can sometimes fail...", + "main": "server.js", + "scripts": {}, + "author": "Nicolas MASSE", + "repository": "https://github.com/nmasse-itix/OpenShift-Examples.git", + "license": "MIT", + "dependencies": { + "express": "latest" + } +} diff --git a/Custom-HTTP-Probe/server.js b/Custom-HTTP-Probe/server.js new file mode 100644 index 0000000..5119fed --- /dev/null +++ b/Custom-HTTP-Probe/server.js @@ -0,0 +1,110 @@ +var express = require("express"); +var app = express(); +var router = express.Router(); +var port = 8080; + +var alive = true; +var countdown = 30; + +// This is our access_log +router.use(function (req,res,next) { + next(); + console.log("[%s] %s %s => %i", new Date().toUTCString(), req.method, req.originalUrl, res.statusCode); +}); + +function getState() { + var response = { + "ready": countdown <= 0, + "alive": alive, + "pod": process.env["HOSTNAME"] + }; + + if (countdown > 0) { + response.countdown = countdown; + } + + return response; +} + +function doCountdown() { + if (countdown > 0) { + countdown--; + setTimeout(doCountdown, 1000); + } +} +// Countdown before announcing our readiness +doCountdown(); + +// Help message +router.get("/",function(req,res){ + var response = { + paths: { + '/please-die': 'This app will die very soon !', + '/please-resuscitate': 'This app will come back to life !', + '/': 'This message', + '/probe/readiness': 'Standard readiness probe', + '/probe/liveness': 'Standard liveness probe', + '/probe/custom': 'A strange custom probe...' + }, + state: getState() + } + res.type('application/json') + .send(JSON.stringify(response)) + .end(); +}); + +router.get("/please-die",function(req,res){ + alive = false; + var response = getState(); + res.type('application/json') + .send(JSON.stringify(response)) + .end(); +}); + +router.get("/please-resuscitate",function(req,res){ + alive = true; + var response = getState(); + res.type('application/json') + .send(JSON.stringify(response)) + .end(); +}); + +router.get("/probe/readiness",function(req,res){ + var response = getState(); + res.type('application/json') + .status(countdown <= 0 ? 200 : 503) + .send(JSON.stringify(response)) + .end(); +}); + +router.get("/probe/liveness",function(req,res){ + var response = getState(); + res.type('application/json') + .status(alive ? 200 : 500) + .send(JSON.stringify(response)) + .end(); +}); + +router.get("/probe/custom",function(req,res){ + if (alive) { + res.type('application/json') + .status(418) + .send(JSON.stringify(getState())) + .end(); + } else { + res.type('text/html') + .status(500) + .send("

I'm dead... X-)

") + .end(); + } +}); + +app.use("/",router); + +app.use("*",function(req,res){ + res.status(404).send("Not found"); +}); + +app.listen(port,function(){ + console.log("Live at Port %i", port); +});