-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
57 lines (48 loc) · 1.49 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
var http = require('http');
var git = require('./lib/git');
var app = require('./lib/app');
var discovery = require('./lib/discovery');
var config = require('./lib/config').get();
var gitPort = process.env.GIT_PORT || config.server.gitPort || 8000;
var gitServer = http.createServer(git);
gitServer.listen(gitPort, function() {
console.log("Public Git server listening on http://" + gitServer.address().address + ":" + gitServer.address().port);
});
var appServer = http.createServer(app);
var io = require('socket.io').listen(appServer);
appServer.listen(app.port, '0.0.0.0', function() {
console.log("Private Express server listening on http://" + appServer.address().address + ":" + appServer.address().port);
});
var sockets = {};
io.sockets.on('connection', function(socket) {
sockets[socket.id] = socket;
socket.on('disconnect', function() {
delete sockets[socket.id];
});
});
[
'repoAvailable',
'repoUnavailable',
'repoNotification'
].forEach(function(event) {
discovery.on(event, function(data) {
Object.keys(sockets).forEach(function(key) {
var socket = sockets[key];
socket.emit(event, data);
});
});
});
announceAll();
setTimeout(announceAll, 2 * 1000);
function announceAll() {
config.repos.forEach(function(repo) {
if (repo.shared) {
discovery.repoAvailable(config.server.description, gitPort, repo);
}
});
}
process.on('exit', function() {
config.repos.forEach(function(repo) {
discovery.repoUnavailable(gitPort, repo);
});
});