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
124 lines (107 loc) · 3.23 KB
/
set.lua
File metadata and controls
124 lines (107 loc) · 3.23 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
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 doresp = utils.doresp
local dolog = utils.dolog
local redisConf = systemConf.redisConf
local divtypes = systemConf.divtypes
local prefixConf = systemConf.prefixConf
local policyLib = prefixConf.policyLibPrefix
local request_body = ngx.var.request_body
local postData = cjson.decode(request_body)
if not request_body then
-- ERRORCODE.PARAMETER_NONE
local errinfo = ERRORINFO.PARAMETER_NONE
local desc = 'request_body or post data'
local response = doresp(errinfo, desc)
dolog(errinfo, desc)
ngx.say(response)
return
end
if not postData then
-- ERRORCODE.PARAMETER_ERROR
local errinfo = ERRORINFO.PARAMETER_ERROR
local desc = 'postData is not a json string'
local response = doresp(errinfo, desc)
dolog(errinfo, desc)
ngx.say(response)
return
end
local divtype = postData.divtype
local divdata = postData.divdata
if not divtype or not divdata then
-- ERRORCODE.PARAMETER_NONE
local errinfo = ERRORINFO.PARAMETER_NONE
local desc = "policy divtype or policy divdata"
local response = doresp(errinfo, desc)
dolog(errinfo, desc)
ngx.say(response)
return
end
if not divtypes[divtype] then
-- ERRORCODE.PARAMETER_TYPE_ERROR
local errinfo = ERRORINFO.PARAMETER_TYPE_ERROR
local desc = "unsupported divtype"
local response = doresp(errinfo, desc)
dolog(errinfo, desc)
ngx.say(response)
return
end
local red = redisModule:new(redisConf)
local ok, err = red:connectdb()
if not ok then
-- ERRORCODE.REDIS_CONNECT_ERROR
-- connect to redis error
local errinfo = ERRORINFO.REDIS_CONNECT_ERROR
local response = doresp(errinfo, err)
dolog(errinfo, err)
ngx.say(response)
return
end
local policyMod
local policy = postData
local pfunc = function()
policyMod = policyModule:new(red.redis, policyLib)
return policyMod:check(policy)
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 chkout = info
local valid = chkout[1]
local err = chkout[2]
local desc = chkout[3]
if not valid then
dolog(err, desc)
local response = doresp(err, desc)
ngx.say(response)
return
end
local pfunc = function() return policyMod:set(policy) 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 data
if info then
data = ' the id of new policy is '..info
end
local response = doresp(ERRORINFO.SUCCESS, data)
ngx.say(response)