Last active
April 22, 2017 08:51
-
-
Save StarpTech/94c2c1591fbb16651b526ac0add52692 to your computer and use it in GitHub Desktop.
Hemera with Hapi
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
'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