-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
David George
committed
Apr 13, 2017
1 parent
9f9dec2
commit 892f55d
Showing
5 changed files
with
140 additions
and
50 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
.env | ||
node_modules | ||
npm-debug.log | ||
npm-debug.log |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |