const express = require('express');
const http = require('http');
const request = require('request');
const path = require('path');
const WebSocket = require('ws');
const compression = require('compression');
const app = express();
const ws = new WebSocket(`ws://localhost:2080`);

require('dotenv').config();

app.set('view engine', 'hbs');

// app.use(compression());
app.use(require('node-sass-middleware')({
  src: path.join(__dirname, 'public'),
  dest: path.join(__dirname, 'public'),
  sourceMap: true
}));

app.use(express.static(path.join(__dirname, 'public')));

app.get('/', function (req, res) {

	let headers = {
		'Authorization': `Basic ${process.env.AUTH}`,
		'Content-Type': 'application/json',
	};

	let options = {
		rejectUnauthorized: false, // not ideal
		url: `${process.env.JIRA_URL}/rest/api/latest/search?jql=${process.env.JIRA_JQL}`,
		headers: headers
	};

	function callback(error, response, body) {

		// only send the issues over
		if (!error) {
			let parsedData = JSON.parse(body);
			let data = {
				meta: {
					total: parsedData.total,
					max: parsedData.maxResults
				},
				issues: parsedData.issues
			}
			ws.send(JSON.stringify(data));
		}
	}

	request(options, callback);

	res.render('index');
});

app.post('/jira/blocked/:project/:ticket/', function (req, res) {

	let headers = {
		'Authorization': `Basic ${process.env.AUTH}`,
		'Content-Type': 'application/json',
	};

	let options = {
		rejectUnauthorized: false, // not ideal
		url: `${process.env.JIRA_URL}/rest/api/latest/search?jql=${process.env.JIRA_JQL}`,
		headers: headers
	};

	function callback(error, response, body) {

		// only send the issues over
		if (!error) {
			let parsedData = JSON.parse(body);
			let data = {
				meta: {
					total: parsedData.total,
					max: parsedData.maxResults
				},
				issues: parsedData.issues,
				alert: []
			}
			ws.send(JSON.stringify(data));
		} else {
			console.log(error);
		}
	}

	ws.on('open', function open() {

		let data = {
			alert: [{
				key: req.params['project'],
				id: req.params['ticket'],
				user: req.params['user']
			}]
		};

		ws.send(JSON.stringify(data));

		request(options, callback);
	});

	res.send("ok");
});

const server = http.createServer(app);
const wss = new WebSocket.Server({ server });

wss.on('connection', function connection(ws) {

	ws.on('open', function open() {
		console.log('WS: open');
	});

	ws.on('close', function close() {
		console.log('WS: connection closed');
	});

	ws.on('message', function incoming(data) {

		wss.clients.forEach(function each(client) {
			client.send(data)
		});
	});
});

server.listen(process.env.PORT || 3000, function listening() {
	console.log('Listening on %d', server.address().port);
});
@@ -229,55 +229,14 @@
 ();%0A
-const ws = new WebSocket(%60ws://localhost:2080%60)
+let ws
 ;%0A%0Ar
@@ -295,19 +295,16 @@
 hbs');%0A%0A
-// 
 app.use(
@@ -319,16 +319,16 @@
 ion());%0A
+
 app.use(
@@ -550,32 +550,153 @@
 n (req, res) %7B%0A%0A
+%09if (ws) %7B%0A%09%09ws.onerror = ws.onopen = ws.onclose = null;%0A%09%09ws.close();%0A%09%7D%0A%0A%09ws  = new WebSocket(%60ws://localhost:2080%60);%0A%0A
 %09let headers = %7B
@@ -1259,16 +1259,18 @@
 %09%09%7D%0A%09%7D%0A%0A
+%0A%0A
 %09request
@@ -1314,24 +1314,24 @@
 dex');%0A%7D);%0A%0A
-
 app.post('/j
@@ -1385,16 +1385,137 @@
 res) %7B%0A%0A
+%09if (ws) %7B%0A%09%09ws.onerror = ws.onopen = ws.onclose = null;%0A%09%09ws.close();%0A%09%7D%0A%0A%09ws  = new WebSocket(%60ws://localhost:2080%60);%0A%0A
 %09let hea

Troubleshoot data display on rpi3