-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
184 lines (139 loc) · 4.07 KB
/
app.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const path = require("path");
const bodyParser = require("body-parser");
const express = require("express");
const mongoose = require("mongoose");
const app = express();
const expressWs = require("express-ws")(app);
const javaScriptUtils = require("./app/utils/javascript-utils/javascript-utils");
const runtimeVariables = require("./configs/runtime-variables");
const Setting = require("./app/db/setting");
let setting;
mongoose.connect(runtimeVariables.dbURI, err => {
if (err) {
if (err.name === "MongoNetworkError") {
throw new Error("Incorrect MongoDB connection uri.");
}
throw err;
}
Setting.find({}, (err, data) => {
if (err) throw err;
const dbIsEmpty = data.length === 0;
if (dbIsEmpty) {
setting = new Setting();
setting.save(err => {
if (err) throw err;
});
} else {
setting = data[0];
}
});
});
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, "resources")));
app.use(express.static(path.join(__dirname, "scripts")));
let timer;
const createPeriodicMessageInterval = ws => {
Setting.findById(setting._id, (err, copiedSettings) => {
if (err) throw err;
timer = setInterval(() => {
// https://github.com/websockets/ws/issues/793
const isConnectionOpen = ws.readyState === ws.OPEN;
if (isConnectionOpen) {
ws.send(copiedSettings.periodicMessage);
}
}, copiedSettings.interval * 1000);
});
};
const sendPeriodicMessageToAllClients = () => {
expressWs.getWss().clients.forEach(client => {
createPeriodicMessageInterval(client);
});
};
app.get("/settings/current", (req, res) => {
if (!javaScriptUtils.isDefined(setting)) {
return res.sendStatus(500);
}
Setting.findById(setting._id, (err, copiedSettings) => {
if (err) throw err;
return res.status(200).json({
currentSettings: copiedSettings
});
});
});
app.post("/settings/onevent/save", (req, res) => {
if (!javaScriptUtils.isDefined(setting)) {
return res.sendStatus(500);
}
Setting.findOneAndUpdate(setting._id, req.body, err => {
if (err) throw err;
return res.sendStatus(200);
});
});
app.post("/settings/periodic/save", (req, res) => {
if (!javaScriptUtils.isDefined(setting)) {
return res.sendStatus(500);
}
Setting.findOneAndUpdate(setting._id, req.body, (err, updatedSettings) => {
if (err) throw err;
if (updatedSettings.isPeriodicMessageSendingActive) {
clearInterval(timer);
sendPeriodicMessageToAllClients();
}
return res.sendStatus(200);
});
});
app.get("/settings/periodic/start", (req, res) => {
if (!javaScriptUtils.isDefined(setting)) {
return res.sendStatus(500);
}
const data = {
isPeriodicMessageSendingActive: true
};
Setting.findOneAndUpdate(setting._id, data, err => {
if (err) throw err;
sendPeriodicMessageToAllClients();
return res.sendStatus(200);
});
});
app.get("/settings/periodic/stop", (req, res) => {
if (!javaScriptUtils.isDefined(setting)) {
return res.sendStatus(500);
}
const data = {
isPeriodicMessageSendingActive: false
};
Setting.findOneAndUpdate(setting._id, data, err => {
if (err) throw err;
clearInterval(timer);
return res.sendStatus(200);
});
});
app.get("/disconnect", (req, res) => {
if (!javaScriptUtils.isDefined(setting)) {
return res.sendStatus(500);
}
expressWs.getWss().clients.forEach(client => {
client.close();
});
return res.sendStatus(200);
});
app.ws("/", ws => {
ws.on("message", () => {
Setting.findById(setting._id, (err, copiedSettings) => {
if (err) throw err;
if (javaScriptUtils.isDefined(copiedSettings.onEventMessage)) {
ws.send(copiedSettings.onEventMessage);
}
});
});
Setting.findById(setting._id, (err, copiedSettings) => {
if (err) throw err;
if (copiedSettings.isPeriodicMessageSendingActive) {
createPeriodicMessageInterval(ws);
}
});
});
app
.use((req, res) => res.sendFile(runtimeVariables.indexFilePath))
.listen(runtimeVariables.port);