-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
173 lines (137 loc) · 3.97 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
const express = require('express');
const app = express();
const monk = require('monk');
const helpers = require('./serverHelpers.js')
const db = monk("localhost:27017/registermate");
const bodyParser = require("body-parser");
const expressSession = require('express-session');
const mdbstore = require('connect-mongodb-session')(expressSession);
const hbs = require('hbs');
const nodemailer = require('nodemailer');
const accounts = require('./accounts.js');
const classManager = require('./classmanager.js');
const registerManager = require('./registermanager.js');
const argv = require('yargs').argv;
if(argv.local)
{
global.URL = "http://localhost:8000"
global.isLocal = true;
var PORT = 8000;
}
else
{
global.URL = "http://rm.doc.gold.ac.uk";
var PORT = 80;
}
//global.URL = "http://www.doc.gold.ac.uk/www/275"
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
global.sessionstore = new mdbstore(
{
uri: 'mongodb://localhost:27017/connect_mongodb_session_test',
collection: 'mySessions'
});
// Catch errors
global.sessionstore.on('error', function(error) {
assert.ifError(error);
assert.ok(false);
});
app.set('trust proxy', 1) // trust first proxy
app.use(expressSession({
secret: 'fubalubalumpadump',
resave: false,
store: global.sessionstore,
cookie: { maxAge: 60000 * 60 * 12}, // 12 hrs
saveUninitialized: false
//flag for secure needs to be set here
}));
db.then(() => {
console.log('Connected correctly to server')
})
const users = db.get('users')
const students = db.get('students')
app.set('view engine', 'hbs');
hbs.registerPartials(__dirname + '/templates');
app.transporter = "";
if(!argv.local)
{
// init reusable transporter object using the default SMTP transport
app.transporter = nodemailer.createTransport({
host: 'igor.gold.ac.uk', //we should now be able to email igor accounts
port: 25,
secure: false, // true for 465, false for other ports
tls: {rejectUnauthorized: false}
});
}
app.use("/libs",express.static(__dirname + '/libs'));
app.use("/clientscripts",express.static(__dirname + '/clientscripts'));
app.listen(PORT, () => console.log('Example app listening on port ' + PORT + '!'));
//////////////////////////////////////INIT SUBMODULES//////////////////////////////////////
const accountsApp = new accounts(app);
const classApp = new classManager(app);
const registerApp = new registerManager(app);
if(argv.generateUsers)
{
accountsApp.generateFakeAccounts(argv.generateUsers);
}
///////////////////////////////////////FRONT PAGES////////////////////////////////////
app.get('/', (req, res) => res.redirect(URL + '/student/'))
app.get('/student', (req, res) =>
{
if(req.session.studentname == null)
{
res.render(__dirname + '/templates/student.hbs', {SERVER_URL: URL})
}
else
{
res.render(__dirname + '/templates/success.hbs',
{SERVER_URL: URL, username: req.session.studentname, isLate: req.session.islate})
}
})
app.get('/teacher', (req, res) =>
{
if (req.session.username != null && req.session.token != null)
{
// Already logged in.
if(req.session.role == "teacher")
{
res.render(__dirname + '/templates/teacherMenu.hbs', {SERVER_URL: URL});
}
else
{
res.render(__dirname + '/templates/adminMenu.hbs', {SERVER_URL: URL});
}
}
else
{
res.render(__dirname + '/templates/login.hbs', {SERVER_URL: URL});
}
})
app.get('/createclass', (req, res) =>
{
if (req.session.username != null && req.session.token != null)
{
res.render(__dirname + '/templates/createClass.hbs', {SERVER_URL: URL});
}
else
{
res.render(__dirname + '/templates/login.hbs', {SERVER_URL: URL});
}
})
app.get('/editclass' , (req, res) => {
if (req.session.username != null && req.session.token != null)
{
if(req.query._id)
{
res.render(__dirname + '/templates/editClass.hbs', {SERVER_URL: URL, CLASS_ID: req.query._id});
}
else
{
res.render(__dirname + '/templates/editClass.hbs', {SERVER_URL: URL});
}
}
else
{
res.render(__dirname + '/templates/login.hbs', {SERVER_URL: URL});
}
})