luaFilter
luaFilter
组件:lua脚本过滤器。可以使用Lua脚本对msg、metadata、msgType进行过滤。根据脚本返回值路由到True或者False链。也可以启动加载lua第三方库,进行例如:加解密、I/O、网络、文件等高级操作。
Lua脚本使用加解密、I/O、网络、文件等高级操作库,需要通过以下配置开启:
config.Properties.PutValue(luaEngine.LoadLuaLibs, "true")
Lua脚本支持Lua5.1语法规范,请参考gopher-lua (opens new window) 。
# 配置
字段 | 类型 | 说明 | 默认值 |
---|---|---|---|
script | string | lua脚本,或者与.lua 后缀的lua脚本文件路径 | 无 |
script
:可以对msg、metadata、msgType进行过滤。只需要提供函数体内容,如果是文件路径,则需要提供完整的脚本函数:function Filter(msg, metadata, msgType) ${script} end
1
2
3- msg:消息内容,如果dataType=JSON,可以使用
msg.temperature
方式操作。如果dataType是其他类型,该字段类型是:string
- metadata:消息元数据,类型:
jsonObject
- msgType:消息类型
- 函数返回值类型:
boolean
,决定和下一个节点的连接关系(Relation Type
)
- msg:消息内容,如果dataType=JSON,可以使用
# Relation Type
- True: 把消息发送到
True
链 - False: 把消息发送到
False
链 - Failure: 执行失败,把消息发送到
Failure
链
# 执行结果
该组件不会改变msg
、metadata
和msgType
内容。
# 配置示例
{
"id": "s1",
"type": "x/luaFilter",
"name": "过滤",
"configuration": {
"script": "return msg.temperature > 50"
}
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 自定义函数
可以通过config.RegisterUdf
注册golang函数,然后在Lua脚本中使用,其注册方式和用法和Js一致。详情参考Udf 。
# 加载Lua模块
可以通过加载Lua自定义或者第三方模块库,扩展Lua能力,使用以下方式把Lua模块注册到引擎中:
import luaEngine "github.com/rulego/rulego-components/pkg/lua_engine"
luaEngine.Preloader.Register(func(state *lua.LState) {
//加载模块
//libs.Preload(state)
})
1
2
3
4
5
6
2
3
4
5
6
使用Golang实现Lua模块参考 (opens new window)
另外框架内置了大量的Lua第三方库模块,使用以下方式启用,默认不加载:
config.Properties.PutValue(luaEngine.LoadLuaLibs, "true")
1
启动后可以进行加解密、I/O、网络、数据库、文件等高级操作,第三方库列表:
- argparse (opens new window) argparse CLI parsing https://github.com/luarocks/argparse (opens new window)
- base64 (opens new window) encoding/base64 (opens new window) api
- cloudwatch (opens new window) aws cloudwatch log access
- cert_util (opens new window) monitoring ssl certs
- chef (opens new window) chef client api
- cmd (opens new window) cmd port
- crypto (opens new window) calculate md5, sha256 hash for string
- db (opens new window) access to databases
- filepath (opens new window) path.filepath port
- goos (opens new window) os port
- http (opens new window) http.client && http.server
- humanize (opens new window) humanize github.com/dustin/go-humanize (opens new window) port
- inspect (opens new window) pretty print github.com/kikito/inspect.lua (opens new window)
- ioutil (opens new window) io/ioutil port
- json (opens new window) json implementation
- log (opens new window) log port
- plugin (opens new window) run lua code in lua code
- pprof (opens new window) pprof http-server for golang from lua
- prometheus (opens new window) prometheus exporter
- regexp (opens new window) regexp port
- runtime runtime port
- pb (opens new window) https://github.com/cheggaaa/pb (opens new window) port (v3)
- shellescape (opens new window) shellescape https://github.com/alessio/shellescape (opens new window) port
- stats (opens new window) stats https://github.com/montanaflynn/stats (opens new window) port
- storage (opens new window) package for store persist data and share values between lua states
- strings (opens new window) strings port (utf supported)
- tac (opens new window) tac line-by-line scanner (from end of file to up)
- tcp (opens new window) raw tcp client lib
- telegram (opens new window) telegram bot
- template (opens new window) template engines
- time (opens new window) time port
- xmlpath (opens new window) gopkg.in/xmlpath.v2 (opens new window) port
- yaml (opens new window) gopkg.in/yaml.v2 (opens new window) port
- zabbix (opens new window) zabbix bot
在 GitHub 上编辑此页 (opens new window)
上次更新: 2024/08/18, 13:07:53