-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
253 lines (214 loc) · 9.15 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
'use strict';
// Parallelize
const numCPUs = require('os').cpus().length;
const cluster = require('cluster');
const consoleStamp = require('console-stamp');
if (cluster.isMaster) {
// Setup timestamps for logging
consoleStamp(console,{
metadata: function () {
return ("[MASTER]");
},
colors: {
stamp: "yellow",
label: "white",
metadata: "red"
}
} );
// Fork workers.
for (let i = 0; i < numCPUs; i++) {
let worker = cluster.fork();
console.log("Spwaning worker " + worker.id);
}
cluster.on('exit', function(worker, code, signal) {
console.log(`worker ${worker.process.pid} died`);
let newWorker = cluster.fork();
console.log("Spwaning worker " + newWorker.id);
});
} else {
// Spawn various workers to listen and answer requests
const express = require('express');
const cookieParser = require('cookie-parser');
const path = require('path');
const compression = require('compression');
const watch = require('node-watch');
const passport = require('passport');
const googleStrategy = require('passport-google-oauth2').Strategy;
const universalAnalytics= require('universal-analytics');
const session = require('express-session');
const MongoStore = require('connect-mongo')(session);
const favicon = require('serve-favicon');
consoleStamp(console, {
metadata: function () {
return ("[Worker " + cluster.worker.id + "]");
},
colors: {
stamp: "yellow",
label: "white",
metadata: "green"
}
} );
// Make each worker connect to mongoose and startup the controllers
require(path.join(__dirname, "index.js")).connect(function(context){
// Define application address
const address = function(config){
var address = "";
if(config.application){
address += config.application.protocol || "http";
address += "://";
address += config.application.hostname || "localhost";
address += config.application.port !== undefined ? ":" : "";
address += config.application.port || "";
} else {
address += "http://localhost:3000";
}
return address;
}(context.config);
// Define application
const app = express();
// Express configuration
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, "views"));
app.set('view engine', 'pug');
// Use
app.use(compression());
// Export static folders
app.use("/public", express.static(path.join(__dirname, "public")));
app.use(favicon(path.join(__dirname, "public", "cell.ico")));
app.use(cookieParser());
app.use(session({
secret: context.config.sessionSecret || 'catLolLog',
store: new MongoStore({
url: context.mongoConnectionString
}),
resave: true,
saveUninitialized: true
}));
// Append google analytics user id. Will track both page visits as well as API calls.
if(context.config.analytics && context.config.analytics.google && context.config.analytics.google.trackingId){
app.use(universalAnalytics.middleware(context.config.analytics.google.trackingId, {cookieName: 'gauid'}));
}
app.use(passport.initialize());
app.use(passport.session());
// Configure passport
const usersDao = context.component('daos').module('users');
const loginsDao = context.component('daos').module('logins');
const google = new googleStrategy({
clientID : context.config.passport.google.clientId,
clientSecret : context.config.passport.google.clientSecret,
callbackURL : address + "/auth/google/callback",
passReqToCallback : true
}, function(request, accessToken, refreshToken, profile, done) {
usersDao.findOrCreate({ googleId: profile.id, displayName: profile.displayName })
.then(function(user){
loginsDao.login(user._id).then(function(loginId){
return done(null, loginId);
}, function(error){
return done(error, null);
});
}, function(error){
return done(error, null);
});
});
passport.use(google);
app.get('/auth/logout', function(request, response){
loginsDao.logout(request.user._id).then(function(){
request.logout();
return response.redirect('/');
});
});
passport.deserializeUser(function(loginId, done) {
loginsDao.findById(loginId)
.then(function(userId){
usersDao.findById(userId).then(function(user){
return done(null, user);
}, function(error){
return done(error, null);
});
}, function(error){
// Means login has expired!
if(error == "No open session"){
done(null, null);
} else {
return done(error, null);
}
});
});
passport.serializeUser(function(loginId, done) {
done(null, loginId);
});
app.get('/auth/google', passport.authenticate('google', { scope: ['profile'] }));
app.get('/auth/google/callback', passport.authenticate('google', { failureRedirect: '/error' }), function(request, response) {
// Successful authentication, redirect home.
response.redirect('/');
});
// Create routers
context.router = new express.Router();
context.api = new express.Router();
app.use(function(request, response, next) {
if (request.method === 'GET') {
return next();
} else if(request.user && request.user.googleId == context.config.passport.admin){
return next();
} else {
response.status(403).render('error', {
title: 'Error',
message: "Whos! Something went wrong. It appears your user is only allowed to perform GET requests. To be allowed to send data to the server, you need to be administrator.",
error: "Only GET allowed."
});
}
});
// Router listens on / and /api
app.use('/api', function(request, response, next) {
// Send API request to google analytics
request.visitor.pageview(request.path).send();
return next();
}, context.api);
app.use('/', function(request, response, next) {
if(request.user){
response.locals.displayName = request.user.displayName;
}
if(context.config.analytics && context.config.analytics.google && context.config.analytics.google.trackingId){
response.locals.analytics = context.config.analytics.google.trackingId;
}
return next();
}, context.router);
if(process.env.NODE_ENV != 'production'){
context.router.use(function(request, response, next) {
// Log each request to the console if in dev mode
console.log("[FRONTEND] Method:", request.method, "Path", request.path, "Query", request.query);
return next();
});
context.api.use(function(request, response, next) {
// Log each request to the console if in dev mode
console.log("[API] Method:", request.method, "Path", request.path, "Query", request.query);
return next();
});
}
// Load all routes
context.component('.').module('routes');
// Global variables:
const proteinsDao = context.component('daos').module('proteins');
proteinsDao.getLocalizations().then(function(localizations){
context.constants.localizations = localizations;
// Make the server listen
app.listen(app.get('port'), function(){
console.log("Express server listening on port ", app.get('port'));
console.log("According to your configuration, the application is reachable at", address);
});
});
});
// Watch in case of file changes, restart worker (basically can keep up server running forever)
watch([
//path.join(__dirname, "views"),
path.join(__dirname, "services"),
path.join(__dirname, "controllers"),
path.join(__dirname, "daos"),
path.join(__dirname, "models"),
path.join(__dirname, "app.js"),
path.join(__dirname, "index.js")
], function() {
console.log('File changed. Worker is gonna perform harakiri.');
cluster.worker.kill();
});
}