Skip to content

Commit 5653ce8

Browse files
committed
Adding log items live via websockets
1 parent 062ec8f commit 5653ce8

File tree

14 files changed

+5554
-40
lines changed

14 files changed

+5554
-40
lines changed

app.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
var app,
55
cors = require('cors'),
66
express = require('express'),
7+
expressWs,
78
exphbs = require('express-handlebars'),
89
hbs,
910
moment = require('moment'),
@@ -55,6 +56,7 @@
5556
});
5657

5758
app = express();
59+
expressWs = require('express-ws')(app);
5860

5961
app.use(morgan('[:mydate] :method :url :status :res[content-length] - :remote-addr - :response-time ms'));
6062

@@ -84,10 +86,14 @@
8486

8587
// Start server
8688
server = app.listen(nconf.get('PORT'), function () {
87-
var host = server.address().address,
88-
port = server.address().port;
89+
app.locals.host = server.address().address;
90+
app.locals.port = server.address().port;
8991

90-
console.log('Listening at http://%s:%s', host, port);
92+
if (app.locals.host.indexOf(':') > -1) {
93+
app.locals.host = '[' + app.locals.host + ']';
94+
}
95+
96+
console.log('Listening at http://%s:%s', app.locals.host, app.locals.port);
9197
})
9298
.on('error', function (error) {
9399
switch (error.code) {

controllers/view-log.js

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,32 @@
55
data = require('../services/data'),
66
errorResult = require('../services/error-result'),
77
express = require('express'),
8+
logEmitter = require('../services/log-emitter'),
89
router = express.Router();
910

10-
function fetchData(db, callback) {
11-
var data = {
11+
function fetchVals(db, id, callback) {
12+
var vals = {
1213
'eventlog': []
1314
};
1415

1516
db.serialize(function() {
16-
db.each("SELECT * FROM log_events ORDER BY time DESC LIMIT 100", function(err, row) {
17+
db.each("SELECT * FROM log_events ORDER BY time DESC LIMIT 1000", function(err, row) {
1718
row.headers = JSON.parse(row.headers);
18-
data.eventlog.push(row);
19+
vals.eventlog.push(row);
1920
}, function () {
20-
callback(null, data);
21+
callback(null, vals);
2122
});
2223
});
2324
}
2425

25-
function processResponse(req, res, data) {
26+
function processResponse(req, res, vals) {
2627
switch (req.accepts('html', 'json')) {
2728
case 'html':
28-
res.render('view-log', data);
29+
vals.wshost = res.app.locals.host + ':' + res.app.locals.port;
30+
res.render('view-log', vals);
2931
break;
3032
case 'json':
31-
res.json(data.eventlog);
33+
res.json(vals.eventlog);
3234
break;
3335
default:
3436
res.status(406).send('Not Acceptable');
@@ -46,15 +48,29 @@
4648
data.getDb(callback);
4749
},
4850
function (db, callback) {
49-
fetchData(db, callback);
51+
fetchVals(db, 0, callback);
5052
},
51-
function (data) {
52-
processResponse(req, res, data);
53+
function (vals) {
54+
processResponse(req, res, vals);
5355
}
5456
], function (errorMessage) {
5557
handleError(req, res, errorMessage);
5658
});
5759
});
5860

61+
router.ws('/', function(ws, req) {
62+
var id = 0;
63+
64+
function sendLogEvent(logEvent) {
65+
ws.send(logEvent);
66+
};
67+
68+
logEmitter.on('logged-event', sendLogEvent);
69+
70+
ws.on('close', function () {
71+
logEmitter.removeListener('logged-event', sendLogEvent);
72+
});
73+
});
74+
5975
module.exports = router;
6076
}());

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"db-migrate-sqlite3": "^0.2.1",
2626
"express": "^4.15.3",
2727
"express-handlebars": "^3.0.0",
28+
"express-ws": "^3.0.0",
2829
"moment": "^2.18.1",
2930
"morgan": "^1.8.2",
3031
"nconf": "^0.8.4",

public/js/eventlog.tpl.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
(function() {
2+
var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};
3+
templates['eventlog'] = template({"1":function(container,depth0,helpers,partials,data) {
4+
var helper;
5+
6+
return " <i class=\"fa fa-info-circle btn btn-default btn-xs\" data-toggle=\"modal\" data-target=\"#headers"
7+
+ container.escapeExpression(((helper = (helper = helpers.id || (depth0 != null ? depth0.id : depth0)) != null ? helper : helpers.helperMissing),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : (container.nullContext || {}),{"name":"id","hash":{},"data":data}) : helper)))
8+
+ "\"></i>";
9+
},"compiler":[7,">= 4.0.0"],"main":function(container,depth0,helpers,partials,data) {
10+
var stack1, helper, alias1=depth0 != null ? depth0 : (container.nullContext || {}), alias2=helpers.helperMissing, alias3="function", alias4=container.escapeExpression;
11+
12+
return "<tr>\n <td>"
13+
+ alias4(((helper = (helper = helpers.eventtype || (depth0 != null ? depth0.eventtype : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"eventtype","hash":{},"data":data}) : helper)))
14+
+ "</td>\n <td>"
15+
+ ((stack1 = ((helper = (helper = helpers.htmltext || (depth0 != null ? depth0.htmltext : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"htmltext","hash":{},"data":data}) : helper))) != null ? stack1 : "")
16+
+ ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.headers : depth0),{"name":"if","hash":{},"fn":container.program(1, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "")
17+
+ "</td>\n <td class=\"date\">"
18+
+ alias4(((helper = (helper = helpers.time || (depth0 != null ? depth0.time : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"time","hash":{},"data":data}) : helper)))
19+
+ "</td>\n <td>"
20+
+ alias4(((helper = (helper = helpers.secs || (depth0 != null ? depth0.secs : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"secs","hash":{},"data":data}) : helper)))
21+
+ "</td>\n</tr>\n";
22+
},"useData":true});
23+
})();

0 commit comments

Comments
 (0)