Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
388 lines (339 loc) · 12.1 KB
/
Copy pathserver.js
File metadata and controls
388 lines (339 loc) · 12.1 KB
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
const express = require('express');
const app = express();
const server = require('http').createServer(app);
// HTTPs server set up
const fs = require('fs');
var sslOptions = {
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem')
};
const httpsserver = require('https').createServer(sslOptions, app);
httpsserver.listen(8443);
const io = require('socket.io').listen(httpsserver);
const pg = require('pg');
const request = require('request');
var serveStatic = require('serve-static'); // serve static files
var easyrtc = require("./easyrtc"); // EasyRTC external module
var bodyParser = require('body-parser')
app.use(bodyParser.json()); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
// Connect to users database
const pool = new pg.Pool({
user: 'g1727138_u',
host: 'db.doc.ic.ac.uk',
database: 'g1727138_u',
password: 'fACG1SB1c9',
port: '5432'
});
// A dictionary of online users
const users = {};
const port = 8000;
server.listen(port);
// Specify to serve files from the public directory
app.use(express.static(__dirname + '/public'));
// set the view engine to ejs
app.set('view engine', 'ejs');
// HTML directory
const html_dir = __dirname + '/public/html/';
app.get('/', function(req, res) {
res.sendFile(__dirname + '/public/homepage/index.html')
});
app.get('/loginpage', function(req, res) {
res.sendFile(html_dir + 'login.html');
});
app.get('/login', function(req, res) {
res.sendFile(html_dir + 'login.html');
});
// When "Login" button is clicked ...
app.post('/login', function(req, res) {
// Query database for user and password
const query = "SELECT DISTINCT id, password FROM users WHERE id = '" + req.body.username + "'";
pool.query(query, (accessErr, accessResult) => {
// If username is not in database ...
if (accessResult.rowCount == 0) {
// ... Reload login page
res.sendFile(html_dir + 'login.html');
// If username and password is correct ...
} else if (accessResult.rows[0].password === req.body.password) {
//... Load tutor page
const listQuery = "SELECT * FROM tutorlist";
pool.query(listQuery, (listErr, listResult) => {
res.render('pages/tutorList', {
username: req.body.username,
tutors: listResult.rows
});
});
// Otherwise, password is incorrect ...
} else {
// ... Reload login page
res.sendFile(html_dir + 'login.html');
}
});
});
app.post('/tutorList', function(req, res) {
const listQuery = "SELECT * FROM tutorlist";
pool.query(listQuery, (listErr, listResult) => {
res.render('pages/tutorList', {
username: req.body.username,
tutors: listResult.rows
});
});
});
// When "Sign Up" button is clicked ...
app.get('/signup', function(req, res) {
res.sendFile(html_dir + 'signup.html');
});
app.post('/create_user', function(req, res) {
// Add tutor/student to the users database
let query = "INSERT INTO users VALUES (";
query += "'" + req.body.email + "', ";
query += "'" + req.body.firstname + "', ";
query += "'" + req.body.lastname + "', ";
query += "'" + req.body.facebook + "', ";
query += "'" + req.body.skype + "', ";
query += "'" + req.body.password + "')";
pool.query(query, (err, result) => {});
// Add tutor to the tutor database
if (req.body.tutor == 'true') {
let query = "INSERT INTO tutorlist VALUES (";
query += "'" + req.body.email + "', ";
query += "'" + req.body.firstname + "', ";
query += "'" + req.body.lastname + "', ";
query += "'" + req.body.description + "', ";
query += "'" + req.body.rph + "', ";
query += "'{" + req.body.skills.split(" ") + "}', ";
query += 0 + ", ";
query += "'" + req.body.facebook + "', ";
query += "'" + req.body.skype + "')";
pool.query(query, (err, result) => {
console.log(err);
});
}
});
// When the name of the tutor is clicked ...
app.get('/tutor_detail', function(req, res) {
const query = "SELECT * FROM tutorlist WHERE id='" + req.query.tutor_id + "'";
const review_query = "SELECT * FROM tutorreviews WHERE tutorid='" + req.query.tutor_id + "'";
pool.query(query, (err, result) => {
pool.query(review_query, (error, review_result) => {
res.render('pages/tutorDetails', {
username: req.query.username,
id: req.query.tutor_id,
image: result.rows[0].image,
firstname: result.rows[0].first_name,
lastname: result.rows[0].last_name,
details: result.rows[0].details,
skills: result.rows[0].skills,
ratings: result.rows[0].ratings,
reviews: review_result.rows
});
})
});
});
app.post('/submit_review', function(req, res) {
// Add review to database
let query = "INSERT INTO tutorreviews VALUES (";
query += "'" + req.body.tutor_id + "', ";
query += "'" + req.body.username + "', ";
query += "'" + req.body.ratings + "', ";
query += "'" + req.body.review_title + "', ";
query += "'" + req.body.comment + "')";
pool.query(query, (err1, result1) => {
let rating_query = "SELECT AVG(rating) FROM tutorreviews WHERE tutorid='" + req.body.tutor_id + "'";
pool.query(rating_query, (err, result) => {
var average = result.rows[0].avg;
if (average === null) {
average = req.body.ratings;
}
console.log("Calculating average rating...");
let update_rating_query = "UPDATE tutorlist SET ratings=" + Math.round(average * 100) / 100 + " WHERE id='" + req.body.tutor_id + "'";
pool.query(update_rating_query, (error, res) => {});
});
});
});
app.post('/get_user_name', function(req, res) {
let query = "SELECT * FROM users WHERE id="
query += "'" + req.body.user + "'";
pool.query(query, (err, result) => {
console.log(err);
var name = result.rows[0].first_name + " " + result.rows[0].last_name;
res.send(name);
});
});
//When "Code Up!" button is clicked ...
app.get('/code', function(req, res) {
res.render('pages/code', {
tutorID: req.query.tutorID,
username: req.query.username
});
//res.sendFile(html_dir + 'code.html');
});
//When "Compile" button is clicked ...
app.post('/compile', function(req, res) {
//var data = "public class Main{ public static void main(String[] args) {System.out.println(\"abc\");}}";
var data = req.body.data;
var lang = req.body.lang;
if (lang == 'Java') {
var compile_run = require('compile-run');
compile_run.runJava(data, "", function(stdout, stderr, err) {
if (!err) {
console.log(stdout);
console.log(stderr);
res.send(stdout + "\n" + stderr);
//res.send(stderr);
} else {
res.send(err);
console.log(err);
}
});
} else if (lang == 'Python') {
console.log("Python selected");
var compile_run = require('compile-run');
compile_run.runPython(data, "", function(stdout, stderr, err) {
if (!err) {
console.log(stdout);
console.log(stderr);
res.send(stdout + "\n" + stderr);
} else {
res.send(err);
console.log(err);
}
});
} else {
res.send("Language not supported. Only Java and Python is supported at the moment.");
}
});
// Code for chatbox function
io.sockets.on('connection', function(socket) {
socket.on('login', function(data) {
console.log("log in");
socket.username = data;
users[socket.username] = socket;
io.sockets.emit('user_connected', Object.keys(users));
});
socket.on('chatbox clicked', function(data) {
//username = data.user
//clicked user's username = data.to
// Query database for user and password
const query = "SELECT * FROM chattest ORDER BY sendtime ASC";
pool.query(query, (accessErr, accessResult) => {
//console.log("Err: " + accessErr);
var count = accessResult.rowCount;
for (var i = 0; i < count; i++) {
//msg from logged in user
if (accessResult.rows[i].fromuser === data.user &&
accessResult.rows[i].touser === data.to) {
var msg = accessResult.rows[i].msg;
if (msg.includes("Please Code with me:")) {
var arrto = data.to.split("@");
var arrfrom = data.user.split("@");
var tobereplace = "username=" + arrto[0];
var replacer = "username=" + arrfrom[0];
var newmsg = msg.replace(tobereplace, replacer);
newmsg = newmsg.replace(tobereplace, replacer);
try {
users[data.user].emit('display history', {
user: data.to,
pos: 'right',
msg: newmsg
});
} catch (error) {
console.log(error);
}
} else {
try {
users[data.user].emit('display history', {
user: data.to,
pos: 'right',
msg: msg
});
} catch (error) {
console.log(error);
}
}
}
//msg from clicked user
else if (accessResult.rows[i].fromuser === data.to &&
accessResult.rows[i].touser === data.user) {
try {
users[data.user].emit('display history', {
user: data.to,
pos: 'left',
msg: accessResult.rows[i].msg
});
} catch (error) {
console.log(error);
}
}
}
});
});
socket.on('to server', function(data) {
try {
if (data.to !== data.from) {
users[data.to].emit('to client', {
from: data.from,
msg: data.msg
});
}
} catch (error) {
console.log(error);
}
var date = new Date().toISOString().slice(0, 19).replace('T', ' ');
let msg = data.msg.replace(/[']/g, "'$&");
console.log(msg);
let query = "INSERT INTO chattest VALUES (";
query += "'" + data.to + "', ";
query += "'" + data.from + "', ";
query += "'" + msg + "', ";
query += "'" + date + "')";
pool.query(query, (err, result) => {
console.log(err);
});
});
socket.on('disconnect', function() {
// Remove the user from the online list
delete users[socket.username];
io.sockets.emit('user_disconnected', Object.keys(users));
});
socket.on('drawing', (data) => socket.broadcast.emit('drawing', data));
});
// Set process name
process.title = "node-easyrtc";
// Setup and configure Express http server. Expect a subfolder called "static" to be the web root.
//var app = express();
//app.use(serveStatic('static', {'index': ['index.html']}));
// Start Express http server on port 8080
//var webServer = http.createServer(app);
// Start Socket.io so it attaches itself to Express server
//var socketServer = socketIo.listen(webServer, {"log level":1});
easyrtc.setOption("logLevel", "debug");
// Overriding the default easyrtcAuth listener, only so we can directly access its callback
easyrtc.events.on("easyrtcAuth", function(socket, easyrtcid, msg, socketCallback, callback) {
easyrtc.events.defaultListeners.easyrtcAuth(socket, easyrtcid, msg, socketCallback, function(err, connectionObj) {
if (err || !msg.msgData || !msg.msgData.credential || !connectionObj) {
callback(err, connectionObj);
return;
}
connectionObj.setField("credential", msg.msgData.credential, {
"isShared": false
});
console.log("[" + easyrtcid + "] Credential saved!", connectionObj.getFieldValueSync("credential"));
callback(err, connectionObj);
});
});
// To test, lets print the credential to the console for every room join!
easyrtc.events.on("roomJoin", function(connectionObj, roomName, roomParameter, callback) {
console.log("[" + connectionObj.getEasyrtcid() + "] Credential retrieved!", connectionObj.getFieldValueSync("credential"));
easyrtc.events.defaultListeners.roomJoin(connectionObj, roomName, roomParameter, callback);
});
// Start EasyRTC server
var rtc = easyrtc.listen(app, io, null, function(err, rtcRef) {
console.log("Initiated");
rtcRef.events.on("roomCreate", function(appObj, creatorConnectionObj, roomName, roomOptions, callback) {
console.log("roomCreate fired! Trying to create: " + roomName);
appObj.events.defaultListeners.roomCreate(appObj, creatorConnectionObj, roomName, roomOptions, callback);
});
});