Skip to content

Instantly share code, notes, and snippets.

@victorpaulo
Created August 28, 2019 20:54
Show Gist options
  • Save victorpaulo/4842bed39438a5f33250c11c9796df53 to your computer and use it in GitHub Desktop.
Save victorpaulo/4842bed39438a5f33250c11c9796df53 to your computer and use it in GitHub Desktop.
ACME product microservice for IBM Watson Assistant
'use strict';
const express = require('express');
const Prometheus = require('prom-client')
const client = require('prom-client');
const bodyParser = require('body-parser');
var mongo = require('./mongo.js');
//Prometheus
const counterProductAll = new client.Counter({
name: 'product_request_calls_counter',
help: 'count product calls'
});
// Constants
const port = process.env.PORT || 8080;
// App
const app = express();
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
//products exposed as POST method and context '/products'
app.post('/products', (req, res) => {
console.log(req.headers)
console.log(req.body);
counterProductAll.inc();
res.set('Content-Type', "application/json")
if (req.body.param_name == 'Products') {
console.log("calling mongodb - retrieving Product list")
mongo.configDB.collection = 'product';
mongo.find(mongo.configDB, {}).then(result => {
console.log(result);
var text = result.map(function(elem){
return elem.name;
}).join("\n\u2022");
res.send({'response': '\n\u2022'.concat(text)})
}).catch(err=> {
res.send({'response': err})
})
}
})
app.get('/metrics', (req, res) => {
res.set('Content-Type', Prometheus.register.contentType)
res.end(Prometheus.register.metrics())
});
var server = app.listen(port, function () {
console.log('Server running at http://127.0.0.1:' + port + '/');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment