• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

lua-resty-libr3: This is Lua-Openresty implementation library base on FFI for li ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

lua-resty-libr3

开源软件地址:

https://gitee.com/iresty/lua-resty-libr3

开源软件介绍:

Name

This 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.

Build StatusLicense

Table of Contents

Status

This 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     } }

Back to TOC

Methods

new

syntax: r3, err = r3router:new()

Creates a r3 object. In case of failures, returns nil and a string describing the error.

syntax: r3, err = r3router:new(routes)

The routes is a array table, like { {...}, {...}, {...} }, Each element in the array is a route, which is a hash table.

The attributes of each element may contain these:

  • path: client request uri.
  • handler: Lua callback function.
  • host: optional, client request host, not only supports normal domain name, but also supports wildcard name, both foo.com and *.foo.com are valid.
  • remote_addr: optional, client remote address like 192.168.1.100, and we can use CIDR format, eg 192.168.1.0/24.
  • methods: optional, It's an array table, we can put one or more method names together. Here is the valid method name: "GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS".

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        },    })

Back to TOC

insert_route

syntax: r3, err = r3:insert_route(path, callback, opts)

  • path: Client request uri.
  • callback: Lua callback function.

opts is optional argument, it is a Lua table.

  • method: It's an array table, we can put one or more method names together.
  • host: optional, client request host, not only supports normal domain name, but also supports wildcard name, both foo.com and *.foo.com are valid.
  • remote_addr: optional, client remote address like 192.168.1.100, and we can use CIDR format, eg 192.168.1.0/24.
-- 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 router

BTW, we can add a router by specifying a lowercase method name.

Valid method name list: get, post, put, delete, patch, head, options.

-- routelocal function foo(params)    ngx.say("foo")endr3:get("/a", foo)r3:post("/b", foo)r3:put("/c", foo)r3:delete("/d", foo)

Back to TOC

compile

syntax: r3:compile()

It compiles our route paths into a prefix tree (trie). You must compile after adding all routes, otherwise it may fail to match.

Back to TOC

dispatch

syntax: ok = r3:dispatch(path, method)

  • path: client request uri.
  • method: method name of client request.

syntax: ok = r3:dispatch(path, opts)

  • path: client request uri.
  • opts: a Lua tale
    • method: optional, method name of client request.
    • host: optional, client request host, not only supports normal domain name, but also supports wildcard name, both foo.com and *.foo.com are valid.
    • remote_addr: optional, client remote address like 192.168.1.100, and we can use CIDR format, eg 192.168.1.0/24.

Dispatchs the path to the controller by method, path and host.

local ok = r3:dispatch(ngx.var.uri, ngx.req.get_method())

Back to TOC

dispatch2

syntax: ok = r3:dispatch2(param_tab, path, method)

syntax: ok = r3:dispatch2(param_tab, path, opts)

Basically the same as dispatch, support for passing in a table object tostore parsing parameters, makes it easier to reuse lua table.

Back to TOC

Install

Dependent 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 install

sudo make install

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
configServer: 通用配置管理平台发布时间:2022-03-25
下一篇:
wnmp PHP集成环境: 基于c# wpf,wnmp PHP集成管理发布时间:2022-03-25
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap