From 892f55d0cd4b4fc7cd926eeb6faf447827a49fac Mon Sep 17 00:00:00 2001 From: David George Date: Thu, 13 Apr 2017 11:11:03 +0000 Subject: [PATCH] Added mqlight connection --- .env => .env.sample | 3 ++ .gitignore | 3 +- app.js | 70 +++++++++++++++++++++++------------------ messagehub.js | 76 +++++++++++++++++++++++++++++++++++++++++++++ package.json | 38 ++++++++++++----------- 5 files changed, 140 insertions(+), 50 deletions(-) rename .env => .env.sample (58%) create mode 100644 messagehub.js diff --git a/.env b/.env.sample similarity index 58% rename from .env rename to .env.sample index 9b1efe4..c6fa498 100644 --- a/.env +++ b/.env.sample @@ -2,3 +2,6 @@ WORKSPACE_ID= CONVERSATION_USERNAME= CONVERSATION_PASSWORD= +MQLIGHT_LOOKUP_URL= +MQLIGHT_USER= +MQLIGHT_PASSWORD= \ No newline at end of file diff --git a/.gitignore b/.gitignore index 93f1361..4987d43 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ +.env node_modules -npm-debug.log +npm-debug.log \ No newline at end of file diff --git a/app.js b/app.js index 8d80817..6eacc08 100755 --- a/app.js +++ b/app.js @@ -1,6 +1,10 @@ +'use strict'; + // Configure dotenv to get environment variables require('dotenv').config(); +var messagehubConfig = require('./messagehub'); + // This application uses express as its web server // for more info, see: http://expressjs.com var express = require('express'); @@ -25,42 +29,46 @@ var conversation = new ConversationV1({ version_date: '2017-02-03' }); -// Enable body parsing -app.use(bodyParser.urlencoded({ extended: false })); -app.use(bodyParser.json()); +messagehubConfig().then(setup); -// Serve the files out of ./public as our main files -app.use(express.static(__dirname + '/public')); +function setup (mqlightClient) { -// Get the app environment from Cloud Foundry -var appEnv = cfenv.getAppEnv(); + // Enable body parsing + app.use(bodyParser.urlencoded({ extended: false })); + app.use(bodyParser.json()); + // Serve the files out of ./public as our main files + app.use(express.static(__dirname + '/public')); -var messageReceived = function (message, fn) { - // Setup payload - var payload = { - workspace_id: process.env.WORKSPACE_ID, - context: message.context, - input: message.input - } + // Get the app environment from Cloud Foundry + var appEnv = cfenv.getAppEnv(); - // Send message to Conversation service - conversation.message(payload, - function(err, data) { - if (err) { - return io.emit('message', err); + var messageReceived = function (message, fn) { + // Setup payload + var payload = { + workspace_id: process.env.WORKSPACE_ID, + context: message.context, + input: message.input + }; + + // Send message to Conversation service + conversation.message(payload, + function (err, data) { + if (err) { + return io.emit('message', err); + } + return fn(data); } - return fn(data); - } - ); -} + ); + } -// Start socket.io -io.on('connection', function (socket) { - socket.on('message', messageReceived); -}); + // Start socket.io + io.on('connection', function (socket) { + socket.on('message', messageReceived); + }); -// Start server -http.listen(appEnv.port, '0.0.0.0', function() { - console.log("server starting on " + appEnv.url); -}); + // Start server + http.listen(appEnv.port, '0.0.0.0', function() { + console.log("server starting on " + appEnv.url); + }); +} diff --git a/messagehub.js b/messagehub.js new file mode 100644 index 0000000..f7cb9d6 --- /dev/null +++ b/messagehub.js @@ -0,0 +1,76 @@ +'use strict'; + +// cfenv provides access to your Cloud Foundry environment +// for more info, see: https://www.npmjs.com/package/cfenv +var cfenv = require('cfenv'); +var mqlight = require('mqlight'); +var uuid = require('node-uuid'); +var winston = require('winston'); + +var SERVICE_NAME = 'messagehub'; + +// Get the app environment from Cloud Foundry +var appEnv = cfenv.getAppEnv(); + +// Establish credentials +var opts = {}; + +var service = appEnv.getService(SERVICE_NAME); + +if (service) { + opts.service = service.credentials.mqlight_lookup_url; + opts.user = service.credentials.user; + opts.password = service.credentials.password; + + if (!opts.hasOwnProperty('service') || + !opts.hasOwnProperty('user') || + !opts.hasOwnProperty('password')) { + throw new Error('Error - Check that app is bound to service'); + } +} +else if (process.env.MQLIGHT_LOOKUP_URL && + process.env.MQLIGHT_USER && + process.env.MQLIGHT_PASSWORD) { + opts.service = process.env.MQLIGHT_LOOKUP_URL; + opts.user = process.env.MQLIGHT_USER; + opts.password = process.env.MQLIGHT_PASSWORD; +} else { + throw new Error('No mqlight service is bound or configured'); +} + +opts.id = 'TUTORIALBOT_' + uuid.v4().substring(0, 7); + +// Message Handler +function handleMessage (data, delivery) { + winston.info('handleMessage', JSON.parse(data)); +} + +function config () { + return new Promise (function (resolve, reject) { + + winston.info('config', opts); + + var mqlightClient = mqlight.createClient(opts, function (err) { + if (err) { + winston.error('Connection to ' + opts.service + ' using client-id ' + + mqlightClient.id + ' failed: ' + err); + reject(err); + } else { + winston.info('Connected to ' + opts.service + ' using client-id ' + + mqlightClient.id); + } + + mqlightClient.on('message', handleMessage); + + mqlightClient.on('error', function (err) { + winston.error(err); + }); + + resolve(mqlightClient); + + }); + + }); +} + +module.exports = config; \ No newline at end of file diff --git a/package.json b/package.json index c69dfe1..384811b 100755 --- a/package.json +++ b/package.json @@ -1,20 +1,22 @@ { - "name": "tutorialbot", - "version": "0.0.1", - "private": true, - "scripts": { - "start": "node app.js" - }, - "dependencies": { - "express": "4.13.x", - "body-parser": "1.17.1", - "cfenv": "1.0.x", - "dotenv": "^2.0.0", - "socket.io": "1.7.3", - "watson-developer-cloud" : "2.8.2" - }, - "repository": {}, - "engines": { - "node": "4.x" - } + "name": "tutorialbot", + "version": "0.0.1", + "private": true, + "scripts": { + "start": "node app.js" + }, + "dependencies": { + "body-parser": "1.17.1", + "cfenv": "1.0.x", + "dotenv": "2.0.0", + "express": "4.13.x", + "socket.io": "1.7.3", + "mqlight": "2.0.2017033100", + "watson-developer-cloud": "2.8.2", + "winston": "2.3.1" + }, + "repository": {}, + "engines": { + "node": "6.x" + } }