-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
255 lines (231 loc) · 6.89 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
const airtable = require("airtable");
const bodyParser = require("body-parser");
const child_process = require("child_process");
const EventSource = require("eventsource");
const express = require("express");
const fs = require("fs");
const helmet = require("helmet");
const https = require("https");
const path = require("path");
const passport = require("passport");
const LocalStrategy = require("passport-local").Strategy;
const queue = require("queue");
const session = require("express-session");
const SSE = require("sse");
const config = require("./config.json");
const base = new airtable({ apiKey: config.apiKey }).base(config.base);
const loadPeople = require("./lib/server/loadPeople.js");
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(helmet());
let peopleList = [];
let printQueue = queue({
autostart: true,
concurrency: 1,
timeout: 4000
});
let pingSub = {
id: 0,
subscribers: [],
subscribe(client) {
let clientID = this.id;
this.subscribers[clientID] = client;
this.id++;
return () => {
delete this.subscribers[clientID];
};
},
ping() {
for (let key of Object.keys(this.subscribers)) {
this.subscribers[key].send({
event: "ping",
data: "pong"
});
}
}
};
let log = msg => {
console.log(`[${new Date().toLocaleString()}] ${msg}`);
};
let lastLoaded = 0;
let docuSignTimeout = undefined;
let docuSignLoadPeople = _ =>
loadPeople(base, people => {
lastLoaded = +new Date();
peopleList = people;
log(`Loaded ${peopleList.length} people`);
pingSub.ping();
docuSignTimeout = undefined;
});
let docuSignES;
if (config.docuSignES != undefined) {
docuSignES = new EventSource(config.docuSignES);
docuSignES.addEventListener("docusign", _ => {
log("Received DocuSign ping");
let now = +new Date();
let elapsed = now - lastLoaded;
if (elapsed < 10000 && docuSignTimeout != undefined) {
docuSignTimeout = setTimeout(docuSignLoadPeople, lastLoaded + 10000 - now);
} else if (elapsed >= 10000) {
docuSignLoadPeople();
}
});
}
app.use(session({ secret: config.sessionSecret }));
app.use(passport.initialize());
app.use(passport.session());
passport.use(
"local",
new LocalStrategy((username, password, done) => {
let user = config.users.find(user => user.username == username && user.password == password);
if (user != undefined) {
log(`Logging in ${username}`);
return done(null, user);
} else {
return done(null, false, { message: "Incorrect username or password" });
}
})
);
app.post(
"/login",
passport.authenticate("local", {
successRedirect: "/",
failureRedirect: "/login.html",
failureFlash: false
})
);
app.get("/logout", (req, res) => {
req.logout();
res.redirect("/");
});
passport.serializeUser((user, done) => done(null, user.id));
passport.deserializeUser((id, done) => done(null, config.users.find(user => user.id == id)));
let authenticationMiddleware = (req, res, next) => {
if (req.isAuthenticated()) {
return next();
} else {
res.redirect("/login.html");
}
};
app.get("/", authenticationMiddleware, (req, res) => {
res.sendFile(path.join(__dirname, "public", "index.html"));
});
app.get("/index.html", authenticationMiddleware, (req, res) => {
res.sendFile(path.join(__dirname, "public", "index.html"));
});
app.use(express.static(path.join(__dirname, "public")));
app.get("/normalize.css", (req, res) => {
res.sendFile(path.join(__dirname, "node_modules", "normalize.css", "normalize.css"));
});
app.get("/hint.css", (req, res) => {
res.sendFile(path.join(__dirname, "node_modules", "hint.css", "hint.min.css"));
});
app.get("/refresh", authenticationMiddleware, (req, res) => {
docuSignLoadPeople();
res.send("Refreshed");
});
app.get("/people", authenticationMiddleware, (req, res) => {
res.json(peopleList);
});
app.post("/print", authenticationMiddleware, (req, res) => {
printQueue.push(cb => {
let filterRegex = /"$`/g;
let firstName = req.body.firstName.replace(filterRegex, "").trim();
let lastName = req.body.lastName.replace(filterRegex, "").trim();
if (firstName.length == 0 || lastName.length == 0) {
console.error(`Can't print '${firstName}' '${lastName}': name(s) are empty`);
res.send("");
return;
} else if (firstName.length > 25 || lastName.length > 25) {
console.error(`Can't print '${firstName}' '${lastName}': name(s) are too long`);
res.send("");
return;
}
// Spread out print jobs just in case DYMO Label chokes on simultaneous jobs
setTimeout(() => {
log(`Printing '${firstName}' '${lastName}'`);
try {
if (req.body.code != undefined && req.body.code.length != 0) {
child_process.execSync(
`osascript printBadge.applescript "${firstName}" "${lastName}" "${req.body.code}"`,
{
cwd: path.join(__dirname, "scripts")
}
);
} else {
child_process.execSync(`osascript printBadge.applescript '${firstName}' '${lastName}'`, {
cwd: path.join(__dirname, "scripts")
});
}
} catch (err) {
console.error(`Failed to print '${firstName}' '${lastName}'`, err);
pingSub.ping();
return;
}
let tableName;
switch (req.body.type) {
case "Attendee":
case "Mentor":
tableName = "AttendeesMentors";
break;
case "Chaperone":
tableName = "Chaperones";
break;
case "Sponsor":
case "Judge":
tableName = "SponsorsJudges";
break;
default:
console.error(`Invalid person type: ${req.body.type}`);
return;
}
base(tableName).update(
req.body.id,
{
"Checked In": true
},
(err, _) => {
if (err) {
console.error(
`Failed to check in ${req.body.type} ${firstName} ${lastName} (id: ${
req.body.id
}) in table ${tableName}`,
err
);
}
peopleList.find(person => person.id == req.body.id).checkedIn = true;
pingSub.ping();
}
);
cb();
}, 1000);
});
res.send("");
});
let server = https.createServer(
{
key: fs.readFileSync("./server.key"),
cert: fs.readFileSync("./server.crt")
},
app
);
let sse = new SSE(server);
sse.on("connection", client => {
let ipAddr = client.req.connection.remoteAddress;
log(`${ipAddr} joined`);
let unsubscribe = pingSub.subscribe(client);
client.on("close", () => {
log(`${ipAddr} left`);
unsubscribe();
});
});
loadPeople(base, people => {
peopleList = people;
log(`Loaded ${peopleList.length} people`);
});
let port = process.env.PORT || 8080;
server.listen(port, () => log(`Server is running on port ${port}`));
printQueue.start(err => {
if (err) console.error(err);
});