Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
feat: add basic health check route on restApiRoot
Browse files Browse the repository at this point in the history
  • Loading branch information
KalleV committed Oct 31, 2018
1 parent e47e807 commit aabe483
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
23 changes: 23 additions & 0 deletions lib/middleware/status.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

/**
* Return basic application status information:
* Date the application was started and uptime in JSON format.
* For example:
* ```js
* {
* "started": "2014-06-05T00:26:49.750Z",
* "uptime": 9.394
* }
* ```
*/
module.exports = function status() {
const started = new Date();

return (req, res) => {
res.send({
started: started,
uptime: (Date.now() - Number(started)) / 1000,
});
};
}
6 changes: 5 additions & 1 deletion lib/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const _ = require('lodash'),
serverUtils = require('./server-utils'),
{SocketIOLoader, Loader} = require('./api'),
dotEnv = require('dotenv'),
path = require('path');
path = require('path'),
status = require('./middleware/status');

// Load environment variables using a local .env file (see: https://www.npmjs.com/package/dotenv)
dotEnv.config({path: path.join(process.cwd(), '.env')});
Expand Down Expand Up @@ -157,6 +158,9 @@ class Services {
this.initialize();
}

// Attach basic health check middleware
this._app.get(mountPoint, status())

// Run all the package API 'config' functions
this._apiLoader.setConfig({
apiLoader: this._apiLoader,
Expand Down
21 changes: 19 additions & 2 deletions test/lib/unit/lib/services_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const supertest = require('supertest'),

describe('Services', () => {

const packagesPath = './test/fixtures/main-package',
apiPackage1Prefix = '/socket-api-package-1-namespace';
const packagesPath = './test/fixtures/main-package'
const apiPackage1Prefix = '/socket-api-package-1-namespace'

let services,
port,
Expand Down Expand Up @@ -123,4 +123,21 @@ describe('Services', () => {
}).toThrowError(/Session store "INVALID STORE" is not supported/i);
});

it('sets a health check endpoint on the restApiRoot', (done) => {
const services = new Services(options);
server = services.start();

const request = supertest(server);

request
.get('/')
.expect(200)
.then((res) => {
expect(res.text).toContain('started');
expect(res.text).toContain('uptime');
done();
})
.catch(done.fail);
})

});

0 comments on commit aabe483

Please sign in to comment.