-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.js
More file actions
123 lines (115 loc) · 4.42 KB
/
app.js
File metadata and controls
123 lines (115 loc) · 4.42 KB
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
var express = require('express')
, flash = require('connect-flash')
, passport = require('passport')
, logger = require('winston')
, RedisStore = require('connect-redis')(express)
, cluster = require('cluster')
, os = require('os')
, config = require('./config')
, routes = require('./routes')
, db = require('./modules/db')
, connect = require('connect')
;
var app = module.exports = express()
, server = require('http').createServer(app)
, socketio = require('socket.io')
, sockets = require('./sockets')
, sessionStore // initialized dynamically depending on config
, SessionSockets = require('session.socket.io')
, sessionSockets // initialized dynamically based on sessionStore
, cookieParser = express.cookieParser(config.session.secret)
;
// Configuration
app.configure(function() {
app.use(express.logger({format: config.logging.express_format}));
app.use(express.compress()); // use gzip compression on static assets
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.set('view options', {layout: false});
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.methodOverride());
if (config.logging.winston_mongodb) {
logger.remove(logger.transports.Console);
require('winston-mongodb').MongoDB; // mongo transport for winston logging
logger.add(logger.transports.MongoDB, config.logging.winston_mongodb);
// Consider letting Winstong log transport handle uncaught exceptions: https://github.com/flatiron/winston#handling-uncaught-exceptions-with-winston
}
if (config.redis) {
logger.info('Connecting to Redis at '
+ config.redis.session_opts.host + ':'
+ config.redis.session_opts.port);
sessionStore = new RedisStore(config.redis.session_opts);
} else {
logger.warn('Redis not configured. Storing sessions in memory. See config.redis');
sessionStore = new connect.middleware.session.MemoryStore();
};
var session_config = {
store: sessionStore,
secret: config.session.secret
}
app.use(express.session(session_config));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash()); // must go before app.router
// express3 style dynamic helpers in middleware:
app.use(function(req, res, next) {
res.locals.title = config.app.title;
res.locals.tagline = config.app.tagline;
res.locals.user = req.user;
res.locals.flash = req.flash();
// Give views access to the node env (e.g. development or production)
// and default to "development" if not specified. Used for example to
// determine whether to show minified js/css or not if in production.
res.locals.NODE_ENV = process.env.NODE_ENV
? process.env.NODE_ENV : 'development',
res.locals.config = config
next();
});
app.use(app.router);
app.use(express.static(__dirname + config.static_assets.dir,
{ maxAge: config.static_assets.max_age }));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Map http routes to endpoints:
routes(app);
var startApp = function() {
var protocol = config.app.ssl ? 'https' : 'http';
var port = process.env.PORT || config.app.port;
var app_url = protocol + '://' + config.app.host + ':' + port;
var env = process.env.NODE_ENV
? ('[' + process.env.NODE_ENV + ']') : '[development]';
var io;
server.listen(port, function() {
logger.info(config.app.title + ' listening at ' + app_url + ' ' + env);
});
// Initialize socket.io sockets with express3 session integration:
io = socketio.listen(server);
sessionSockets = new SessionSockets(io, sessionStore, cookieParser);
var listeners = []; // Add listener modules to this array to start socket.io
sockets(io, sessionSockets, []);
}
var startCluster = function (onWorker, onDeath) {
if (cluster.isMaster) {
// Fork workers: 1 http listener per CPU (core), all sharing the same port.
logger.info('Initializing ' + os.cpus().length + ' workers in this cluster.');
for (var i = 0; i < os.cpus().length; i++) {
cluster.fork();
}
cluster.on('death', onDeath);
} else {
onWorker();
}
};
if (config.app.cluster) {
startCluster(startApp, function(worker) {
winston.error('worker ' + worker.pid + ' died'); // A worker process died
});
} else {
startApp();
}