-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.js
260 lines (241 loc) · 7.03 KB
/
main.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
const express = require('express'),
mongoose = require('mongoose'),
passport = require('passport'),
localStrategy = require('passport-local'),
passportLocalMongoose = require('passport-local-mongoose'),
indexRouter = require('./routes/indexRouters'),
notificationRouter = require('./routes/notificationRouter'),
roomRouter = require('./routes/roomRouter'),
postRouter = require('./routes/postRouter'),
downloadRouter = require('./routes/downloadRouter'),
Student = require('./models/students'),
Teacher = require('./models/teachers'),
bodyParser = require('body-parser'),
Class = require('./models/class'),
flash = require('connect-flash'),
compiler = require('compilex'),
Notif = require('./models/notif'),
socketio = require('socket.io'),
jwt = require('jwt-simple'),
hackerEarth = require('hackerearth-node'),
config = require('config'),
connectDB = require('./config/db'),
cors = require('cors');
const HACKER_EARTH_CLIENT_SECRET_KEY = config.get('hackerEarthClientSecret');
const app = express();
mongoose.set('useCreateIndex', true);
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(flash());
const expressServer = app.listen(4000, () => console.log('Server is on!'));
const io = socketio(expressServer);
// MongoDB Atlas Connection
connectDB();
app.use(
require('express-session')({
secret: 'CALLBACK',
resave: false,
saveUninitialized: false,
})
);
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/public'));
app.use(passport.initialize());
app.use(passport.session());
passport.use('student', new localStrategy(Student.authenticate()));
passport.use('teacher', new localStrategy(Teacher.authenticate()));
passport.serializeUser((user, done) => {
done(null, user);
});
passport.deserializeUser((user, done) => {
if (user != null) {
done(null, user);
}
});
app.use((req, res, next) => {
res.locals.currentUser = req.user;
res.locals.out = req.flash('out');
res.locals.failureFlash = req.flash('failureFlash');
res.locals.mail = req.flash('mail');
res.locals.login = req.isAuthenticated();
next();
});
app.use(
cors({
allowedHeaders: ['sessionId', 'Content-Type'],
exposedHeaders: ['sessionId'],
origin: '*',
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
preflightContinue: false,
})
);
// Routers
app.use('/post', postRouter);
app.use('/notification', notificationRouter);
app.use('/download', downloadRouter);
app.use('/room', roomRouter.router);
app.use('/', indexRouter);
app.use('/register', require('./routes/registerRouter'));
var option = { stats: true };
compiler.init(option);
// Compilex & Hackerearth-node
// For C language
app.post('/compile/c', function (req, res) {
console.log('entered here');
var code = req.body.code;
var input = req.body.input;
// console.log(code);
// console.log(input);
// compiler.flush(function() {
// console.log('All previous temporary files flushed !');
// });
if (input === '') {
var envData = { OS: 'linux', cmd: 'gcc', options: { timeout: 1000 } };
compiler.compileCPP(envData, code, function (data) {
if (data.error) {
res.send({
output: data.error,
status: false,
});
} else {
output = data.output;
res.send({
output: output,
status: true,
});
}
});
} else {
var envData = { OS: 'linux', cmd: 'gcc', options: { timeout: 1000 } };
compiler.compileCPPWithInput(envData, code, input, function (data) {
if (data.error) {
res.send({
output: data.error,
status: false,
});
} else {
console.log(data.output);
res.send({
output: data.output,
status: true,
});
}
});
}
});
// For Java language
app.post('/compile/java', function (req, res) {
var code = req.body.code;
var input = req.body.input;
const hackerEarthy = new hackerEarth(HACKER_EARTH_CLIENT_SECRET_KEY, '');
var config = {};
config.time_limit = 1; //your time limit in integer
config.memory_limit = 323244; //your memory limit in integer
config.source = code; //your source code for which you want to use hackerEarth api
config.input = input; //input against which you have to test your source code
config.language = 'JAVA'; //language
//compile your code & run
hackerEarthy
.compile(config)
.then((result) => {
console.log(result);
const apiResult = JSON.parse(result);
var compileStatus = apiResult.compile_status;
if (compileStatus == 'OK') {
hackerEarthy
.run(config)
.then((result) => {
console.log(result);
const apiResult = JSON.parse(result);
console.log('result', apiResult.run_status.output);
res.send({
output: apiResult.run_status.output,
status: true,
});
})
.catch((err) => {
res.send({
output: apiResult,
status: false,
});
});
} else {
res.send({
output: compileStatus,
status: true,
});
}
})
.catch((err) => {
res.send({
output: 'Api Error',
status: false,
});
});
});
// /For Python Language
app.post('/compile/py', function (req, res) {
var code = req.body.code;
var input = req.body.input;
if (input === '') {
var envData = { OS: 'linux', options: { timeout: 1000 } };
compiler.compilePython(envData, code, function (data) {
if (data.error) {
res.send({
output: data.error,
status: false,
});
} else {
console.log(data.output);
res.send({
output: data.output,
status: true,
});
}
});
} else {
var envData = { OS: 'linux', options: { timeout: 1000 } };
compiler.compilePythonWithInput(envData, code, input, function (data) {
if (data.error) {
res.send({
output: data.error,
status: true,
});
} else {
console.log(data.output);
res.send({
output: data.output,
status: true,
});
}
});
}
});
var classes1 = [
{
year: 'TE',
department: 'IT',
batch_name: 'D1',
},
{
year: 'TE',
department: 'IT',
batch_name: 'D3',
},
];
var classes2 = [
{
year: 'TE',
department: 'IT',
batch_name: 'D1',
},
{
year: 'SE',
department: 'IT',
batch_name: 'D1',
},
];
io.on('connection', (socket) => {
roomRouter.page2(io, socket);
return io;
});