Skip to content

Commit c5d65a2

Browse files
authored
change: used paths, hosts, remote_addrs, methods and vars to (#14)
* change: used `paths`, `hosts`, `remote_addrs`, `methods` and `vars` to filter user rule.
1 parent f056343 commit c5d65a2

File tree

10 files changed

+255
-161
lines changed

10 files changed

+255
-161
lines changed

README.md

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -34,35 +34,20 @@ Synopsis
3434
local radix = require("resty.radixtree")
3535
local rx = radix.new({
3636
{
37-
path = "/aa",
38-
metadata = "metadata /aa",
39-
host = "foo.com",
40-
method = {"GET", "POST"}, -- multiple method
41-
remote_addr = "127.0.0.1",
42-
},
43-
{
44-
path = "/bb*",
45-
metadata = "metadata /bb",
46-
host = {"*.bar.com", "gloo.com"}, -- multiple host
47-
method = {"GET", "POST", "PUT"},
48-
remote_addr = "fe80:fe80::/64",
49-
vars = { -- multiple var
37+
paths = {"/bb*", "/aa"},
38+
hosts = {"*.bar.com", "foo.com"},
39+
methods = {"GET", "POST", "PUT"},
40+
remote_addrs = {"127.0.0.1","192.168.0.0/16",
41+
"::1", "fe80::/32"},
42+
vars = {
5043
{"arg_name", "==", "json"},
51-
{"arg_weight", ">", "10"},
44+
{"arg_weight", ">", 10},
5245
},
53-
filter_fun = function(vars) -- callback fun
46+
filter_fun = function(vars)
5447
return vars["arg_name"] == "json"
5548
end,
56-
},
57-
{
58-
path = "/cc",
59-
metadata = "metadata /cc",
60-
remote_addr = {"127.0.0.1","192.168.0.0/16",
61-
"::1", "fe80::/32"} -- multiple remote_addr
62-
},
63-
{
64-
path = {"/dd", "/dd/ee", "/dd/ff/*"}, -- multiple path
65-
metadata = "metadata /dd and /dd/ee"
49+
50+
metadata = "metadata /bb",
6651
}
6752
})
6853

@@ -89,17 +74,16 @@ The routes is a array table, like `{ {...}, {...}, {...} }`, Each element in the
8974

9075
The attributes of each element may contain these:
9176

92-
|name |option |description|
93-
|-------- |--------|-----------|
94-
|path |required|Client request uri, the default is a full match. But if the end of the path is `*`, it means that this is a prefix path. For example `/foo*`, it'll match `/foo/bar` or `/foo/glo/grey` etc. We can set multiple `path` by an array table, eg: `{"/", "/aa", "/bb"}`.|
95-
|metadata |option |Will return this field if using `rx:match` to match route.|
96-
|handler |option |Will call this function using `rx:dispatch` to match route.|
97-
|host |option |Client request host, not only supports normal domain name, but also supports wildcard name, both `foo.com` and `*.foo.com` are valid. We can set multiple `host` by an array table, eg: `{"foo.com", "bar.com"}`.|
98-
|remote_addr|option |Client remote address like `192.168.1.100`, and we can use CIDR format, eg `192.168.1.0/24`. BTW, In addition to supporting the IPv6 format, multiple IP addresses are allowed, this field will be an array table at this case, the elements of array shoud be a string IPv4 or IPv6 address. We can set multiple `remote_addr` by an array table, eg: `{"127.0.0.1", "192.0.0.0/8"}`.|
99-
|method |option |It's an array table, we can put one or more method names together. Here is the valid method list: "GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS", "CONNECT" and "TRACE".|
100-
|vars |option |It is an array of one or more {var, operator, val} elements. For example: {{var, operator, val}, {var, operator, val}, ...}. `{"arg_key", "==", "val"}` means the value of argument `key` expect to `val`.|
101-
|filter_fun |option |User defined filter function, We can use it to achieve matching logic for special scenes. `radixtree` will only pass one parameter which named `vars` when matching route.|
102-
77+
|name |option |description|example|
78+
|:-------- |:--------|:-----------|:-----|
79+
|paths |required|A list of client request uri. The default is a full match, but if the end of the path is `*`, it means that this is a prefix path. For example `/foo*`, it'll match `/foo/bar` or `/foo/glo/grey` etc.|{"/", "/aa", "/bb"}|
80+
|hosts |option |A list of client request host, not only supports normal domain name, but also supports wildcard name.|{"foo.com", "*.bar.com"}|
81+
|remote_addrs|option |A list of client remote address(IPv4 and IPv6), and we can use CIDR format, eg `192.168.1.0/24`.|{"127.0.0.1", "192.0.0.0/8", "::1", "fe80::/32"}|
82+
|methods |option |A list of method name. Here is full valid method list: "GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS", "CONNECT" and "TRACE".|{"GET", "POST"}|
83+
|vars |option |A list of `{var, operator, val}`. For example: {{var, operator, val}, {var, operator, val}, ...}, `{"arg_name", "==", "json"}` means the value of argument `name` expect to `json`.|{{"arg_name", "==", "json"}, {"arg_age", ">", 18}}|
84+
|filter_fun |option |User defined filter function, We can use it to achieve matching logic for special scenes. `radixtree` will only pass one parameter which named `vars` when matching route.|function(vars) return vars["arg_name"] == "json" end|
85+
|metadata |option |Will return this field if using `rx:match` to match route.||
86+
|handler |option |Will call this function using `rx:dispatch` to match route.||
10387
[Back to TOC](#table-of-contents)
10488

10589

@@ -112,7 +96,7 @@ match
11296
* `opts`: a Lua tale (optional).
11397
* `method`: optional, method name of client request.
11498
* `host`: optional, client request host, not only supports normal domain name, but also supports wildcard name, both `foo.com` and `*.foo.com` are valid.
115-
* `remote_addr`: optional, client remote address like `192.168.1.100`, and we can use CIDR format, eg `192.168.1.0/24`.
99+
* `remote_addr`: optional, client remote address like `192.168.1.100`.
116100
* `vars`: optional, a Lua table to fetch variable, default value is `ngx.var` to fetch Ningx builtin variable.
117101

118102
Matchs the route by `method`, `path` and `host`, and return `metadata` if successful.
@@ -132,7 +116,7 @@ dispatch
132116
* `opts`: a Lua tale (optional).
133117
* `method`: optional, method name of client request.
134118
* `host`: optional, client request host, not only supports normal domain name, but also supports wildcard name, both `foo.com` and `*.foo.com` are valid.
135-
* `remote_addr`: optional, client remote address like `192.168.1.100`, and we can use CIDR format, eg `192.168.1.0/24`.
119+
* `remote_addr`: optional, client remote address like `192.168.1.100`.
136120
* `vars`: optional, a Lua table to fetch variable, default value is `ngx.var` to fetch Ningx builtin variable.
137121

138122
Dispatchs the route by `method`, `path` and `host`, and call `handler` function if successful.

lib/resty/radixtree.lua

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ local function pre_insert_route(self, path, route)
200200
end
201201
end
202202

203-
local method = route.method
203+
local method = route.methods
204204
local bit_methods
205205
if type(method) ~= "table" then
206206
bit_methods = method and METHODS[method] or 0
@@ -214,10 +214,10 @@ local function pre_insert_route(self, path, route)
214214

215215
clear_tab(route_opts)
216216

217-
local host = route.host
218-
if type(host) == "table" and #host > 0 then
217+
local hosts = route.hosts
218+
if type(hosts) == "table" and #hosts > 0 then
219219
route_opts.hosts = {}
220-
for _, h in ipairs(host) do
220+
for _, h in ipairs(hosts) do
221221
local host_is_wildcard = false
222222
if h and h:sub(1, 1) == '*' then
223223
host_is_wildcard = true
@@ -228,14 +228,14 @@ local function pre_insert_route(self, path, route)
228228
insert_tab(route_opts.hosts, h)
229229
end
230230

231-
elseif type(host) == "string" then
231+
elseif type(hosts) == "string" then
232232
local host_is_wildcard = false
233-
if host and host:sub(1, 1) == '*' then
233+
if hosts and hosts:sub(1, 1) == '*' then
234234
host_is_wildcard = true
235-
host = host:sub(2):reverse()
235+
hosts = hosts:sub(2):reverse()
236236
end
237237

238-
route_opts.hosts = {host_is_wildcard, host}
238+
route_opts.hosts = {host_is_wildcard, hosts}
239239
end
240240

241241
if path:sub(#path) == "*" then
@@ -253,7 +253,8 @@ local function pre_insert_route(self, path, route)
253253
route_opts.filter_fun = route.filter_fun
254254

255255
local err
256-
route_opts.matcher_ins, err = parse_remote_addr(route.remote_addr)
256+
local remote_addrs = route.remote_addrs
257+
route_opts.matcher_ins, err = parse_remote_addr(remote_addrs)
257258
if err then
258259
error("invalid IP address: " .. err, 2)
259260
end
@@ -278,11 +279,12 @@ function _M.new(routes)
278279
-- register routes
279280
for i = 1, route_n do
280281
local route = routes[i]
281-
if type(route.path) == "string" then
282-
pre_insert_route(self, route.path, route)
282+
local paths = route.paths
283+
if type(paths) == "string" then
284+
pre_insert_route(self, paths, route)
283285

284286
else
285-
for _, path in ipairs(route.path) do
287+
for _, path in ipairs(paths) do
286288
pre_insert_route(self, path, route)
287289
end
288290
end

t/filter-fun.t

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ __DATA__
1414
local radix = require("resty.radixtree")
1515
local rx = radix.new({
1616
{
17-
path = "/aa",
17+
paths = "/aa",
1818
metadata = "metadata /aa",
1919
filter_fun = function(vars)
2020
ngx.log(ngx.WARN, "start to filter")
@@ -46,7 +46,7 @@ metadata /aa
4646
local radix = require("resty.radixtree")
4747
local rx = radix.new({
4848
{
49-
path = "/aa",
49+
paths = "/aa",
5050
metadata = "metadata /aa",
5151
filter_fun = function(vars)
5252
ngx.log(ngx.WARN, "start to filter")

t/handler.t

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,25 @@ __DATA__
1414
local radix = require("resty.radixtree")
1515
local rx = radix.new({
1616
{
17-
path = "/",
17+
paths ="/",
1818
handler = function (ctx)
1919
ngx.say("handler /")
2020
end,
2121
},
2222
{
23-
path = "/*",
23+
paths ="/*",
2424
handler = function (ctx)
2525
ngx.say("handler /*")
2626
end,
2727
},
2828
{
29-
path = "/aa",
29+
paths ="/aa",
3030
handler = function (ctx)
3131
ngx.say("handler /aa")
3232
end,
3333
},
3434
{
35-
path = "/aa*",
35+
paths ="/aa*",
3636
handler = function (ctx)
3737
ngx.say("handler /aa*")
3838
end,
@@ -68,7 +68,7 @@ true
6868
local radix = require("resty.radixtree")
6969
local rx = radix.new({
7070
{
71-
path = "/aa*",
71+
paths ="/aa*",
7272
handler = function (n)
7373
ngx.say("handler /aa*", n)
7474
end,
@@ -100,13 +100,13 @@ nil
100100
local radix = require("resty.radixtree")
101101
local rx = radix.new({
102102
{
103-
path = "/aa*",
103+
paths ="/aa*",
104104
handler = function ()
105105
ngx.say("handler /aa*")
106106
end,
107107
},
108108
{
109-
path = "/bb*",
109+
paths ="/bb*",
110110
handler = function ()
111111
ngx.say("handler /bb*")
112112
end,
@@ -141,19 +141,19 @@ true
141141
local radix = require("resty.radixtree")
142142
local rx = radix.new({
143143
{
144-
path = "/aa",
144+
paths ="/aa",
145145
handler = function ()
146146
ngx.say("handler /aa")
147147
end,
148148
},
149149
{
150-
path = "/aa/bb",
150+
paths ="/aa/bb",
151151
handler = function ()
152152
ngx.say("handler /bb")
153153
end,
154154
},
155155
{
156-
path = "/aa/bb/cc",
156+
paths ="/aa/bb/cc",
157157
handler = function ()
158158
ngx.say("handler /aa/bb/cc")
159159
end,
@@ -180,23 +180,23 @@ true
180180
local radix = require("resty.radixtree")
181181
local rx = radix.new({
182182
{
183-
path = "/aa*",
183+
paths ="/aa*",
184184
handler = function ()
185185
ngx.say("handler /aa*")
186186
end,
187187
},
188188
{
189-
path = "/aa/bb*",
189+
paths ="/aa/bb*",
190190
handler = function ()
191191
ngx.say("handler /aa/bb*")
192192
end,
193193
},
194194
{
195-
path = "/aa/bb/cc*",
195+
paths ="/aa/bb/cc*",
196+
methods = {"POST", "PUT"},
196197
handler = function ()
197198
ngx.say("handler /aa/bb/cc*")
198199
end,
199-
method = {"POST", "PUT"}
200200
}
201201
})
202202
@@ -226,23 +226,23 @@ true
226226
local radix = require("resty.radixtree")
227227
local rx = radix.new({
228228
{
229-
path = "/aa",
229+
paths ="/aa",
230230
handler = function ()
231231
ngx.say("handler /aa")
232232
end,
233233
},
234234
{
235-
path = "/aa/bb",
235+
paths ="/aa/bb",
236236
handler = function ()
237237
ngx.say("handler /aa/bb")
238238
end,
239239
},
240240
{
241-
path = "/aa/bb/cc",
241+
paths ="/aa/bb/cc",
242+
methods = {"POST", "PUT"},
242243
handler = function ()
243244
ngx.say("handler /aa/bb/cc")
244245
end,
245-
method = {"POST", "PUT"}
246246
}
247247
})
248248

t/host.t

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ __DATA__
1414
local radix = require("resty.radixtree")
1515
local rx = radix.new({
1616
{
17-
path = "/aa*",
17+
paths = {"/aa*"},
1818
metadata = "metadata /aa",
19-
host = "foo.com"
19+
hosts = {"foo.com"},
2020
}
2121
})
2222
@@ -41,9 +41,9 @@ nil
4141
local radix = require("resty.radixtree")
4242
local rx = radix.new({
4343
{
44-
path = "/aa*",
44+
paths = {"/aa*"},
45+
hosts = {"*.foo.com"},
4546
metadata = "metadata /aa",
46-
host = "*.foo.com"
4747
}
4848
})
4949
@@ -72,9 +72,9 @@ metadata /aa
7272
local radix = require("resty.radixtree")
7373
local rx = radix.new({
7474
{
75-
path = "/aa*",
75+
paths = {"/aa*"},
7676
metadata = "metadata /aa",
77-
host = {"foo.com", "bar.com"}
77+
hosts = {"foo.com", "bar.com"},
7878
}
7979
})
8080
@@ -101,9 +101,9 @@ nil
101101
local radix = require("resty.radixtree")
102102
local rx = radix.new({
103103
{
104-
path = "/aa*",
104+
paths = {"/aa*"},
105105
metadata = "metadata /aa",
106-
host = {"foo.com", "*.bar.com"}
106+
hosts = {"foo.com", "*.bar.com"}
107107
}
108108
})
109109

0 commit comments

Comments
 (0)