Refactor log reading

This commit is contained in:
Calvin Montgomery 2013-08-11 22:32:02 -04:00
parent b06d8ff09f
commit 0bf80a375d
1 changed files with 97 additions and 67 deletions

110
api.js
View File

@ -251,7 +251,7 @@ module.exports = function (Server) {
return; return;
} }
var row = Auth.login(name, oldpw); var row = Auth.login(name, oldpw, "");
if(!row) { if(!row) {
res.jsonp({ res.jsonp({
success: false, success: false,
@ -464,7 +464,7 @@ module.exports = function (Server) {
return; return;
} }
var row = Auth.login(name, pw); var row = Auth.login(name, pw, "");
if(!row) { if(!row) {
res.jsonp({ res.jsonp({
success: false, success: false,
@ -496,7 +496,7 @@ module.exports = function (Server) {
var name = req.query.name; var name = req.query.name;
var session = req.query.session; var session = req.query.session;
var row = Auth.login(name, session); var row = Auth.login(name, "", session);
if(!row) { if(!row) {
res.jsonp({ res.jsonp({
success: false, success: false,
@ -512,27 +512,31 @@ module.exports = function (Server) {
}); });
}); });
/* END REGION */
var x = { /* REGION log reading */
handleReadActionLog: function (params, req, res) {
var name = params.name || ""; /* action log */
var pw = params.pw || ""; app.get("/api/logging/actionlog", function (req, res) {
var session = params.session || ""; res.type("application/jsonp");
var types = params.actions || ""; var name = req.query.name;
var row = Auth.login(name, pw, session); var session = req.query.session;
var types = req.query.actions;
var row = Auth.login(name, "", session);
if(!row || row.global_rank < 255) { if(!row || row.global_rank < 255) {
res.send(403); res.send(403);
return; return;
} }
var actiontypes = types.split(","); types = types.split(",");
var actions = ActionLog.readLog(actiontypes); var actions = ActionLog.readLog(actiontypes);
this.sendJSON(res, actions); res.jsonp(actions);
}, });
// Helper function /* helper function to pipe the last N bytes of a file */
pipeLast: function (res, file, len) { function pipeLast(res, file, len) {
fs.stat(file, function(err, data) { fs.stat(file, function (err, data) {
if(err) { if(err) {
res.send(500); res.send(500);
return; return;
@ -542,44 +546,70 @@ module.exports = function (Server) {
start = 0; start = 0;
} }
var end = data.size - 1; var end = data.size - 1;
fs.createReadStream(file, {start: start, end: end}).pipe(res); fs.createReadStream(file, { start: start, end: end })
.pipe(res);
}); });
}, }
handleReadLog: function (params, req, res) { app.get("/api/logging/syslog", function (req, res) {
var name = params.name || ""; res.type("text/plain");
var pw = params.pw || ""; res.setHeader("Access-Control-Allow-Origin", "*");
var session = params.session || "";
var row = Auth.login(name, pw, session); var name = req.query.name;
var session = req.query.session;
var row = Auth.login(name, "", session);
if(!row || row.global_rank < 255) { if(!row || row.global_rank < 255) {
res.send(403); res.send(403);
return; return;
} }
pipeLast(res, "sys.log", 1048576);
});
app.get("/api/logging/errorlog", function (req, res) {
res.type("text/plain");
res.setHeader("Access-Control-Allow-Origin", "*"); res.setHeader("Access-Control-Allow-Origin", "*");
var type = params.type || ""; var name = req.query.name;
if(type == "sys") { var session = req.query.session;
this.pipeLast(res, "sys.log", 1024*1024);
var row = Auth.login(name, "", session);
if(!row || row.global_rank < 255) {
res.send(403);
return;
} }
else if(type == "err") {
this.pipeLast(res, "error.log", 1024*1024); pipeLast(res, "error.log", 1048576);
});
app.get("/api/logging/channels/:channel", function (req, res) {
res.type("text/plain");
res.setHeader("Access-Control-Allow-Origin", "*");
var name = req.query.name;
var session = req.query.session;
var row = Auth.login(name, "", session);
if(!row || row.global_rank < 255) {
res.send(403);
return;
} }
else if(type == "channel") {
var chan = params.channel || ""; var chan = req.params.channel || "";
if(!chan.match(/^[\w-_]+$/)) {
res.send(400);
return;
}
fs.exists("chanlogs/" + chan + ".log", function(exists) { fs.exists("chanlogs/" + chan + ".log", function(exists) {
if(exists) { if(exists) {
this.pipeLast(res, "chanlogs/" + chan + ".log", 1024*1024); pipeLast(res, "chanlogs/" + chan + ".log", 1048576);
} } else {
else {
res.send(404); res.send(404);
} }
}.bind(this)); });
} });
else {
res.send(400);
}
}
};
return null; return null;
} }