forked from G33kLabs/Node.VaSH
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·224 lines (188 loc) · 6.62 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
// -- Change dir to local dir
var path = require("path");
var current_path = __dirname+'';
process.chdir(path.normalize(__dirname+'/')) ;
GLOBAL.root_path = process.cwd() ;
// -- Load Libs
var fs = require('fs'),
util = require('util'),
express = require('express'),
passport = require('passport'),
cluster = require('cluster'),
colors = require('colors'),
http = require('http'),
numCPUs = require('os').cpus().length ;
// -- Load Globals
GLOBAL.mkdirp = require('mkdirp') ;
GLOBAL.jQuery = require('jquery') ;
GLOBAL.tools = require(root_path+'/libs/server/tools.kit') ;
require('datejs');
// -- Config
var config = {
redis: {
host: 'localhost',
port: 6379,
db: 1
},
server: {
host: 'localhost',
port: 10000
},
session: {
cookname: 'express.sid',
password: 'N0d3*)s3Ss10n',
cookoptions: {
path: '/',
httpOnly: true,
maxAge: 31*24*3600*1000
}
},
env: 'dev'
}
// -- Detect Prod Env // You can customize this test
if ( (/^\/var\/www/).test(root_path) || /^\/home\/www/.test(root_path) ) {
config.env = 'prod'
}
// -- Set number of CPUs instances
numCPUs = (config.env == 'dev') ? 1 : Math.max(2, numCPUs) ;
///////////////////////////////////////////////////////////////////////// LOG TO FILE
/*
var logToFile = require('logtofile'),
logPath = path.normalize(__dirname+'/logs/'),
logFile = config.server.host+'_'+config.server.port+'.log' ;
// -> Write message to console
tools.log('[>] Output logs to : '+logPath+logFile)
// -> Make logs dir
mkdirp.sync(logPath);
// -> At this time, all tools.log calls will be redirected into log files
GLOBAL.logger = logToFile.create({
directory: logPath,
fileName: logFile
});
*/
//////////////////////////////////////////////////////// WELCOME MESSAGE /////////////
var welcome = [
'',
'____ ____ ___ _______. __ __ ',
'\\ \\ / / / \\ / || | | | ',
' \\ \\/ / / ^ \\ | (----`| |__| | ',
' \\ / / /_\\ \\ \\ \\ | __ | ',
' \\ / / _____ \\ .----) | | | | | ',
' \\__/ /__/ \\__\\ |_______/ |__| |__| ',
''
].join('\n');
tools.log(welcome.rainbow.bold);
tools.warning(' [ ] Running environement : '+((config.env=='prod') ? 'PODUCTION' : 'DEVELOPMENT')) ;
///////////////////////////////////////////////////////////// MASTER CLUSTER /////////////
if (cluster.isMaster) {
// Helper to fork a new instance
function WorkerFork() {
var worker = cluster.fork();
worker.on('message', function(msg) {
tools.log("[>] "+id+" | New message :: "+json(msg));
});
}
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
setTimeout(function() {
WorkerFork() ;
}, i*30000) ;
}
// On exit, restart worker
cluster.on('exit', function(worker, code, signal) {
var exitCode = worker.process.exitCode;
tools.warning('[*] '+worker.id+' | worker died : '+worker.process.pid+' ('+exitCode+').');
if ( ! cluster.isExiting ) {
tools.warning('[>] '+worker.id+' | worker restarting...');
setTimeout(function() {
WorkerFork() ;
}, 500)
}
});
// Message when worker is lonline
cluster.on('online', function(worker) {
tools.log("[>] "+worker.id+" | worker responded after it was forked");
worker.send('Hello you !');
});
///////////////////////////////////////////////////////////// PROTECT EXIT /////////////
// Start reading from stdin so we don't exit.
process.stdin.resume();
process.on('SIGINT', function () {
cluster.isExiting = true ;
async.series({
foo: function(callback) {
callback(null, true)
}
}, function() {
tools.log('[*] Exit as a gentleman !') ;
process.exit(0);
})
});
}
///////////////////////////////////////////////////////////// WORKER CLUSTER /////////////
else {
// -- Init message
tools.log('[>] Worker is running...');
// -- Inter processes message receiption
process.on('message', function(msg) {
tools.debug('[>] Master message :: '+json(msg));
});
///////////////////////////////////////////////////////////// APPLICATION /////////////
// Create API server
var app = express()
, server = http.createServer(app) ;
// Register VaSH as an express middleware plugin
express.vash = require('./libs/server/express.vash') ;
// Add session && router support
app.use(express.favicon());
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.errorHandler());
app.use(express.methodOverride());
// app.use(express.csrf());
// -- Express sessions
var RedisStore = require('connect-redis')(express),
sessionStore = new RedisStore,
Session = require('connect').middleware.session.Session ;
// -- Configure session
app.use(express.session({
secret: config.session.password,
key: config.session.cookname,
cookie: config.session.cookoptions,
store: sessionStore
}));
// -- Initialize passport middleware
app.use(passport.initialize());
app.use(passport.session());
// -- Passport session setup
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(obj, done) {
done(null, obj);
});
// -- Initialize VaSH middleware
app.use(express.vash({ env: config.env, cache: 3600*1000, passport: passport }).get);
// -- Start Server
server.listen(config.server.port);
tools.debug(' [*] WebServer STARTED : http://'+config.server.host+':'+config.server.port+'/') ;
///////////////////////////////////////////////////////////// WEBSOCKET /////////////
var sio = require('socket.io')
, RedisStore = sio.RedisStore
, io = sio.listen(server, {
'log level': 2,
'logger': {
debug: function(msg) { return false; tools.debug('[ ] Socket.IO :: '+msg) },
info: function(msg) { tools.log('[>] Socket.IO :: '+msg, 'cyan') },
warn: function(msg) { tools.warning('[!] Socket.IO :: '+msg) },
error: function(msg) { tools.tools('[!] Socket.IO :: '+msg) }
}
});
// Set store
io.set('store', new RedisStore);
// Bind connections
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
});
}