This repository has been archived by the owner on Feb 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
front.js
124 lines (102 loc) · 3.85 KB
/
front.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
'use strict';
var clusterLib = require('cluster');
var expressLib = require('express');
var helmetLib = require('helmet');
var mongooseLib = require('mongoose');
var requireDirLib = require('require-dir');
var consoleLib = require(__dirname + '/lib/console');
var constantsLib = require(__dirname + '/lib/constants');
if (clusterLib.isMaster) {
var cluserPerCpu = 1;
var clusterCount = parseInt(require('os').cpus().length * cluserPerCpu) || 1;
clusterLib.on('fork', function(worker) {
consoleLib.info('Worker ' + worker.id + ' created');
});
clusterLib.on('exit', function (worker) {
consoleLib.warn('Worker ' + worker.id + ' died, forking a new one');
clusterLib.fork();
});
for (var i = 0 ; i < clusterCount ; ++i) {
clusterLib.fork();
}
global.processId = 'Master';
mongooseLib.connect(constantsLib.databaseUri);
mongooseLib.connection.on('error', function (err) {
// Handle error
consoleLib.error('Could not open DB connection: ' + err);
mongooseLib.connection.close();
mongooseLib.connect(constantsLib.databaseUri);
});
mongooseLib.connection.once('open', function () {
// Handle open;
consoleLib.log('DB connection open');
requireDirLib(__dirname + '/models/');
consoleLib.log('Collections sync\'ed');
if (mongooseLib.connection.collections['sessions']) {
return mongooseLib.connection.collections['sessions'].drop(function(err) {
if (err) {
consoleLib.error(err);
return process.exit(1);
}
consoleLib.info('Sessions dropped');
require(__dirname + '/scripts/front/daemons').execute();
return ;
});
}
require(__dirname + '/scripts/front/daemons').execute();
return ;
});
}
else {
global.processId = 'Worker ' + clusterLib.worker.id;
mongooseLib.connect(constantsLib.databaseUri);
mongooseLib.connection.on('error', function (err) {
// Handle error
consoleLib.error('Could not open DB connection: ' + err);
mongooseLib.connection.close();
mongooseLib.connect(constantsLib.databaseUri);
});
mongooseLib.connection.once('open', function () {
// Handle open;
consoleLib.log('DB connection open');
requireDirLib(__dirname + '/models/');
consoleLib.log('Collections sync\'ed');
});
var app = expressLib();
app.configure(function() {
app.disable('x-powered-by');
if ('debug' == process.env.NODE_ENV)
app.use(expressLib.errorHandler({ dumpExceptions: true, showStack: true }));
app.use(expressLib.logger('dev'));
// Security headers
app.use(helmetLib.xframe());
app.use(helmetLib.xssFilter());
app.use(helmetLib.nosniff());
app.use(helmetLib.nocache());
app.use(expressLib.bodyParser());
app.use(expressLib.methodOverride());
var mongoStore = require('connect-mongo')(expressLib);
app.use(expressLib.cookieParser());
app.use(expressLib.session({
store: new mongoStore ({
url: constantsLib.databaseUri + '/sessions'
},
function() {
consoleLib.info("MongoStore connected!");
}),
secret: '7iGofFxdVeCafeq35BDrOdoV',
}));
app.use(function(req, res, next) {
if (!req.params) req.params = {};
if (!req.body) req.body = {};
if (!req.session) req.session = {};
if (!res.locals) res.locals = {};
return next();
});
require(__dirname + '/controllers/front/_routes').init(app);
app.use(app.router);
app.enable('jsonp callback');
});
var server = null;
server = app.listen(7337);
}