-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
216 lines (178 loc) · 5.93 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
require('dotenv').config();
const r = require('rethinkdb');
const express = require('express');
const app = require('express')();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const path = require('path');
const bodyparser = require('body-parser');
const passport = require('passport');
const cookieSession = require('cookie-session');
require('./public/modules/passport');
require("babel-polyfill");
const hackerEarthsdk=require('hackerearth-node'); //require the Library
const hackerEarth = new hackerEarthsdk(
process.env.HACKEREARTH_CLIENT_SECRET, //client secret key
''
);
const PORT = process.env.port|| 7001;
app.use(cookieSession({
name: 'tuto-session',
keys: ['key1', 'key2']
}))
// Static Middleware
app.set('view engine', 'ejs');
app.use('/public/',express.static(path.join(__dirname, 'public')))
app.use('/node_modules',express.static(path.join(__dirname,'node_modules')));
app.use(bodyparser.urlencoded({extended:false}));
app.use(bodyparser.json());
const isLoggedIn = (req, res, next) => {
if (req.user) {
next();
} else {
res.render(__dirname+'/views/templates/401.ejs')
res.end()
}
}
app.use(passport.initialize());
app.use(passport.session());
// Example protected and unprotected routes
app.get('/', (req, res) => res.render(__dirname+'/views/pages/login.ejs'))
app.get('/failed', (req, res) => res.send('You Failed to log in!'))
// In this route you can see that if the user is logged in u can acess his info in: req.user
app.get('/home', isLoggedIn, (req, res) =>{
var hash= req.body.room;
var username = req.body.user;
console.log("this is / = " + hash+username);
res.render(__dirname + '/views/pages/home.ejs',{
name:req.user.displayName,
pic:req.user.photos[0].value,
email:req.user.emails[0].value
});
})
// Auth Routes
app.get('/google', passport.authenticate('google', { scope: ['profile', 'email'] }));
app.get('/google/callback', passport.authenticate('google', { failureRedirect: '/failed' }),
function(req, res) {
// Successful authentication, redirect home.
res.redirect('/createrooms');
}
);
app.get('/logout', (req, res) => {
console.log(req.session);
req.session = null;
console.log(req.session);
req.logout();
res.redirect('/');
})
app.get('/createrooms',isLoggedIn, function(req,res,next){
res.render(__dirname + '/views/pages/create_room.ejs',{
name:req.user.displayName,
});
})
app.get('/joinrooms',isLoggedIn, function(req,res,next){
res.render(__dirname + '/views/pages/join_room.ejs',{
name:req.user.displayName,
});
})
r.connect({ host: process.env.RETHINKDB_HOST || "localhost", port: process.env.RETHINKDB_PORT || 28015,}, function(err, conn) {
if (err) throw err;
r
.db("test")
.tableList()
.run(conn, function(err, response) {
if (response.indexOf("edit") > -1) {
// do nothing it is created...
console.log("Table exists, skipping creation...");
console.log("Tables - " + response);
} else {
// create table...
console.log("Table does not exist. Creating table");
r
.db("test")
.tableCreate("edit")
.run(conn);
}
});
// Socket Stuff
io.on("connection", function(socket) {
// console.log( socket.client.conn.server.clientsCount + " users connected" );
io.sockets.emit('activeuser',{
description: socket.client.conn.server.clientsCount
})
console.log("user connected with id of" +socket.id);
socket.on("disconnect", function() {
//console.log( socket.client.conn.server.clientsCount );
io.sockets.emit('activeuser',{
description: socket.client.conn.server.clientsCount })
console.log("user disconnected with id of "+ socket.id );
});
socket.on("document-update", function(msg) {
console.log(msg);
r
.table("edit")
.insert(
{ id: msg.id, value: msg.value, user: msg.user },
{ conflict: "update" }
)
.run(conn, function(err, res) {
if (err) throw err;
//console.log(JSON.stringify(res, null, 2));
});
});
r
.table("edit")
.changes()
.run(conn, function(err, cursor) {
if (err) throw err;
cursor.each(function(err, row) {
if (err) throw err;
io.emit("doc", row);
});
});
});
app.get("/getData/:id", function(req, res, next) {
r
.table("edit")
.get(req.params.id)
.run(conn, function(err, result) {
if (err) throw err;
res.send(result);
//return next(result);
});
});
});
app.post('/home',(req,respo)=>{
let codes =req.body.data;
let inputs = req.body.input;
let lang = req.body.lang_val;
console.log("180 "+ inputs);
let cp,rn;
const config = {};
config.time_limit=1; //your time limit in integer
config.memory_limit=323244; //your memory limit in integer
config.source=codes; //your source code for which you want to use hackerEarth api
config.input=inputs; //input against which you have to test your source code
config.language=lang; //optional choose any one of them or none
hackerEarth.compile(config, (err, code) => {
if (err) {
console.log(err);
}
rn= JSON.parse(code);
// respo.write(rn)
});
console.log(cp);
hackerEarth.run(config, (err, code) => {
if (err) {
console.log(err);
}
rn=JSON.parse(code);
console.log(rn);
//respo.write(JSON.stringify(rn))
respo.json(rn);
});
})
http.listen(PORT, (err)=> {
if (err) console.log(err);
console.log("listening on: "+ PORT);
});