-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
84 lines (64 loc) · 1.88 KB
/
index.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
var fs = require('fs');
var join = require('path').join;
var hapi = require('hapi');
var async = require('async');
var SocketIO = require('socket.io');
var Comm = require('./src/lib/Comm.js');
///////////////////////////////////////////////////////////
// helpers
var Config = require(join(__dirname, 'config.js'));
var log = console.log.bind(console);
var Log = require(join(__dirname, 'src/lib/Log.js'));
///////////////////////////////////////////////////////////
// server
var server = new hapi.Server();
server.connection({
address: Config.address || '0.0.0.0',
port: Config.port || 80
});
// set relative path
server.path(join(__dirname, 'www'));
// attach socket.io to web server
var io = SocketIO(server.listener);
///////////////////////////////////////////////////////////
// socket
io.on('connection', function(socket){
Log.i('new connection');
Comm.patch(socket);
});
global.broadcast = function(event){
var len = arguments.length;
var args = Array.prototype.slice.call(arguments,1,len);
io.emit(event, { args: args } )
};
///////////////////////////////////////////////////////////
var registerLout = function(done){
server.register({ register: require('lout') }, function(err) {
done()
});
};
var setRoutes = function(done){
var root = 'src/route';
// api
server.route(Comm.routes);
fs.readdir( join(__dirname, root), function(err,files){
if(err) return done(err);
for(var i=0; i<files.length; i++){
var file = files[i];
if( file.match(/(\.js|\.json)$/) ){
var p = join(__dirname, root, file );
Log.i('setRoutes:', p);
server.route( require(p) );
}
}
done()
})
};
var startServer = function(done){
server.start(function(){
Log.i('listening', server.info.uri);
done()
})
};
async.series([registerLout, setRoutes, startServer]);
///////////////////////////////////////////////////////////