Skip to content

Commit

Permalink
support pool rule clone
Browse files Browse the repository at this point in the history
  • Loading branch information
rehorn committed May 9, 2014
1 parent 2ff5b54 commit a3cac0a
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 54 deletions.
9 changes: 6 additions & 3 deletions lib/livepool/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function parseUrl(urlString) {
};
};

config.loadRules = function() {
config.loadRules = function(callback) {
var module = path.resolve(__dirname, '../../', 'rules/pool.js');
if (!util.exists(module)) {
logger = require('./logger');
Expand All @@ -77,6 +77,7 @@ config.loadRules = function() {

_.each(poolData.children, function(proj) {

proj = proj || {};
proj.id = proj.id || util.getUUId();
proj.type = 'proj';
proj.checked = _.isUndefined(proj.checked) ? true : proj.checked;
Expand All @@ -89,6 +90,7 @@ config.loadRules = function() {

// process data, 适配tree
_.each(proj.children, function(ch) {
ch = ch || {};
ch.id = ch.id || util.getUUId();
ch.type = 'group';
ch.checked = _.isUndefined(ch.checked) ? true : ch.checked;
Expand Down Expand Up @@ -154,9 +156,10 @@ config.loadRules = function() {
});
// console.log(config.handlers);
// console.log(config.routers);
callback && callback();
};

config.saveRules = function(rules) {
config.saveRules = function(callback) {
var module = path.resolve(__dirname, '../../', 'rules/pool.js');
var backup = path.resolve(__dirname, '../../', 'rules/pool-bak-' + util.formatDate(new Date(), 'yyyyMMdd-hh') + '.js');
var pool = 'module.exports=' + JSON.stringify(config.pool, null, 4);
Expand All @@ -166,7 +169,7 @@ config.saveRules = function(rules) {
if (err) throw err;
fs.writeFile(module, pool, function(err) {
if (err) throw err;
config.loadRules();
config.loadRules(callback);
});
});
};
Expand Down
3 changes: 1 addition & 2 deletions lib/livepool/responder/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ var mime = require('mime');
function localFileResponder(handler, req, res, options) {

var filepath = handler.respond.filepath;
// console.log(filepath)
fs.existsSync(filepath) && fs.stat(filepath, function(err, stat) {
if (err) {
throw err;
Expand All @@ -14,7 +13,7 @@ function localFileResponder(handler, req, res, options) {
throw new Error('The responder is not a file!');
}

res.statusCode = 200;
res.statusCode = options.statusCode || 200;
res.setHeader('Content-Length', stat.size);
res.setHeader('Content-Type', mime.lookup(filepath));
res.setHeader('Server', 'livepool');
Expand Down
13 changes: 9 additions & 4 deletions lib/livepool/responder/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ function goProxy(req, res, router, options) {
// TODO req.connect.remoteAddress -> 127.0.0.1
// hack -> use dns resolve to get ip of hostname
// known issue: multi ip don't know which one is used for socket connection
var re = /(d+).(d+).(d+).(d+)/g;
var re = /((?:(?:25[0-5]|2[0-4]\d|[01]?\d?\d)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d?\d))/;
if (re.test(urlObj.host)) {
notify.hostIp(options.sid, req, res, [urlObj.host]);
} else {
dns.resolve4(urlObj.host, function(err, address) {
if (err) throw err;
if (err) {
return ;
};
notify.hostIp(options.sid, req, res, address);
});
}
Expand All @@ -70,8 +72,11 @@ function goProxy(req, res, router, options) {
proxy.web(req, res, proxyOptions, function(e) {
console.log('[proxy error]: ' + req.url);
console.log(e.stack);
res.statusCode = 404;
templateResponder('404_Plain.dat', req, res);

var options = {
statusCode: 503
};
templateResponder('503_ProxyError.html', req, res, options);
});
};

Expand Down
8 changes: 6 additions & 2 deletions lib/livepool/responder/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ var path = require('path');
var localFileResponder = require('./local');

function templateResponder(tpl, req, res, options) {
options = options || {};
var _handler = {};
var filepath = path.join(__dirname, '../../template', tpl);
_handler.respond = {
filepath: path.join(__dirname, 'livepool/lib/template', tpl)
filepath: filepath
};
localFileResponder(_handler, req, res, {});
localFileResponder(_handler, req, res, {
statusCode: options.statusCode
});
};

module.exports = templateResponder;
File renamed without changes.
4 changes: 4 additions & 0 deletions lib/webui/public/js/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ var initSocketIO = function() {
poolTreeStore.load();
});

io.on('poolTreeUpdated', function() {
poolTreeStore.load();
});

io.on('config', function(config) {
if (config.keep) {
$cmp.btnKeep.setChecked(config.keep);
Expand Down
4 changes: 2 additions & 2 deletions lib/webui/public/js/keymap.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
var initKeyMap = function() {

// keymap
new Ext.KeyMap(Ext.getDoc(), {
// ctrl+x to remove all session @ session grid
new Ext.KeyMap('gridpanel', {
key: 'x',
ctrl: true,
handler: function(key, event) {
store.removeAll();
},
stopEvent: true
});

};
2 changes: 2 additions & 0 deletions lib/webui/public/js/liveapp.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,8 @@ var initDom = function() {
$cmp.filterResStatusCodeN200 = Ext.getCmp('filterResStatusCodeN200');
$cmp.filterResStatusCodeRedirect = Ext.getCmp('filterResStatusCodeRedirect');
$cmp.filterResImages = Ext.getCmp('filterResImages');

$cmp.poolClone = Ext.getCmp('poolClone');
};

var initApp = function() {
Expand Down
4 changes: 2 additions & 2 deletions lib/webui/public/js/view.session.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ var MAIN_SESSION_GRID = {
// return 'x-grid-record-gray2';
// }
return 'x-grid-record-blue2';
} else if (record.data.result == '304' || record.data.result == '302' || record.data.contentType.indexOf('image/') >= 0) {
} else if (_.contains(['302', '304'], record.data.result) || record.data.contentType.indexOf('image/') >= 0) {
return 'x-grid-record-gray';
} else if (record.data.result == '404' || record.data.result == '503') {
} else if (_.contains(['404', '403', '502', '503', '504'], record.data.result)) {
return 'x-grid-record-red';
} else if (record.data.contentType.indexOf('javascript') >= 0) {
return 'x-grid-record-green';
Expand Down
120 changes: 91 additions & 29 deletions lib/webui/public/js/view.tab.pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var showEditWin = function(type, actionMode, model, options) {
var match = '';
var action = '';
var name = '';
options = options || {};
var items = [];
if (model) {
match = model.data.match;
Expand Down Expand Up @@ -77,31 +78,53 @@ var showEditWin = function(type, actionMode, model, options) {
}
} else {
if (type == 'proj') {
var root = poolTreeStore.getRootNode();
var pid = +new Date();
var proj = {
// id: pid,
type: 'proj',
name: Ext.getCmp('edit_win_name').getValue(),
match: Ext.getCmp('edit_win_mactch').getValue(),
checked: true
};

var node = root.appendChild(proj);
node.appendChild({
// id: +new Date(),
name: 'handler',
type: 'group',
checked: true,
children: []
});
node.appendChild({
// id: +new Date(),
name: 'router',
type: 'group',
checked: true,
children: []
});
// with idx to trigger additional action
// clone project
if (options.idx) {
var idx = options.idx;
Ext.Ajax.request({
url: '/pool/clone',
params: {
idx: idx
},
method: 'POST',
success: function(response) {
win.close();
},
failure: function(response) {}
});
return ;
} else {
var root = poolTreeStore.getRootNode();
var pid = +new Date();
var proj = {
// id: pid,
type: 'proj',
name: Ext.getCmp('edit_win_name').getValue(),
match: Ext.getCmp('edit_win_mactch').getValue(),
checked: true
};
var projHandlers = options.projHandlers || [];
var projRoutes = options.projRoutes || [];

var node = root.appendChild(proj);
var handler = node.appendChild({
// id: +new Date(),
name: 'handler',
type: 'group',
checked: true,
children: []
});
var router = node.appendChild({
// id: +new Date(),
name: 'router',
type: 'group',
checked: true,
children: projRoutes
});
}

} else {
var sel = $cmp.poolTree.getSelectionModel().hasSelection() && $cmp.poolTree.getSelectionModel().getSelection()[0];
var node = {
Expand Down Expand Up @@ -129,8 +152,9 @@ var showEditWin = function(type, actionMode, model, options) {
}
sel.appendChild(node);

if (options && options.idx) {
// additional to save session
// with idx to trigger additional action
// save session to _livepool/* and add local repalcement
if (options.idx) {
var projBase = sel.parentNode.data.match;
var idx = options.idx;
Ext.Ajax.request({
Expand All @@ -141,10 +165,10 @@ var showEditWin = function(type, actionMode, model, options) {
ruleAction: Ext.getCmp('edit_win_action').getValue()
},
method: 'POST',
success: function(response, options) {
success: function(response) {

},
failure: function(response, options) {
failure: function(response) {

}
});
Expand Down Expand Up @@ -222,6 +246,33 @@ var TAB_POOL = {
handler: function() {
showEditWin('rule', 'add');
}
}, {
xtype: 'button',
id: 'poolClone',
iconCls: 'icomoon-flip2',
text: 'Clone',
handler: function() {
if($cmp.poolTree.getSelectionModel().hasSelection()){
var sel = $cmp.poolTree.getSelectionModel().getSelection()[0];
if(sel.data.type == 'proj'){
var model = {};
model.data = {
match: sel.data.match,
action: '',
name: sel.data.name
};
showEditWin('proj', 'add', model, {
idx: sel.data.id
});
}else{
// rule
var node = sel.data;
delete node.id;
sel.parentNode.appendChild(node);
poolTreeStore.sync();
}
}
}
}, {
text: 'Export',
xtype: 'splitbutton',
Expand All @@ -230,7 +281,7 @@ var TAB_POOL = {
window.location.href = '/rule/exportall';
},
menu: [{
text: 'Export Selected',
text: 'Selected Project',
handler: function() {
window.location.href = '/rule/export';
}
Expand Down Expand Up @@ -389,6 +440,17 @@ var TAB_POOL = {
},
itemclick: function(aNode, record, item, index, e, eOpts) {

},
selectionchange: function(that, selected, eOpts){
if(selected && selected.length >= 0){
var sel = selected[0];
// rule
if(sel.data.type == 'group'){
$cmp.poolClone.setDisabled(true);
}else{
$cmp.poolClone.setDisabled(false);
}
}
}
}
}],
Expand Down
Loading

0 comments on commit a3cac0a

Please sign in to comment.