-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
executable file
·174 lines (138 loc) · 5.16 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
var cookieParser = require('cookie-parser');
var MongoClient = require('mongodb').MongoClient
var express = require('express');
var path = require('path');
var fs = require('fs');
var https = require('https');
var logger = require('morgan');
var bodyParser = require('body-parser');
var winston = require('winston');
var commandLineArgs = require('command-line-args');
var cluster = require('cluster');
winston.remove(winston.transports.Console);
winston.add(winston.transports.Console, { timestamp: function() {return new Date();}, 'formatter': customLogFormatter});
var optionList = [
{ name: 'help', type: Boolean, description: 'Dispay this message'},
{ name: 'debug', type: Boolean, description: 'Run the server in debug mode. This will disable multiple processes from being spawned.'},
{ name: 'dbURI', type: String, defaultValue: 'mongodb://localhost', group: ['database'], description: 'The database server URI to connect to. Default: mongodb://localhost' },
{ name: 'port', type: Number, defaultValue: 3000, group: ['server'], description: 'The port on which to listen for requests. Default: 3000' },
]
var options = commandLineArgs(optionList)
if (options._all.help) {
var numOpts = optionList.length;
console.log("usage: node app.js <options>")
for(o=0;o<numOpts;o++) {
console.log("\t--"+optionList[o].name+"\t"+optionList[o].description);
}
process.exit();
}
var numProcesses = 1;
if (options.server.mode == 'http') {
numProcesses = require('os').cpus().length;
}
// Debugging is easier with a single process!
if (cluster.isMaster && !options._all.debug) {
for (var i = 0; i < numProcesses; i++) {
cluster.fork();
}
cluster.on('exit', function(deadWorker, code, signal) {
// Restart the worker
var worker = cluster.fork();
// Note the process IDs
var newPID = worker.process.pid;
var oldPID = deadWorker.process.pid;
// Log the event
winston.info('worker '+oldPID+' died.');
winston.info('worker '+newPID+' started.');
});
}
else {
var mongoUrl = options.database.dbURI;
MongoClient.connect(mongoUrl, function (err, db) {
if (err) {
console.log('Error connecting to Mongo')
}
else {
var app = express();
//app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));
app.use(cookieParser());
// Configuring Passport
var passport = require('passport');
var expressSession = require('express-session');
var MongoDBStore = require('connect-mongo')(expressSession);
var store = new MongoDBStore({url: mongoUrl});
var session = expressSession({
store: store,
resave: true,
secret: 'mySecretKey',
rolling: true,
saveUninitialized: false,
cookie: {maxAge: 1200000 } // 20 minutes
});
app.use(session);
app.use(passport.initialize());
app.use(passport.session());
// Using the flash middleware provided by connect-flash to store messages in session
// and displaying in templates
var flash = require('connect-flash');
app.use(flash());
// Initialize Passport
var initPassport = require('./passport/init');
initPassport(passport, db);
app.use(express.static(path.join(__dirname, './public')));
app.set('views', __dirname+'/views');
app.set('view engine', 'pug')
// Ensure that all requests have a handle to the DB connection
app.use(function (req, res, next) {
req.database = db;
next();
});
var routes = require('./routes/index')(passport);
app.use('/', routes);
// catch 404 and forward to error handler
app.use(function (req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
app.use(function(err, req, res, next) {
if (res.headersSent) {
return next(err);
}
res.render('error', {error: err});
});
app.set('port', options.server.port);
/* Use this if you have a cert and want https*/
/*
var server = https.createServer({
key: fs.readFileSync('ssl/key.pem'),
cert: fs.readFileSync('ssl/certificate.pem')
}, app).listen(app.get('port'), function () {
winston.info('Server listening on port ' + server.address().port);
});
*/
/*HTTP*/
app.get('*', function(request, response) {
console.log(`!!!!!!!!! catch all :: ${request.originalUrl}`)
console.dir(request.headers)
});
var server = app.listen(app.get('port'), function () {
winston.info('Server listening on port ' + server.address().port);
});
// Only start the websocket notification component if we are in 'socket' mode which implies single process.
if (options.server.mode == 'socket') {
var notification = require('./routes/notification');
notification(server, session, store, db);
}
}
});
}
function customLogFormatter (options) {
// Return string will be passed to logger.
return options.timestamp().toTimeString() + ' ['+
options.level.toUpperCase() +'] [' +
process.pid + '] ' +
(undefined !== options.message ? options.message : '') ;
}