forked from CNSRE/ABTestingGateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset.lua
More file actions
162 lines (139 loc) · 4.69 KB
/
set.lua
File metadata and controls
162 lines (139 loc) · 4.69 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
local runtimeModule = require('abtesting.adapter.runtime')
local policyModule = require('abtesting.adapter.policy')
local redisModule = require('abtesting.utils.redis')
local systemConf = require('abtesting.utils.init')
local handler = require('abtesting.error.handler').handler
local utils = require('abtesting.utils.utils')
local ERRORINFO = require('abtesting.error.errcode').info
local cjson = require('cjson.safe')
local redisConf = systemConf.redisConf
local prefixConf = systemConf.prefixConf
local runtimeLib = prefixConf.runtimeInfoPrefix
local policyLib = prefixConf.policyLibPrefix
local domain_name = prefixConf.domainname
local divtypes = systemConf.divtypes
local separator = ':'
local fields = {}
fields.divtype = 'divtype'
fields.divdata = 'divdata'
fields.idCount = 'idCount'
local doresp = utils.doresp
local dolog = utils.dolog
local domainName = domain_name
if not domainName or domainName == ngx.null
or string.len(domainName) < 1 then
local info = ERRORINFO.PARAMETER_NONE
local desc = "domainName is blank and please set it in nginx.conf"
local response = doresp(info, desc)
dolog(info, desc)
ngx.say(response)
return
end
local policyID = ngx.var.arg_policyid
if policyID then
policyID = tonumber(ngx.var.arg_policyid)
if not policyID or policyID < 0 then
local info = ERRORINFO.PARAMETER_TYPE_ERROR
local desc = "policyID should be a positive Integer"
local response = doresp(info, desc)
dolog(info, desc)
ngx.say(response)
return
end
end
if not policyID then
local request_body = ngx.var.request_body
local postData = cjson.decode(request_body)
if not request_body then
-- ERRORCODE.PARAMETER_NONE
local info = ERRORINFO.PARAMETER_NONE
local desc = 'request_body or post data to get policyID'
local response = doresp(info, desc)
dolog(info, desc)
ngx.say(response)
return
end
if not postData then
-- ERRORCODE.PARAMETER_ERROR
local info = ERRORINFO.PARAMETER_ERROR
local desc = 'postData is not a json string'
local response = doresp(info, desc)
dolog(info, desc)
ngx.say(response)
return
end
policyID = postData.policyid
if not policyID then
local info = ERRORINFO.PARAMETER_ERROR
local desc = "policyID is needed"
local response = doresp(info, desc)
dolog(info, desc)
ngx.say(response)
return
end
policyID = tonumber(postData.policyid)
if not policyID or policyID < 0 then
local info = ERRORINFO.PARAMETER_TYPE_ERROR
local desc = "policyID should be a positive Integer"
local response = doresp(info, desc)
dolog(info, desc)
ngx.say(response)
return
end
end
local red = redisModule:new(redisConf)
local ok, err = red:connectdb()
if not ok then
local errinfo = ERRORINFO.REDIS_CONNECT_ERROR
local response = doresp(errinfo, err)
dolog(errinfo, err)
ngx.say(response)
return
end
local pfunc = function()
local policyMod = policyModule:new(red.redis, policyLib)
return policyMod:get(policyID)
end
local status, info = xpcall(pfunc, handler)
if not status then
local errinfo = info[1]
local errstack = info[2]
local err, desc = errinfo[1], errinfo[2]
local response = doresp(err, desc)
dolog(err, desc, nil, errstack)
ngx.say(response)
return
end
local divtype = info.divtype
local divdata = info.divdata
if divtype == ngx.null or
divdata == ngx.null then
local err = ERRORINFO.POLICY_BLANK_ERROR
local desc = 'policy NO.'..policyID
local response = doresp(err, desc)
dolog(err, desc)
ngx.say(response)
return
end
if not divtypes[divtype] then
-- unsupported divtype
end
local pfunc = function()
local divModulename = table.concat({'abtesting', 'diversion', divtype}, '.')
local divDataKey = table.concat({policyLib, policyID, fields.divdata}, ':')
local userInfoModulename= table.concat({'abtesting', 'userinfo', divtypes[divtype]}, '.')
local runtimeMod = runtimeModule:new(red.redis, runtimeLib)
return runtimeMod:set(domainName, divModulename, divDataKey, userInfoModulename)
end
local status, info = xpcall(pfunc, handler)
if not status then
local errinfo = info[1]
local errstack = info[2]
local err, desc = errinfo[1], errinfo[2]
local response = doresp(err, desc)
dolog(err, desc, nil, errstack)
ngx.say(response)
return
end
local response = doresp(ERRORINFO.SUCCESS)
ngx.say(response)