forked from theodi/nodejs-oauth-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
774 lines (643 loc) · 24.4 KB
/
index.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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
//Loads the config fomr config.env to process.env (turn off prior to deployment)
require("dotenv").config({ path: "./config.env" });
// index.js
/* EXPRESS */
const express = require('express');
const cors = require("cors");
const http = require('http');
const https = require('https');
const fs = require('fs-extra');
const dbo = require("./db/conn");
const tmp = require('tmp-promise');
const tar = require('tar');
const path = require('path');
const rimraf = require('rimraf');
const { ObjectId } = require("mongodb");
const session = require('express-session');
var api = require('./api');
var adaptapi = require('./adaptHandler');
var userHandler = require('./userHandler');
const xmlToHtml = require("./xml-to-html");
const adaptdb = require("./db/adapt-aat");
const aatBase = process.env.AAT_BASE;
const multer = require('multer');
const fetch = require('node-fetch');
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, './uploads'); // Set the destination directory for uploaded files
},
filename: (req, file, cb) => {
cb(null, file.originalname); // Set the filename for uploaded files
},
});
const upload = multer({ storage });
if (process.env.SSLKEY) {
var privateKey = fs.readFileSync(process.env.SSLKEY, 'utf8');
var certificate = fs.readFileSync(process.env.SSLCERT, 'utf8');
var credentials = {key: privateKey, cert: certificate};
}
const app = express();
app.use(express.urlencoded());
module.exports = app;
app.set('view engine', 'ejs');
app.use(session({
resave: false,
saveUninitialized: true,
secret: 'SECRET'
}));
//Put the user object in a global veriable so it can be accessed from templates
app.use(function(req, res, next) {
try {
res.locals.user = req.session.passport.user;
next();
} catch (error) {
res.locals.user = req.session.user;
next();
}
});
app.use(express.static(__dirname + '/public'));
app.use(cors());
app.use(express.json());
app.get('/', function(req, res) {
if (req.session.passport) {
res.redirect("/profile");
} else {
res.locals.pageTitle ="ODI Template (NodeJS + Express + OAuth)";
res.render('pages/auth');
}
});
/* PASSPORT SETUP */
const passport = require('passport');
app.use(passport.initialize());
app.use(passport.session());
app.set('view engine', 'ejs');
app.post('/logout', function(req, res, next){
req.logout(function(err) {
if (err) { return next(err); }
res.redirect('/');
});
});
app.get('/error', (req, res) => res.send("error logging in"));
passport.serializeUser(function(user, cb) {
cb(null, user);
});
passport.deserializeUser(function(obj, cb) {
cb(null, obj);
});
/* Google AUTH */
const GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
const GOOGLE_CLIENT_ID = process.env.GOOGLE_CLIENT_ID;
const GOOGLE_CLIENT_SECRET = process.env.GOOGLE_CLIENT_SECRET;
const CALLBACK_URL = "//"+process.env.HOST+"/auth/google/callback"
passport.use(new GoogleStrategy({
clientID: GOOGLE_CLIENT_ID,
clientSecret: GOOGLE_CLIENT_SECRET,
callbackURL: CALLBACK_URL
},
function(accessToken, refreshToken, profile, done, req, res) {
/* No idea what I'm doing here */
email = profile.emails[0].value;
var dbConnect = dbo.getDb();
dbConnect
.collection("Users")
.find({'emails.0.value': email})
.toArray(function(err,items) {
if (items.length < 1) {
userHandler.insertUser(res,dbo,profile);
return done(null, profile);
} else if (items[0].suspended.toString() == "true") {
return done(null, null);
} else {
userHandler.updateLastLoginTime(res,dbo,email);
return done(null, profile);
}
});
}
));
app.get('/auth/google',
passport.authenticate('google', { scope : ['profile', 'email'] }));
app.get('/auth/google/callback',
passport.authenticate('google', { failureRedirect: '/error' }),
function(req, res) {
// Successful authentication, redirect success.
res.redirect('/profile');
});
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated())
return next();
else
unauthorised(res);
}
app.use('/private', ensureAuthenticated);
app.use('/private', express.static(__dirname + '/private'));
/* Define all the pages */
/* Do not require login */
app.get('/page1', function(req, res) {
res.locals.pageTitle = "Page 1";
res.render('pages/page1');
});
/*
* e.g. /questionSummary?activity=https://learning.theodi.org/xapi/activities/mit-moral-machine-test#/id/630f81656b4097008b2afd6f_branching_0
* e.g. https://theodi.stream.org/xapi/activities/learning-lockker-stand-alone-xapi-test-dt%23/id/5fd8d72191349e0067628eb3
*/
app.get('/questionSummary', function(req, res) {
res.locals.pageTitle = "Question insights";
res.locals.activity = encodeURIComponent(req.query.activity);
res.render('pages/questionSummary');
});
app.post('/interactions', express.json(), async function(req, res){
// Extract relevant data from the request body
const { studentId, _component, _componentId, _userAnswer, _userFeedback } = req.body;
// Check if the required fields are present in the request body
if (!_userAnswer || !_userFeedback || !_component || !studentId || !_componentId) {
return res.status(400).json({ error: 'Missing required fields' });
}
const timestamp = new Date().getTime();
try {
var dbConnect = dbo.getDb();
const newInteraction = {
studentId,
timestamp,
_component,
_componentId,
_userAnswer,
_userFeedback
};
const interactionsCollection = dbConnect.collection('Interactions');
// Insert the new interaction into the "Interactions" collection
const result = await interactionsCollection.insertOne(newInteraction);
// Return the ID of the created object to the user
res.status(201).json({ id: result.insertedId });
} catch (err) {
// Handle any errors that occur during the database operation
console.error('Error saving interaction:', err);
res.status(500).json({ error: 'Error saving interaction' });
}
});
app.post('/userAnswer', express.json(), async function(req, res) {
// Extract relevant data from the request body
const { componentID, componentType, userAnswer, items } = req.body;
// Check if the required fields are present in the request body
if (!componentID || !componentType || !userAnswer || !items) {
return res.status(400).json({ error: 'Missing required fields' });
}
const timestamp = new Date().getTime();
try {
// Assuming you have a database connection similar to 'dbo.getDb()'
var dbConnect = dbo.getDb();
const newUserAnswer = {
timestamp,
componentID,
componentType,
userAnswer,
items,
};
const transmitterDataCollection = dbConnect.collection('TransmitterData');
// Insert the new userAnswer into the "TransmitterData" collection
const result = await transmitterDataCollection.insertOne(newUserAnswer);
// Return the ID of the created object to the user
res.status(201).json({ id: result.insertedId });
} catch (err) {
// Handle any errors that occur during the database operation
console.error('Error saving userAnswer:', err);
res.status(500).json({ error: 'Error saving userAnswer' });
}
});
async function fetchAnswerSummary(componentID) {
try {
var dbConnect = dbo.getDb();
// Find all documents with the specified componentID
const cursor = dbConnect.collection('TransmitterData').find({ componentID });
// Initialize an array to store the collated userAnswers
let collatedAnswers = [];
// Iterate through the documents and collate userAnswers
await cursor.forEach(doc => {
const userAnswer = doc.userAnswer;
// Ensure that collatedAnswers array has the same length as userAnswer
if (collatedAnswers.length !== userAnswer.length) {
collatedAnswers = Array(userAnswer.length).fill(0);
}
for (let i = 0; i < userAnswer.length; i++) {
if (userAnswer[i]) {
collatedAnswers[i]++;
}
}
});
return collatedAnswers;
} catch (error) {
throw error;
}
}
app.get('/answerSummary/:componentID', async (req, res) => {
const componentID = req.params.componentID;
try {
const answerSummary = await fetchAnswerSummary(componentID);
res.json(answerSummary);
} catch (error) {
console.error('Error fetching answer summary:', error);
res.status(500).json({ error: 'Error fetching answer summary' });
}
});
// GET /interactions endpoint with query parameters
app.get('/interactions', async (req, res) => {
try {
const studentId = req.query.studentId;
const _componentId = req.query.componentId;
if (!studentId || !_componentId) {
return res.status(400).json({ error: 'Both studentId and componentId query parameters are required.' });
}
var dbConnect = dbo.getDb();
const interactionsCollection = dbConnect.collection('Interactions');
// Query the database for matching documents and sort by timestamp in descending order (most recent first)
const query = { studentId, _componentId };
const sortOptions = { timestamp: -1 };
const result = await interactionsCollection.find(query).sort(sortOptions).limit(1).toArray();
if (result.length === 0) {
return res.status(404).json({ error: 'No matching interaction found.' });
}
// Return the most recent interaction to the user
res.status(200).json(result[0]);
} catch (err) {
// Handle any errors that occur during the database operation
console.error('Error fetching interaction:', err);
res.status(500).json({ error: 'Error fetching interaction' });
}
});
app.get('/interactions/:id', async function(req, res) {
const interactionId = req.params.id;
// Check if the provided ID is a valid ObjectId
if (!ObjectId.isValid(interactionId)) {
return res.status(400).json({ error: 'Invalid ID format' });
}
try {
var dbConnect = dbo.getDb();
const interactionsCollection = dbConnect.collection('Interactions');
// Find the interaction by its ID
const interaction = await interactionsCollection.findOne({ _id: new ObjectId(interactionId) });
if (interaction) {
res.status(200).json(interaction);
} else {
res.status(404).json({ error: 'Interaction not found' });
}
} catch(err) {
// Handle any errors that occur during the database operation
console.error('Error fetching interaction:', err);
res.status(500).json({ error: 'Error fetching interaction' });
}
});
/* Require user to be logged in */
function unauthorised(res) {
res.locals.pageTitle = "401 Unauthorised";
return res.status(401).render("errors/401");
}
module.exports = {
unauthorised,
// Other exports as needed...
};
function forbidden(res) {
res.locals.pageTitle = "403 Forbidden";
return res.status(401).render("errors/403");
}
app.get('/profile', function(req, res) {
if (!req.isAuthenticated()) { unauthorised(res); return; }
res.locals.pageTitle = "Profile page";
// Get the user's IP address
let ip = req.headers['x-forwarded-for'] || req.socket.remoteAddress;
if (ip && ip.includes(',')) {
ip = ip.split(',')[0].trim();
}
var dbConnect = dbo.getDb();
dbConnect
.collection("Users")
.find({'emails.0.value': email})
.toArray(function(err,items) {
res.locals.profile = {};
res.locals.profile.userType = items[0].userType;
res.locals.profile.suspended = items[0].suspended;
res.locals.profile.lastLogin = items[0].lastLogin;
res.locals.profile.ip = ip;
req.session.profile = res.locals.profile;
res.render('pages/profile');
});
});
app.post("/moodle/course-extractor", upload.single('file'), async (req, res) => {
try {
// Access the uploaded file's path
const uploadedFilePath = req.file.path;
// Create a temporary directory for processing
const tmpDir = await tmp.dir();
// Rename the file if it has a .mbz extension to .tar.gz for clarity
if (path.extname(uploadedFilePath) === ".mbz") {
const newPath = path.join(tmpDir.path, 'upload.tar.gz');
await fs.rename(uploadedFilePath, newPath);
} else {
const newPath = path.join(tmpDir.path, 'upload.tar.gz');
await fs.copy(uploadedFilePath, newPath);
}
// Extract .tar.gz to the temporary directory
await tar.x({
file: path.join(tmpDir.path, 'upload.tar.gz'),
cwd: tmpDir.path,
});
// Perform the conversion using the extracted files in tmpDirPath
const activitiesPath = path.join(tmpDir.path, "activities");
const conversionResult = await xmlToHtml.convertFiles(activitiesPath);
// Clean up the temporary directory
await rimraf.sync(tmpDir.path);
// Send a response indicating success
res.send(conversionResult);
} catch (err) {
console.error(err);
res.status(500).send(err.message);
}
});
app.get('/moodle/course-extractor', function(req,res) {
if (!req.isAuthenticated()) { unauthorised(res); return; }
if (req.session.profile.userType == "user") { forbidden(res); return; }
res.locals.pageTitle = "Course " + req.params.id;
res.locals.id = req.params.id;
res.render('pages/moodle-extractor');
});
app.get('/courses', function(req, res) {
if (!req.isAuthenticated()) { unauthorised(res); return; }
if (req.session.profile.userType == "user") { forbidden(res); return; }
res.locals.pageTitle = "Courses";
res.render('pages/courses');
});
app.get('/course/:id', function(req, res) {
if (!req.isAuthenticated()) { unauthorised(res); return; }
if (req.session.profile.userType == "user") { forbidden(res); return; }
res.locals.pageTitle = "Course" + req.params.id;
res.locals.id = req.params.id;
res.locals.aatBase = aatBase + "/";
res.render('pages/contentObject');
});
app.get('/course/:id/dashboard', function(req,res) {
if (!req.isAuthenticated()) { unauthorised(res); return; }
if (req.session.profile.userType == "user") { forbidden(res); return; }
res.locals.pageTitle = "Course " + req.params.id;
res.locals.id = req.params.id;
res.render('pages/contentObjectDashboard');
});
app.get('/course/:id/transcript', function(req,res) {
if (!req.isAuthenticated()) { unauthorised(res); return; }
if (req.session.profile.userType == "user") { forbidden(res); return; }
res.locals.pageTitle = "Course " + req.params.id;
res.locals.id = req.params.id;
res.render('pages/contentObjectTranscript');
});
app.get('/users/', function(req,res) {
if (!req.isAuthenticated()) { unauthorised(res); return; }
if (req.session.profile.userType != "admin") { forbidden(res); return; }
res.locals.pageTitle = "Users";
res.render('pages/users');
});
app.get('/user/:id', function(req,res) {
if (!req.isAuthenticated()) { unauthorised(res); return; }
if (req.session.profile.userType != "admin") { forbidden(res); return; }
res.locals.pageTitle = "Edit user";
res.render('pages/editUserProfile', {userid: req.params.id, msg: ""});
});
/* API requests private methods */
app.get('/api/activityData', function (req, res) {
if (!req.isAuthenticated()) { unauthorised(res); return; }
if (req.session.profile.userType == "user") { forbidden(res); return; }
api.getActivityData(req, res, dbo);
});
/* The complete adapt API */
/**
* api/courses
* List all courses
*/
app.get('/api/courses', function(req,res) {
if (!req.isAuthenticated() && req.headers.host.split(":")[0] != "localhost") { unauthorised(res); return; }
if (req.session.profile.userType == "user") { forbidden(res); return; }
adaptapi.getObjectsFromCollection(req, res, adaptdb, "courses");
});
app.get('/api/contentObjects', function(req,res) {
if (!req.isAuthenticated() && req.headers.host.split(":")[0] != "localhost") { unauthorised(res); return; }
if (req.session.profile.userType == "user") { forbidden(res); return; }
adaptapi.getObjectsFromCollection(req, res, adaptdb, "contentobjects");
});
app.get('/api/contentObjects/summary', function(req,res) {
if (!req.isAuthenticated() && req.headers.host.split(":")[0] != "localhost") { unauthorised(res); return; }
if (req.session.profile.userType == "user") { forbidden(res); return; }
adaptapi.getCacheData(req, res, adaptdb, dbo);
});
app.get('/api/courseDetail', function(req,res) {
if (!req.isAuthenticated() && req.headers.host.split(":")[0] != "localhost") { unauthorised(res); return; }
if (req.session.profile.userType == "user") { forbidden(res); return; }
adaptapi.getCourses(req, res, dbo);
});
app.head('/api/contentObject/:id/transcript', function(req,res) {
//if (!req.isAuthenticated() && req.headers.host.split(":")[0] != "localhost") { unauthorised(res); return; }
//if (req.session.profile.userType == "user") { forbidden(res); return; }
adaptapi.getContentObjectHead(req, res, adaptdb, req.params.id);
});
app.get('/api/contentObject/:id/transcript', function(req,res) {
//if (!req.isAuthenticated() && req.headers.host.split(":")[0] != "localhost") { unauthorised(res); return; }
//if (req.session.profile.userType == "user") { forbidden(res); return; }
adaptapi.getContentObjectTranscript(req, res, adaptdb, req.params.id);
});
app.get('/api/contentObject/:id/outcomesMetadata', function(req,res) {
//if (!req.isAuthenticated() && req.headers.host.split(":")[0] != "localhost") { unauthorised(res); return; }
//if (req.session.profile.userType == "user") { forbidden(res); return; }
adaptapi.getContentObjectOutcomesMetadata(req, res, adaptdb, req.params.id);
});
app.head('/api/contentObject/:id', function(req,res) {
//if (!req.isAuthenticated() && req.headers.host.split(":")[0] != "localhost") { unauthorised(res); return; }
//if (req.session.profile.userType == "user") { forbidden(res); return; }
adaptapi.getContentObjectHead(req, res, adaptdb, req.params.id);
});
app.get('/api/contentObject/:id', function(req,res) {
if (!req.isAuthenticated() && req.headers.host.split(":")[0] != "localhost") { unauthorised(res); return; }
if (req.session.profile.userType == "user") { forbidden(res); return; }
adaptapi.getContentObject(req, res, adaptdb, req.params.id);
});
app.get('/api/course/:id/config', function(req,res) {
if (!req.isAuthenticated() && req.headers.host.split(":")[0] != "localhost") { unauthorised(res); return; }
if (req.session.profile.userType == "user") { forbidden(res); return; }
adaptapi.getCourseConfig(req, res, adaptdb, req.params.id);
});
/*
* Get Users
*
*/
app.get('/api/users', function(req,res) {
if (!req.isAuthenticated()) { unauthorised(res); return; }
if (req.session.profile.userType != "admin") { forbidden(res); return; }
userHandler.getUsers(req,res,dbo,req.query.format);
});
app.get('/api/user/:id', function(req,res) {
if (!req.isAuthenticated()) { unauthorised(res); return; }
if (req.session.profile.userType != "admin") { forbidden(res); return; }
userHandler.getUser(req,res,dbo,req.params.id,req.query.format);
});
/*
* Update user
*/
app.post('/user/:id', function(req, res, next){
if (!req.isAuthenticated()) { unauthorised(res); return; }
if (req.session.profile.userType != "admin") { forbidden(res); return; }
userHandler.updateUser(req,res,dbo,req.params.id);
});
/*
* API requests, public methods
*/
app.get('/api/questionSummary', function (req, res) {
api.getQuestionSummaryData(req, res, dbo);
});
/**
* api/collection
* Get all items in a collection for a specific parentId
* e.g. api/contentobjects/?parentId=xxxxxx
* e.g. api/blocks/?parentId=xxxxxx
*/
app.get('/api/:collection/', function(req,res) {
if (!req.isAuthenticated() && req.headers.host.split(":")[0] != "localhost") { unauthorised(res); return; }
if (req.session.profile.userType == "user") { forbidden(res); return; }
if (req.query.parentId) {
adaptapi.findObjectsWithParentId(req, res, adaptdb, req.params.collection, req.query.parentId);
} else {
return res.status(400).render("errors/400");
}
});
/**
* api/collection/:id
* Get data for a single item e.g. article, course, block, component
* e.g. api/contentobject/xxxxxx
* e.g. api/block/xxxxxx
*/
app.get('/api/:collection/:id', function(req,res) {
if (!req.isAuthenticated() && req.headers.host.split(":")[0] != "localhost") { unauthorised(res); return; }
if (req.session.profile.userType == "user") { forbidden(res); return; }
adaptapi.getObjectById(req, res, adaptdb, req.params.collection, req.params.id);
});
app.post('/api/:collection/:id', async function (req, res) {
if (!req.isAuthenticated()) {
unauthorised(res);
return;
}
const collection = req.params.collection;
const id = req.params.id;
try {
// Extract the JSON data from the request body
const requestData = req.body;
// Make an HTTP POST request to the external service using fetch
const response = await fetch(aatBase + ':3035/' + collection + '/' + id, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(requestData), // Pass the JSON data in the request body
});
// Check if the external service request was successful
if (response.ok) {
// Forward the response directly to the client
response.body.pipe(res);
} else {
// Handle the case where the external service returns an error
//console.error('External service returned an error:', response.statusText);
res.status(response.status).send(response.statusText);
}
} catch (error) {
// Handle any network or server errors
console.error('Error calling external service:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
app.post('/course/:courseId/resetPageLevelProgress', async function(req,res) {
if (!req.isAuthenticated()) { unauthorised(res); return; }
const courseId = req.params.courseId;
try {
// Make an HTTP GET request to the external service using fetch
const response = await fetch(aatBase+':3035/resetPageLevelProgress?course-id='+courseId, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
});
if (!response.ok) {
throw new Error(`External service returned status: ${response.status}`);
}
const data = await response.json();
// Return the response from the external service
res.status(response.status).json(data);
} catch (error) {
console.error('Error calling external service:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
app.post('/api/createAccess', async function (req, res) {
// Check authentication
if (!req.isAuthenticated()) {
unauthorised(res);
return;
}
// Extract data from the request body
const { ip, email } = req.body;
try {
// Make a request to the firewall manager endpoint
const response = await fetch(`${process.env.FIREWALL_MANAGER}/firewall`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ ip, email })
});
// Check if the request was successful
if (!response.ok) {
throw new Error('Failed to fetch firewall status');
}
// Parse the response JSON
const data = await response.json();
// Send the response back to the client
res.json(data);
} catch (error) {
// Handle any errors
console.error('Error fetching firewall status:', error.message);
res.status(500).json({ error: 'Internal Server Error' });
}
});
// Routes handled elsewhere
const packageHandler = require('./packageHandler');
app.use('/course/:courseId/packages', packageHandler);
//Keep this at the END!
app.get('*', function(req, res){
res.locals.pageTitle = "404 Not Found";
return res.status(404).render("errors/404");
});
adaptdb.connectToServer(function (err) {
if(err) {
console.log(err);
}
});
if (process.env.UPDATE_COURSE_CACHE == "true") {
setTimeout(() => {
adaptapi.updateCourseCache(adaptdb,dbo);
},1000);
setInterval(() => {
adaptapi.updateCourseCache(adaptdb,dbo);
},1800000);
}
/* Run server */
// perform a database connection when the server starts
dbo.connectToServer(function (err) {
if (err) {
console.error(err);
process.exit();
}
const port = process.env.PORT || 80;
// start the Express server
var httpServer = http.createServer(app);
httpServer.listen(port, () => {
console.log(`Server is running on port: ${port}`);
});
if (process.env.SSLKEY) {
const securePort = process.env.SECUREPORT || 443;
var httpsServer = https.createServer(credentials,app);
httpsServer.listen(securePort, () => {
console.log(`Server is running on port: 443`);
});
}
});