2025-11-04 09:33:40 +08:00
|
|
|
|
---
|
|
|
|
|
|
--- Generated by EmmyLua(https://github.com/EmmyLua)
|
|
|
|
|
|
--- Created by frankly.
|
|
|
|
|
|
--- DateTime: 2025/11/3 18:44
|
|
|
|
|
|
---
|
2025-11-04 21:57:42 +08:00
|
|
|
|
--[[
|
|
|
|
|
|
在"ngx_lua"模块的"init_by_lua_file"命令中执行;
|
|
|
|
|
|
只在启动nginx时初始化一次。
|
|
|
|
|
|
--]]
|
2025-11-08 10:09:25 +08:00
|
|
|
|
--只在第一个worker进程中执行一次
|
|
|
|
|
|
if ngx.worker.id() ~= 0 then
|
|
|
|
|
|
return
|
|
|
|
|
|
end
|
2025-11-04 09:33:40 +08:00
|
|
|
|
|
2025-11-06 11:51:06 +08:00
|
|
|
|
--判断程序是否加载权限数据
|
|
|
|
|
|
--local dict = ngx.shared.dict
|
|
|
|
|
|
--local load = dict:get("RBAC")
|
|
|
|
|
|
--if load then
|
|
|
|
|
|
-- return
|
|
|
|
|
|
--end
|
|
|
|
|
|
|
2025-11-10 19:34:43 +08:00
|
|
|
|
local conf = require("config")
|
2025-11-08 10:09:25 +08:00
|
|
|
|
print("init application woker id:", ngx.worker.id())
|
2025-11-06 11:51:06 +08:00
|
|
|
|
|
2025-11-04 21:57:42 +08:00
|
|
|
|
--初始化,获取系统默认的用户权限,为实现RBAC框架做权限数据准备
|
2025-11-06 11:51:06 +08:00
|
|
|
|
local function handler()
|
2025-11-08 00:01:43 +08:00
|
|
|
|
--与redis进行连接
|
2025-11-06 11:51:06 +08:00
|
|
|
|
local redis = require("resty.redis")
|
|
|
|
|
|
local red = redis:new()
|
|
|
|
|
|
-- 设置超时时间
|
2025-11-10 19:34:43 +08:00
|
|
|
|
red:set_timeout(conf.REDIS.TIMEOUT) -- 1秒
|
2025-11-06 11:51:06 +08:00
|
|
|
|
|
|
|
|
|
|
-- 连接到 Redis
|
2025-11-10 19:34:43 +08:00
|
|
|
|
local ok, err = red:connect(conf.REDIS.HOST, conf.REDIS.PORT)
|
2025-11-06 11:51:06 +08:00
|
|
|
|
if not ok then
|
|
|
|
|
|
ngx.log(ngx.ERR, "redis failed to connect: "..err)
|
|
|
|
|
|
return
|
|
|
|
|
|
end
|
|
|
|
|
|
--需要密码时对密码进行处理
|
2025-11-10 19:34:43 +08:00
|
|
|
|
if conf.REDIS.PASSWORD ~= nil then
|
|
|
|
|
|
local res, err = red:auth(conf.REDIS.PASSWORD)
|
2025-11-06 11:51:06 +08:00
|
|
|
|
if not res then
|
|
|
|
|
|
ngx.log(ngx.ERR, "redis failed to connect, password error: "..err)
|
|
|
|
|
|
return
|
|
|
|
|
|
end
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- 从连接池中获取连接
|
2025-11-10 19:34:43 +08:00
|
|
|
|
--red:set_keepalive(conf.REDIS.POOL_MAX_IDLE_TIME, conf.REDIS.POOL_SIZE)
|
2025-11-06 11:51:06 +08:00
|
|
|
|
|
2025-11-08 00:01:43 +08:00
|
|
|
|
--读取用户表、角色表和权限表中配置的权限数据
|
2025-11-10 19:34:43 +08:00
|
|
|
|
local roleDao = require("dao.system.role")
|
2025-11-08 00:01:43 +08:00
|
|
|
|
--获取数据表中的记录数
|
|
|
|
|
|
local code, res = roleDao:getAllSystemRoles()
|
|
|
|
|
|
if res == nil then return end
|
2025-11-08 15:10:36 +08:00
|
|
|
|
--管道进行redis处理
|
|
|
|
|
|
red:init_pipeline()
|
2025-11-08 00:01:43 +08:00
|
|
|
|
--读取角色id和角色名称
|
|
|
|
|
|
for _, row in pairs(res) do
|
|
|
|
|
|
local id = row.id --:1
|
|
|
|
|
|
local name = row.role_name --:admin
|
|
|
|
|
|
--row.status:0,
|
|
|
|
|
|
local code, rest = roleDao:getPermission2roleId(id)
|
|
|
|
|
|
for _, ret in pairs(rest) do
|
2025-11-08 10:09:25 +08:00
|
|
|
|
--获取数据表中的数据
|
2025-11-08 00:01:43 +08:00
|
|
|
|
local permid = ret.permission_id
|
|
|
|
|
|
local perm = ret.permission_code
|
|
|
|
|
|
local key = name.."-"..perm
|
2025-11-08 10:09:25 +08:00
|
|
|
|
--role_name-permission_code 组成key进行验证 存储到redis中
|
2025-11-08 15:10:36 +08:00
|
|
|
|
red:set(key, "1")
|
2025-11-08 00:01:43 +08:00
|
|
|
|
end
|
2025-11-06 11:51:06 +08:00
|
|
|
|
end
|
2025-11-08 15:10:36 +08:00
|
|
|
|
local results, err = red:commit_pipeline()
|
|
|
|
|
|
if not results then
|
|
|
|
|
|
ngx.log(ngx.ERR, "init failed to commit the pipelined requests: ", err)
|
|
|
|
|
|
end
|
2025-11-06 11:51:06 +08:00
|
|
|
|
--关闭redis连接
|
|
|
|
|
|
red:close()
|
|
|
|
|
|
|
2025-11-08 10:09:25 +08:00
|
|
|
|
--共享数据字典进行数据存储
|
2025-11-06 11:51:06 +08:00
|
|
|
|
--dict:set("RBAC", "1")
|
2025-11-08 15:41:10 +08:00
|
|
|
|
|
|
|
|
|
|
print("init application success")
|
2025-11-06 11:51:06 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- 设置定时器,执行一次handler函数
|
|
|
|
|
|
local ok, err = ngx.timer.at(0, handler)
|
|
|
|
|
|
if not ok then
|
|
|
|
|
|
ngx.log(ngx.ERR, "failed to create timer")
|
|
|
|
|
|
return
|
|
|
|
|
|
end
|