Skip to content
This repository has been archived by the owner on Dec 11, 2020. It is now read-only.

adding support for reading args without preceding / e.g /hello?a=b #14

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ http {

local ok, errmsg = r:execute(
ngx.var.request_method,
ngx.var.request_uri,
ngx.var.uri,
ngx.req.get_uri_args(), -- all these parameters
ngx.req.get_post_args(), -- will be merged in order
{other_arg = 1}) -- into a single "params" table
Expand Down
23 changes: 15 additions & 8 deletions router.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,25 @@ local router = {

local COLON_BYTE = string.byte(':', 1)

local STR_STR_MATCH_SLASH = "[^/.]+"
local STR_STR_SPLIT_SLASH = "([^/]+)(.*)"
local STR_LEAF = "LEAF"
local STR_TABLE = "table"
local STR_STRING = "string"
local STR_ALL_METHODS = "get post put patch delete trace connect options head"
local STR_MATCH_PERCENT_S = "%S+"

local function match_one_path(node, path, f)
for token in path:gmatch("[^/.]+") do
for token in path:gmatch(STR_STR_MATCH_SLASH) do
node[token] = node[token] or {}
node = node[token]
end
node["LEAF"] = f
node[STR_LEAF] = f
end

local function resolve(path, node, params)
local _, _, current_token, path = path:find("([^/.]+)(.*)")
if not current_token then return node["LEAF"], params end

local _, _, current_token, path = path:find(STR_STR_SPLIT_SLASH)
if not current_token then return node[STR_LEAF], params end
for child_token, child_node in pairs(node) do
if child_token == current_token then
local f, bindings = resolve(path, child_node, params)
Expand All @@ -67,7 +74,7 @@ local function resolve(path, node, params)
end

local function merge(destination, origin, visited)
if type(origin) ~= 'table' then return origin end
if type(origin) ~= STR_TABLE then return origin end
if visited[origin] then return visited[origin] end
if destination == nil then destination = {} end

Expand Down Expand Up @@ -108,7 +115,7 @@ function Router:execute(method, path, ...)
end

function Router:match(method, path, f)
if type(method) == 'string' then -- always make the method to table.
if type(method) == STR_STRING then -- always make the method to table.
method = {[method] = {[path] = f}}
end
for m, routes in pairs(method) do
Expand All @@ -119,7 +126,7 @@ function Router:match(method, path, f)
end
end

for method in ("get post put patch delete trace connect options head"):gmatch("%S+") do
for method in (STR_ALL_METHODS):gmatch(STR_MATCH_PERCENT_S) do
Router[method] = function(self, path, f) -- Router.get = function(self, path, f)
return self:match(method:upper(), path, f) -- return self:match('GET', path, f)
end -- end
Expand Down