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,歡迎你們多多指導,共同進步。html
GNU Make 4.1
golang 1.9.2+
git
luajit 2.1
antpath.go (執行make的時候自動下載)
lua2go v1.0 (執行make的時候自動下載)
cjson (OpenResty自帶優良庫)
#直接執行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.
# 生成文件 vibrant@vibrant-Thinkpad-T440P:~/lua/lua-antpath$ ls libcgoantpath.so LICENSE lua2go.lua main.lua Makefile README.md
將libcgoantpath.so和lua2go.lua 拷貝到你的本身的項目中使用,使用例子在main.lua文件中。
#獲取lua-antpath項目的main.lua程序並使用luajit執行它。 luajit ./main.lua
文件調用以下:java
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 --