-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
148 lines (110 loc) · 3.54 KB
/
Copy pathserver.js
File metadata and controls
148 lines (110 loc) · 3.54 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
var http = require('http');
var fs = require('fs');
var socketio = require('socket.io');
var logger = require('winston');
var mongo = require('mongodb');
var program = require('commander');
var websocket;
// var mongoUri = "mongodb://10.122.33.125/sie-logs";
// var mongoCollection = "log";
// var serverPort = "8080";
var mongoUri = null;
var mongoCollection = null;
var serverPort = null;
function checkParams(){
if (program.mongoUri != null){
mongoUri = program.mongoUri;
} else if(process.env.MONGO_URI != null) {
mongoUri = process.env.MONGO_URI;
}
if (program.mongoCollection != null){
mongoCollection = program.mongoCollection;
} else if(process.env.MONGO_COLLECTION != null) {
mongoCollection = process.env.MONGO_COLLECTION;
}
if (program.port != null){
serverPort = program.port;
} else if(process.env.PORT != null) {
serverPort = process.env.PORT;
}
logger.info(mongoUri);
logger.info(mongoCollection);
logger.info(serverPort);
if(mongoUri && mongoCollection && serverPort)
return true;
}
// subscriber function
var subscribe = function(){
var args = [].slice.call(arguments);
var next = args.pop();
var filter = args.shift() || {};
//console.info(next + ' ' + filter);
if('function' !== typeof next) throw('Callback function not defined');
// connect to MongoDB
mongo.MongoClient.connect("mongodb://" + mongoUri, function(err, db){
if(err)
throw('db err: ' + err);
// make sure you have created capped collection "messages" on db "test"
db.collection(mongoCollection, function(err, coll) {
if(err)
throw('collection err: ' + err);
// seek to latest object
var seekCursor = coll.find(filter).limit(1);//.sort({$natural: -1}).limit(1);
seekCursor.nextObject(function(err, latest) {
if(err)
throw('seek err: ' + err);
if (latest) {
filter._id = { $gt: latest._id }
}
// set MongoDB cursor options
var cursorOptions = {
tailable: true,
awaitdata: true,
numberOfRetries: -1
};
// create stream and listen
var stream = coll.find(filter, cursorOptions).stream();//.sort({$natural: -1}).stream();
// call the callback
stream.on('data', next);
});
});
});
};
var emitMessages = function(document){
if(websocket){
logger.info(document);
websocket.emit('message',document);
} else {
logger.info("nobody is listening");
}
};
program
.version('0.0.1')
.option('-p, --port [port]', 'The HTTP port used')
.option('-m, --mongoUri [uri]', 'The MongoDB host and port like 127.0.0.1:27017')
.option('-c, --mongoCollection [collection]', 'The MongoDB collection to tail')
.parse(process.argv);
if(!checkParams()){
process.exit(1);
}
subscribe(emitMessages);
var filterStream = function(q){
console.info("filter received: " + q);
subscribe(JSON.parse(q), emitMessages);
};
// Chargement du fichier index.html affiché au client
var server = http.createServer(function(req, res) {
fs.readFile('./index.html', 'utf-8', function(error, content) {
res.writeHead(200, {"Content-Type": "text/html"});
res.end(content);
});
});
io = socketio.listen(server);
// Quand un client se connecte, on le note dans la console
io.sockets.on('connection', function (socket) {
logger.info('Un client est connecté !');
//enregistre la connexion ouverte avec le client
websocket = socket;
websocket.on('filter',filterStream);
});
server.listen(serverPort);