From 30d16de03b7461923ac47a5d39093012247c6953 Mon Sep 17 00:00:00 2001 From: wanglei <34475144@qqcom> Date: Tue, 30 Sep 2025 10:23:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=96=87=E4=BB=B6=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=EF=BC=8C=E4=BF=AE=E6=94=B9=E9=85=8D=E7=BD=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/nginx.conf | 13 +++++++--- src/test/testObjClass.lua | 5 ++++ src/{ => test}/testPostgres.lua | 0 src/test/testRadixtree.lua | 40 +++++++++++++++++++++++++++++ src/test/testRedis.lua | 45 +++++++++++++++++++++++++++++++++ src/testRadixtree.lua | 13 ---------- 6 files changed, 100 insertions(+), 16 deletions(-) create mode 100644 src/test/testObjClass.lua rename src/{ => test}/testPostgres.lua (100%) create mode 100644 src/test/testRadixtree.lua create mode 100644 src/test/testRedis.lua delete mode 100644 src/testRadixtree.lua diff --git a/conf/nginx.conf b/conf/nginx.conf index 60e1f13..0c9a194 100644 --- a/conf/nginx.conf +++ b/conf/nginx.conf @@ -22,11 +22,18 @@ http { location /api { content_by_lua_file '/home/frankly/work/AuthPlatform/src/api/api.lua'; } + location /testTree { - content_by_lua_file '/home/frankly/work/AuthPlatform/src/testRadixtree.lua'; + content_by_lua_file '/home/frankly/work/AuthPlatform/src/test/testRadixtree.lua'; } - location /test { - content_by_lua_file '/home/frankly/work/AuthPlatform/src/testPostgres.lua'; + location /testSQL { + content_by_lua_file '/home/frankly/work/AuthPlatform/src/test/testPostgres.lua'; + } + location /testRedis { + content_by_lua_file '/home/frankly/work/AuthPlatform/src/test/testRedis.lua'; + } + location /testObj { + content_by_lua_file '/home/frankly/work/AuthPlatform/src/test/testObjClass.lua'; } } } \ No newline at end of file diff --git a/src/test/testObjClass.lua b/src/test/testObjClass.lua new file mode 100644 index 0000000..28ed7db --- /dev/null +++ b/src/test/testObjClass.lua @@ -0,0 +1,5 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by frankly. +--- DateTime: 2025/9/29 10:14 +--- \ No newline at end of file diff --git a/src/testPostgres.lua b/src/test/testPostgres.lua similarity index 100% rename from src/testPostgres.lua rename to src/test/testPostgres.lua diff --git a/src/test/testRadixtree.lua b/src/test/testRadixtree.lua new file mode 100644 index 0000000..2e553b9 --- /dev/null +++ b/src/test/testRadixtree.lua @@ -0,0 +1,40 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by frankly. +--- DateTime: 2025/9/28 10:27 +--- +local radix = require("resty.radixtree") +local rx = radix.new({ + { + paths = { "/login/*action" }, + metadata = { "metadata /login/action" }, + methods = { "GET", "POST", "PUT" }, + remote_addrs = { "127.0.0.1", "192.168.0.0/16", "::1", "fe80::/32" } + }, + { + paths = { "/user/:name" }, + metadata = { "metadata /user/name" }, + methods = { "GET" }, + }, + { + paths = { "/admin/:name", "/superuser/:name" }, + metadata = { "metadata /admin/name" }, + methods = { "GET", "POST", "PUT" }, + filter_fun = function(vars, opts) + return vars["arg_access"] == "admin" + end + } +}) + +local opts = { + method = "POST", + remote_addr = "127.0.0.1", + matched = {} +} + +-- matches the first route +ngx.say(rx:match("/login/update", opts)) -- metadata /login/action +ngx.say("action: ", opts.matched.action) -- action: update + +ngx.say(rx:match("/login/register", opts)) -- metadata /login/action +ngx.say("action: ", opts.matched.action) -- action: register diff --git a/src/test/testRedis.lua b/src/test/testRedis.lua new file mode 100644 index 0000000..d264f53 --- /dev/null +++ b/src/test/testRedis.lua @@ -0,0 +1,45 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by frankly. +--- DateTime: 2025/9/29 10:10 +--- + +local redis = require("resty.redis") +local red = redis:new() + +-- 设置超时时间 +red:set_timeout(1000) -- 1秒 + +-- 连接到 Redis +local ok, err = red:connect("127.0.0.1", 6379) +if not ok then + ngx.say("failed to connect: ", err) + return +end + +-- 设置 key-value +local ok, err = red:set("some_key", "hello world") +if not ok then + ngx.say("failed to set key: ", err) + return +end +ngx.say("set key successfully") + +-- 获取 key 的值 +local res, err = red:get("some_key") +if err then + ngx.say("failed to get key: ", err) + return +end +if res then + ngx.say("got: ", res) +else + ngx.say("key not found.") +end + +-- 关闭连接 +local ok, err = red:set_keepalive(10000, 50) -- 空闲10秒,最大空闲连接数50 +if not ok then + ngx.say("failed to set keepalive: ", err) + return +end diff --git a/src/testRadixtree.lua b/src/testRadixtree.lua deleted file mode 100644 index bf26548..0000000 --- a/src/testRadixtree.lua +++ /dev/null @@ -1,13 +0,0 @@ ---- ---- Generated by EmmyLua(https://github.com/EmmyLua) ---- Created by frankly. ---- DateTime: 2025/9/28 10:27 ---- -local radix = require("resty.radixtree") -local rx = radix.new({ - path = "/api/*", - host = "*.example.com", - method = "GET", - remote_addr = "127.0.0.1" -}) -ngx.say(rx.match("/api/v1/data", {path = "/api/v1/data"}) == true)