<commit_before>/*******************************************************************************
 * Copyright (c) 2014 IBM Corporation and other Contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html 
 *
 * Contributors:
 * IBM - Initial Contribution
 *******************************************************************************/

var PUBLISH_TOPIC = "mqlight/sample/words";
	
var SUBSCRIBE_TOPIC = "mqlight/sample/wordsuppercase";

var SHARE_ID = "node-front-end";

var mqlightServiceName = "mqlight";

var http = require('http');
var express = require('express');
var fs = require('fs');
var mqlight = require('mqlight');
var bodyParser = require('body-parser');

/*
 * Establish MQ credentials
 */
var opts = {};
var mqlightService = {};
if (process.env.VCAP_SERVICES) {
	var services = JSON.parse(process.env.VCAP_SERVICES);
	console.log( 'Running BlueMix');
	if (services[ mqlightServiceName ] == null) {
		throw 'Error - Check that app is bound to service';
	}
	mqlightService = services[mqlightServiceName][0];
	opts.service = mqlightService.credentials.connectionLookupURI;
	opts.user = mqlightService.credentials.username;
	opts.password = <PASSWORD>;
} else {
	opts.service = 'amqp://localhost:5672';
}

/*
 * Establish HTTP credentials, then configure Express
 */
var httpOpts = {};
httpOpts.port = (process.env.VCAP_APP_PORT || 3000);

var app = express();



/*
 * Create our MQ Light client
 * If we are not running in Bluemix, then default to a local MQ Light connection  
 */
var mqlightSubInitialised = false;
var mqlightClient = mqlight.createClient(opts, function(err) {
	if (err) {
		console.error('Connection to ' + opts.service + ' using client-id ' + mqlightClient.id + ' failed: ' + err);
	} else {
		console.log('Connected to ' + opts.service + ' using client-id ' + mqlightClient.id);
	}
	/*
	 * Create our subscription
	 */
	mqlightClient.on('message', processMessage);
	mqlightClient.subscribe(SUBSCRIBE_TOPIC, SHARE_ID, 
		{credit : 1,
			autoConfirm : false,
			qos : 1}, function(err) {
				if (err) console.err("Failed to subscribe: " + err); 
				else {
					console.log("Subscribed");
					mqlightSubInitialised = true;
				}
			});
});

/*
 * Store a maximum of one message from the MQ Light server, for the browser to poll. 
 * The polling GET REST handler does the confirm
 */
var heldMsg;
function processMessage(data, delivery) {
	try {
		data = JSON.parse(data);
		console.log("Received response: " + JSON.stringify(data));
	} catch (e) {
		// Expected if we're receiving a Javascript object
	}
	heldMsg = {"data" : data, "delivery" : delivery};
}

/*
 * Add static HTTP content handling
 */
function staticContentHandler(req,res) {
  var url = req.url.substr(1);
  if (url == '') { url = __dirname + '/web/index.html';};
  if (url == 'style.css') {res.contentType('text/css');}
  fs.readFile(url,
	function (err, data) {
		if (err) {
			res.writeHead(404);
			return res.end('Not found');
		}
		res.writeHead(200);
		return res.end(data);
	});
}
app.all('/', staticContentHandler);
app.all('/*.html', staticContentHandler);
app.all('/*.css', staticContentHandler);
app.all('/images/*', staticContentHandler);

/*
 * Use JSON for our REST payloads
 */
app.use(bodyParser.json());

/*
 * POST handler to publish words to our topic
 */
app.post('/rest/words', function(req,res) {
	// Check we've initialised our subscription<commit_msg>Fixing web paths<commit_after>/*******************************************************************************
 * Copyright (c) 2014 IBM Corporation and other Contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html 
 *
 * Contributors:
 * IBM - Initial Contribution
 *******************************************************************************/

var PUBLISH_TOPIC = "mqlight/sample/words";
	
var SUBSCRIBE_TOPIC = "mqlight/sample/wordsuppercase";

var SHARE_ID = "node-front-end";

var mqlightServiceName = "mqlight";

var http = require('http');
var express = require('express');
var fs = require('fs');
var mqlight = require('mqlight');
var bodyParser = require('body-parser');

/*
 * Establish MQ credentials
 */
var opts = {};
var mqlightService = {};
if (process.env.VCAP_SERVICES) {
	var services = JSON.parse(process.env.VCAP_SERVICES);
	console.log( 'Running BlueMix');
	if (services[ mqlightServiceName ] == null) {
		throw 'Error - Check that app is bound to service';
	}
	mqlightService = services[mqlightServiceName][0];
	opts.service = mqlightService.credentials.connectionLookupURI;
	opts.user = mqlightService.credentials.username;
	opts.password = <PASSWORD>Service.credentials.password;
} else {
	opts.service = 'amqp://localhost:5672';
}

/*
 * Establish HTTP credentials, then configure Express
 */
var httpOpts = {};
httpOpts.port = (process.env.VCAP_APP_PORT || 3000);

var app = express();



/*
 * Create our MQ Light client
 * If we are not running in Bluemix, then default to a local MQ Light connection  
 */
var mqlightSubInitialised = false;
var mqlightClient = mqlight.createClient(opts, function(err) {
	if (err) {
		console.error('Connection to ' + opts.service + ' using client-id ' + mqlightClient.id + ' failed: ' + err);
	} else {
		console.log('Connected to ' + opts.service + ' using client-id ' + mqlightClient.id);
	}
	/*
	 * Create our subscription
	 */
	mqlightClient.on('message', processMessage);
	mqlightClient.subscribe(SUBSCRIBE_TOPIC, SHARE_ID, 
		{credit : 1,
			autoConfirm : false,
			qos : 1}, function(err) {
				if (err) console.err("Failed to subscribe: " + err); 
				else {
					console.log("Subscribed");
					mqlightSubInitialised = true;
				}
			});
});

/*
 * Store a maximum of one message from the MQ Light server, for the browser to poll. 
 * The polling GET REST handler does the confirm
 */
var heldMsg;
function processMessage(data, delivery) {
	try {
		data = JSON.parse(data);
		console.log("Received response: " + JSON.stringify(data));
	} catch (e) {
		// Expected if we're receiving a Javascript object
	}
	heldMsg = {"data" : data, "delivery" : delivery};
}

/*
 * Add static HTTP content handling
 */
function staticContentHandler(req,res) {
  var url = 'web/' + req.url.substr(1);
  if (url == 'web/') { url = __dirname + '/web/index.html'; }
  if (url == 'web/style.css') { res.contentType('text/css'); }
  fs.readFile(url,
	function (err, data) {
		if (err) {
			res.writeHead(404);
			return res.end('Not found');
		}
		res.writeHead(200);
		return res.end(data);
	});
}
app.all('/', staticContentHandler);
app.all('/*.html', staticContentHandler);
app.all('/*.css', staticContentHandler);
app.all('/images/*', staticContentHandler);

/*
 * Use JSON for our REST payloads
 */
app.use(bodyParser.json());

/*
 * POST handler to publish words to our topic
 */
app.post('/rest/words', function(req,res) {
	// Check we've initialised our subscription
{'fraction_non_alphanumeric': 0.18079800498753118, 'fraction_numerical': 0.0072042116929897475, 'mean_word_length': 4.250181818181818, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6562741', 'n_tokens_mistral': 2416, 'n_tokens_neox': 2227, 'n_words': 607}