1
|
|
|
"use strict"; |
2
|
|
|
|
3
|
|
|
const express = require("express"); |
4
|
|
|
const serveStatic = require("serve-static"); |
5
|
|
|
const path = require("path"); |
|
|
|
|
6
|
|
|
const SSE = require("sse"); |
7
|
|
|
const redis = require("redis"); |
|
|
|
|
8
|
|
|
const amqp = require("amqplib/callback_api"); |
9
|
|
|
|
10
|
|
|
const app = express(); |
11
|
|
|
|
12
|
|
|
// const passport = require('passport') |
13
|
|
|
// const BasicStrategy = require('passport-http').BasicStrategy |
14
|
|
|
// passport.use(new BasicStrategy( |
15
|
|
|
// function(username, password, done) { |
16
|
|
|
// //todo: obviously needs to change |
17
|
|
|
// if (username.valueOf() === "fullcycle" && password.valueOf() === "mining") |
18
|
|
|
// return done(null, true); |
19
|
|
|
// else |
20
|
|
|
// { |
21
|
|
|
// console.log("rejected!") |
22
|
|
|
// return done(null, false); |
23
|
|
|
// } |
24
|
|
|
// } |
25
|
|
|
// )); |
26
|
|
|
|
27
|
|
|
const services = require("./services"); |
28
|
|
|
const messages = require("./messages"); |
|
|
|
|
29
|
|
|
|
30
|
|
|
var api = require("./api.js"); |
31
|
|
|
|
32
|
|
|
function bail(err, conn) { |
33
|
|
|
console.error("bailing..."); |
34
|
|
|
console.error(err); |
35
|
|
|
if (conn) conn.close(function() { |
|
|
|
|
36
|
|
|
// if (doexit) |
37
|
|
|
// process.exit(1); |
38
|
|
|
}); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
//route all other calls to the home page. this is causing "path is not defined" in line 179 |
43
|
|
|
// app.get("/*", function(req, res) { |
44
|
|
|
// res.sendFile(path.join(__dirname, "/index.html"), function(err) { |
45
|
|
|
// if (err) { |
46
|
|
|
// res.status(500).send(err) |
47
|
|
|
// } |
48
|
|
|
// }) |
49
|
|
|
// }); |
50
|
|
|
|
51
|
|
|
//in production this serves up the react bundle |
52
|
|
|
app.use(serveStatic("../web/build") |
53
|
|
|
//, |
54
|
|
|
// passport.authenticate("basic", { session: false }) |
55
|
|
|
); |
56
|
|
|
app.use("/api", api); |
57
|
|
|
var server = app.listen(services.web.port, () => console.log(`Listening on port ${services.web.port}`)); |
|
|
|
|
58
|
|
|
function onWebError(error) { |
59
|
|
|
if (error.syscall !== "listen") { |
60
|
|
|
throw error; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
var bind = typeof port === "string" ? "Pipe " + port : "Port " + port; |
|
|
|
|
64
|
|
|
|
65
|
|
|
// handle specific listen errors with friendly messages |
66
|
|
|
switch (error.code) { |
67
|
|
|
case "EACCES": |
68
|
|
|
console.error(error.code + ":" + bind + " requires elevated privileges"); |
69
|
|
|
process.exit(1); |
|
|
|
|
70
|
|
|
break; |
71
|
|
|
case "EADDRINUSE": |
72
|
|
|
console.error(error.code + ":" + bind + " is already in use. Close the other app and try again"); |
73
|
|
|
process.exit(1); |
|
|
|
|
74
|
|
|
break; |
75
|
|
|
default: |
76
|
|
|
throw error; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
server.on("error", onWebError); |
80
|
|
|
|
81
|
|
|
var busConnect = null; |
82
|
|
|
|
83
|
|
|
function on_connect(err, conn) { |
84
|
|
|
if (err !== null) return bail(err); |
|
|
|
|
85
|
|
|
process.once("SIGINT", function() { conn.close(); }); |
86
|
|
|
|
87
|
|
|
busConnect = conn; |
|
|
|
|
88
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
//set up the full cycle alerts feed to send alerts to the browser |
92
|
|
|
var sse = new SSE(server); |
93
|
|
|
sse.on("connection", function (sseConnection) { |
94
|
|
|
console.log("new sse connection"); |
|
|
|
|
95
|
|
|
|
96
|
|
|
const qAlert = "alert"; |
97
|
|
|
let alertChannel = null; |
98
|
|
|
|
99
|
|
|
function alertMessage(msg) { |
100
|
|
|
if (msg) { |
101
|
|
|
//msg.content.toString() |
102
|
|
|
console.log(" [x] '%s'", "received alert message"); |
|
|
|
|
103
|
|
|
sseConnection.send({ |
104
|
|
|
event: "full-cycle-alert", |
105
|
|
|
data: msg.content.toString() |
106
|
|
|
}); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
function on_channel_open_alert(err, ch) { |
111
|
|
|
if (err !== null) { return bail(err, busConnect); } |
112
|
|
|
alertChannel = ch; |
113
|
|
|
ch.on("error", function (err) { |
114
|
|
|
console.error(err); |
115
|
|
|
console.log("channel Closed"); |
|
|
|
|
116
|
|
|
}); |
117
|
|
|
ch.assertQueue("", {exclusive: true}, function(err, ok) { |
118
|
|
|
var q = ok.queue; |
119
|
|
|
ch.bindQueue(q, qAlert, ""); |
120
|
|
|
ch.consume(q, alertMessage, {noAck: true}, function(err, ok) { |
|
|
|
|
121
|
|
|
if (err !== null) return bail(err, busConnect); |
|
|
|
|
122
|
|
|
console.log(" [*] Waiting for alert. To exit press CTRL+C."); |
|
|
|
|
123
|
|
|
}); |
124
|
|
|
}); |
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
const qMiner = "statisticsupdated"; |
128
|
|
|
let miner_channel = null; |
129
|
|
|
|
130
|
|
|
function minerMessage(msg) { |
131
|
|
|
if (msg) { |
132
|
|
|
//msg.content.toString() |
133
|
|
|
console.log(" [x] '%s'", "received miner message"); |
|
|
|
|
134
|
|
|
sseConnection.send({ |
135
|
|
|
event: "full-cycle-miner", |
136
|
|
|
data: msg.content.toString() |
137
|
|
|
}); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
function on_channel_open_miner(err, ch) { |
142
|
|
|
if (err !== null) return bail(err, busConnect); |
|
|
|
|
143
|
|
|
miner_channel = ch; |
144
|
|
|
ch.on("error", function (err) { |
145
|
|
|
console.error(err); |
146
|
|
|
console.log("miner channel Closed"); |
|
|
|
|
147
|
|
|
}); |
148
|
|
|
ch.assertQueue("", {exclusive: true}, function(err, ok) { |
149
|
|
|
var q = ok.queue; |
150
|
|
|
ch.bindQueue(q, qMiner, ""); |
151
|
|
|
ch.consume(q, minerMessage, {noAck: true}, function(err, ok) { |
|
|
|
|
152
|
|
|
if (err !== null) return bail(err, busConnect); |
|
|
|
|
153
|
|
|
console.log(" [*] Waiting for miner stats. To exit press CTRL+C."); |
|
|
|
|
154
|
|
|
}); |
155
|
|
|
}); |
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
const qSensor = "sensor"; |
159
|
|
|
let sensor_channel = null; |
160
|
|
|
function sensorMessage(msg) { |
161
|
|
|
if (msg) { |
162
|
|
|
//msg.content.toString() |
163
|
|
|
console.log(" [x] '%s'", "received sensor message"); |
|
|
|
|
164
|
|
|
sseConnection.send({ |
165
|
|
|
event: "full-cycle-sensor", |
166
|
|
|
data: msg.content.toString() |
167
|
|
|
}); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
function on_channel_open_sensor(err, ch) { |
172
|
|
|
if (err !== null) return bail(err, busConnect); |
|
|
|
|
173
|
|
|
sensor_channel = ch; |
174
|
|
|
ch.on("error", function (err) { |
175
|
|
|
console.error(err) |
176
|
|
|
console.log("sensor channel Closed"); |
|
|
|
|
177
|
|
|
}); |
178
|
|
|
ch.assertQueue("", {exclusive: true}, function(err, ok) { |
179
|
|
|
var q = ok.queue; |
180
|
|
|
ch.bindQueue(q, qSensor, ""); |
181
|
|
|
ch.consume(q, sensorMessage, {noAck: true}, function(err, ok) { |
|
|
|
|
182
|
|
|
if (err !== null) return bail(err, busConnect); |
|
|
|
|
183
|
|
|
console.log(" [*] Waiting for sensor. To exit press CTRL+C."); |
|
|
|
|
184
|
|
|
}); |
185
|
|
|
}); |
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
if (busConnect) |
189
|
|
|
{ |
190
|
|
|
busConnect.createChannel(on_channel_open_alert); |
191
|
|
|
busConnect.createChannel(on_channel_open_miner); |
192
|
|
|
busConnect.createChannel(on_channel_open_sensor); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
sseConnection.on("close", function () { |
196
|
|
|
console.log("lost sse connection"); |
|
|
|
|
197
|
|
|
if (alertChannel) alertChannel.close(); |
|
|
|
|
198
|
|
|
if (miner_channel) miner_channel.close(); |
|
|
|
|
199
|
|
|
if (sensor_channel) sensor_channel.close(); |
|
|
|
|
200
|
|
|
}); |
201
|
|
|
|
202
|
|
|
}); |
203
|
|
|
|
204
|
|
|
try { |
205
|
|
|
amqp.connect(services.messagebus.connection, on_connect); |
206
|
|
|
} |
207
|
|
|
catch(error) { |
208
|
|
|
console.error(error); |
209
|
|
|
} |
210
|
|
|
|