lua-resty-router - Lua http router for the ngx_lua
This library is considered production ready.
This Lua library is a http router for the ngx_lua nginx module:
http://wiki.nginx.org/HttpLuaModule
This Lua library takes advantage of ngx_lua's cosocket API, which ensures 100% nonblocking behavior.
Note that at least ngx_lua 0.5.0rc29 or OpenResty 1.0.15.7 is required.
lua_package_path "/path/to/lua-resty-router/lib/?.lua;;";
server {
location /test {
content_by_lua '
local Router = require "resty.router"
router = Router:new()
router:get("/a/:b/:c", function(params)
ngx.print(params["b"].."-"..params["c"])
end)
router:post("/b/c/*.html", function(params)
ngx.print("echo html")
end)
router:any("/c/d/", function(params)
ngx.print("hello, world")
end)
';
}
}
The key
argument provided in the following methods will be automatically escaped according to the URI escaping rules before sending to the memcached server.
syntax: r, err = router:new()
Creates a router object, never return an error.
local R = require("resty.router")
local router = R:new()
router:get("/GetRoute", handler)
router:post("/PostRoute", handler)
router:head("/HeadRoute", handler)
router:put("/PutRoute", handler)
router:delete("/DeleteRoute", handler)
router:patch("/PatchRoute", handler)
router:options("/OptionsRoute", handler)
router:any("/AnyRoute", handler)
router:run()
local R = require("resty.router")
local router = R:new()
-- catch all when on route is match
router:get("/*", function(params) -- /* or * is ok
ngx.say("catch all")
ngx.exit(200)
end)
-- This handler will match /user/john but will not match neither /user/ or /user
router:get("/user/:name", function(params)
local name = params["name"]
ngx.print("Hello", name)
ngx.exit(200)
end)
-- However, this one will match /user/john/ and also /user/john/send
-- If no other routers match /user/john, it will redirect to /user/john/
router.get("/user/:name/*", function(params)
local name = params("name")
ngx.print("Hello", name)
ngx.exit(200)
end)
-- This one will match /user/jhon/send.html, also match any uri start with /user/jhon/ and end with .html
router:get("/user/jhon/*.html", function(params)
ngx.print("Hello")
ngx.exit(200)
end)
router:run()
Type of parameter handler should be function or string, when type is:
a.b
, when ret type is table, search method handle
, if ret type is function, return function would be called.Method run would find route, and callback the handler. when not handler was found and notfound_handler is set, callback the handler.
local R = require("resty.router")
local router = R:new()
router:run() -- or router:run(notfound_handler)
set_by_lua*
, log_by_lua*
, andheader_filter_by_lua*
where the ngx_lua cosocket API is not available.resty.memcached
object instance cannot be stored in a Lua variable at the Lua module level,resty.memcached
instance.resty.memcached
objects in function localngx.ctx
table. These places all have their own data copies forTianyi "git-hulk" Lin () [email protected].
This module is licensed under the BSD license.
Copyright (C) 2012-2017, by Tianyi "git-hulk" Lin () [email protected].
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.