Compare commits

...

3 Commits

5 changed files with 391 additions and 161 deletions

View File

@ -7,8 +7,7 @@
return { return {
APP_ENV = "dev", -- dev/prod APP_ENV = "dev", -- dev/prod
locale = 'en', locale = 'zh',
fallback_locale = 'zh',
time_zone = "+8:00", -- UTC + 8 time_zone = "+8:00", -- UTC + 8

View File

@ -2,14 +2,16 @@
--- Generated by EmmyLua(https://github.com/EmmyLua) --- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by . --- Created by .
--- DateTime: 2025/9/25 08:19 --- DateTime: 2025/9/25 08:19
--- 业务逻辑 --- 业务逻辑 对用户数据表进行数据表业务处理
local cjson = require('cjson') local cjson = require('cjson')
local pgmoon = require('pgmoon') local pgmoon = require('pgmoon')
local dbconf = require("config.database") local dbconf = require("config.database")
local status = require("config.status") local status = require("config.status")
local snowflake = require("util.snowflake")
local _M = {} local _M = {}
--获取一个数据库操作连接
local function get_con(cfg) local function get_con(cfg)
local code = 0 local code = 0
-- 创建一个新的连接 -- 创建一个新的连接
@ -17,13 +19,22 @@ local function get_con(cfg)
---- 连接到数据库 ---- 连接到数据库
local ok, err = conn:connect() local ok, err = conn:connect()
if not ok then if not ok then
error("Connection failed: " .. err) print("Connection failed: " .. err)
code = 0x000002 code = 0x000002
end end
--ngx.say("Connection success") --ngx.say("Connection success")
return code,conn return code,conn
end end
local function getUuid()
local workerId = 0 -- 假设当前机器的ID是1范围在[0, 31]之间
local datacenterId = 0 -- 数据中心ID范围在[0, 31]之间
local snow = snowflake.new(workerId, datacenterId)
local id = snow:generateUniqueId()-- 生成ID
--print("Generated ID:", snow.int64_to_string(id))
return snow.int64_to_string(id)
end
-- 查询数据表中的所有用户信息 -- 查询数据表中的所有用户信息
function _M.getAllUser() function _M.getAllUser()
--组装sql语句 --组装sql语句
@ -35,8 +46,8 @@ function _M.getAllUser()
--执行数据库操作 --执行数据库操作
local res = conn:query(sql) local res = conn:query(sql)
if not res then if not res then
error("Query failed: " .. err) print("get all users Query failed: "..sql)
code = 0x000003 return 0x000003,res
end end
--整理数据库结果返回值 --整理数据库结果返回值
--for _, row in ipairs(res) do --for _, row in ipairs(res) do
@ -60,8 +71,8 @@ function _M.getUser(id)
--执行数据库操作 --执行数据库操作
local res = conn:query(sql) local res = conn:query(sql)
if not res then if not res then
error("Query failed: " .. err) print("Query failed: "..sql)
code = 0x000003 return 0x000003,res
end end
--关闭数据库 --关闭数据库
conn:disconnect() conn:disconnect()
@ -78,14 +89,36 @@ function _M.addUser(jsonData)
if success == false then if success == false then
return 0x000001,res return 0x000001,res
end end
--解析json中的键和数据值
local keys = ""
local values = ""
for key, value in pairs(result) do
keys = keys..key
local val = type(value)
if val == "string" then
values = values.."\'"..value.."\'"
else
values = values..value
end
keys = keys..","
values = values..","
end
--去掉组装最后一位逗号(,)
--local newKeys = keys:sub(1, #keys -1)
--local newValues = values:sub(1, #values -1)
--自己增加对应的uuid数据值
local newKeys = keys.."uuid"
local uuid = getUuid()
local newValues = values.."\'"..uuid.."\'"
--组装sql语句 --组装sql语句
local sql = "select * from \"T_Users\"" local sql = string.format("insert into \"T_Users\"(%s)values(%s)", newKeys, newValues)
--ngx.say(sql)
--获取数据库连接 --获取数据库连接
local code,conn = get_con(dbconf.postgres) local code,conn = get_con(dbconf.postgres)
--执行数据库操作 --执行数据库操作
res = conn:query(sql) res = conn:query(sql)
if not res then if not res then
error("Query failed: " .. err) print("adduser sql failed: "..sql)
return 0x000003,res return 0x000003,res
end end
--关闭数据库 --关闭数据库
@ -102,8 +135,8 @@ function _M.delete_user(id)
--执行数据库操作 --执行数据库操作
local res = conn:query(sql) local res = conn:query(sql)
if not res then if not res then
error("exec sql failed: " .. err) print("delete exec sql failed: "..sql)
code = 0x000003 return 0x000003,res
end end
--关闭数据库 --关闭数据库
conn:disconnect() conn:disconnect()

View File

@ -3,21 +3,28 @@
--- Created by admin. --- Created by admin.
--- DateTime: 2025/10/15 09:12 --- DateTime: 2025/10/15 09:12
--- ---
local snowflake = require("util.snowflake")
local workerId = 0 -- 假设当前机器的ID是1范围在[0, 31]之间
local datacenterId = 0 -- 数据中心ID范围在[0, 31]之间
local snow = snowflake.new(workerId, datacenterId)
local id = snow:generateUniqueId()-- 生成ID
ngx.say("Generated ID:"..snow.int64_to_string(id))
--读取请求体的数据 --读取请求体的数据
ngx.req.read_body() --ngx.req.read_body()
--获取请求数据 --获取请求数据
local body_data = ngx.req.get_body_data() --local body_data = ngx.req.get_body_data()
--if not body_data then --if not body_data then
-- ngx.say("read file error:", err) -- ngx.say("read file error:", err)
-- return ngx.exit(400) -- return ngx.exit(400)
--end --end
local len = #body_data --local len = #body_data
--local file_name = ngx.req.get_body_file() --local file_name = ngx.req.get_body_file()
ngx.say("file length:", len) --ngx.say("file length:", len)
--ngx.req.read_body_in_buffer(ngx.var.request_body_file) --ngx.req.read_body_in_buffer(ngx.var.request_body_file)

View File

@ -54,7 +54,6 @@ function _M:raw(http_status, http_body)
} }
end end
function _M:error(http_status, http_headers, http_body) function _M:error(http_status, http_headers, http_body)
return { return {
status = http_status, status = http_status,
@ -63,7 +62,6 @@ function _M:error(http_status, http_headers, http_body)
} }
end end
function _M:send(response) function _M:send(response)
ngx.status = response.status ngx.status = response.status
if response.headers ~= nil then if response.headers ~= nil then

193
src/util/snowflake.lua Normal file
View File

@ -0,0 +1,193 @@
local bit = require("bit")
--创建一个雪花算法的类
local snowflake = {}
snowflake.__index = snowflake
local sequence = 0 --序列号
-- 基础配置
local twepoch = 1420041600 -- 时间戳起点2015-01-01
local workerIdBits = 5 -- 机器ID位数
local datacenterIdBits = 5 -- 数据中心ID位数
local timestampBits = 41 -- 时间戳位数
local sequenceBits = 12 -- 序列号位数
local max = math.pow(2, 32) --4294967296
local max_workerId = bit.lshift(1 , workerIdBits) - 1
local max_datacenterId = bit.lshift(1 , datacenterIdBits) - 1
-- 位移偏移量
local timestampShift = sequenceBits + workerIdBits + datacenterIdBits -- 时间戳左移22位
local datacenterIdShift = sequenceBits + datacenterIdBits -- 机器ID左移位数12位
local workerIdShift = sequenceBits -- 数据中心ID左移位数17位
-- 生成序列的掩码这里为4095 (0b111111111111=0xfff=4095)
local sequenceMask = bit.lshift(1, sequenceBits) - 1;
--print("sequenceMask:", sequenceMask)
local lastTimestamp = -1; --上次生成ID的时间戳
--大数据数据左移
local function bit_lshift(x, n)
if n == 0 then return x end
return x * (2 ^ n)
end
--大数据数据右移
local function bit_rshift(x, n)
if n == 0 then return x end
x = x % 2^32 -- 处理负数问题
local res = x / 2^n
return res - res % 1 -- 取整操作,处理小数部分
end
-- 将一个大整数转换为32位整数数组
local function split_into_32bit_chunks(n)
local chunks = {}
while n > 0 do
table.insert(chunks, 1, n % 0x100000000) -- 取32位部分
n = math.floor(n / 0x100000000) -- 移除已处理的32位
end
return chunks
end
-- 执行按位与操作
local function bitwise_and(a, b)
local chunks_a = split_into_32bit_chunks(a)
local chunks_b = split_into_32bit_chunks(b)
local result = 0
local max_len = math.max(#chunks_a, #chunks_b)
print("max_len:", max_len)
for i = 0, max_len do
local chunk_a = chunks_a[i] or 0
local chunk_b = chunks_b[i] or 0
result = result * 0x100000000 + bit.band(chunk_a, chunk_b)
end
return result
end
local function bitwise_or(num1, num2)
local tmp1 = num1
local tmp2 = num2
local str = ""
while (tmp1 ~= 0 or tmp2 ~= 0)
do
local bit1 = tmp1 % 2
local bit2 = tmp2 % 2
if bit1 == 0 and bit2 == 0 then
str = "0" .. str
elseif bit1 == 1 or bit2 == 1 then
str = "1" .. str
else
str = "0" .. str
end
tmp1 = math.modf(tmp1 / 2)
tmp2 = math.modf(tmp2 / 2)
--print("tmp1:", tmp1)
--print("tmp2:", tmp2)
end
return tonumber(str, 2)
end
local function format_number(n, base)
local str = tostring(n)
--print("str:", str)
if base == 16 then -- 十六进制处理示例
return str:gsub("^0[xX]0*", "0x") -- 处理前导零和可能的'0x'
elseif base == 10 then -- 十进制处理示例
--print("value11:", str:gsub("^%-0*", ""))
--return str:gsub("^%-0*", "") -- 处理负数和前导零
return str:gsub("^0*([1-9]%d*)$", "%1")
else -- 其他基数可以按需添加处理逻辑
return str -- 或者根据需要返回其他格式化结果
end
end
function snowflake.int64_to_string(n)
local result = ""
local base = 100000000 -- 选择一个基数例如10^8
while n > 0 do
local part = n % base
result = string.format("%08d", part) .. result
n = math.floor(n / base)
end
local value = format_number(result, 10)
--print("value:", value)
return value
end
--构造函数
function snowflake.new(workerId, datacenterId)
if workerId < 0 then workerId = 0 end
if workerId > max_workerId then workerId = max_workerId end
if datacenterId < 0 then datacenterId = 0 end
if datacenterId > max_datacenterId then datacenterId = max_datacenterId end
local instance = {workerId = workerId, datacenterId = datacenterId}
return setmetatable(instance, snowflake)
end
--获取当前时间戳(毫秒)
function snowflake:getCurrentTimestamp()
local timestamp = os.time()
return timestamp
end
--获取新的时间戳
function snowflake:getNextTimestamp(lastTimestamp)
local timestamp = math.floor(os.time());
while (timestamp <= lastTimestamp)
do
timestamp = math.floor(os.time());
end
return timestamp;
end
-- 雪花算法的实现
function snowflake:generateUniqueId()
--local curtime = os.time()
--print("current time: ", curtime)
local timestamp = self.getCurrentTimestamp() -- 当前时间戳(毫秒)
-- 如果是同一时间生成的,则进行毫秒内序列
if lastTimestamp == timestamp then
sequence = bit.band((sequence + 1), sequenceMask);
-- 毫秒内序列溢出
if sequence == 0 then
--阻塞到下一个毫秒,获得新的时间戳
timestamp = self.getNextTimestamp(lastTimestamp)
end
else
sequence = 0
end
lastTimestamp = timestamp;
--print("sequence:", sequence)
--print("lastTimestamp:", lastTimestamp)
local current_time = lastTimestamp - twepoch
--print("current time: ", current_time)
-- 位运算生成ID
--((timestamp - twepoch) << timestampLeftShift)
local timeNew = bit_lshift(current_time, timestampShift)
--print("timeNew: ", timeNew)
--(datacenterId << datacenterIdShift)
local centerIdNew = bit_lshift(self.datacenterId, datacenterIdShift)
--print("centerIdNew: ", centerIdNew)
--(workerId << workerIdShift)
local workerIdNew = bit_lshift(self.workerId, workerIdShift)
--print("workerIdNew: ", workerIdNew)
--((timestamp - twepoch) << timestampLeftShift)
--| (datacenterId << datacenterIdShift)
--| (workerId << workerIdShift) | sequence
--将相关数据值进行位或操作
local tmp1 = bitwise_or(timeNew, centerIdNew)
local tmp2 = bitwise_or(tmp1, workerIdNew)
local id = bitwise_or(tmp2, sequence)
return id
end
return snowflake
--使用方法示例
--local workerId = 0 -- 假设当前机器的ID是1范围在[0, 31]之间
--local datacenterId = 0 -- 数据中心ID范围在[0, 31]之间
--local test = snowflake.new(workerId, datacenterId)
--local id = test:generateUniqueId()-- 生成ID
--print("Generated ID:", test:int64_to_string(id))