Last active
August 29, 2015 14:27
-
-
Save danielb2/69827c33cdfb957815ad to your computer and use it in GitHub Desktop.
solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Load modules | |
var Hoek = require('hoek'); | |
var Joi = require('joi'); | |
var pkg = require('../package.json'); | |
// Declare internals | |
var internals = { | |
schema: { | |
path: Joi.string().optional().default('/hapi-info') | |
} | |
}; | |
internals.generateHapiInfo = function() { | |
var self = this; | |
var payload = { | |
server: { | |
node: process.version, | |
hapi: self._sources[0].server.version, | |
host: self.info.host, | |
port: self.info.port, | |
uri: self.info.uri | |
}, | |
plugins: [] | |
}; | |
for (var i = 0, il = self._sources.length; i < il; ++i) { | |
var source = self._sources[i]; | |
if (!source._registrations) { | |
continue; | |
} | |
var registrations = Object.keys(source._registrations); | |
for (var j = 0, ij = registrations.length; j < ij; ++j) { | |
var pluginKey = registrations[j]; | |
var plugin = source._registrations[pluginKey]; | |
payload.plugins.push({ name: plugin.name, version: plugin.version }); | |
} | |
} | |
return payload; | |
}; | |
exports.register = function (server, options, next) { | |
var validation = Joi.validate(options, internals.schema); | |
Hoek.assert(validation.error === null, 'Invalid option(s)'); | |
options = validation.value; | |
server.expose('info', internals.generateHapiInfo.bind(server)); | |
server.route({ | |
method: 'GET', | |
path: options.path, | |
handler: function (request, reply) { | |
var payload = server.plugins[pkg.name].info(); | |
return reply(payload); | |
} | |
}); | |
next(); | |
}; | |
exports.register.attributes = { | |
pkg: pkg | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Load modules | |
var Code = require('code'); | |
var Glue = require('glue'); | |
var Hapi = require('hapi'); | |
var Lab = require('lab'); | |
// Test shortcuts | |
var lab = exports.lab = Lab.script(); | |
var expect = Code.expect; | |
var describe = lab.describe; | |
var it = lab.test; | |
var internals = {}; | |
internals.prepareServer = function (options, callback) { | |
var server = new Hapi.Server(); | |
var manifest = { | |
plugins: [ | |
{ '../': options }, | |
{ './plugins/blah.js': null }, | |
{ './plugins/main.js': null } | |
] | |
}; | |
Glue.compose(manifest, { relativeTo: __dirname }, function (err, server) { | |
expect(err).to.not.exist(); | |
return callback(err, server); | |
}); | |
}; | |
describe('routes', function () { | |
it('prints plugin and server information', function (done) { | |
internals.prepareServer({}, function (err, server) { | |
server.inject('/hapi-info', function (res) { | |
var result = { | |
server: { | |
node: process.version, | |
hapi: '8.8.1', | |
host: server.info.host, | |
port: server.info.port, | |
uri: server.info.uri | |
}, | |
plugins: [{ | |
name: 'hapi-info', | |
version: require('./../package.json').version | |
}, | |
{ | |
name: 'blah', | |
version: '1.2.3' | |
}, | |
{ | |
name: 'main', | |
version: '0.1.1' | |
}] | |
}; | |
expect(res.result).to.deep.equal(result); | |
done(); | |
}); | |
}); | |
}); | |
it('prints plugin and server to a different path', function (done) { | |
internals.prepareServer({ path: '/foo' }, function (err, server) { | |
server.inject('/foo', function (res) { | |
var result = { | |
server: { | |
node: process.version, | |
hapi: '8.8.1', | |
host: server.info.host, | |
port: server.info.port, | |
uri: server.info.uri | |
}, | |
plugins: [{ | |
name: 'hapi-info', | |
version: require('./../package.json').version | |
}, | |
{ | |
name: 'blah', | |
version: '1.2.3' | |
}, | |
{ | |
name: 'main', | |
version: '0.1.1' | |
}] | |
}; | |
expect(res.result).to.deep.equal(result); | |
done(); | |
}); | |
}); | |
}); | |
it('prints plugin and server info using expose', function (done) { | |
internals.prepareServer({ path: '/foo' }, function (err, server) { | |
var info = server.plugins['hapi-info'].info(); | |
var result = { | |
server: { | |
node: process.version, | |
hapi: '8.8.1', | |
host: server.info.host, | |
port: server.info.port, | |
uri: server.info.uri | |
}, | |
plugins: [{ | |
name: 'hapi-info', | |
version: require('./../package.json').version | |
}, | |
{ | |
name: 'blah', | |
version: '1.2.3' | |
}, | |
{ | |
name: 'main', | |
version: '0.1.1' | |
}] | |
}; | |
expect(info).to.deep.equal(result); | |
done(); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment