lua-resty-taurus

一个简单的 OpenResty/LuaJIT 规则引擎,名为 Taurus

$ opm get toruneko/lua-resty-taurus

名称

lua-resty-taurus - 一个简单的 OpenResty/LuaJIT 规则引擎,名为 Taurus

状态

该库被认为已准备好投入生产。

构建状态: [!Travis](https://travis-ci.org/toruneko/lua-resty-taurus)

描述

此库需要使用 ngx_lua 模块 构建的 nginx,以及 LuaJIT 2.0

概要

        # nginx.conf:
    
        lua_package_path "/path/to/lua-resty-taurus/lib/?.lua;;";
    
        server {
            location = /t {
                content_by_lua_block {
                    local r = {
                        rules = {
                            {
                                name = "rulename",
                                when = {
                                    equals = {
                                        ["request.query.param"] = "demo"
                                    }
                                },
                                ["do"] = {
                                    proxy = "proxy"
                                }
                            }
                        }
                    }
                    local taurus = require "resty.taurus"
                    local rule_engine = taurus.compile(r)
    
                    local fact = require "resty.taurus.context.fact"
                    local decision = require "resty.taurus.context.decision"
                    local decide = decision.new()
                    local matched = rule_engine:match(fact.new(
                        ngx.req.get_headers(),
                        ngx.req.get_uri_args(),
                        ngx.req.get_post_args(),
                        {}
                    ), decide)
                    ngx.say(decide.rulename)
                    ngx.say(decide.upstream)
                }
            }
        }
        

了解更多关于规则定义的信息: defining-processors

方法

要加载此库,

  1. 您需要在 ngx_lua 的 lua_package_path 指令中指定此库的路径。例如,lua_package_path "/path/to/lua-resty-taurus/lib/?.lua;;";

  2. 您可以使用 require 将库加载到一个本地 Lua 变量中

        local taurus = require "resty.taurus"

编译

语法: rule_engine = taurus.compile(r)

创建一个新的规则引擎对象实例

    -- creates a rule engine object
    local taurus = require "resty.taurus"
    local rule_engine = taurus.compile(r)

匹配

语法: matched = rule_engine:match(fact, decide)

    local fact = require "resty.taurus.context.fact"
    local decision = require "resty.taurus.context.decision"
    local decide = decision.new()
    local matched = rule_engine:match(fact.new(
        ngx.req.get_headers(),
        ngx.req.get_uri_args(),
        ngx.req.get_post_args(),
        {}
    ), decide)
    ngx.say(decide.rulename)
    ngx.say(decide.upstream)

表达式定义

并且

    rules:
      - name: [rulename]
        when:
          and: 
            - [conditions]
            - [conditions]
        do:
          [action]

或者

    rules:
      - name: [rulename]
        when:
          or: 
            - [conditions]
            - [conditions]
        do:
          [action]

    rules:
      - name: [rulename]
        when:
          not:
            [conditions]
        do:
          [action]

规则定义

    rules:
      - name: [rulename]
        when:
          [conditions]
        do:
          [action]

包含

contains 规则定义事实值包含预期值。

    contains:
      request.query.param: demo

等于

equals 规则定义事实值等于预期值。

    equals:
      request.query.param: demo

拥有字段

has fields 规则定义事实包含预期字段。

    has_fields: 
      - request.query.param

在数组中

in array 规则定义事实值包含一些预期值。

    in_array:
      request.query.param: 
        - demo1
        - demo2

以…开头

start with 规则定义事实值以预期前缀开头。

    startwith:
      request.query.param: demo

以…结尾

end with 规则定义事实值以预期后缀结尾。

    endwith:
      request.query.param: demo

范围

range 规则定义事实值大于或小于预期数字。

    range:
      request.query.param:
        gt: 100
        lte: 200

计数

count 规则定义事实值的计数。在预期的时间窗口(秒)内,事实计数超过预期总数,则此规则将匹配。

    count:
      request.query.param: 
        dict_name: count
        total: 1
        window: 1

作者

戴建豪 (toruneko) <toruneko@outlook.com>

版权和许可

此模块根据 MIT 许可证授权。

版权所有 (C) 2018,戴建豪 (toruneko) <toruneko@outlook.com>

保留所有权利。

在满足以下条件的情况下,允许以源代码和二进制形式重新分发和使用,无论是否修改:

  • 源代码的再分发必须保留上述版权声明、此条件列表和以下免责声明。

  • 二进制形式的再分发必须在随分发提供的文档和/或其他材料中复制上述版权声明、此条件列表和以下免责声明。

本软件由版权持有人和贡献者“按原样”提供,并且任何明示或暗示的担保,包括但不限于适销性和特定用途适用性的暗示担保,均被排除在外。在任何情况下,版权持有人或贡献者均不对任何直接、间接、附带、特殊、惩罚性或后果性损害(包括但不限于替代商品或服务的采购;使用、数据或利润损失;或业务中断)负责,无论这些损害是如何引起的以及基于任何责任理论,无论是合同、严格责任还是侵权行为(包括疏忽或其他原因),即使已被告知可能发生此类损害。

另请参阅

  • ngx_lua 模块:https://github.com/openresty/lua-nginx-module =back

POD 错误

嗨!以上文档存在一些编码错误,解释如下:

大约在第 287 行

=over 未关闭 =back

作者

戴建豪 (toruneko)

许可证

mit

依赖项

版本