Skip to content

Instantly share code, notes, and snippets.

@StarpTech
Last active April 22, 2017 08:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StarpTech/94c2c1591fbb16651b526ac0add52692 to your computer and use it in GitHub Desktop.
Save StarpTech/94c2c1591fbb16651b526ac0add52692 to your computer and use it in GitHub Desktop.
Hemera with Hapi
'use strict'
const Hapi = require('hapi')
const server = new Hapi.Server()
server.connection({ port: 80 })
server.register({
register: require('hapi-hemera'),
options: {
hemera: {
name: 'test',
load: {
sampleInterval: 1
},
logLevel: 'info'
},
nats: 'nats://localhost:4222',
plugins: [
require('hemera-joi'),
require('hemera-stats')
]
}
}).then(() => {
return server.start()
}).then(() => {
console.log(`Server running at: ${server.info.uri}`);
// hemera can be used when the plugin was initialized
let Joi = server.hemera.exposition['hemera-joi'].joi
server.hemera.add({
topic: 'math',
cmd: 'add',
a: Joi.number().required(),
b: Joi.number().required()
}, function (req, cb) {
cb(null, parseInt(req.a) + parseInt(req.b))
})
server.route({
method: 'GET',
path: '/api/add',
handler: function (request, reply) {
request.hemera.act({
topic: 'math',
cmd: 'add',
a: request.query.a,
b: request.query.b,
refresh: !!request.query.refresh
}, function (err, result) {
if (err) {
return reply(Boom.wrap(err.cause, 400))
}
return reply(result)
})
}
})
})
.catch(console.error)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment