Authored by chunhua.zhang

simple switch

... ... @@ -8,8 +8,8 @@ package.loaded[modname]=M
-- switch.
-- switch_to_aws: http://erp.yoho.yohoops.org/switch/?method=switch&percentage=10
-- switch query: http://erp.yoho.yohoops.org/switch/?method=query
-- switch_to_aws: http://erp.yoho.yohoops.org/switch?method=switch&percentage=10
-- switch query: http://erp.yoho.yohoops.org/switch?method=query
-- mal ip controller api --------
function M:switch()
... ...
local redirectPath = "/apigateway" ;
local http_request = require "http_request"
local switch_controller = require "switch_controller"
--- modify content-type, add utf-8 charset if absent
if ngx.req.get_headers()["content-type"] ~= nil and ngx.req.get_headers()["content-type"] == 'application/x-www-form-urlencoded' then
ngx.req.set_header("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
end
--- if url match the service url, just redirect
if ngx.var.service_matched == "1" then
local uri = ngx.var.uri
if nil == ngx.req.get_uri_args() then
return ngx.exec(redirectPath ..uri);
else
return ngx.exec(redirectPath ..uri, ngx.req.get_uri_args());
end
function do_direct(redirect_path)
local request_method = ngx.var.request_method;
if "GET" == request_method then
ngx.exec(redirect_path,ngx.req.get_uri_args());
else
ngx.exec(redirect_path);
end
end
-- method ==9 is not supported , ngx will return directly with reponse message: "please update your app "
local VERSION_ONLINE_NEW=0;
local METHOD_NEW={
["app.passport.signin"]=9,
function route(redirectPath)
--- modify content-type, add utf-8 charset if absent---
if ngx.req.get_headers()["content-type"] ~= nil and ngx.req.get_headers()["content-type"] == 'application/x-www-form-urlencoded' then
ngx.req.set_header("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
end
["app.drawline.getQueueList"]=4,
["app.drawline.addQueue"]=4,
["app.drawline.getUserActivityInfo"]=4,
["app.drawline.getActivityInfo"]=4,
["app.drawline.getLuckyUserList"]=4,
["app.drawline.changeTaskValue"]=4,
["wap.activity.getActivityInfo"]=4,
["app.activity.getInfoOfOrderShare"]=4,
["wap.order.drawOrderShareCoupon"]=4,
["wap.order.registerAndSendCoupon"]=4--,
--["wechat.user.follow"]=5,
--["wechat.user.unfollow"]=5,
--["wechat.token.getmini"]=5,
--["wechat.token.get"]=5
};
--decode request method
local request_method = ngx.var.request_method;
--if header "content_type" is multipart/form-data, send to IDC
--multipart/form-data maybe used to upload image, as user header image
local request_headers = ngx.req.get_headers();
if request_headers["content_type"] ~= nil and string.sub(request_headers["content_type"],1,19) == "multipart/form-data" then
ngx.log(ngx.INFO,"redirect don't support multipart/form-data, method is " .. request_method .. ", send to api.open.yohobuy.com");
if "GET" == request_method then
ngx.exec(redirectPath,ngx.req.get_uri_args());
--- if url match the service url, just redirect path with uri
if ngx.var.service_matched == "1" then
local uri = ngx.var.uri
if nil == ngx.req.get_uri_args() then
return ngx.exec(redirectPath ..uri);
else
ngx.exec(redirectPath);
end
else
--if header "content_type" is x-www-form-urlencoded, decode arguments
local args = nil ;
if "GET" == request_method then
args = ngx.req.get_uri_args()
elseif "POST"== request_method then
ngx.req.read_body()
args = ngx.req.get_post_args()
return ngx.exec(redirectPath ..uri, ngx.req.get_uri_args());
end
end
--decode arguments "method" and "v" in request data
local VERSION_ONLINE_NEW=0;
local METHOD_NEW={
---- 9 for forbidden
["app.passport.signin"]=9,
---- 4 for /activityApi
["app.drawline.getQueueList"]=4,
["app.drawline.addQueue"]=4,
["app.drawline.getUserActivityInfo"]=4,
["app.drawline.getActivityInfo"]=4,
["app.drawline.getLuckyUserList"]=4,
["app.drawline.changeTaskValue"]=4,
["wap.activity.getActivityInfo"]=4,
["app.activity.getInfoOfOrderShare"]=4,
["wap.order.drawOrderShareCoupon"]=4,
["wap.order.registerAndSendCoupon"]=4
};
local method = http_request.get_req_param("method")
-- method not allowed currently
if method and METHOD_NEW[method] == 9 then
ngx.header["Content-type"]="application/json;charset=utf-8"
local rsp = '{"alg": "SALT_MD5","code": 112232323,"data": {},"md5": "f4a7a490bb6666b005008d795ed14e5d", "message": "请升级客户端版本!"}'
ngx.say(rsp)
ngx.exit(ngx.HTTP_OK)
end
-- redirect to activity
if method and METHOD_NEW[method] == 4 then
do_direct("/activityApi")
else
do_direct(redirectPath)
end
end
-- if args is nil?
if args == nil then
ngx.log(ngx.INFO, "can not get any args!")
ngx.exit(ngx.HTTP_OK )
end
---- main
function main()
local method = args["method"];
local version = tonumber(args["v"]);
local redirectPath = "/apigateway" ;
if version == nil then
version=1
end
if method == nil then
method="not_support_method"
end
local percentage = switch_controller.get_percentage()
if percentage and percentage >= 1 then
local uid = http_request.get_req_param("uid")
if not uid then
uid = 0
end
--ngx.log(0,method);
--ngx.log(0,version);
--ngx.log(0,METHOD_NEW[method]);
---- make sure method must be present -------
if method == nil then
ngx.log(ngx.INFO, "method not allowed")
ngx.exit(ngx.HTTP_NOT_ALLOWED )
end
local hash = tonumber(uid) % 100
if hash <= percentage then
redirectPath = "apiaws";
end
end
if METHOD_NEW[method] == 9 then
ngx.header["Content-type"]="application/json;charset=utf-8"
local rsp = '{"alg": "SALT_MD5","code": 112232323,"data": {},"md5": "f4a7a490bb6666b005008d795ed14e5d", "message": "请升级客户端版本!"}'
ngx.say(rsp)
ngx.exit(ngx.HTTP_OK)
end
if METHOD_NEW[method] == 4 then
redirectPath = "/activityApi";
ngx.log(ngx.INFO,"request method is " .. request_method .. ", version is " .. version .. ",method is " .. method .. ", send to apigateway" .. redirectPath);
if "GET" == request_method then
ngx.exec(redirectPath,ngx.req.get_uri_args());
elseif "POST"== request_method then
ngx.exec(redirectPath);
end
elseif METHOD_NEW[method] == 5 then
ngx.log(ngx.INFO,"request method is " .. request_method .. ", version is " .. version .. ",method is " .. method .. ", send to activityApi");
if "GET" == request_method then
ngx.exec("/wechatApi",ngx.req.get_uri_args());
elseif "POST"== request_method then
ngx.exec("/wechatApi");
end
else
if "GET" == request_method then
ngx.exec(redirectPath,ngx.req.get_uri_args());
elseif "POST"== request_method then
ngx.exec(redirectPath);
end
route(redirectPath)
end
end
end
---- run -----
main()
\ No newline at end of file
... ...
... ... @@ -203,7 +203,18 @@ server {
proxy_set_header Host $host;
}
location /apiaws{
internal;
proxy_redirect off;
proxy_pass http://java-aws/gateway;
proxy_set_header X-Real-IP $real_ip;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Accept-Encoding "gzip";
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $host;
}
location = /activityApi{
internal;
... ...