-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
373 lines (330 loc) · 11.6 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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
const express = require('express');
require('dotenv').config()
const MongoClient = require('mongodb').MongoClient
const app = express();
const exphbs = require('express-handlebars');
// Set express-handlebars as the template engine
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
//Intialize DB variables
let db
const uri = process.env.DB_HOST+process.env.DB_USER+":"+process.env.DB_PASS
+process.env.DB_SERVER+process.env.DB_NAME+process.env.DB_PARAMS
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true })
//Parse incoming data as JSON
app.use(express.json())
async function renderQuestionsAnnouncements(req, res, next) {
let questions = db.collection('questions').find({roomID: req.params.roomID}).toArray()
let announcements = db.collection('announcements').find({roomID: req.params.roomID}).toArray()
let roomNameServer = await db.collection('rooms').find({roomID: req.params.roomID}).next();
res.status(200).render('home', {
announcementArray: await announcements,
questionsArray: await questions,
roomIdHome: req.params.roomID,
roomNameHome: roomNameServer.roomName
})
}
app.get('/', function(req, res, next){
// Go to the landing page where user can create and join labs.
res.status(200).render('createJoinLab')
});
// Password must be checked for the room with the corresponding pin.
function passwordGood(pin, password){
return true
}
// User pressed the join room button. Take them to their room.
app.post('/rooms/join', function(req, res, next){
console.log("Join lab requested")
if(req.body && req.body.pin){
console.log("Join lab request: ", req.body.pin)
db.collection("rooms").findOne({roomID: req.body.pin})
.then(function(result) {
if(result){
console.log("Redirecting user to questions and announcements for the associated room.")
res.status(200).send()
}
else
{
console.log("Room doesn't exist")
res.status(403).send()
}
})
}
else{
console.log("Request invalid")
res.status(403).send()
}
})
//Handle request to room's pages
app.get('/:roomID', function(req, res, next) {
db.collection("rooms").findOne({roomID: req.params.roomID})
.then(function(result) {
if(result)
{
renderQuestionsAnnouncements(req, res, next)
} else {
next()
}
})
});
app.use(express.static('public'));
//Handle POSTS to add questions
app.post('/question/add', function(req, res, next) {
if(req.body && req.body.questionText && req.body.questionName && req.body.roomID)
{
console.log("Post request added: ")
console.log("- questionText: ", req.body.questionText)
console.log("- questionName: ", req.body.questionName)
console.log("- roomID: ", req.body.roomID)
db.collection("rooms").findOne({roomID: req.body.roomID})
.then(function(result) {
if (result) {
let questionObj = {text: req.body.questionText, name: req.body.questionName, roomID: req.body.roomID}
//Add to DB
db.collection("questions").insertOne(questionObj)
.then(function() {
res.status(200).send("Successfully added question!")
})
} else {
res.status(400).send("Error. Room does not exist!")
}
})
}
else
{
res.status(400).send("Request does not match path!")
}
})
// Endpoint to retrieve all data corresponding to a room
app.get('/:roomID/all-data', function(req, res, next) {
// Get questions
let questionsCursor = db.collection('questions').find({
roomID: req.params.roomID
});
questionsCursor.toArray().then(function(questions) {
// Get announcements
let announcementsCursor = db.collection('announcements').find({
roomID: req.params.roomID
});
announcementsCursor.toArray().then(function(announcements) {
// Get queueEntries
db.collection('rooms').findOne({roomID: req.params.roomID}).then(function(room) {
let people = room["people"];
for (let i = 0; i < people.length; i++) {
people[i].position = i + 1;
}
res.status(200).send(JSON.stringify(
{
questions: questions,
announcements: announcements,
people: people
}
));
});
});
});
})
//Handle POSTS to add announcements
//{announcementText: textValue, announcementAuthor: authorValue, taPassword: passwordValue, roomName: 'room1'}
app.post('/announcements/add', function(req, res, next) {
// First, make sure the password is correct
db.collection("rooms").findOne({roomID: req.body.roomID})
.then(function(result) {
if (result) {
// Verify password
if (result.password === req.body.password) {
let announcementObj = {text: req.body.announcementText, name: req.body.announcementAuthor, roomID:req.body.roomID}
//Add to DB
db.collection("announcements").insertOne(announcementObj)
.then(function() {
res.status(200).send("Successfully added announcement!")
})
}
else {
res.status(403).send("Invalid password!")
}
} else {
res.status(400).send("Error. Room not found!")
}
})
})
function roomExists(roomID) {
return true
}
//Handle POSTS to create rooms
app.post('/rooms/create', function(req, res, next) {
if(req.body && req.body.roomID && req.body.roomName && req.body.roomPassword)
{
if(!passwordGood(req.body.roomPassword)){
res.status(403).send("Invalid TA password.")
return
}
let roomObj = {roomID: req.body.roomID, roomName: req.body.roomName, password: req.body.roomPassword, people: []}
db.collection("rooms").findOne({roomID: req.body.roomID})
.then(function(result) {
if (!result) {
let roomObj = {roomID: req.body.roomID, roomName: req.body.roomName, password: req.body.roomPassword, people: []}
//Add to DB
db.collection("rooms").insertOne(roomObj).then(function(){
res.status(200).send("Successfully created room!")
})
} else {
res.status(400).send("Error. Room already exists!")
}
})
}
else
{
res.status(400).send("Request does not contain the correct JSON!")
}
})
//Update queue by adding person
app.put('/:roomID/queue/add', function(req, res, next) {
if(req.body && req.body.name &&
req.body.roomNumber && req.body.reqType && req.params.roomID)
{
if(req.body.reqType != "Checkoff" && req.body.reqType != "Question"){
res.status(400).send("Invalid request type!")
}
//Make sure room exists first
db.collection("rooms").findOne({roomID: req.params.roomID})
.then(function(result){
if(result) {
let personObj = { name: req.body.name,
roomNumber: req.body.roomNumber, reqType: req.body.reqType}
//Add to DB
db.collection("rooms").updateOne({roomID: req.params.roomID}, {$push: {people: personObj}})
.then(function() {
res.status(200).send("Successfully added to the queue!")
})
} else {
res.status(400).send("Room does not exist!")
}
})
} else {
res.status(400).send("Request does not contain the correct JSON!")
}
})
//Update queue by removing 1st person
app.delete('/:roomID/queue/remove', function(req, res, next) {
// TODO: Check auth
if(req.params.roomID)
{
//Make sure room exists first
db.collection("rooms").findOne({roomID: req.params.roomID}).then(function(result){
if(result) {
//Add to DB
db.collection("rooms").updateOne({roomID: req.params.roomID}, {$pull: {"people": result.people[req.body.index]}})
.then(function() {
res.status(200).send("Successfully removed from the queue!")
})
} else {
res.status(400).send("Room does not exist!")
}
})
} else {
res.status(400).send("Request does not match path!")
}
})
app.get('/:roomID/queue', function(req, res, next) {
db.collection("rooms").findOne({roomID: req.params.roomID}).then(function(result){
if(result) {
let people = result["people"];
for (let i = 0; i < people.length; i++) {
people[i].position = i + 1;
}
context = {people: people, roomID: req.params.roomID, roomName: result["roomName"], roomIdHome: req.params.roomID}
res.status(200).render('queue', context)
} else {
res.status(400).send("Room does not exist!")
}
})
// TODO: Get people from db
// Temporary solution: hard coded people
// context = {
// people: [
// {
// position: "1",
// name: "Bob",
// roomNumber: "1",
// reqType: "Question"
// },
// {
// position: "2",
// name: "Sally",
// roomNumber: "26",
// reqType: "Checkoff"
// },
// {
// position: "3",
// name: "Joey",
// roomNumber: "2",
// reqType: "Question"
// }
// ],
// roomID: req.params.roomID,
// roomName: "Super cool lab room thingy"
// };
// res.status(200).render('queue', context)
/*
// Make sure that the request is valid and the twit exists
if (roomExists(req.params.roomID)) { // TODO: implement this function lol
let context = {
roomID: req.params.roomID
};
res.status(200).render('queue', context);
}
else {
next();
}
*/
});
app.get('/:roomID/queue/ta', function(req, res, next) {
db.collection("rooms").findOne({roomID: req.params.roomID}).then(function(result){
if(result) {
// Check if auth headers are present
if (!req.headers.authorization) {
res.setHeader('WWW-Authenticate', 'Basic')
res.status(401).render('redirectToQueue', {roomID: req.params.roomID})
}
else {
// Check that password is correct (username doesn't really matter)
let auth = new Buffer.from(req.headers.authorization.split(' ')[1], 'base64').toString().split(':');
let username = auth[0];
let password = auth[1];
if (password === result.password) {
let people = result["people"];
for (let i = 0; i < people.length; i++) {
people[i].position = i + 1;
}
context = {people: people, roomID: req.params.roomID, roomName: result["roomName"], password: password, roomIdHome: req.params.roomID}
res.status(200).render('queue', context)
}
else {
res.setHeader('WWW-Authenticate', 'Basic')
res.status(401).render('redirectToQueue', {roomID: req.params.roomID})
}
}
} else {
res.status(400).send("Room does not exist!")
}
})
})
// 404 page
app.get('*', function(req, res, next) {
// TODO: change this to an actual 404 page
console.log(req.url);
res.status(404).render('404');
});
let port = process.env.PORT || 3000;
//Ensure DB connection before starting server
client.connect(function(err) {
db = client.db(process.env.DB_NAME)
if (err)
{
console.log(err)
}
app.listen(port, function() {
console.log('Server is listening on port ' + port);
});
})