mirror of
https://github.com/alibaba/anyproxy.git
synced 2025-08-04 21:39:04 +00:00
save map config to local file
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user