From 477478510e83ac07036b8216925cbc9d67da9f9a Mon Sep 17 00:00:00 2001 From: wanglei <34475144@qqcom> Date: Fri, 7 Nov 2025 07:58:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8C=85=E5=BC=95=E7=94=A8?= =?UTF-8?q?=E5=90=A6=E5=88=99=E6=89=BE=E4=B8=8D=E5=88=B0=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E7=9A=84=E7=BB=93=E6=9E=84=E9=85=8D=E7=BD=AE=EF=BC=8C=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0radixtree=E4=B8=AD=E5=A2=9E=E5=8A=A0metadata=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E4=BC=A0=E5=88=B0=E5=9B=9E=E8=B0=83=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E4=B8=AD=EF=BC=8C=E5=A4=9A=E6=96=B9=E4=BE=8B=E5=AD=90=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E4=BC=A0=E4=B8=8D=E8=BF=9B=E5=8E=BB=EF=BC=8C=E5=92=8C?= =?UTF-8?q?=E7=9B=AE=E5=89=8D=E4=BD=BF=E7=94=A8=E7=9A=84radixtree1.7?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E6=9C=89=E5=85=B3=EF=BC=8C=E4=BD=BF=E7=94=A8?= =?UTF-8?q?luarocks=E9=BB=98=E8=AE=A4=E5=AE=89=E8=A3=85=E7=9A=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/nginx.conf | 4 ++-- src/api/system/user.lua | 13 ++++++++----- src/auth/jwt-auth.lua | 7 +++++-- src/init.lua | 5 +++-- src/service/system/user.lua | 20 ++++++++++++++------ src/share/helpers.lua | 1 + src/share/model.lua | 1 + src/share/redis.lua | 1 + src/test/test.lua | 36 ++++++++++++++++++++++++++++++++++++ src/test/testRadixtree.lua | 2 +- src/util/token.lua | 1 + 11 files changed, 73 insertions(+), 18 deletions(-) diff --git a/conf/nginx.conf b/conf/nginx.conf index 10fe14f..5df8465 100644 --- a/conf/nginx.conf +++ b/conf/nginx.conf @@ -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'; diff --git a/src/api/system/user.lua b/src/api/system/user.lua index 7cc3eb0..6375d89 100644 --- a/src/api/system/user.lua +++ b/src/api/system/user.lua @@ -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 = {} } diff --git a/src/auth/jwt-auth.lua b/src/auth/jwt-auth.lua index 9e0b18f..1f9acf9 100644 --- a/src/auth/jwt-auth.lua +++ b/src/auth/jwt-auth.lua @@ -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)) \ No newline at end of file diff --git a/src/init.lua b/src/init.lua index db7b20a..f1e2c31 100644 --- a/src/init.lua +++ b/src/init.lua @@ -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") diff --git a/src/service/system/user.lua b/src/service/system/user.lua index aca8aa9..6cedcbc 100644 --- a/src/service/system/user.lua +++ b/src/service/system/user.lua @@ -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 diff --git a/src/share/helpers.lua b/src/share/helpers.lua index abacb5a..a347a24 100644 --- a/src/share/helpers.lua +++ b/src/share/helpers.lua @@ -5,6 +5,7 @@ --- local snowflake = require("share.snowflake") local cjson = require("cjson.safe") +require("config") local _M = {} diff --git a/src/share/model.lua b/src/share/model.lua index 03f1955..0c88312 100644 --- a/src/share/model.lua +++ b/src/share/model.lua @@ -1,3 +1,4 @@ +require("config") local Database = require('share.database') local helpers = require('share.helpers') local implode = helpers.implode diff --git a/src/share/redis.lua b/src/share/redis.lua index 9fd297c..52c0ed9 100644 --- a/src/share/redis.lua +++ b/src/share/redis.lua @@ -1,4 +1,5 @@ local redis = require("resty.redis") +require("config") local _M = setmetatable({}, {__index = function(self, key) local red = redis:new() diff --git a/src/test/test.lua b/src/test/test.lua index 6854e87..ec8fef7 100644 --- a/src/test/test.lua +++ b/src/test/test.lua @@ -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() diff --git a/src/test/testRadixtree.lua b/src/test/testRadixtree.lua index 2e553b9..c8105e2 100644 --- a/src/test/testRadixtree.lua +++ b/src/test/testRadixtree.lua @@ -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 diff --git a/src/util/token.lua b/src/util/token.lua index ac69369..e96c8f2 100644 --- a/src/util/token.lua +++ b/src/util/token.lua @@ -6,6 +6,7 @@ local jwt = require("resty.jwt") local jsonschema = require("jsonschema") +require("config") local _M = {}