Authored by 周奇琪

Merge branch 'hotfix/grayProxy'

... ... @@ -5,5 +5,5 @@
module.exports = {
url:'http://admin.portal.yohobuy.com',
sessionKeep:'/account/profile/display',
timeout:5000
timeout:30000
};
\ No newline at end of file
... ...
... ... @@ -5,6 +5,7 @@
var request = require('request');
var config = require('../config/gray');
var _ = require('lodash');
/**
* 灰度界面配置
... ... @@ -12,25 +13,16 @@ var config = require('../config/gray');
* @return {Function} 返中间件
*/
module.exports = function(proxyRoute) {
/**
* 灰度界面中间件
* @param {Object} req 请求对象
* @param {Object} res 响应对象
* @param {Function} next 执行下个中间件
* @return {void} 无返回
*/
return function(req, ress, next) {
//日志记录器
var logger = req.app.logger;
//方法名称
var method = req.method;
//访问路由路径
var path = req.route?req.route.path:'';
//路由的hash key
var key = proxyRoute.genKey(method,path);
//登出,清掉cookie
if(path === '/logout') {
req.session.gray = null;
... ... @@ -43,60 +35,39 @@ module.exports = function(proxyRoute) {
//老系统调用地址
var callUrl = config.url + req.originalUrl;
//gray 透传cookie
var options = {
method:method,
url:callUrl,
timeout:config.timeout,
headers: {
'Cookie':req.session.gray
}
headers: req.headers,
useQuerystring:true,
form:req.body
}
//删除提交内容长度,代理会改变长度
delete options.headers['content-length'];
//设置代理host
options.headers['host'] = config.url.replace('http://','');
if(!req.session.gray) {
logger.log('warn','grayroute: cookie missed',req.session.user);
} else {
options.headers['cookie'] = req.session.gray;
}
//如果是Ajax请求加上 Ajax头
if(req.headers['x-requested-with']) {
options.headers['X-Requested-With'] = 'XMLHttpRequest';
}
if(req.headers['referer']) {
options.headers['Referer'] = req.headers['referer'];
}
//如果是POST,设置form表单的传参
if(method === "POST") {
options.form = req.body;
}
logger.log('info','grayroute: request options: %j',options,{});
//发起代理请求
request(options,callback);
//代理回调
function callback(err,res,body) {
if(err) {
logger.log('error','grayroute: request error:',err);
ress.status(500).send('');
} else if(res&&res.statusCode === 200) {
setGrayCookie(req,res);
var ContentType = res.caseless.get('Content-Type');
ress.append('Content-Type',ContentType);
ress.send(body);
} else if(res&&res.statusCode === 302) {
var curCookie = setGrayCookie(req,res);
if(!curCookie||curCookie.length<1) {
res.session.user = null;
//发起代理请求
if(req.is('html')) {
request(options,function(err,res,body) {
if(err) {
logger.log('error','grayroute: request error:',err);
ress.status(500).send('');
} else {
setGrayCookie(req,res);
ress.status(res.statusCode).send(body);
}
//跳转
ress.redirect(res.caseless.get('location'));
} else if(res){
ress.status(res.statusCode).send(body);
} else {
ress.status(500).send('500 ERROR!');
}
});
} else {
request(options).pipe(ress);//如果是静态资源,直接管道传递结果
}
} else {
... ... @@ -111,12 +82,11 @@ module.exports = function(proxyRoute) {
var code = res?res.statusCode:'no response';
logger.log('info','grayroute: session keep status:'+code);
});
next();
}
}
}
/**
* 灰度cookie设置
* @param {Object} req express request
... ...
... ... @@ -79,11 +79,10 @@ function _callGetMenu(pid,callback) {
if(err) {
callback(err);
} else {
var menuData = [];
var menuData = {menu:[],right:{}};
if(ret.data && ret.data.length>0) {
menuData = _makeMenu(ret.data);
}
_getAllMenu(function(ret){
menuData.noRight = filterRight(ret,menuData);
delete menuData.right;
... ...
{{#if menu}}
<!-- sidebar start -->
<div class="leftpanel">
<h5 class="leftpanel-title">Navigation</h5>
... ... @@ -11,4 +10,3 @@
{{> ui/menu}}
</div>
{{/if}}
\ No newline at end of file
... ...