-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
executable file
·264 lines (228 loc) · 8.25 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
259
260
261
262
263
264
// Copyright 2015 Sinfonier Project
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
var express = require('express'),
partials = require('express-partials'),
expressValidator = require('express-validator'),
fs = require('fs'),
async = require('async'),
Db = require('mongodb').Db,
Server = require('mongodb').Server,
connect = require('connect'),
methodOverride = require('method-override'),
flash = require('connect-flash'),
bodyParser = require('body-parser'),
cookieParser = require('cookie-parser'),
multer = require('multer'),
morgan = require('morgan');
var session = require('express-session');
var mongoStore = require('connect-mongo')(session);
// express server
var app = express();
app.root = __dirname;
// Load config file
app.config = require(app.root + '/config/' + app.set('env') + '.js');
process.loadQueue = async.queue(function(module,cb){
module.callSerializedLoad(cb);
}, 1);
// Database Connect to mongo
app.db = new Db(app.config.database.db,
new Server(app.config.database.host, app.config.database.port, app.config.database.options),{fsync:true}
);
console.log("Connecting to MongoDB...");
app.db.open(function(err) {
// Don't start without MongoDB running
if(err) {
console.log(err);
return;
}
var setup = function()
{
// Middleware setup
app.use(morgan('combined')); // Enable request logging
app.use(express.static(app.root + '/public')); //Static content
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(multer()); // multipart
app.use(expressValidator({})); // this line must be immediately after express.bodyParser()!
app.use(methodOverride(function(req, res){
if (req.body && typeof req.body === 'object' && '_method' in req.body) {
// look in urlencoded POST bodies and delete it
var method = req.body._method
delete req.body._method
return method
}
})); // sets a hidden input of _method to an arbitrary HTTP method
app.use(cookieParser('secret supersecret')); // Required by session
// Use connect-mongodb SessionStore
app.use( session({
cookie: {maxAge: 120 * 60 * 1000}, // two hours
secret: app.config.sessions.secret,
store: new mongoStore({db: app.db}),
resave:true,
saveUninitialized:true
})
);
app.use(flash());
app.use(partials());
// User model
User = require(app.root + '/lib/models/User').init(app.db);
// Topology model
Topology = require(app.root + '/lib/models/Topology').init(app.db);
// Module model
Module = require(app.root + '/lib/models/Module').init(app.db);
// Version model
Version = require(app.root + '/lib/models/Version').init(app.db);
// Notification model
Notification = require(app.root + '/lib/models/Notification').init(app.db);
// Authentication => populate req.current_user
app.use(
(function() {
return function(req, res, next) {
User.authenticate(req, res, function(current_user) {
req.current_user = current_user;
next();
}, function() {
next();
});
};
})()
);
app.use(function(req,res,next){
// req.flash to html helper
res.locals.messages = function(){
var buf = [],
messages = req.flash(),
types = Object.keys(messages);
if (!types) {
return '';
}
len = types.length;
if (!len) {
return '';
}
buf.push('<div id="messages">');
for (var i = 0 ; i < len; ++i) {
var type = types[i],
msgs = messages[type], j;
buf.push(' <ul class="' + type + '">');
for (j = 0, len = msgs.length; j < len; ++j) {
var msg = msgs[j];
buf.push(' <li>' + msg + '</li>');
}
buf.push(' </ul>');
}
buf.push('</div>');
return buf.join('\n');
};
res.locals.current_user = req.current_user;
next();
});
app.set('views', app.root + '/lib/views');
app.set('view engine', 'ejs');
// Middleware for authentication checking
app.require_login = function(req, res, next) {
if (!!req.current_user) {
next()
} else {
function endsWith(str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
}
var str = req.path;
var suffix = '.json';
//AJAX calls not redirected to login
if (req.param("format") === "json" ||
"XMLHttpRequest" === req.headers["x-requested-width"] ||
str.indexOf(suffix, str.length - suffix.length) !== -1)
{
return next(new Error('Not logged in'));
}
req.session.returnTo = req.url;
req.flash('error', 'Not logged in');
return res.redirect("/sessions/signin");
}
//next(new Error('Unauthorized'));
};
// Middleware for admin checking
app.require_admin = function(req, res, next) {
(!!req.current_user && req.current_user.role == 'admin') ? next() : next(new Error('Unauthorized'));
};
// Require all helpers & controllers
var loadDirs = ['helpers', 'controllers'];
loadDirs.forEach(function(dir) {
fs.readdirSync(app.root + '/lib/'+dir+'/').forEach(function(file) {
// test if JS file
if(file.substr(-3) == ".js") {
require(app.root + '/lib/'+dir+'/'+file).expressRoutes(app);
}
});
});
app.use(function(req,res,next) {
res.render('404', { status: 404, locals: { title: 'NotFound', action: 'error' }});
} );
app.use(function(err, req, res, next){
console.log(err.message);
console.log(err.stack);
if (err.message == "Unauthorized" || err.message == "Not logged in") {
var str = req.path;
var suffix = '.json';
if (req.accepts("json") || req.param("format") === "json" ||
"XMLHttpRequest" === req.headers["x-requested-width"] ||
str.indexOf(suffix, str.length - suffix.length) !== -1)
{
return res.status(401).json( {error:"Not logged in"} );
}
res.render('401', { status: 401, locals: { title: 'Not logged in', action: 'error', error: err }});
}
if (err.message == "Not Found") {
res.render('404', { status: 404, locals: { title: 'Not Found', action: 'error', error: err }});
}
else {
res.render('500', { status: 500, locals: { title: 'Error', action: 'error', error: err }});
}
});
// Catch all other routes
/*
app.use(function(req,res) {
res.render('404', { status: 404, locals: { title: 'NotFound', action: 'error' }});
} );
*/
// start the server
app.listen(app.config.server.port, app.config.server.ip);
console.log("App started in '"+app.set('env')+"' environment !\n" +
"Listening on http://"+app.config.server.host+":"+app.config.server.port);
// Intercept exceptions after init so that the server doesn't crash at each uncatched exception
process.on('uncaughtException', function (err) {
console.log('Caught exception: ' + err);
console.log(err.stack);
});
}
if (app.config.database.user) {
app.db.authenticate(app.config.database.user, app.config.database.pwd, function (err, result) {
if(err) {
console.log(err);
return;
}
setup();
});
}
else{
setup();
}
});