资讯专栏INFORMATION COLUMN

OpenResty下使用Apache Ant Path匹配库

TerryCai / 1177人阅读

摘要:下使用匹配库一简介是一个基于与的高性能平台而相对于编译型语言性能比较差,所以我们使用编写库的方式集成到项目中去。使用调用来实现匹配。执行测试获取项目的程序并使用执行它。

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.1  
golang 1.9.2+
git
3.2 运行环境
luajit 2.1  
antpath.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 

# 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 --

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/40588.html

相关文章

  • OpenResty使用Apache Ant Path匹配

    摘要:下使用匹配库一简介是一个基于与的高性能平台而相对于编译型语言性能比较差,所以我们使用编写库的方式集成到项目中去。使用调用来实现匹配。执行测试获取项目的程序并使用执行它。 OpenResty下使用Apache Ant Path匹配库 一、简介   OpenResty是一个基于 Nginx 与 Lua 的高性能 Web 平台,而lua相对于编译型语言性能比较差,所以我们使用编写shared...

    luxixing 评论0 收藏0
  • Openresty的开发闭环初探

    摘要:多返回值开始变得越来越与众不同了允许函数返回多个结果。这种情况函数没有足够的返回值时也会用来补充。中的索引习惯以开始。 showImg(https://segmentfault.com/img/bVIcQU?w=136&h=103); 为什么值得入手? Nginx作为现在使用最广泛的高性能后端服务器,Openresty为之提供了动态预言的灵活,当性能与灵活走在了一起,无疑对于被之前陷于...

    ruicbAndroid 评论0 收藏0
  • Linux安装Jenkins+JMeter+ANT自动化测试平台

    前排提示 下面几个工具运行都需要基于jdk,请大家自行百度安装jdk,并配置好环境变量 安装jmeter 下载包,解压,配置JMeter_home 点击这里下载最新的jmeter的tar包,直接放到服务器的/root目录中输入命令tar zxvf apache-jmeter-5.1.1.tgz解压 showImg(https://i.loli.net/2019/03/26/5c99f35e9a30...

    cncoder 评论0 收藏0
  • 日志平台(网关层) - 基于Openresty+ELKF+Kafka

    摘要:现在用方式调用接口,中使用方式输入内容日志平台网关层基于。日志平台网关层基于到此为止,提取经过网关的接口信息,并将其写入日志文件就完成了,所有的接口日志都写入了文件中。 背景介绍 1、问题现状与尝试 没有做日志记录的线上系统,绝对是给系统运维人员留下的坑。尤其是前后端分离的项目,后端的接口日志可以解决对接、测试和运维时的很多问题。之前项目上发布的接口都是通过Oracle Service...

    yvonne 评论0 收藏0
  • 日志平台(网关层) - 基于Openresty+ELKF+Kafka

    摘要:现在用方式调用接口,中使用方式输入内容日志平台网关层基于。日志平台网关层基于到此为止,提取经过网关的接口信息,并将其写入日志文件就完成了,所有的接口日志都写入了文件中。 背景介绍 1、问题现状与尝试 没有做日志记录的线上系统,绝对是给系统运维人员留下的坑。尤其是前后端分离的项目,后端的接口日志可以解决对接、测试和运维时的很多问题。之前项目上发布的接口都是通过Oracle Service...

    xumenger 评论0 收藏0

发表评论

0条评论

TerryCai

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<