-
Notifications
You must be signed in to change notification settings - Fork 37
/
server.js
114 lines (94 loc) Β· 3.38 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const dotenv = require("dotenv");
if (process.env.NODE_ENV !== "test") {
dotenv.config();
}
const path = require("path");
const http = require("http");
const debug = require("debug")("server");
const express = require("express");
const socket = require("socket.io");
const expressSanitizer = require("express-sanitizer");
const bodyParser = require("body-parser");
const DashboardService = require("./lib/services/dashboard.service.js");
const DashboardMigrationService = require("./lib/services/dashboard-migrations.service.js");
const createDashboardFileIfItDoesNotExist = require("./lib/utilities/createDashboardFileIfItDoesNotExist.js");
const liveHub = require("./lib/liveHub.js");
const simHub = require("./lib/simHub.js");
const routes = require("./lib/routes.js");
// hub options
const connectionString = process.env.CONNECTION_STRING;
const consumerGroup = process.env.CONSUMER_GROUP || "$Default";
const partitionFilter = process.env.PARTITION_FILTER || [];
// server options
const simulating = process.env.SIMULATING || "true";
const platform = process.env.PLATFORM || "default";
const port = process.env.PORT || 3000;
const app = express();
const server = http.createServer(app);
const socketOptions = platform === "azure" ? { perMessageDeflate: false } : {};
const io = socket(server, socketOptions);
function receiveHandler(message) {
debug("firehose:", message.body);
io.sockets.emit("message", message);
}
function errorHandler(error) {
console.error(`there was a receiver error: ${error.toString()}`);
}
const hubOptions = {
connectionString,
consumerGroup,
partitionFilter,
receiveHandler,
errorHandler
};
const hub = simulating === "true" ? simHub : liveHub;
async function startServer(iotHubService) {
app.use(express.static(path.join(__dirname, "public")));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(expressSanitizer());
app.use("/", routes({ iotHubService }));
io.on("connection", function(socket) {
debug("a user connected");
socket.emit("hello");
});
// Make sure that the dashboard file is not modified and server is not started when running tests.
if (process.env.NODE_ENV !== "test") {
try {
const result = await createDashboardFileIfItDoesNotExist();
console.info(result.message);
console.info(`Your dashboard file is stored at β${result.filePath}β.`);
const dashboardSettings = await DashboardService.getDashboardSettings();
const wasUpgraded = DashboardMigrationService.upgradeDashboard(
dashboardSettings
);
if (wasUpgraded) {
console.info(
`Your dashboard was upgraded to version ${dashboardSettings.version}`
);
await DashboardService.saveDashboardSettings(dashboardSettings);
}
} catch (error) {
debug(error);
console.error(error);
}
// Note: This is `server.listen` intentionally. When using `app.listen`, it breaks socket.io.
server.listen(port, function() {
console.info(
`The hub server is now awake and listening at port ${port}.`
);
});
}
}
async function startHubServer() {
console.info("Starting the hub server β¦");
try {
const iotHubService = await hub.startService(hubOptions);
startServer(iotHubService);
} catch (error) {
console.error("Could not start the hub server.");
throw error;
}
}
startHubServer();
module.exports = app;