1
0
mirror of https://github.com/yldio/copilot.git synced 2025-01-10 11:00:13 +02:00
copilot/spikes/metrics-service/http2/src/routes/metrics.js

39 lines
855 B
JavaScript
Raw Normal View History

2016-11-04 19:09:58 +02:00
const str = require('string-to-stream');
let messageId = 0;
module.exports = (server) => {
server.route({
method: 'GET',
path: '/stats',
handler: (request, reply) => {
request.raw.res.setHeader('Content-Type', 'text/event-stream');
const intervalId = setInterval(() => {
const resourcePath = `/resource/${messageId}`;
if (!request.raw.res.push) {
clearInterval(intervalId);
return;
}
const stream = request.raw.res.push(resourcePath, {
status: 200,
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
});
str(JSON.stringify({
msg: messageId
})).pipe(stream);
request.raw.res.write(`data:${resourcePath}\n\n`);
messageId += 1;
}, 100);
}
});
};