This repository has been archived by the owner on Dec 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
94 lines (82 loc) · 2.09 KB
/
server.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
if(process.env.NODE_ENV == 'production') {
require('newrelic');
}
var http = require('http')
, path = require('path')
, fs = require('fs')
, async = require('async')
, uglify = require('uglify-js')
, cleancss = require('clean-css')
, app = require('./app')
, server = http.createServer(app)
, io = require('socket.io').listen(server, {log: false})
, socketioMiddleware = require('./middleware/socketio')
, _ = require('lodash')
, atomifyjs = require('atomify-js')
, atomifycss = require('atomify-css')
, pool = []
, broadcast;
var socketCount = 0;
// Broadcasts something to all connected clients
broadcast = function (evt, data) {
_.each(pool, function (socket) {
socket.emit(evt, data);
});
};
socketioMiddleware.references = {
ref: io
, broadcast: broadcast
};
io.sockets.on('connection', function (socket) {
pool.push(socket);
socketCount +=1;
// broadcast socket count
broadcast('news', socketCount);
socket.on('disconnect', function () {
_.remove(pool, socket);
socketCount -=1;
broadcast('news', socketCount);
});
});
// Compile front-end
async.series([
function (next) {
atomifyjs({
entry: path.join(__dirname, 'frontend', 'index.js')
}, function (err, src) {
if(err)
return next(err);
if(process.env.NODE_ENV == 'production')
src = uglify.minify(src, {fromString: true}).code;
fs.writeFile(path.join(__dirname, 'public', 'pulse.js'), src, function (err) {
if(err)
return next(err);
next();
});
});
}
, function (next) {
atomifycss({
entry: path.join(__dirname, 'frontend', 'index.css')
}, function (err, src) {
if(err)
return next(err);
if(process.env.NODE_ENV == 'production')
src = cleancss({
keepSpecialComments: false
}).minify(src);
fs.writeFile(path.join(__dirname, 'public', 'pulse.css'), src, function (err) {
if(err)
return next(err);
next();
});
});
}
], function (err) {
if(err) {
throw err;
}
else {
server.listen(app.get('port'));
}
});