diff --git a/roles/openresty/files/java-nginx-config/nginx/conf/lua/switch_controller.lua b/roles/openresty/files/java-nginx-config/nginx/conf/lua/switch_controller.lua index 5cf413d..87c2e26 100644 --- a/roles/openresty/files/java-nginx-config/nginx/conf/lua/switch_controller.lua +++ b/roles/openresty/files/java-nginx-config/nginx/conf/lua/switch_controller.lua @@ -36,5 +36,10 @@ function M:init() function M:get_percentage() - return lua_context.lua_conf_cache:get("switch:percentage") + local per = lua_context.lua_conf_cache:get("switch:percentage") + if not per then + return 0 + else + return tonumber(per) + end end diff --git a/roles/openresty/files/java-nginx-config/nginx/conf/redirect.lua b/roles/openresty/files/java-nginx-config/nginx/conf/redirect.lua index ed66a30..e83ae99 100644 --- a/roles/openresty/files/java-nginx-config/nginx/conf/redirect.lua +++ b/roles/openresty/files/java-nginx-config/nginx/conf/redirect.lua @@ -2,15 +2,16 @@ local http_request = require "http_request" local switch_controller = require "switch_controller" -function do_direct(redirect_path) +function do_direct(redirectPath) local request_method = ngx.var.request_method; if "GET" == request_method then - ngx.exec(redirect_path,ngx.req.get_uri_args()); + return ngx.exec(redirectPath,ngx.req.get_uri_args()); else - ngx.exec(redirect_path); + return ngx.exec(redirectPath); end end + function route(redirectPath) --- modify content-type, add utf-8 charset if absent--- @@ -19,7 +20,7 @@ function route(redirectPath) end --- if url match the service url, just redirect path with uri - if ngx.var.service_matched == "1" then + if ngx.var.service_matched and ngx.var.service_matched == "1" then local uri = ngx.var.uri if nil == ngx.req.get_uri_args() then return ngx.exec(redirectPath ..uri); @@ -74,9 +75,9 @@ function main() local redirectPath = "/apigateway" ; init_read_body() - + local percentage = switch_controller.get_percentage() - if percentage and percentage >= 1 then + if percentage >= 1 then local uid = http_request.get_req_param("uid") if not uid then uid = 0 @@ -93,4 +94,18 @@ function main() end ---- run ----- -main() \ No newline at end of file +status , errMsg = pcall(main) +if not status then + ngx.log(ngx.ERR, "call method [main] failed.", errMsg) + --- default + if ngx.var.service_matched and 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 + else + do_direct("/apigateway") + end +end