增加包引用否则找不到相关的结构配置,增加radixtree中增加metadata数据传到回调函数中,多方例子测试传不进去,和目前使用的radixtree1.7版本有关,使用luarocks默认安装的

This commit is contained in:
wanglei 2025-11-07 07:58:29 +08:00
parent 22226239da
commit 477478510e
11 changed files with 73 additions and 18 deletions

View File

@ -56,8 +56,8 @@ http {
include 'system/system.conf';
#测试接口配置
location /testSQL {
content_by_lua_file '${APP_PATH}/src/test/testPostgres.lua';
location /testTree {
content_by_lua_file '${APP_PATH}/src/test/testRadixtree.lua';
}
location /testRBAC {
content_by_lua_file '${APP_PATH}/src/test/testRBAC.lua';

View File

@ -14,32 +14,32 @@ local routes = {
{
paths = { "/api/system/users" },
methods = { "GET" },
params = { "system::users::list" },
handler = systemUser.getSystemUsers,
metadata = "system::users::list",
},
{
paths = { "/api/system/users/:id" },
methods = { "GET" },
handler = systemUser.getSystemUser,
metadata = "system::users::view",
params = { "system::users::view" }
},
{
paths = { "/api/system/users" },
methods = { "POST" },
handler = systemUser.addSystemUser,
metadata = "system::users::add",
params = { "system::users::add" }
},
{
paths = { "/api/system/users/:id" },
methods = { "DELETE" },
handler = systemUser.deleteSystemUser,
metadata = "system::users::delete",
params = { "system::users::delete" }
},
{
paths = { "/api/system/users/:id" },
methods = { "PUT" },
handler = systemUser.updateSystemUser,
metadata = "system::users::edit",
params = { "system::users::edit" }
},
}
@ -51,9 +51,12 @@ if not rx then
end
--获取访问的uri地址
--local uri = ngx.var.request_uri
local uri = ngx.var.uri
local opts = {
host = ngx.var.host,
method = ngx.var.request_method,
remote_addr = ngx.var.remote_addr,
matched = {}
}

View File

@ -1,6 +1,7 @@
local jwt = require "resty.jwt"
local cjson = require("cjson.safe")
local jsonschema = require("jsonschema")
require("config")
-- 定义一个JSON Schema
local schema = {
@ -49,9 +50,11 @@ if jwt_obj.payload.exp and ngx.time() > jwt_obj.payload.exp then
end
-- Access claims in the payload
local claims = verified.claims
-- write the uid variable
ngx.var.uid = jwt_obj.payload
ngx.ctx.userid = jwt_obj.payload.userid
ngx.ctx.username = jwt_obj.payload.username
ngx.ctx.role = jwt_obj.payload.role
ngx.log(ngx.WARN, "claims: ".. cjson.encode(jwt_obj.payload))
--全部校验完成后,说明令牌有效,返回令牌数据
ngx.log(ngx.INFO, "令牌校验通过 JWT: " .. cjson.encode(jwt_obj))

View File

@ -7,6 +7,7 @@
"ngx_lua""init_by_lua_file";
nginx时初始化一次
--]]
require("config")
print("init application...")
--判断程序是否加载权限数据
@ -37,7 +38,7 @@ local function handler()
local red = redis:new()
-- 设置超时时间
red:set_timeout(conf.REDIS.TIMEOUT) -- 1秒
red:set_timeout(SYSTEM_CONFIG.REDIS.TIMEOUT) -- 1秒
-- 连接到 Redis
local ok, err = red:connect(SYSTEM_CONFIG.REDIS.HOST, SYSTEM_CONFIG.REDIS.PORT)
@ -55,7 +56,7 @@ local function handler()
end
-- 从连接池中获取连接
red:set_keepalive(SYSTEM_CONFIG.REDIS.POOL_MAX_IDLE_TIME, SYSTEM_CONFIG.REDIS.POOL_SIZE)
--red:set_keepalive(SYSTEM_CONFIG.REDIS.POOL_MAX_IDLE_TIME, SYSTEM_CONFIG.REDIS.POOL_SIZE)
-- 设置 key-value
local ok, err = red:set("admin-system:user:add", "1")

View File

@ -23,15 +23,23 @@ local function getUserId()
return userid
end
--判断用户是都有权限使用接口
--local payload = ngx.var.uid
--获取所有用户信息
function _M.getSystemUsers(m)
--获取登录的用户信息
local payload = ngx.var.uid
local metadata = m.metadata
ngx.log(ngx.INFO, "metadata value:"..metadata)
local userid = ngx.ctx.userid
local username = ngx.ctx.username
local role = ngx.ctx.role
ngx.log(ngx.INFO, "userid:"..userid.." username:"..username.." role:"..role)
local vars = m.params
print(vars)
local method = m._method
local path = m._path
ngx.log(ngx.INFO, "path:"..path.." method:"..method)
--local metadata = m.matched.action
--ngx.log(ngx.INFO, "metadata value:"..metadata)
--获取页码和请求的数据量
--local args = ngx.req.get_uri_args()
local pageNum = ngx.var.pagenum or 1

View File

@ -5,6 +5,7 @@
---
local snowflake = require("share.snowflake")
local cjson = require("cjson.safe")
require("config")
local _M = {}

View File

@ -1,3 +1,4 @@
require("config")
local Database = require('share.database')
local helpers = require('share.helpers')
local implode = helpers.implode

View File

@ -1,4 +1,5 @@
local redis = require("resty.redis")
require("config")
local _M = setmetatable({}, {__index = function(self, key)
local red = redis:new()

View File

@ -17,6 +17,42 @@ local redis = require("share.redis")
--max =a and b or c--a?b:c
local radix = require("resty.radixtree")
-- 路由处理函数注册表
local function user_handler(params)
print(params.name)
print(params.metadata.service)
end
-- 创建路由规则
local routes = {
{
paths = {"/user/:name"},
metadata = {
service = "user-service",
timeout = 3000,
},
methods = {"GET"},
handler = user_handler,
}
}
-- 初始化radixtree实例
local rx = radix.new(routes)
-- 路由分发主函数
-- 构建dispatch参数
local opts = {
--host = ngx.var.host,
method = ngx.var.request_method,
--remote_addr = ngx.var.remote_addr,
matched = {}
}
-- 使用dispatch方法进行路由匹配
local ok = rx:dispatch("/user/123", opts, opts.matched)
--[[
--获取用户相关的角色数据的数据
local function init_task()

View File

@ -33,7 +33,7 @@ local opts = {
}
-- matches the first route
ngx.say(rx:match("/login/update", opts)) -- metadata /login/action
ngx.say(rx:dispatch("/login/update", opts)) -- metadata /login/action
ngx.say("action: ", opts.matched.action) -- action: update
ngx.say(rx:match("/login/register", opts)) -- metadata /login/action

View File

@ -6,6 +6,7 @@
local jwt = require("resty.jwt")
local jsonschema = require("jsonschema")
require("config")
local _M = {}