解析请求参数 inId,orderInId值,首字母第一位是否是0,如果是0的话,转发到新的后端服务。

local cjson = require "cjson";
local headers = ngx.req.get_headers();
local method = ngx.var.request_method;
if "GET" == method then
elseif "POST" == method then
  ngx.req.read_body();
  if string.find(headers["content-type"], "application/json") then
    local data = ngx.req.get_body_data();
    local json = cjson.decode(data);
    local inId = json["inId"]

    if inId ~= nil then
      local firstInId = string.sub(inId, 1, 1)
      if firstInId == '0' then
        ngx.exec('@new_api')
      else
        ngx.exec('@api')
      end     
    end

    local orderInId = json["orderInId"]
    if orderInId ~= nil then
      local firstOrderInId = string.sub(orderInId, 1, 1)
      if firstOrderInId == '0' then
        ngx.exec('@new_api')
      else
        ngx.exec('@api')
      end     
    end
  else
      new_api = false
      args = ngx.req.get_post_args();
      for key, value in pairs(args) do
        if value ~= nil and ( key == "inId" or key == "orderInId" ) then
          local firstChar = string.sub(value,1,1)
          if firstChar == "0" then
            new_api = true
            break
          end
        end
      end
      
      if new_api then 
        ngx.exec('@new_api')
      else
        ngx.exec('@api')
      end
  end
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49