Skip to content

Commit

Permalink
code style: test all green
Browse files Browse the repository at this point in the history
  • Loading branch information
vm-001 committed Dec 19, 2023
1 parent 4dc78d8 commit fa64a66
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 156 deletions.
4 changes: 2 additions & 2 deletions benchmark/match-prefix.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local match_times = 1000 * 1000

local routes = {}
for i = 1, route_count do
routes[i] = {paths = {"/" .. ngx.md5(i) .. "/*"}, metadata = i}
routes[i] = {paths = {"/" .. (i) .. "/*"}, metadata = i}
end

local rx = radix.new(routes)
Expand All @@ -13,7 +13,7 @@ ngx.update_time()
local start_time = ngx.now()

local res
local path = "/" .. ngx.md5(500) .. "/a"
local path = "/" .. 500 .. "/a"
for _ = 1, match_times do
res = rx:match(path)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/router_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe("Router", function()
handler = "2",
},
})
assert.equal("1", router:match("/aa"))
assert.equal("1", router:match("/a"))
end)

describe("parameter matching", function()
Expand Down
28 changes: 7 additions & 21 deletions src/resty/router.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,55 +68,42 @@ function Router.new(routes, options)
return setmetatable(self, mt)
end


local function find_route(routes, ctx)
if routes[0] == 1 then
local route = routes[1]
if route:is_match(ctx) then
return route
end
return nil
end
for _, route in ipairs(routes) do
for n = 1, routes[0], 1 do
local route = routes[n]
if route:is_match(ctx) then
return route
end
end
return nil
end

--local function ReverseTable(myTable)
-- if myTable and #myTable > 1 then
-- local temp = nil
-- for n = 1, math.floor(#myTable / 2) do
-- temp = myTable[n]
-- myTable[n] = myTable[#myTable - (n - 1)]
-- myTable[#myTable - (n - 1)] = temp
-- end
-- end
-- return myTable
--end



-- path: 请求路径
-- ctx(请求的参数):
-- method:
--
function Router:match(path, ctx)
ctx = ctx or EMPTY
local test = ctx.matched ~= nil
if test then
clear_table(ctx.matched)
end

local route

local routes = self.static[path]
if routes then
local route = find_route(routes, ctx)
route = find_route(routes, ctx)
if route then
return route.handler
end
end

local route
local values, count = self.trie:traverse(path, ctx)
if count == 1 then
route = find_route(values[1], ctx)
Expand Down Expand Up @@ -150,7 +137,6 @@ function Router:match(path, ctx)
return nil
end


function Router:dispatch(path, ctx, ...)
local handler = self:match(path, ctx)
if handler == nil then
Expand Down
Loading

0 comments on commit fa64a66

Please sign in to comment.