-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
144 lines (122 loc) · 3.93 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
/**
* Module dependencies.
*/
var express = require('express'),
routes = require('./routes'),
http = require('http'),
path = require('path'),
os = require("os"),
redis = require('redis'),
cluster = require("cluster"),
socketio = require('socket.io'),
redisStore = require('socket.io/lib/stores/redis'),
cpu = require('./plugins/cpu.js');
/**
* Application
*/
if (cluster.isMaster) {
var cpuLength = os.cpus().length;
var cpuPublisher = redis.createClient();
for (var i = 0; i < cpuLength; i++) {
var worker = cluster.fork();
console.log('worker started... PID: %s', worker.process.pid);
}
cluster.on('death', function(worker) {
console.log('worker %s died. restart...', worker.pid);
cluster.fork();
});
cpu.start();
cpu.on('message', function(data) {
cpuPublisher.publish("cpu-data", JSON.stringify({ message: data }) );
});
} else {
var app = express();
var root = __dirname;
var secret = 'as9d87as89dh9ash9d98h298dh932hx932pklpa';
app.configure(function() {
app.set('port', process.env.PORT || 3000);
app.set('views', root + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
//app.use(express.logger( 'default' ));
app.use(express.bodyParser());
app.use(express.compress());
/*
app.use(express.cookieParser(secret));
app.use(express.cookieSession({
key: 'monitor-app',
secret: secret,
cookie: {
maxAge: 60 * 60 * 1000
}
}));
app.use(express.session({
secret: secret,
store: new MongoStore({
db: 'express'
})
}));
app.use(express.session({
store: new RedisStore({
host: '127.0.0.1',
port: '6379',
prefix: 'session:redis:'
})
}));
*/
app.use(function(req, res, next) {
res.setHeader('X-Powered-By', 'MonitorApp');
next();
});
app.use(express.methodOverride());
app.use(express.static(path.join(root, 'public')));
app.use(app.router);
app.use(routes.error404);
});
app.configure('development', function() {
app.use(express.errorHandler());
});
app.get('/', routes.index);
var port = app.get('port');
var server = http.createServer(app).listen(port, function() {
console.log("Express server listening on port " + port);
});
var io = socketio.listen(server);
io.configure(function() {
var db = redis.createClient();
var redisPub = redis.createClient();
var redisSub = redis.createClient();
var redisClient = redis.createClient();
io.enable('browser client minification');
io.enable('browser client etag');
io.enable('browser client gzip');
io.set('log level', 0);
io.set('store', new redisStore({
redisPub: redisPub,
redisSub: redisSub,
redisClient : redisClient
}));
io.sockets.on('connection', function (socket) {
var cpuSubscriber = redis.createClient();
cpuSubscriber.subscribe("cpu-data");
cpuSubscriber.on("message", function(channel, message) {
socket.emit('cpu-data', message );
});
socket.on("disconnect", function() {
cpuSubscriber.unsubscribe("cpu-data");
cpuSubscriber.quit();
delete cpuSubscriber;
});
});
/**
*Socket.IO Authorization
*/
io.set('authorization', function(handshakeData, callback) {
if (handshakeData.xdomain) {
callback('Cross-domain connections are not allowed', false);
} else {
callback(null, true);
}
});
});
}