-
Notifications
You must be signed in to change notification settings - Fork 861
/
app.js
258 lines (220 loc) · 7.29 KB
/
app.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
/* eslint-disable no-empty-label */
'use strict';
const express = require('express');
const cookie = require('cookie');
const compression = require('compression');
const path = require('path');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const generator = require('./backend/generator.js');
const deploy = require('./backend/deploy.js');
const config = require('../config.json');
const passport = require('passport');
const logger = require('./backend/buildlogger.js');
const Strategy = require('passport-github').Strategy;
const session = require('express-session');
const MemoryStore = session.MemoryStore;
const sessionStore = new MemoryStore();
const fs = require('fs');
const clientId = process.env.GITHUB_CLIENT_ID || config.GITHUB_CLIENT_ID;
const clientSecret = process.env.GITHUB_CLIENT_SECRET || config.GITHUB_CLIENT_SECRET;
const sessionSecret = process.env.SESSION_SECRET || config.SESSION_SECRET;
const callbackUrl = process.env.CALLBACK_URL || config.CALLBACK_URL;
const sentryUrl = process.env.SENTRY_DSN;
const ss = require('socket.io-stream');
const Raven = require('raven');
const redisClient = require('redis').createClient(process.env.REDIS_URL);
const Queue = require('bee-queue');
const queue = new Queue('generator-queue', {redis: redisClient});
const app = express();
// eslint-disable-next-line new-cap
const server = require('http').Server(app);
const io = require('socket.io')(server);
let parsedCookie, sid, folder;
let id = 0;
let count = 0;
let filename = '';
const emitter = null;
const socketObj = {};
if (sentryUrl) {
Raven.config(sentryUrl).install();
}
app.use(compression());
app.use(require('cookie-parser')());
app.use(session({secret: sessionSecret, resave: true, saveUninitialized: true, store: sessionStore}));
app.use(passport.initialize());
app.use(passport.session());
passport.use(new Strategy({
clientID: clientId,
clientSecret: clientSecret,
callbackURL: callbackUrl
}, function(accessToken, refreshToken, profile, cb) {
profile.token = accessToken;
return cb(null, profile);
}));
passport.serializeUser(function(user, cb) {
cb(null, user);
});
passport.deserializeUser(function(obj, cb) {
cb(null, obj);
});
io.on('connection', function(socket) {
socket.on('disconnect', function() {
});
id = id + 1;
socket.connId = id;
ss(socket).on('file', function(stream, file) {
generator.startZipUpload(count, socket);
console.log(file);
filename = path.join(__dirname, '..', 'uploads/connection-' + count.toString()) + '/upload.zip';
count += 1;
stream.pipe(fs.createWriteStream(filename));
});
socket.on('finished', function(msg) {
console.log(msg);
});
socket.on('progress', function(percent) {
console.log(percent + ' %');
});
socket.on('cancelled', function(msg) {
console.log(msg);
});
socket.on('live', function(formData) {
const req = {body: formData};
const job = queue.createJob(req);
job.on('succeeded', function() {
console.log('completed job ' + job.id);
});
job.save(async function(err, currentJob) {
if (err) {
console.log('job failed to save');
}
const currJobId = currentJob.id;
socketObj[currJobId] = socket;
console.log('saved job ' + currJobId);
const jobs = await queue.getJobs('waiting', {start: 0, end: 25});
const activeJob = await queue.getJobs('active', {start: 0, end: 25});
const jobIds = jobs.map((currJob) => currJob.id);
if (jobIds.indexOf(currJobId) !== -1) {
socket.emit('waiting');
logger.addLog('Info', 'Request waiting number: ' + (currJobId - activeJob[0].id), socket);
}
});
});
socket.on('upload', function(fileData) {
generator.uploadJsonZip(fileData, socket);
});
});
function getSocket(jobId) {
let tries = 0;
return new Promise((resolve, reject) => {
const tryGettingSocket = () => {
if (tries > 1)
console.log('Trying to get socket for job', jobId, 'Attempt: ', tries);
const socket = socketObj[jobId];
if (socket) {
resolve(socket);
} else {
if (tries > 5) {
reject(new Error('Socket not found in object after ' + tries + ' tries'));
return;
}
tries++;
setTimeout(tryGettingSocket, 2**tries);
}
}
tryGettingSocket();
});
}
queue.on('ready', function() {
queue.process(function(job, done) {
console.log('processing job ' + job.id);
const processId = job.id;
const jobs = new Promise(function(resolve) {
resolve(queue.getJobs('waiting', {start: 0, end: 25}));
});
getSocket(processId)
.then(socket => {
generator.createDistDir(job.data, socket, done);
jobs.then(function(waitingJobs) {
waitingJobs.forEach(function(waitingJob) {
logger.addLog('Info', 'Request waiting number: ' + (waitingJob.id - job.id), socketObj[waitingJob.id]);
});
});
})
.catch(error => {
console.error(error);
done(null);
});
});
console.log('processing jobs...');
});
io.of('/deploy').on('connection', function(socket) {
socket.on('start', function(msg) {
console.log(msg);
socket.abortDeploy = false;
parsedCookie = cookie.parse(socket.request.headers.cookie);
sid = cookieParser.signedCookie(parsedCookie['connect.sid'], sessionSecret);
folder = cookieParser.signedCookie(parsedCookie.folder, sessionSecret);
sessionStore.get(sid, function(err, currSession) {
if (err) {
console.log('error while getting session information');
console.log(err);
}
deploy(currSession.token, folder, currSession.owner, socket, function() {
console.log('Deploy Process Finished');
});
});
});
socket.on('stop', function(msg) {
console.log(msg);
socket.abortDeploy = true;
});
});
const errorHandler = function(err, req, res, next) {
res.sendFile(__dirname + '/www/404.html');
next();
console.log(err);
};
app.set('port', process.env.PORT || config.PORT);
// Use the www folder as static frontend
app.use('/', express.static(__dirname + '/www'));
app.use('/live/preview', express.static(__dirname + '/../dist'));
app.get('/download/:email/:appname', function(req, res) {
generator.pipeZipToRes(req.params.email, req.params.appname, res);
}).use(errorHandler);
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.post('/generate', function(req, res) {
generator.createDistDir(req, res, function(appFolder, url) {
console.log('App folder is ' + appFolder + ' and URL is ' + url);
});
});
app.get('/auth', passport.authenticate('github', {
scope: ['public_repo', 'delete_repo', 'read:org']
}));
app.get('/auth/callback',
passport.authenticate('github', {failureRedirect: '/auth', successRedirect: '/deploy'}));
app.get('/deploy', function(req, res) {
if (req.user === undefined) {
res.redirect('/');
return;
}
req.session.token = req.user.token;
req.session.owner = req.user.username;
res.sendFile(__dirname + '/www/deploy.html');
});
app.use('*', function(req, res) {
res.sendFile(__dirname + '/www/404.html');
});
server.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});
module.exports = {
getApp: function() {
return app;
},
getCount: function() {
return count;
}
};