-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
94 lines (72 loc) · 2.07 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
84
85
86
87
88
89
90
91
92
93
var http = require('http');
var net = require('net');
var through2 = require('through2');
var shoe = require('shoe');
var httphandler = require('./lib/httphandler');
var webSocketHandler = require('./lib/wshandler');
var commonHandler = require('./lib/commonhandler');
var boardHandler = require('./lib/boardhandler.js');
var makeRoutes = require('./lib/routes');
// expects a pinoccio-db object.
module.exports = function(db,options){
options = options||{};
var boards = {};
var routes = makeRoutes(options.routes)
// add time to logs.
var o = through2.obj(function(data,enc,cb){
cb(false,{log:data,time:Date.now()})
});
var handler = commonHandler(db,boards,routes);
var servers = [];
servers.push(http.createServer(httphandler(handler))
.listen(options.port||8002,function(err,data){
if(err) o.emit('error',err);
else o.log(["listening",this.address()]);
}));
// todo add https server if cert is in options.
var shoeServer = shoe(webSocketHandler(handler));
servers.forEach(function(s){
s.on('upgrade',function(){
// hack around shoe so all headers are available in handler even when it goes through wsHandler
req.headers['host'] = xtend(req.headers,{});
})
});
var boardServers = [];
//"port":22756,
//"ssl-port":22757,
boardServers.push(net.createServer(boardHandler(db,boards))
.listen(22756,function(err){
if(err) o.emit('error',err);
else o.log(["boardSerrver",this.address()]);
}));
// add tls if cert is in options.
o.servers = servers;
o.shoe = shoeServer;
o.boardServers = boardServers;
o._close = function(){
servers.forEach(function(s){
s.close();
});
boardServers.forEach(function(s){
s.close();
});
}
o.log = function(msg){
if(!o.writeable || o.closed) return;
o.write(msg);
}
//TODO!
o.close = function(){
o.closed = true;
//o.once('end',function(){
// this.emit('close');
//});
//.once('error',function)
//o.end();
//this.end();
this._close();
this.emit('close');
this.end();
}
return o;
}