AuthPlatform/src/init.lua

110 lines
3.0 KiB
Lua
Raw Normal View History

---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by frankly.
--- DateTime: 2025/11/3 18:44
---
--[[
"ngx_lua""init_by_lua_file";
nginx时初始化一次
--]]
require("config")
print("init application...")
--判断程序是否加载权限数据
--local dict = ngx.shared.dict
--local load = dict:get("RBAC")
--if load then
-- return
--end
--只在第一个worker进程中执行一次
if ngx.worker.id() ~= 0 then
return
end
--初始化获取系统默认的用户权限为实现RBAC框架做权限数据准备
local function handler()
--读取用户表、角色表和权限表中配置的权限数据
local roleDao = require("dao.role")
--获取数据表中的记录数
local code, res = roleDao:all()
if res == nil then return end
ngx.log(ngx.INFO, "user count:"..res)
--读取角色id和角色名称
for _, row in pairs(res) do
for key, value in pairs(row) do
ngx.say(key .. ":" .. tostring(value))
end
end
--“admin-system::users::edit“ ”1“
--将取到的数据存储到redis中后续进行验证使用
local redis = require("resty.redis")
local red = redis:new()
-- 设置超时时间
red:set_timeout(SYSTEM_CONFIG.REDIS.TIMEOUT) -- 1秒
-- 连接到 Redis
local ok, err = red:connect(SYSTEM_CONFIG.REDIS.HOST, SYSTEM_CONFIG.REDIS.PORT)
if not ok then
ngx.log(ngx.ERR, "redis failed to connect: "..err)
return
end
--需要密码时对密码进行处理
if SYSTEM_CONFIG.REDIS.PASSWORD ~= nil then
local res, err = red:auth(SYSTEM_CONFIG.REDIS.PASSWORD)
if not res then
ngx.log(ngx.ERR, "redis failed to connect, password error: "..err)
return
end
end
-- 从连接池中获取连接
--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")
if not ok then
ngx.log(ngx.ERR, "redis failed to set key: "..err)
return
end
local ok, err = red:set("admin-system:user:edit", "1")
if not ok then
ngx.log(ngx.ERR, "failed to set key: "..err)
return
end
local ok, err = red:set("admin-system:user:delete", "1")
if not ok then
ngx.log(ngx.ERR, "failed to set key: "..err)
return
end
local ok, err = red:set("admin-system:user:view", "1")
if not ok then
ngx.log(ngx.ERR, "failed to set key: "..err)
return
end
local ok, err = red:set("admin-system:user:list", "1")
if not ok then
ngx.log(ngx.ERR, "failed to set key: "..err)
return
end
ngx.log(ngx.INFO, "set key successfully")
--关闭redis连接
red:close()
--dict:set("RBAC", "1")
end
-- 设置定时器执行一次handler函数
local ok, err = ngx.timer.at(0, handler)
if not ok then
ngx.log(ngx.ERR, "failed to create timer")
return
end