OpenResty下使用Apache Ant Path匹配库
OpenResty下使用Apache Ant Path匹配库
一、简介
OpenResty是一个基于 Nginx 与 Lua 的高性能 Web 平台,而lua相对于编译型语言性能比较差,所以我们使用编写sharedobject库的方式集成到OpenResty项目中去。luajit使用ffi调用libcgoantpath.so来实现pattern匹配。
基于以上思路我们实现了一个符合Apache Ant Path标准的动态共享库,Git地址:go-antpath v1.1,为了方大家使用我们还封装了lua版本的lua-antpath v1.0.1,欢迎大家多多指导,共同进步。
二、参考
- http://ant.apache.org/manual/api/org/apache/tools/ant/
- https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/util/PathMatcher.html
- go-antpath
- lua-antpath
- https://github.com/golang/go/wiki/cgo
- https://golang.org/cmd/cgo/
- https://groups.google.com/forum/#!topic/golang-nuts/Nb-nfVdAyF0
三、编译及运行环境
3.1 编译环境
GNU Make 4.1golang 1.9.2+
git
3.2 运行环境
luajit 2.1antpath.go (执行make的时候自动下载)
lua2go v1.0 (执行make的时候自动下载)
cjson (OpenResty自带优良库)
四、使用
4.1 make
#直接执行make会默认执行make all make #执行过程 vibrant@vibrant-Thinkpad-T440P:~/lua/lua-antpath$ make Cloning into 'go-antpath'... remote: Enumerating objects: 11, done. remote: Counting objects: 100% (11/11), done. remote: Compressing objects: 100% (8/8), done. remote: Total 267 (delta 4), reused 9 (delta 3), pack-reused 256 Receiving objects: 100% (267/267), 58.44 KiB | 160.00 KiB/s, done. Resolving deltas: 100% (171/171), done. Note: checking out '784165d119eea7faa4a880f00f1ea0d672c50799'. You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout. If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example: git checkout -b <new-branch-name> # remove the go-antpath project in gopath. # move to GOPATH get from internet --2019-04-11 00:42:52-- https://raw.githubusercontent.com/vibrantbyte/go-antpath/v1.1.1/antpath.go Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.196.133 Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.196.133|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 1901 (1.9K) [text/plain] Saving to: ‘antpath.go’ antpath.go 100%[===================>] 1.86K --.-KB/s in 0s 2019-04-11 00:42:53 (99.0 MB/s) - ‘antpath.go’ saved [1901/1901] --2019-04-11 00:42:53-- https://raw.githubusercontent.com/vibrantbyte/lua2go/v1.0/lua/lua2go.lua Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.196.133 Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.196.133|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 5135 (5.0K) [text/plain] Saving to: ‘lua2go.lua’ lua2go.lua 100%[===================>] 5.01K --.-KB/s in 0s 2019-04-11 00:42:54 (26.2 MB/s) - ‘lua2go.lua’ saved [5135/5135] execute script go build -ldflags "-s -w" -buildmode=c-shared -o libcgoantpath.so ./antpath.go install application clean successful all is execute.
4.2 复制文件
# 生成文件 vibrant@vibrant-Thinkpad-T440P:~/lua/lua-antpath$ ls libcgoantpath.so LICENSE lua2go.lua main.lua Makefile README.md将libcgoantpath.so和lua2go.lua 拷贝到你的自己的项目中使用,使用例子在main.lua文件中。
4.3 执行测试
#获取lua-antpath项目的main.lua程序并使用luajit执行它。 luajit ./main.lua
文件调用如下:
local lua2go = require('lua2go') local antpath = lua2go.Load('./libcgoantpath.so') local cjson = require("cjson") lua2go.Externs[[ extern char* Version(); extern void Increment(GoInt* value); extern GoBool IsPattern(GoString path); extern GoBool Match(GoString pattern,GoString path); extern GoBool MatchStart(GoString pattern,GoString path); extern GoString ExtractPathWithinPattern(GoString pattern,GoString path); extern GoString ExtractUriTemplateVariables(GoString pattern,GoString path); extern GoString Combine(GoString pattern1,GoString pattern2); extern void SetPathSeparator(GoString pathSeparator); extern void SetCaseSensitive(GoInt8 caseSensitive); extern void SetTrimTokens(GoInt8 trimTokens); extern void SetCachePatterns(GoInt8 cachePatterns); ]] -- 使用Version函数 -- begin -- local version = antpath.Version() -- 1. 获取版本号信息 local v = lua2go.ToLuaString(version) -- 2. 打印版本号信息 print(v) -- 使用Version函数 -- end -- print("--------------------------") -- 使用Increment函数 -- begin -- -- 指针操作 local intPtr = lua2go.ToGoPointer(1) antpath.Increment(intPtr) print(lua2go.ToLua(intPtr[0])) -- 使用Increment函数 -- end -- print("--------------------------") -- 使用Match函数 -- begin -- -- 1. 验证是否匹配 local bn = antpath.Match(lua2go.ToGoString("/*/1.html"),lua2go.ToGoString("/100/1.html")) -- 2. 打印匹配信息 print(lua2go.ToLuaBool(bn)) -- 使用Match函数 -- end -- print("--------------------------") -- 使用IsPattern函数 -- begin -- local ispattern = antpath.IsPattern(lua2go.ToGoString("/*/1.html")) print(lua2go.ToLuaBool(ispattern)) -- 使用IsPattern函数 -- end -- print("--------------------------") -- 使用Combine函数 -- begin -- local combine = antpath.Combine(lua2go.ToGoString("/hotels/*"),lua2go.ToGoString("/booking")) -- 1. 打印需要的pattern信息 print(lua2go.ToLuaString(combine.p)) -- 2. 使用完成后回收信息 lua2go.AddToGC(combine.p) -- 使用Combine函数 -- end -- print("--------------------------") -- 使用MatchStart函数 -- begin -- local start = antpath.MatchStart(lua2go.ToGoString("/*/1.html"),lua2go.ToGoString("/100/1.html")) print(lua2go.ToLuaBool(start)) -- 使用MatchStart函数 -- end -- print("--------------------------") -- 使用ExtractPathWithinPattern函数 -- begin -- local variable = antpath.ExtractPathWithinPattern(lua2go.ToGoString("/docs/cvs/*.html"),lua2go.ToGoString("/docs/cvs/commit.html")) print(lua2go.ToLuaString(variable.p)) -- 使用完成后回收信息 lua2go.AddToGC(variable.p) -- 使用ExtractPathWithinPattern函数 -- end -- print("--------------------------") -- 使用ExtractUriTemplateVariables函数 -- begin -- local map = antpath.ExtractUriTemplateVariables(lua2go.ToGoString("/hotels/{hotel}"),lua2go.ToGoString("/hotels/11232")) local mapstr = lua2go.ToLuaString(map.p) local data = cjson.decode(mapstr) print(data.hotel) lua2go.AddToGC(map.p) -- 使用fExtractUriTemplateVariables函数 -- end -- print("--------------------------") -- 设置fields信息 -- begin -- antpath.SetPathSeparator(lua2go.ToGoString("/")) print("设置SetPathSeparator成功") antpath.SetCaseSensitive(0) print("设置SetCaseSensitive成功") antpath.SetTrimTokens(1) print("设置SetTrimTokens成功") antpath.SetCachePatterns(1) print("设置SetCachePatterns成功") -- 设置fields信息 -- end --
相关推荐
oLeiShen 2020-06-25
dahege 2020-06-25
wqiaofujiang 2020-06-16
zllbirdonland 2020-06-16
咻咻ing 2020-06-14
liwf 2020-06-10
CSDNMrWang 2020-05-11
陈云佳 2020-04-21
Neptune 2020-04-20
aolishuai 2020-04-15
工作中的点点滴滴 2020-02-15
长安长夜Saint 2020-02-14
liwf 2020-01-10
taoqilin 2020-01-09
始终不够 2019-12-06
byourb 2019-11-29
徐中海C 2019-11-17
hong0 2019-11-17
houjinkai 2019-11-16