hapi-lbstatus
Advanced tools
Comparing version 1.0.0 to 2.0.0
24
index.js
@@ -1,1 +0,23 @@ | ||
module.exports = require('./lib/route'); | ||
var service = require('./lib/provider'); | ||
exports.register = function(plugin, options, next){ | ||
plugin.route( | ||
{ | ||
method: "GET", | ||
path: "/_lbstatus", | ||
config: { | ||
handler: function(request, reply) { | ||
service.lbstatus(plugin.servers, options, function(result){ | ||
reply(result).code(200); | ||
}); | ||
} | ||
} | ||
} | ||
); | ||
next(); | ||
}; | ||
exports.register.attributes = { | ||
pkg: require('./package.json') | ||
}; |
@@ -1,13 +0,9 @@ | ||
var fs = require("fs"), | ||
async = require("async"), | ||
_getStatus = function(applicationStatus, dependenciesStatusCode){ | ||
if(applicationStatus.indexOf("ON") > -1 && dependenciesStatusCode < 500){ | ||
return "OTWEB_ON"; | ||
} | ||
return "OTWEB_OFF"; | ||
getStatus = function(applicationStatus, dependenciesStatusCode){ | ||
return (applicationStatus === "ON" && dependenciesStatusCode < 500); | ||
}, | ||
_readFile = function(server, filepath, callback){ | ||
readFile = function(server, filepath, callback){ | ||
fs.readFile(filepath, {encoding: "UTF8"}, function(err, file){ | ||
@@ -24,3 +20,3 @@ if(err){ | ||
applicationOffLine = function(server, file, callback){ | ||
_readFile(server, file, function(file){ | ||
readFile(server, file, function(file){ | ||
callback(file); | ||
@@ -38,9 +34,17 @@ }); | ||
lbstatus = function(server, config, callback){ | ||
applicationOffLine(server, config.file, function(file){ | ||
checkDependencies(server, config.liveness, config.headers, function(statusCode){ | ||
callback( | ||
_getStatus(file, statusCode) | ||
); | ||
lbstatus = function(servers, config, callback){ | ||
var on = config.on || "ON"; | ||
var off = config.off || "ON"; | ||
var results = []; | ||
async.forEach(servers, function(server, done){ | ||
applicationOffLine(server, config.file, function(file){ | ||
checkDependencies(server, config.liveness, config.headers, function(statusCode){ | ||
results.push(getStatus(file, statusCode)); | ||
done(); | ||
}); | ||
}); | ||
}, function(){ | ||
var failures = results.filter(function(i){ return i === false; }); | ||
callback(failures.length === 0 ? on : off); | ||
}); | ||
@@ -47,0 +51,0 @@ }; |
{ | ||
"name": "hapi-lbstatus", | ||
"version": "1.0.0", | ||
"description": "lbstatus endpoint for hapi", | ||
"version": "2.0.0", | ||
"description": "lbstatus plugin for hapi", | ||
"main": "index.js", | ||
@@ -14,3 +14,4 @@ "scripts": { | ||
"keywords": [ | ||
"hapi" | ||
"hapi", | ||
"hapiplugin" | ||
], | ||
@@ -26,8 +27,10 @@ "author": "Andy Royle <ajroyle@gmail.com>", | ||
"grunt-cli": "^0.1.13", | ||
"grunt-contrib-clean": "^0.5.0", | ||
"grunt-contrib-jshint": "^0.10.0", | ||
"grunt-contrib-clean": "^0.5.0", | ||
"grunt-mocha-test": "^0.10.2", | ||
"should": "^3.3.1" | ||
}, | ||
"dependencies": {} | ||
"dependencies": { | ||
"async": "^0.9.0" | ||
} | ||
} |
#Hapi lbstatus | ||
[![Build Status](https://travis-ci.org/opentable/hapi-lbstatus.png?branch=master)](https://travis-ci.org/opentable/hapi-lbstatus) [![NPM version](https://badge.fury.io/js/hapi-lbstatus.png)](http://badge.fury.io/js/hapi-lbstatus) ![Dependencies](https://david-dm.org/opentable/hapi-lbstatus.png) | ||
Shared code for the _lbstatus endpoint. Essentially a poor-man's service discovery, will be used until the front-door service is ready. | ||
Shared code for the `_lbstatus` endpoint. Essentially a poor-man's service discovery, will be used until the front-door service is ready. | ||
Reads the specified file and looks for the value 'ON' or 'OFF'. Returns OTWEB_ON or OTWEB_OFF from the endpoint. | ||
Reads the specified file and looks for the value 'ON' or 'OFF' and returns ON or OFF from the endpoint. | ||
If the file is missing, or empty, or an exception occurs, then default value is OFF. | ||
Also performs a liveness check by injecting a request to a specified endpoint (using server.inject). | ||
Also performs a liveness check by injecting a request to a specified endpoint (using server.inject). If the response is an error (or times out) it returns "OFF". | ||
@@ -16,17 +16,29 @@ Installation: | ||
Usage: | ||
``` | ||
var lbstatus = require("hapi-lbstatus"); | ||
var server = hapi.createServer(); | ||
lbstatus.configure(server, { | ||
file: '/etc/lbstatus/myappname', | ||
liveness: '/my/api/123', | ||
headers: { | ||
server.pack.require("hapi-lbstatus", | ||
{ | ||
file: '/etc/lbstatus/myappname', | ||
liveness: '/my/api/123', | ||
headers: { | ||
// optional headers to apply when making the liveness check | ||
'accept-language': 'en-US' | ||
'accept-language': 'en-US' | ||
}, | ||
on: "MYAPP_ON", // override the default return value of 'ON' | ||
off: "MYAPP_OFF" // override the default return value of 'OFF' | ||
}, | ||
function (err){ | ||
if(err){ | ||
throw err; | ||
} | ||
} | ||
}); | ||
``` | ||
); | ||
``` | ||
Notes: | ||
- Supports pack servers as well as single instances | ||
- Supports hapi v5+ |
describe('lbstatus tests', function(){ | ||
var should = require('should'), | ||
lbstatus = require('../index'), | ||
r = [], | ||
server = { | ||
route: function(routes){ | ||
r = r.concat(routes); | ||
}, | ||
provider = require('../lib/provider'), | ||
servers = [{ | ||
inject: function(options, callback){ | ||
@@ -13,23 +9,33 @@ callback({ statusCode: 200 }); | ||
log: function(){} | ||
}; | ||
}], | ||
badservers = [{ | ||
inject: function(options, callback){ | ||
callback({ statusCode: 500 }); | ||
}, | ||
log: function(){} | ||
}]; | ||
beforeEach(function(){ | ||
r = []; | ||
}); | ||
it('should register the route', function(){ | ||
var p = require('../index.js'), | ||
r = [], | ||
plugin = { | ||
route: function(route) { | ||
r.push(route); | ||
} | ||
}; | ||
it('should register the route', function(){ | ||
lbstatus.configure(server, {}); | ||
p.register(plugin, {}, function(){}); | ||
r.length.should.eql(1); | ||
r[0].path.should.eql('/_lbstatus'); | ||
}); | ||
it('should read the file', function(done){ | ||
lbstatus.configure(server, { | ||
provider.lbstatus(servers, { | ||
file: __dirname + '/statusfile-on', | ||
liveness: '/my/app/123' | ||
}); | ||
r[0].config.handler({}, function(result){ | ||
liveness: '/my/app/123', | ||
on: 'OTWEB_ON', | ||
off: 'OTWEB_OFF' | ||
}, function(result){ | ||
result.should.eql('OTWEB_ON'); | ||
done(); | ||
return { code: function(){} }; | ||
}); | ||
@@ -39,11 +45,10 @@ }); | ||
it('should return off when the file says off', function(done){ | ||
lbstatus.configure(server, { | ||
provider.lbstatus(servers, { | ||
file: __dirname + '/statusfile-off', | ||
liveness: '/my/app/123' | ||
}); | ||
r[0].config.handler({}, function(result){ | ||
liveness: '/my/app/123', | ||
on: 'OTWEB_ON', | ||
off: 'OTWEB_OFF' | ||
}, function(result){ | ||
result.should.eql('OTWEB_OFF'); | ||
done(); | ||
return { code: function(){} }; | ||
}); | ||
@@ -53,11 +58,22 @@ }); | ||
it('should return off when the file is missing', function(done){ | ||
lbstatus.configure(server, { | ||
provider.lbstatus(servers, { | ||
file: __dirname + '/statusfile-missing', | ||
liveness: '/my/app/123' | ||
liveness: '/my/app/123', | ||
on: 'OTWEB_ON', | ||
off: 'OTWEB_OFF' | ||
}, function(result){ | ||
result.should.eql('OTWEB_OFF'); | ||
done(); | ||
}); | ||
}); | ||
r[0].config.handler({}, function(result){ | ||
result.should.eql('OTWEB_OFF'); | ||
it('should use the config value for returning the string', function(done){ | ||
provider.lbstatus(servers, { | ||
file: __dirname + '/statusfile-on', | ||
liveness: '/my/app/123', | ||
on: 'blarg', | ||
off: 'flarg' | ||
}, function(result){ | ||
result.should.eql('blarg'); | ||
done(); | ||
return { code: function(){} }; | ||
}); | ||
@@ -67,17 +83,12 @@ }); | ||
it('should return off when the liveness check fails', function(done){ | ||
lbstatus.configure(server, { | ||
provider.lbstatus(badservers, { | ||
file: __dirname + '/statusfile-on', | ||
liveness: '/my/app/123' | ||
}); | ||
server.inject = function(options, callback){ | ||
callback({ code: 500 }); | ||
}; | ||
r[0].config.handler({}, function(result){ | ||
liveness: '/my/app/123', | ||
on: 'OTWEB_ON', | ||
off: 'OTWEB_OFF' | ||
}, function(result){ | ||
result.should.eql('OTWEB_OFF'); | ||
done(); | ||
return { code: function(){} }; | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9141
173
44
1
12
+ Addedasync@^0.9.0
+ Addedasync@0.9.2(transitive)