-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
559 lines (522 loc) · 14.2 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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
import express from 'express'
import crypto from 'crypto'
import mongoose from 'mongoose'
import cors from 'cors'
const app = express()
const port = 3000
import fetch from 'node-fetch'
// const eventId = "63f3b59791f11324dc3d29a3"
// const eventId = "640c6ba0a82bac563edf2655"
const eventId = process.env['eventId']
const roundTime = [{
startTime: new Date(process.env['startTime']),
endTime: new Date(process.env['endTime'])
}, {
startTime: Date.UTC(2024, 2, 20, 10, 30, 0),
endTime: Date.UTC(2024, 2, 20, 11, 0, 0)
}]
app.use(cors({
origin: '*',
optionsSuccessStatus: 200
}));
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
async function dbInit() {
await mongoose.connect(process.env['mongoURI'])
const Question = new mongoose.model('Question', new mongoose.Schema({
question: {
type: String,
required: true
},
description: {
type: String,
required: true
},
points: {
type: Number,
required: true
},
testcases: [{input: String, output: String}],
roundNo: {
type: Number,
required: true
}
}))
const User = new mongoose.model('User', new mongoose.Schema({
name: String,
userId: String,
token: String,
points: Number,
runsLeft: {
type: Number,
default: 3
},
startTime: [Number],
endTime: [Number],
questions: [String],
qualified: {
type: Boolean,
default: true
},
email: String,
phone: String,
gender: String,
dateOfBirth: String,
college: String,
degree: String,
year: Number,
stream: String,
coins: Number,
profileImageUrl: String
}))
return [Question, User]
}
const pModels = dbInit()
async function authenticateTokenUser(req) {
const authHeader = req.headers['authorization']
const token = authHeader && authHeader.split(' ')[1]
console.log(token)
if(!token) return false
const user = await (await pModels)[1].find({token: token})
if(user) return token
return false
}
app.get('/', function(req, res) {
res.send({
status: 'OK'
})
})
const token = process.env['token']
const adminAcc = [
{
userId: "admin",
hash: "hwYWBbB5F+t+hOagKMFgeorDcTm74lYcI9spoZ3e7IA="
},
{
userId: ""
}
]
async function authenticateTokenAdmin(req) {
const authHeader = req.headers['authorization']
const token = authHeader && authHeader.split(' ')[1]
console.log(token)
if(!token) return false
if(token===process.env['token']) return token
return false
}
app.post('/admin/login', async (req, res) => {
/*
Admin Login endpoint expects a JSON in it's body in this format
{
"userId": email or whatever is used to login
"pass": password
}
it will return a token which can be passed to authorised endpoints
*/
if(!req.body) {
res.status(400)
res.send({
error: "req body invalid"
})
}
const userId = req.body.userId
const hash = crypto.createHash('sha256').update(req.body.pass).digest('base64')
for (let acc of adminAcc) {
if(acc.userId===userId && acc.hash===hash) {
res.send({
token: token,
user: {
userType: "admin"
}
})
} else {
res.status(401)
res.send({
error: "Invalid Username or Password"
})
}
}
})
app.get('/questions', async (req, res) => {
/*
Returns all the questions as an array
*/
const Question = (await pModels)[0]
try {
if(req.query.id) {
const question = await Question.findById(req.query.id)
console.log(question)
res.send(question)
return
}
const questions = await Question.find()
console.log(questions)
res.send(questions)
} catch(error) {
res.send({
error: error
})
}
})
app.post('/questions', async (req, res) => {
/*
Add a question to the db, expects to follow schema but doesn't enforce it
{
title: String,
points: int,
testcases: [{input: String, output: String},...]
roundNo: int
}
the request body must be
{
question: Question,
token: String
}
*/
const question = req.body
if(authenticateTokenAdmin(req)) {
const Question = (await pModels)[0]
try {
const q = new Question(question)
console.log(q)
await q.save()
res.send("Success")
} catch (error) {
res.status(400)
res.send({
error: error
})
}
} else {
res.send("Invalid Token")
}
})
// app.post('/admin/advance', async (req, res) => {
// /*
// Call to move to next round, requires token to auth
// sets qualified to false for all the users below threshold
// */
// if (req.body!==token) {
// res.status(401)
// res.send({
// error: "Invalid Token"
// })
// } else {
// const users = await (await pModels)[1].find({ points: { $gt: 50 } })
// for (const user of users) {
// user.qualified=false
// await user.save()
// }
// res.send("Success")
// }
// })
app.post('/user/login', async (req, res) => {
/*
Login endpoint for users, expects a JSON in it's body in this format
{
"userId": for now, is espektro id, will be changed to email,
"password": password
}
*/
const userId = req.body.userId
const password = req.body.password
const User = (await pModels)[1]
const resp = await fetch(`https://tessarus.gdsckgec.in/api/events/checkin/`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
eventId: eventId,
espektroId: userId,
password: password
})
})
const respJson = await resp.json()
if(!respJson.user) {
console.log(respJson)
res.status(500)
res.send({ error: "Invalid Credentials"})
return
}
const user = respJson.user
const userInDB = await User.findById(user._id)
if(userInDB) {
res.send({
token: userInDB.token
})
} else {
const newUser = new User({
_id: user._id,
name: user.name,
userId: user.espektroId,
token: crypto.randomBytes(16).toString('base64'),
points: 0,
startTime: [],
endTime: [],
questionsCompleted: [],
email: user.email,
phone: user.phone,
gender: user.gender,
dateOfBirth: user.dateOfBirth,
college: user.college,
degree: user.degree,
year: user.year,
stream: user.stream,
coins: user.coins,
profileImageUrl: user.profileImageUrl
})
await newUser.save()
res.send({
token: newUser.token
})
}
})
app.get('/user/get', async (req, res) => {
/* Get the user details */
const token = await authenticateTokenUser(req)
if (!token) {
res.status(401)
res.send({
error: "No Token"
})
return
}
const User = (await pModels)[1]
try {
const user =await User.find({ token: token })
console.log(user)
res.send(user)
} catch {
res.status(401)
res.send({
error: "Invalid Token"
})
}
})
app.post('/user/start', async (req, res) => {
const token = await authenticateTokenUser(req)
if (!token) {
res.status(401)
res.send({
error: "No Token"
})
return
}
const { roundNo } = req.body
const [Question, User] = await pModels
if(Date.now() < roundTime[roundNo-1].startTime) {
console.log(roundTime[roundNo-1].startTime)
res.status(401)
res.send({
error: "Round not started"
})
return
}
try {
const user = (await User.find({ token: token }))[0]
console.log(user)
if(!user.qualified) {
res.status(401)
res.send({
error: "User not qualified"
})
return
}
// if(user.startTime[roundNo-1]) {
// res.status(401)
// res.send({
// error: "User already started the round"
// })
// return
// }
user.startTime[roundNo-1] = Date.now()
// Get a random question from the round
const questions = await Question.find({ roundNo: roundNo })
const question = questions[Math.floor(Math.random()*questions.length)];
user.questions[roundNo-1] = question._id
console.log(user)
await user.save()
res.send(question)
} catch(error) {
console.log(error)
res.status(401)
res.send({
error: error
})
}
})
app.post('/user/run', async (req, res) => {
const token = await authenticateTokenUser(req)
if (!token) {
res.status(401)
res.send({
error: "No Token"
})
return
}
const User = (await pModels)[1]
const user = (await User.find({ token: token }))[0]
if(!user) {
res.status(401)
res.send({
error: "Invalid Token"
})
}
if(user.runsLeft <= 0) {
res.status(401)
res.send({
error: "No runs left"
})
return
} else {
user.runsLeft--
await user.save()
}
// const { source_code, language_id, stdin } = req.body
console.log(req.body)
const judge = 'http://18.138.227.89:2358'
try {
const resp = await fetch(`${ judge }/submissions/?base64_encoded=true&wait=true`, {
method: 'POST',
body: JSON.stringify(req.body),
headers: {'Content-Type': 'application/json'}
})
const respJson = await resp.json()
console.log(respJson)
res.send(respJson)
} catch {
res.status(500)
res.send({
error: "Judge is down"
})
}
})
app.post('/user/disqualify', async (req, res) => {
const token = await authenticateTokenUser(req)
if (!token) {
res.status(401)
res.send({
error: "No Token"
})
return
}
const [_, User] = await pModels
try {
const user = (await User.find({ token: token }))[0]
user.qualified = false
await user.save()
res.send({
message: "User disqualified"
})
} catch(error) {
console.log(error)
res.status(401)
res.send({
error: error
})
}
})
app.post('/user/submit', async (req, res) => {
const token = await authenticateTokenUser(req)
console.log(token)
if (!token) {
res.status(401)
res.send({
error: "No Token"
})
return
}
const { code, langId } = req.body
const roundNo = req.body.roundNo - 1
const [Question, User] = await pModels
if(Date.now() > roundTime[roundNo].endTime) {
res.status(401)
res.send({
error: "Round has already ended"
})
return
}
const user = (await User.find({ token: token }))[0]
if(Date.now() - user.startTime[roundNo] > 3600000) {
res.status(401)
res.send({
error: "Time limit exceeded"
})
return
}
// console.log(user)
if(!user.qualified) {
res.status(401)
res.send({
error: "User not qualified"
})
return
}
if(user.endTime[roundNo]) {
res.status(401)
res.send({
error: "User has submitted the round"
})
return
}
user.endTime[roundNo] = Date.now()
const judge = 'https://judge0-new.gdsckgec.in/'
const q = await Question.findById(user.questions[roundNo])
let correct=0
try {
for(let i=0; i<q.testcases.length; i++){
const reqBody = {
source_code: code,
language_id: langId,
stdin: Buffer.from(q.testcases[i].input).toString('base64'),
expected_output: Buffer.from(q.testcases[i].output).toString('base64')
}
console.log(reqBody)
const resp = (await (await fetch(`${ judge }/submissions/?base64_encoded=true&wait=true`, {
method: 'POST',
body: JSON.stringify(reqBody),
headers: {'Content-Type': 'application/json'}
})).json())
console.log(resp)
if(resp.status.id == 3) correct++
}
} catch (error) {
console.error(error)
res.status(500)
res.send({
error: error
})
return
}
user.points = (correct*100)/q.testcases.length
if(user.points>50) {
user.qualified = true
} else {
user.qualified = false
}
await user.save()
res.send(user)
})
app.get('/leaderboard/:roundNo', async (req, res) => {
const User = (await pModels)[1]
const roundNo = req.params.roundNo-1
const users = await User.find()
users.sort((a, b) => {
if(a.points > b.points) {
return -1
} else if(a.points < b.points) {
return 1
} else {
if(a.endTime[roundNo] - a.startTime[roundNo] > b.endTime[roundNo] - b.startTime[roundNo]) {
return 1
} else {
return -1
}
}
})
res.send(users.filter(user => user.endTime[roundNo]))
})
app.listen(port, () => {
console.log(`Hello world app listening on port ${port}!`)
})