lua-resty-oakrouting - The APIOAK Routing component
location / {
content_by_lua_block {
local oakrouting = require("resty.oakrouting")
local oak_routing = oakrouting.new({
{
path = "/test/get",
method = "GET",
handler = function()
ngx.say("hello, GET")
end
},
{
path = "/test/post",
method = "POST",
handler = function()
ngx.say("hello, POST")
end
},
{
path = "/test/put",
method = "PUT",
handler = function()
ngx.say("hello, PUT")
end
},
{
path = "/test/delete",
method = "DELETE",
handler = function()
ngx.say("hello, DELETE")
end
},
{
path = "/test/parameter/{gateway}",
method = "DELETE",
handler = function(params)
ngx.say("hello, " .. params.gateway)
end
},
})
local succeed = oak:dispatch("/test/get", "GET")
if not succeed then
ngx.say("Matched URI: /test/get FAIL")
end
succeed = oak:dispatch("/test/post", "POST")
if not succeed then
ngx.say("Matched URI: /test/post FAIL")
end
succeed = oak:dispatch("/test/put", "PUT")
if not succeed then
ngx.say("Matched URI: /test/put FAIL")
end
succeed = oak:dispatch("/test/delete", "DELETE")
if not succeed then
ngx.say("Matched URI: /test/delete FAIL")
end
succeed = oak:dispatch("/test/parameter/apioak", "GET")
if not succeed then
ngx.say("Matched URI: /test/parameter/apioak FAIL")
end
}
}
syntax: res, err = oakrouting:new(routers)
Create and initialize a routing object.
syntax: oakrouting:get(path, handler)
Added GET
method routing.
syntax: oakrouting:post(path, handler)
Added POST
method routing.
syntax: oakrouting:put(path, handler)
Added PUT
method routing.
syntax: oakrouting:delete(path, handler)
Added DELETE
method routing.
syntax: oakrouting:any(method, path, handler)
Added custom routing.
syntax: succeed = oakrouting:dispatch(path, method)
Match the routing path and execute the handle.
Installation via LuaRocks
sudo luarocks install lua-resty-oakrouting