-
Notifications
You must be signed in to change notification settings - Fork 11
/
server.js
367 lines (292 loc) · 9.95 KB
/
server.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
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
'use strict';
/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
require('babel/register');
var express = require('express');
var serialize = require('serialize-javascript');
var navigateAction = require('fluxible-router').navigateAction;
var debug = require('debug')('dockunit');
var React = require('react');
var app = require('./app');
var csrf = require('csurf');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var htmlComponent = React.createFactory(require('./components/Html.jsx'));
var updateCsrfToken = require('./actions/updateCsrfToken');
var updateCurrentUser = require('./actions/updateCurrentUser');
var updateLoginHeaderStatus = require('./actions/updateLoginHeaderStatus');
var updateLoginFormStatus = require('./actions/updateLoginFormStatus');
var readPost = require('./actions/readPost');
var constants = require('./constants');
var session = require('express-session');
var RedisStore = require('connect-redis')(session);
var createElement = require('fluxible-addons-react/createElementWithContext');
var sm = require('sitemap');
var favicon = require('express-favicon');
var robots = require('robots.txt');
var server = express();
server.set('state namespace', 'App');
server.use('/public', express.static(__dirname + '/build'));
server.use(cookieParser());
server.use(session({
secret: constants.sessionSecret,
resave: false,
saveUninitialized: false,
store: new RedisStore({
host: '127.0.0.1',
port: 6379
})
}));
server.use(bodyParser.json({ limit: '20mb' }));
server.use(bodyParser.urlencoded({ extended: true, limit: '20mb' }));
// Create DB connection
global.db = mongoose.connect(constants.mongoServer);
var LocalStrategy = require('passport-local').Strategy;
var passport = require('passport');
var User = require('./models/User');
var Project = require('./models/Project');
var Build = require('./models/Build');
server.post('/webhooks', require('./webhooks'));
var http = require('http').Server(server);
// Create Socket.io instance and setup client router
global.io = require('socket.io')(http);
var Sockets = new (require('./clients/Sockets'))();
server.use(csrf({cookie: true}));
var Github = require('./clients/Github');
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function (err, user) {
done(err, user);
});
});
// Setup passport strategy
passport.use(new LocalStrategy(function(username, password, done) {
User.findOne({ username: username }, function(err, user) {
if (err) {
return done(err);
}
if (!user) {
debug('User not found');
return done(null, false, { message: 'Unknown user ' + username });
}
user.comparePassword(password, function(err, isMatch) {
if (err) {
return done(err);
}
if(isMatch) {
return done(null, user);
} else {
debug('Password mismatch');
return done(null, false, { message: 'Invalid password' });
}
});
});
}));
server.use(passport.initialize());
server.use(passport.session());
// Get access to the fetchr plugin instance
var fetchrPlugin = app.getPlugin('FetchrPlugin');
// Register our "REST" services
fetchrPlugin.registerService(require('./services/users'));
fetchrPlugin.registerService(require('./services/projects'));
fetchrPlugin.registerService(require('./services/github'));
fetchrPlugin.registerService(require('./services/builds'));
// Set up the fetchr middleware
server.use(fetchrPlugin.getXhrPath(), fetchrPlugin.getMiddleware());
var sitemap = sm.createSitemap({
hostname: 'https://dockunit.io',
cacheTime: 600000, // 600 sec - cache purge period
urls: [
{ url: '/', changefreq: 'daily', priority: 0.8 },
{ url: '/register/', changefreq: 'daily', priority: 0.5 },
{ url: '/about/', changefreq: 'daily', priority: 0.3 },
{ url: '/blog/', changefreq: 'daily', priority: 0.7 }
]
});
server.get('/sitemap.xml', function(req, res) {
sitemap.toXML(function (xml) {
res.header('Content-Type', 'application/xml');
res.send(xml);
});
});
server.use(favicon(__dirname + '/assets/img/favicon.ico'));
server.use(robots(__dirname + '/robots.txt'));
server.use(function(req, res, next) {
var url = req.url;
if (url.match(/^\/svg\/*/i)) {
var branch,
repository,
ImageBuilder,
image,
urlParse = url.match(/\/svg\/([^\/]+)\/([^?]+)(.*)?/i);
if (urlParse) {
repository = urlParse[1] + '/' + urlParse[2];
if (!urlParse[3] || '?' === urlParse[3].trim()) {
branch = false;
} else {
branch = urlParse[3].replace(/\?(.*)/ig, '$1');
branch = branch.replace(/(.*)&(.*)/ig, '$1');
if (branch.match(/^date=.*/ig)) {
branch = false;
}
}
res.set('Cache-Control', 'max-age=3600, must-revalidate');
if (repository) {
ImageBuilder = require('./clients/ImageBuilder');
image = new ImageBuilder(repository, branch);
image.build().then(function() {
res.type('svg');
res.send(image.getImage()).end();
}, function() {
res.type('svg');
res.send('<svg xmlns="http://www.w3.org/2000/svg" width="1" height="1"></svg>').end();
});
return;
}
}
res.status(404).send('Not found').end();
}
return next();
});
// Handle login
server.post('/login', function(req, res, next) {
passport.authenticate('local', function(error, user, info) {
if (error) {
return next(error);
}
var url = req.body.redirectPath || '/';
if (!user) {
debug('Could not find user or wrong password');
req.session.errorNum = 1;
req.session.loginType = req.body.type;
req.session.redirectUrl = url;
return res.redirect(url);
}
req.logIn(user, function(error) {
if (error) {
return next(error);
}
debug('Logging in user');
if ('/login' === url) {
url = '/';
}
return res.redirect(url);
});
})(req, res, next);
});
// Handle logout
server.get('/logout', function(req, res){
debug('Logging out');
req.logout();
res.redirect('/');
});
// Handle Github access token
server.get('/projects/authorize', function(req, res){
if (req.user) {
var user = req.user.toObject();
if (req.query && user.githubStateToken === req.query.state && req.query.code) {
// POST to github for access token :)
Github.tokens.create(req.query.code, req.user.username).then(function(token) {
debug('Received github token ' + token);
user.githubAccessToken = token;
// GET username from token
Github.tokens.getUser(token).then(function(githubUsername) {
debug('Received github username ' + githubUsername);
var userQuery = User.find({ username: user.username });
userQuery.update({ githubAccessToken: token, githubUsername: githubUsername }, function() {
res.redirect('/projects');
});
});
});
}
}
});
// Handle main request
server.use(function(req, res, next) {
var context = app.createContext({
req: req,
xhrContext: {
_csrf: req.csrfToken()
}
});
if (req.user) {
var user = req.user.toObject();
debug('Updating current user context to ' + user);
context.executeAction(updateLoginHeaderStatus, 0);
context.executeAction(updateLoginFormStatus, 0);
req.session.errorNum = 0;
context.executeAction(updateCurrentUser, user);
} else if (req.session && req.session.errorNum && 1 === req.session.errorNum) {
debug('Updating login status');
if ('header' === req.session.loginType) {
context.executeAction(updateLoginHeaderStatus, req.session.errorNum);
} else {
req.url = '/login';
context.executeAction(updateLoginFormStatus, req.session.errorNum);
}
req.session.errorNum = 0;
}
debug('Setting Csrf token to ' + req.csrfToken());
context.executeAction(updateCsrfToken, req.csrfToken());
debug('Executing navigate action');
context.executeAction(navigateAction, {
url: req.url,
method: req.method
}, function (err) {
if (err) {
if (err.status && err.status === 404) {
next();
} else {
next(err);
}
return;
}
if (req.url.match(/^\/blog\/.+$/i)) {
context.executeAction(readPost, req.url.replace(/^\/blog\/(.+)\/?$/i, '$1'), function() {
debug('Exposing context state');
var exposed = 'window.App=' + serialize(app.dehydrate(context)) + ';';
debug('Rendering Application component into html');
var html = React.renderToStaticMarkup(htmlComponent({
context: context.getComponentContext(),
state: exposed,
markup: React.renderToString(createElement(context))
}));
debug('Sending markup');
res.type('html');
res.write('<!DOCTYPE html>' + html);
res.end();
});
} else {
debug('Exposing context state');
var exposed = 'window.App=' + serialize(app.dehydrate(context)) + ';';
debug('Rendering Application component into html');
var html = React.renderToStaticMarkup(htmlComponent({
context: context.getComponentContext(),
state: exposed,
markup: React.renderToString(createElement(context))
}));
debug('Sending markup');
res.type('html');
res.write('<!DOCTYPE html>' + html);
res.end();
}
});
});
server.set('port', process.env.PORT || 3000);
http.listen(server.get('port'), function() {
console.log('Listening on port ' + server.get('port'));
});