optimize recorder

This commit is contained in:
OttoMao
2015-08-06 23:13:17 +08:00
parent edaf638f3c
commit 39ae0f4648
7 changed files with 101 additions and 43 deletions

View File

@@ -148,6 +148,12 @@ function Recorder(option){
db.find({},cb);
};
self.getRecords = function(idStart, limit, cb){
limit = limit || 10;
idStart = typeof idStart == "number" ? idStart : (id - limit);
db.find({ _id: { $gte: parseInt(idStart) } }).limit(limit).exec(cb);
};
self.db = db;
}

View File

@@ -1,58 +1,75 @@
var utils = require("./util"),
bodyParser = require("body-parser"),
path = require("path"),
fs = require("fs");
fs = require("fs"),
Promise = require("promise");
var isRootCAFileExists = require("./certMgr.js").isRootCAFileExists(),
interceptFlag = false;
//e.g. [ { keyword: 'aaa', local: '/Users/Stella/061739.pdf' } ]
var mapConfig = [];
var configFile = "mapConfig.json";
var mapConfig = [],
configFile = "mapConfig.json";
function saveMapConfig(content,cb){
try{
new Promise(function(resolve,reject){
var anyproxyHome = utils.getAnyProxyHome(),
mapCfgPath = path.join(anyproxyHome,configFile);
if(typeof content == "object"){
content = JSON.stringify(content);
}
fs.writeFile(mapCfgPath,content,cb);
}catch(e){
cb && cb(e)
}
resolve({
path :mapCfgPath,
content :content
});
})
.then(function(config){
return new Promise(function(resolve,reject){
fs.writeFile(config.path, config.content, function(e){
if(e){
reject(e);
}else{
resolve();
}
});
});
})
.catch(function(e){
cb && cb(e);
})
.done(function(){
cb && cb();
});
}
function getMapConfig(cb){
try{
var read = Promise.denodeify(fs.readFile);
new Promise(function(resolve,reject){
var anyproxyHome = utils.getAnyProxyHome(),
mapCfgPath = path.join(anyproxyHome,configFile);
fs.readFile(mapCfgPath,{encoding:"utf8"},function(err,content){
if(err){
cb && cb(err)
}else{
try{
var obj = JSON.parse(content);
cb && cb(null,obj);
}catch(e){
cb && cb(e);
}
}
});
}catch(e){
cb && cb(e)
}
resolve(mapCfgPath);
})
.then(read)
.then(function(content){
return JSON.parse(content);
})
.catch(function(e){
cb && cb(e);
})
.done(function(obj){
cb && cb(null,obj);
});
}
//load saved config file
getMapConfig(function(err,result){
if(result){
mapConfig = result;
}
});
setTimeout(function(){
//load saved config file
getMapConfig(function(err,result){
if(result){
mapConfig = result;
}
});
},1000);
module.exports = {

View File

@@ -37,15 +37,15 @@ function webInterface(config){
return next();
});
// app.get("/summary",function(req,res){
// recorder.getSummaryList(function(err,docs){
// if(err){
// res.end(err.toString());
// }else{
// res.json(docs.slice(docs.length -500));
// }
// });
// });
app.get("/lastestLog",function(req,res){
recorder.getRecords(null,60,function(err,docs){
if(err){
res.end(err.toString());
}else{
res.json(docs);
}
});
});
app.get("/fetchCrtFile",function(req,res){
if(crtFilePath){
@@ -108,6 +108,7 @@ function webInterface(config){
app.use(express.static(staticDir));
//plugin from rule file
if(typeof userRule._plugIntoWebinterface == "function"){
userRule._plugIntoWebinterface(app,function(){
app.listen(port);