save map config to local file

This commit is contained in:
OttoMao
2015-08-06 17:26:22 +08:00
parent ef19ccf72b
commit edaf638f3c
4 changed files with 69 additions and 4 deletions

View File

@@ -1,5 +1,6 @@
var utils = require("./util"),
bodyParser = require("body-parser"),
path = require("path"),
fs = require("fs");
var isRootCAFileExists = require("./certMgr.js").isRootCAFileExists(),
@@ -8,6 +9,52 @@ var isRootCAFileExists = require("./certMgr.js").isRootCAFileExists(),
//e.g. [ { keyword: 'aaa', local: '/Users/Stella/061739.pdf' } ]
var mapConfig = [];
var configFile = "mapConfig.json";
function saveMapConfig(content,cb){
try{
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)
}
}
function getMapConfig(cb){
try{
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)
}
}
//load saved config file
getMapConfig(function(err,result){
if(result){
mapConfig = result;
}
});
module.exports = {
summary:function(){
var tip = "the default rule for AnyProxy which supports CORS.";
@@ -110,6 +157,8 @@ module.exports = {
app.post("/setMapConfig",function(req,res){
mapConfig = req.body;
res.json(mapConfig);
saveMapConfig(mapConfig);
});
cb();

View File

@@ -21,9 +21,25 @@ module.exports.merge = function(baseObj, extendObj){
return baseObj;
}
module.exports.getUserHome = function(){
function getUserHome(){
return process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
}
module.exports.getUserHome = getUserHome;
module.exports.getAnyProxyHome = function(){
var home = path.join(util.getUserHome(),"/.anyproxy/");
if(!fs.existsSync(home)){
try{
fs.mkdirSync(home,0777);
}catch(e){
return null;
}
}
return home;
}
module.exports.simpleRender = function(str, object, regexp){
return String(str).replace(regexp || (/\{\{([^{}]+)\}\}/g), function(match, name){