Skip to content

Commit

Permalink
add health endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
rcruzper committed Aug 28, 2019
1 parent e678406 commit cff6dd2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/actuatorMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@
var express = require('express');
var infoRoute = require('./infoRoute');
var metricsRoute = require('./metricsRoute');
var healthRoute = require('./healthRoute');

module.exports = function actuatorMiddleware(endpoint) {
var router = express.Router();

var infoPath = '/info';
var metricsPath = '/metrics';
var healthPath = "/health";

if (endpoint) {
infoPath = endpoint + infoPath;
metricsPath = endpoint + metricsPath;
healthPath = endpoint + healthPath;
}

router.get(infoPath, infoRoute);
router.get(metricsPath, metricsRoute);
router.get(healthPath, healthRoute);

return router;
};
7 changes: 7 additions & 0 deletions lib/healthRoute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

module.exports = function metrics(req, res) {
res.status(200).json({
status: "UP"
});
};
31 changes: 31 additions & 0 deletions test/functional/health.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

const request = require('supertest');
const express = require('express');
const expect = require('chai').expect;
const actuator = require('../../lib/actuatorMiddleware.js');

let app;

describe('GET /health', function() {
beforeEach(function() {
app = express();
app.use(actuator());
});

afterEach(function() {
app.close;
});

it('should return status UP', function(done) {
request(app)
.get('/health')
.end(function(err, res) {
expect(res.statusCode).to.equal(200);
expect(res.headers['content-type']).to.equal('application/json; charset=utf-8');
expect(res.body.status).to.equal("UP");
done();
});
});

});
4 changes: 2 additions & 2 deletions test/unit/lib/actuatorMiddlewareSpec.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ describe('actuator middleware', function () {
it('should mount the info route on the default endpoint', function () {
actuator();

expect(router.get).to.have.been.calledTwice;
expect(router.get).to.have.been.calledThrice;
expect(router.get).to.have.been.calledWithExactly('/info', infoRoute);
});

it('should mount the info route on the given endpoint', function () {
actuator('/foobar');

expect(router.get).to.have.been.calledTwice;
expect(router.get).to.have.been.calledThrice;
expect(router.get).to.have.been.calledWithExactly('/foobar/info', infoRoute);
});
});

0 comments on commit cff6dd2

Please sign in to comment.