- Status: In development
hapi-hemera is a Hemera micro-services plugin for hapi. The plugin integrates the Hemera functionality into hapi and provide tools to map its actions to server methods and views for easy access.
const server = new Hapi.Server()
server.connection()
server.register({
register: require('hapi-hemera'),
options: {
hemera:{
name: 'test',
load: {
sampleInterval: 1000
},
logLevel: 'info'
},
nats: 'nats://localhost:4242',
// If you want to add hemera plugins
plugins: [
require('hemera-joi'),
require('hemera-stats')
],
// In case you want to use hapi server methods
methods: {
add: {
pattern: {
topic: 'math',
cmd: 'add',
timeout$: 5000,
}
// Optional caching parameters
cache: {
expiresIn: 60000,
staleIn: 30000,
staleTimeout: 10000,
generateTimeout: 100
}
}
}
}
})
server.route({
method: 'POST',
path: '/add',
handler: function (request, reply) {
return reply.act({ topic: 'math', cmd: 'add', a: 2, b: 2 })
}
}
server.route({
method: 'POST',
path: '/add',
handler: function (request, reply) {
return reply.hemera.act({ topic: 'math', cmd: 'add', a: 2, b: 2 },
function(err, result) {
reply(err || result)
})
}
}
server.route({
method: 'POST',
path: '/add',
handler: function (request, reply) {
server.methods.add({ a: 1, b: 2 }, (err, resp) => {
reply(err || result)
})
}
}
// as string
server.action('generate', 'topic:generator,cmd:id')
// or as object
server.action('generate', {
topic: 'generator',
cmd: 'id'
})
// or with cache
server.action('generate', 'topic:generator,cmd:id', {
cache: {
expiresIn: 1000,
generateTimeout: 3000
}
})
// call
server.methods.generate((err, result) => {})
Use body
server.route({
method: 'POST',
path: '/foo/{topic}/{cmd}',
handler: {
act: {
pattern: {
timeout$: 5000
}
}
}
})
curl -H "Content-Type: application/json" -X POST -d '{"a":2,"b":2}' http://localhost:3000/foo/math/add
use query parameters
server.route({
method: 'POST',
path: '/math/{cmd}',
config: {
validate: {
query: {
a: Joi.number().required(),
b: Joi.number().required()
}
}
},
handler: {
act: {
pattern: {
topic: 'math',
timeout$: 5000
}
}
}
})
curl http://localhost:3000/foo/math/add?a=2&b=2