-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
76 lines (63 loc) · 2.27 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
var fs = require('fs'),
http = require('http'),
log4js = require('log4js'),
nowjs = require('now'),
path = require('path'),
util = require('util');
// Parse configuration
var config_file = '';
var config = null;
var reload = function (current, previous) {
var _current = current.mtime.getTime();
var _previous = previous.mtime.getTime();
if (_current != _previous) {
var config_path = path.join(__dirname, config_file);
delete require.cache[config_path];
config = require(config_file).config;
log4js.configure(config.log);
console.info ('Reload log configuration success');
}
}
if (path.existsSync('config.js')) {
config = require('./config.js').config;
config_file = './config.js';
fs.watchFile('config.js', reload);
log4js.configure(config.log);
} else {
config = require('./config-default.js').config;
config_file = './config-default.js';
fs.watchFile('config-default.js', reload);
log4js.configure(config.log);
}
// Initial store
var Store = require('./model/store.js').store;
var store = new Store(config.mongo);
// Initial router
var Router = require('./router.js').router;
var router = new Router(config.base, config.routes, store);
// Initial server and now.js
var httpServer = http.createServer(router.route);
httpServer.listen(config.port);
console.info ('Listen to ' + config.port);
var everyoneLogger = log4js.getLogger('socketio');
var everyone = nowjs.initialize(httpServer,
{ host: config.host,
socketio: {
transports: config.transports,
logger: { log: everyoneLogger.log,
info: everyoneLogger.info,
debug: everyoneLogger.debug,
warn: everyoneLogger.warn,
error: everyoneLogger.error,
isLevelEnabled: function(otherLevel) {
return everyoneLogger.level.isLessThanOrEqualTo(otherLevel);
},
emit: function(level, event) {
event.categoryName = everyoneLogger.category;
everyoneLogger.emit(level, event);
} } } });
// Initial everyone
for (var index = 0; index < config.everyone.length; index++) {
config.everyone[index] (nowjs, everyone.now, store);
}
console.info ('Everyone is listen to everybody, ready!');