在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:lua-resty-libr3开源软件地址:https://gitee.com/iresty/lua-resty-libr3开源软件介绍:NameThis is Lua-Openresty implementation library base on FFI for libr3. NOTICE:The Apache APISIX has changed the router to lua-resty-radixtree, which is better than this library. It is highly recommended that you use the new routing implementation. Table of ContentsStatusThis repository is an experimental. Synopsis location / { content_by_lua_block { -- r3 router local r3 = require("resty.r3").new(); local encode_json = require("cjson.safe").encode function foo(params) -- foo handler ngx.say("foo: ", encode_json(params)) end -- routing r3:get("/foo/{id}/{name}", foo) -- don't forget!!! r3:compile() -- dispatch local ok = r3:dispatch("/foo/a/b", ngx.req.get_method()) if not ok then ngx.exit(404) end } } Methodsnew
Creates a r3 object. In case of failures, returns
The routes is a array table, like The attributes of each element may contain these:
Example: -- foo handlerfunction foo(params) ngx.say("foo: ", require("cjson").encode(params))endlocal r3route = require "resty.r3"local r3 = r3route.new({ { path = [[/foo/{:\w+}/{:\w+}"]], method = {"GET"}, handler = foo }, { path = [[/bar/{:\w+}/{:\w+}]], host = "*.bar.com", handler = foo }, { path = [[/alice/{:\w+}/{:\w+}]], remote_addr = "192.168.1.0/24", handler = foo }, { path = [[/bob/{:\w+}/{:\w+}]], method = {"GET"}, host = "*.bob.com", remote_addr = "192.168.1.0/24", handler = foo }, }) insert_route
-- routelocal function foo(params) ngx.say("foo")endlocal r3route = require "resty.r3"local r3 = r3route.new()r3:insert_route("/a", foo)r3:insert_route("/b", foo, {method = {"GET"}}) add routerBTW, we can add a router by specifying a lowercase method name. Valid method name list: -- routelocal function foo(params) ngx.say("foo")endr3:get("/a", foo)r3:post("/b", foo)r3:put("/c", foo)r3:delete("/d", foo) compile
It compiles our route paths into a prefix tree (trie). You must compile after adding all routes, otherwise it may fail to match. dispatch
Dispatchs the path to the controller by local ok = r3:dispatch(ngx.var.uri, ngx.req.get_method()) dispatch2
Basically the same as InstallDependent library# Ubuntusudo apt-get install check libpcre3 libpcre3-dev build-essential libtool \ automake autoconf pkg-config# CentOS 7sodu yum install gcc gcc-c++ git make automake autoconf pcre pcre-devel \ libtool pkgconfig Compile and installsudo make install |
请发表评论