删除不使用的插件文件,增加使用pgmoon插件库进行数据库操作的功能,并实现根据用户提供的id或者查询所有用户返回数据表中的数据
This commit is contained in:
parent
7529cb9114
commit
756f9b7343
|
|
@ -1,4 +1,5 @@
|
|||
worker_processes 1;
|
||||
worker_processes 1; #nginx worker 数量
|
||||
error_log logs/error.log; #指定错误日志文件路径
|
||||
|
||||
#worker_rlimit_nofile 65535;
|
||||
|
||||
|
|
@ -26,5 +27,7 @@ http {
|
|||
}
|
||||
|
||||
include 'system/user.conf';
|
||||
|
||||
include 'system/test.conf';
|
||||
}
|
||||
}
|
||||
4
conf/system/test.conf
Normal file
4
conf/system/test.conf
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
#测试接口文件
|
||||
location /testSQL {
|
||||
content_by_lua_file '/home/frankly/work/AuthPlatform/src/test/testPostgres.lua';
|
||||
}
|
||||
|
|
@ -1 +0,0 @@
|
|||
11428
|
||||
|
|
@ -6,46 +6,40 @@
|
|||
local radix = require("resty.radixtree")
|
||||
local userApi = require("api.system.user")
|
||||
|
||||
--定义相关路由,前端接口url地址
|
||||
local routes = {
|
||||
{
|
||||
paths = { "/api/user" },
|
||||
metadata = { "metadata /user" },
|
||||
metadata = { "metadata user" },
|
||||
methods = { "GET", "POST" },
|
||||
handler = userApi.get_allusers,
|
||||
},
|
||||
{
|
||||
paths = { "/api/user/:id" },
|
||||
metadata = { "metadata /user/id" },
|
||||
metadata = { "metadata /api/user/id" },
|
||||
methods = { "GET", "PUT", "DELETE" },
|
||||
handler = userApi.get_users,
|
||||
handler = userApi.get_user,
|
||||
},
|
||||
{
|
||||
paths = { "/api/admin/:name", "/api/superuser/:name" },
|
||||
metadata = { "metadata /admin/name" },
|
||||
methods = { "GET", "POST", "PUT" },
|
||||
filter_fun = function(vars, opts)
|
||||
return vars["arg_access"] == "admin"
|
||||
end
|
||||
}
|
||||
}
|
||||
|
||||
-- 初始化路由
|
||||
local rx, err = radix.new(routes)
|
||||
if not rx then
|
||||
ngx.say("-----")
|
||||
ngx.status = 404
|
||||
ngx.say("Not Found")
|
||||
--ngx.exit()
|
||||
ngx.exit(ngx.HTTP_NOT_FOUND)
|
||||
end
|
||||
|
||||
--获取访问的uri地址
|
||||
local uri = ngx.var.uri
|
||||
local opts = {
|
||||
method = ngx.var.request_method,
|
||||
--vars = ngx.var,
|
||||
matched = {}
|
||||
}
|
||||
|
||||
-- 进行路由匹配和相关函数调用
|
||||
local ok = rx:dispatch(uri, opts, opts.matched)
|
||||
if not ok then
|
||||
ngx.status = 404
|
||||
ngx.say("Not Found")
|
||||
--ngx.exit()
|
||||
end
|
||||
ngx.exit(ngx.HTTP_NOT_FOUND)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -6,14 +6,17 @@
|
|||
|
||||
local _M = {}
|
||||
|
||||
local dao = require("service.system.user")
|
||||
|
||||
--获取所有用户信息
|
||||
function _M.get_allusers()
|
||||
|
||||
dao.getAllUser()
|
||||
end
|
||||
|
||||
--获取所有用户信息
|
||||
function _M.get_users(req)
|
||||
|
||||
--根据用户id获取用户信息
|
||||
function _M.get_user(m)
|
||||
local id = m.id
|
||||
dao.getUser(id)
|
||||
end
|
||||
|
||||
return _M
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
local env = require('env')
|
||||
local env = require('config.config')
|
||||
|
||||
return {
|
||||
redis_prefix = 'Auth:',
|
||||
|
|
@ -11,8 +11,8 @@ return {
|
|||
postgres = {
|
||||
host = env.POSTGRES.HOST,
|
||||
port = env.POSTGRES.PORT,
|
||||
username = env.POSTGRES.USERNAME,
|
||||
user = env.POSTGRES.USERNAME,
|
||||
password = env.POSTGRES.PASSWORD,
|
||||
dbname = env.POSTGRES.DATABASE
|
||||
database = env.POSTGRES.DATABASE
|
||||
},
|
||||
}
|
||||
|
|
@ -4,24 +4,73 @@
|
|||
--- DateTime: 2025/9/25 08:19
|
||||
--- 业务逻辑
|
||||
local cjson = require('cjson')
|
||||
local pgmoon = require('pgmoon');
|
||||
local pgmoon = require('pgmoon')
|
||||
local dbconf = require("config.database")
|
||||
|
||||
local _M = {}
|
||||
|
||||
-- 创建一个新的连接
|
||||
local conn = pgmoon.new(db_config.postgres)
|
||||
|
||||
-- 连接到数据库
|
||||
conn:connect(function(err)
|
||||
if err then
|
||||
print("Error connecting to database: ", err)
|
||||
else
|
||||
print("Connected to the PostgreSQL server.")
|
||||
local function get_con(cfg)
|
||||
-- 创建一个新的连接
|
||||
local conn = pgmoon.new(cfg);
|
||||
---- 连接到数据库
|
||||
local ok, err = conn:connect()
|
||||
if not ok then
|
||||
error("Connection failed: " .. err)
|
||||
ngx.exit(ngx.HTTP_NOT_FOUND)
|
||||
end
|
||||
end)
|
||||
|
||||
function _M.getAllUser()
|
||||
|
||||
--ngx.say("Connection success")
|
||||
return conn
|
||||
end
|
||||
|
||||
return _M
|
||||
-- 查询数据表中的所有用户信息
|
||||
function _M.getAllUser()
|
||||
--组装sql语句
|
||||
local sql = "select * from \"T_Users\""
|
||||
--获取数据库连接
|
||||
local conn = get_con(dbconf.postgres)
|
||||
--设置数据库的编码格式
|
||||
--conn:exec("SET NAMES UTF8")
|
||||
--执行数据库操作
|
||||
local res = conn:query(sql)
|
||||
if not res then
|
||||
error("Query failed: " .. err)
|
||||
--ngx.say(err)
|
||||
ngx.exit(ngx.HTTP_NOT_FOUND)
|
||||
end
|
||||
--整理数据库结果返回值
|
||||
for _, row in ipairs(res) do
|
||||
for key, value in pairs(row) do
|
||||
ngx.say(key .. ":" .. tostring(value))
|
||||
end
|
||||
end
|
||||
|
||||
--关闭数据库
|
||||
conn:disconnect()
|
||||
end
|
||||
|
||||
--根据用户id获取用户信息
|
||||
function _M.getUser(id)
|
||||
--组装sql语句
|
||||
local sql = "select * from \"T_Users\" where id="..id
|
||||
--获取数据库连接
|
||||
local conn = get_con(dbconf.postgres)
|
||||
--设置数据库的编码格式
|
||||
--conn:exec("SET NAMES UTF8")
|
||||
--执行数据库操作
|
||||
local res = conn:query(sql)
|
||||
if not res then
|
||||
error("Query failed: " .. err)
|
||||
--ngx.say(err)
|
||||
ngx.exit(ngx.HTTP_NOT_FOUND)
|
||||
end
|
||||
--整理数据库结果返回值
|
||||
for _, row in ipairs(res) do
|
||||
for key, value in pairs(row) do
|
||||
ngx.say(key .. ":" .. tostring(value))
|
||||
end
|
||||
end
|
||||
|
||||
--关闭数据库
|
||||
conn:disconnect()
|
||||
end
|
||||
return _M
|
||||
|
|
|
|||
|
|
@ -1 +0,0 @@
|
|||
return require('pgmoon.init')
|
||||
|
|
@ -1,196 +0,0 @@
|
|||
local OIDS = {
|
||||
boolean = 1000,
|
||||
number = 1231,
|
||||
string = 1009,
|
||||
array_json = 199,
|
||||
array_jsonb = 3807
|
||||
}
|
||||
local is_array
|
||||
is_array = function(oid)
|
||||
for k, v in pairs(OIDS) do
|
||||
if v == oid then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
local PostgresArray
|
||||
do
|
||||
local _class_0
|
||||
local _base_0 = { }
|
||||
_base_0.__index = _base_0
|
||||
_class_0 = setmetatable({
|
||||
__init = function() end,
|
||||
__base = _base_0,
|
||||
__name = "PostgresArray"
|
||||
}, {
|
||||
__index = _base_0,
|
||||
__call = function(cls, ...)
|
||||
local _self_0 = setmetatable({}, _base_0)
|
||||
cls.__init(_self_0, ...)
|
||||
return _self_0
|
||||
end
|
||||
})
|
||||
_base_0.__class = _class_0
|
||||
local self = _class_0
|
||||
self.__base.pgmoon_serialize = function(v, pg)
|
||||
local escaped
|
||||
do
|
||||
local _accum_0 = { }
|
||||
local _len_0 = 1
|
||||
for _index_0 = 1, #v do
|
||||
local val = v[_index_0]
|
||||
if val == pg.NULL then
|
||||
_accum_0[_len_0] = "NULL"
|
||||
else
|
||||
local _exp_0 = type(val)
|
||||
if "number" == _exp_0 then
|
||||
_accum_0[_len_0] = tostring(val)
|
||||
elseif "string" == _exp_0 then
|
||||
_accum_0[_len_0] = '"' .. val:gsub('"', [[\"]]) .. '"'
|
||||
elseif "boolean" == _exp_0 then
|
||||
_accum_0[_len_0] = val and "t" or "f"
|
||||
elseif "table" == _exp_0 then
|
||||
local _oid, _value
|
||||
do
|
||||
local v_mt = getmetatable(val)
|
||||
if v_mt then
|
||||
if v_mt.pgmoon_serialize then
|
||||
_oid, _value = v_mt.pgmoon_serialize(val, pg)
|
||||
end
|
||||
end
|
||||
end
|
||||
if _oid then
|
||||
if is_array(_oid) then
|
||||
_accum_0[_len_0] = _value
|
||||
else
|
||||
_accum_0[_len_0] = '"' .. _value:gsub('"', [[\"]]) .. '"'
|
||||
end
|
||||
else
|
||||
return nil, "table does not implement pgmoon_serialize, can't serialize"
|
||||
end
|
||||
end
|
||||
end
|
||||
_len_0 = _len_0 + 1
|
||||
end
|
||||
escaped = _accum_0
|
||||
end
|
||||
local type_oid = 0
|
||||
for _index_0 = 1, #v do
|
||||
local _continue_0 = false
|
||||
repeat
|
||||
do
|
||||
local val = v[_index_0]
|
||||
if val == pg.NULL then
|
||||
_continue_0 = true
|
||||
break
|
||||
end
|
||||
type_oid = OIDS[type(val)] or type_oid
|
||||
break
|
||||
end
|
||||
_continue_0 = true
|
||||
until true
|
||||
if not _continue_0 then
|
||||
break
|
||||
end
|
||||
end
|
||||
return type_oid, "{" .. tostring(table.concat(escaped, ",")) .. "}"
|
||||
end
|
||||
PostgresArray = _class_0
|
||||
end
|
||||
getmetatable(PostgresArray).__call = function(self, t)
|
||||
return setmetatable(t, self.__base)
|
||||
end
|
||||
local default_escape_literal = nil
|
||||
local insert, concat
|
||||
do
|
||||
local _obj_0 = table
|
||||
insert, concat = _obj_0.insert, _obj_0.concat
|
||||
end
|
||||
local encode_array
|
||||
do
|
||||
local append_buffer
|
||||
append_buffer = function(escape_literal, buffer, values)
|
||||
for _index_0 = 1, #values do
|
||||
local item = values[_index_0]
|
||||
if type(item) == "table" and not getmetatable(item) then
|
||||
insert(buffer, "[")
|
||||
append_buffer(escape_literal, buffer, item)
|
||||
buffer[#buffer] = "]"
|
||||
insert(buffer, ",")
|
||||
else
|
||||
insert(buffer, escape_literal(item))
|
||||
insert(buffer, ",")
|
||||
end
|
||||
end
|
||||
return buffer
|
||||
end
|
||||
encode_array = function(tbl, escape_literal)
|
||||
escape_literal = escape_literal or default_escape_literal
|
||||
if not (escape_literal) then
|
||||
local Postgres
|
||||
Postgres = require("pgmoon").Postgres
|
||||
default_escape_literal = function(v)
|
||||
return Postgres.escape_literal(nil, v)
|
||||
end
|
||||
escape_literal = default_escape_literal
|
||||
end
|
||||
local buffer = append_buffer(escape_literal, {
|
||||
"ARRAY["
|
||||
}, tbl)
|
||||
if buffer[#buffer] == "," then
|
||||
buffer[#buffer] = "]"
|
||||
else
|
||||
insert(buffer, "]")
|
||||
end
|
||||
return concat(buffer)
|
||||
end
|
||||
end
|
||||
local convert_values
|
||||
convert_values = function(array, fn, pg)
|
||||
for idx, v in ipairs(array) do
|
||||
if type(v) == "table" then
|
||||
convert_values(v, fn)
|
||||
else
|
||||
if v == "NULL" then
|
||||
array[idx] = pg.NULL
|
||||
elseif fn then
|
||||
array[idx] = fn(v)
|
||||
else
|
||||
array[idx] = v
|
||||
end
|
||||
end
|
||||
end
|
||||
return array
|
||||
end
|
||||
local decode_array
|
||||
do
|
||||
local P, R, S, V, Ct, C, Cs
|
||||
do
|
||||
local _obj_0 = require("lpeg")
|
||||
P, R, S, V, Ct, C, Cs = _obj_0.P, _obj_0.R, _obj_0.S, _obj_0.V, _obj_0.Ct, _obj_0.C, _obj_0.Cs
|
||||
end
|
||||
local g = P({
|
||||
"array",
|
||||
array = Ct(V("open") * (V("value") * (P(",") * V("value")) ^ 0) ^ -1 * V("close")),
|
||||
value = V("invalid_char") + V("string") + V("array") + V("literal"),
|
||||
string = P('"') * Cs((P([[\\]]) / [[\]] + P([[\"]]) / [["]] + (P(1) - P('"'))) ^ 0) * P('"'),
|
||||
literal = C((P(1) - S("},")) ^ 1),
|
||||
invalid_char = S(" \t\r\n") / function()
|
||||
return error("got unexpected whitespace")
|
||||
end,
|
||||
open = P("{"),
|
||||
delim = P(","),
|
||||
close = P("}")
|
||||
})
|
||||
decode_array = function(str, convert_fn, pg)
|
||||
local out = (assert(g:match(str), "failed to parse postgresql array"))
|
||||
setmetatable(out, PostgresArray.__base)
|
||||
return convert_values(out, convert_fn, (pg or require("pgmoon").Postgres))
|
||||
end
|
||||
end
|
||||
return {
|
||||
encode_array = encode_array,
|
||||
decode_array = decode_array,
|
||||
PostgresArray = PostgresArray
|
||||
}
|
||||
|
|
@ -1,67 +0,0 @@
|
|||
local rshift, lshift, band, bxor
|
||||
local load_code
|
||||
load_code = function(str)
|
||||
local sent = false
|
||||
return pcall(load(function()
|
||||
if sent then
|
||||
return nil
|
||||
end
|
||||
sent = true
|
||||
return str
|
||||
end))
|
||||
end
|
||||
local ok
|
||||
ok, band = load_code([[ return function(a,b)
|
||||
a = a & b
|
||||
if a > 0x7FFFFFFF then
|
||||
-- extend the sign bit
|
||||
a = ~0xFFFFFFFF | a
|
||||
end
|
||||
return a
|
||||
end
|
||||
]])
|
||||
if ok then
|
||||
local _
|
||||
_, bxor = load_code([[ return function(a,b)
|
||||
a = a ~ b
|
||||
if a > 0x7FFFFFFF then
|
||||
-- extend the sign bit
|
||||
a = ~0xFFFFFFFF | a
|
||||
end
|
||||
return a
|
||||
end
|
||||
]])
|
||||
_, lshift = load_code([[ return function(x,y)
|
||||
-- limit to 32-bit shifts
|
||||
y = y % 32
|
||||
x = x << y
|
||||
if x > 0x7FFFFFFF then
|
||||
-- extend the sign bit
|
||||
x = ~0xFFFFFFFF | x
|
||||
end
|
||||
return x
|
||||
end
|
||||
]])
|
||||
_, rshift = load_code([[ return function(x,y)
|
||||
y = y % 32
|
||||
-- truncate to 32-bit before applying shift
|
||||
x = x & 0xFFFFFFFF
|
||||
x = x >> y
|
||||
if x > 0x7FFFFFFF then
|
||||
x = ~0xFFFFFFFF | x
|
||||
end
|
||||
return x
|
||||
end
|
||||
]])
|
||||
else
|
||||
do
|
||||
local _obj_0 = require("bit")
|
||||
rshift, lshift, band, bxor = _obj_0.rshift, _obj_0.lshift, _obj_0.band, _obj_0.bxor
|
||||
end
|
||||
end
|
||||
return {
|
||||
rshift = rshift,
|
||||
lshift = lshift,
|
||||
band = band,
|
||||
bxor = bxor
|
||||
}
|
||||
|
|
@ -1,75 +0,0 @@
|
|||
local flatten
|
||||
flatten = require("pgmoon.util").flatten
|
||||
local CqueuesSocket
|
||||
do
|
||||
local _class_0
|
||||
local _base_0 = {
|
||||
connect = function(self, host, port, opts)
|
||||
local socket = require("cqueues.socket")
|
||||
local errno = require("cqueues.errno")
|
||||
self.sock = socket.connect({
|
||||
host = host,
|
||||
port = port
|
||||
})
|
||||
if self.timeout then
|
||||
self.sock:settimeout(self.timeout)
|
||||
end
|
||||
self.sock:setmode("bn", "bn")
|
||||
local success, err = self.sock:connect()
|
||||
if not (success) then
|
||||
return nil, errno.strerror(err)
|
||||
end
|
||||
return true
|
||||
end,
|
||||
starttls = function(self, ...)
|
||||
return self.sock:starttls(...)
|
||||
end,
|
||||
getpeercertificate = function(self)
|
||||
local ssl = assert(self.sock:checktls())
|
||||
return assert(ssl:getPeerCertificate(), "no peer certificate available")
|
||||
end,
|
||||
send = function(self, ...)
|
||||
return self.sock:write(flatten(...))
|
||||
end,
|
||||
receive = function(self, ...)
|
||||
return self.sock:read(...)
|
||||
end,
|
||||
close = function(self)
|
||||
return self.sock:close()
|
||||
end,
|
||||
settimeout = function(self, t)
|
||||
if t then
|
||||
t = t / 1000
|
||||
end
|
||||
if self.sock then
|
||||
return self.sock:settimeout(t)
|
||||
else
|
||||
self.timeout = t
|
||||
end
|
||||
end,
|
||||
getreusedtimes = function(self)
|
||||
return 0
|
||||
end,
|
||||
setkeepalive = function(self)
|
||||
return error("You attempted to call setkeepalive on a cqueues.socket. This method is only available for the ngx cosocket API for releasing a socket back into the connection pool")
|
||||
end
|
||||
}
|
||||
_base_0.__index = _base_0
|
||||
_class_0 = setmetatable({
|
||||
__init = function() end,
|
||||
__base = _base_0,
|
||||
__name = "CqueuesSocket"
|
||||
}, {
|
||||
__index = _base_0,
|
||||
__call = function(cls, ...)
|
||||
local _self_0 = setmetatable({}, _base_0)
|
||||
cls.__init(_self_0, ...)
|
||||
return _self_0
|
||||
end
|
||||
})
|
||||
_base_0.__class = _class_0
|
||||
CqueuesSocket = _class_0
|
||||
end
|
||||
return {
|
||||
CqueuesSocket = CqueuesSocket
|
||||
}
|
||||
|
|
@ -1,178 +0,0 @@
|
|||
local md5
|
||||
if ngx then
|
||||
md5 = ngx.md5
|
||||
elseif pcall(function()
|
||||
return require("openssl.digest")
|
||||
end) then
|
||||
local openssl_digest = require("openssl.digest")
|
||||
local hex_char
|
||||
hex_char = function(c)
|
||||
return string.format("%02x", string.byte(c))
|
||||
end
|
||||
local hex
|
||||
hex = function(str)
|
||||
return (str:gsub(".", hex_char))
|
||||
end
|
||||
md5 = function(str)
|
||||
return hex(openssl_digest.new("md5"):final(str))
|
||||
end
|
||||
elseif pcall(function()
|
||||
return require("crypto")
|
||||
end) then
|
||||
local crypto = require("crypto")
|
||||
md5 = function(str)
|
||||
return crypto.digest("md5", str)
|
||||
end
|
||||
else
|
||||
md5 = function()
|
||||
return error("Either luaossl (recommended) or LuaCrypto is required to calculate md5")
|
||||
end
|
||||
end
|
||||
local hmac_sha256
|
||||
if pcall(function()
|
||||
return require("openssl.hmac")
|
||||
end) then
|
||||
hmac_sha256 = function(key, str)
|
||||
local openssl_hmac = require("openssl.hmac")
|
||||
local hmac = assert(openssl_hmac.new(key, "sha256"))
|
||||
hmac:update(str)
|
||||
return assert(hmac:final())
|
||||
end
|
||||
elseif pcall(function()
|
||||
return require("resty.openssl.hmac")
|
||||
end) then
|
||||
hmac_sha256 = function(key, str)
|
||||
local openssl_hmac = require("resty.openssl.hmac")
|
||||
local hmac = assert(openssl_hmac.new(key, "sha256"))
|
||||
hmac:update(str)
|
||||
return assert(hmac:final())
|
||||
end
|
||||
else
|
||||
hmac_sha256 = function()
|
||||
return error("Either luaossl or resty.openssl is required to calculate hmac sha256 digest")
|
||||
end
|
||||
end
|
||||
local digest_sha256
|
||||
if pcall(function()
|
||||
return require("openssl.digest")
|
||||
end) then
|
||||
digest_sha256 = function(str)
|
||||
local digest = assert(require("openssl.digest").new("sha256"))
|
||||
digest:update(str)
|
||||
return assert(digest:final())
|
||||
end
|
||||
elseif pcall(function()
|
||||
return require("resty.sha256")
|
||||
end) then
|
||||
digest_sha256 = function(str)
|
||||
local digest = assert(require("resty.sha256"):new())
|
||||
digest:update(str)
|
||||
return assert(digest:final())
|
||||
end
|
||||
elseif pcall(function()
|
||||
return require("resty.openssl.digest")
|
||||
end) then
|
||||
digest_sha256 = function(str)
|
||||
local digest = assert(require("resty.openssl.digest").new("sha256"))
|
||||
digest:update(str)
|
||||
return assert(digest:final())
|
||||
end
|
||||
else
|
||||
digest_sha256 = function()
|
||||
return error("Either luaossl or resty.openssl is required to calculate sha256 digest")
|
||||
end
|
||||
end
|
||||
local kdf_derive_sha256
|
||||
if pcall(function()
|
||||
return require("openssl.kdf")
|
||||
end) then
|
||||
kdf_derive_sha256 = function(str, salt, i)
|
||||
local openssl_kdf = require("openssl.kdf")
|
||||
local decode_base64
|
||||
decode_base64 = require("pgmoon.util").decode_base64
|
||||
salt = decode_base64(salt)
|
||||
local key, err = openssl_kdf.derive({
|
||||
type = "PBKDF2",
|
||||
md = "sha256",
|
||||
salt = salt,
|
||||
iter = i,
|
||||
pass = str,
|
||||
outlen = 32
|
||||
})
|
||||
if not (key) then
|
||||
return nil, "failed to derive pbkdf2 key: " .. tostring(err)
|
||||
end
|
||||
return key
|
||||
end
|
||||
elseif pcall(function()
|
||||
return require("resty.openssl.kdf")
|
||||
end) then
|
||||
kdf_derive_sha256 = function(str, salt, i)
|
||||
local openssl_kdf = require("resty.openssl.kdf")
|
||||
local decode_base64
|
||||
decode_base64 = require("pgmoon.util").decode_base64
|
||||
salt = decode_base64(salt)
|
||||
local key, err = openssl_kdf.derive({
|
||||
type = openssl_kdf.PBKDF2,
|
||||
md = "sha256",
|
||||
salt = salt,
|
||||
pbkdf2_iter = i,
|
||||
pass = str,
|
||||
outlen = 32
|
||||
})
|
||||
if not (key) then
|
||||
return nil, "failed to derive pbkdf2 key: " .. tostring(err)
|
||||
end
|
||||
return key
|
||||
end
|
||||
else
|
||||
kdf_derive_sha256 = function()
|
||||
return error("Either luaossl or resty.openssl is required to derive pbkdf2 key")
|
||||
end
|
||||
end
|
||||
local random_bytes
|
||||
if pcall(function()
|
||||
return require("openssl.rand")
|
||||
end) then
|
||||
random_bytes = require("openssl.rand").bytes
|
||||
elseif pcall(function()
|
||||
return require("resty.random")
|
||||
end) then
|
||||
random_bytes = require("resty.random").bytes
|
||||
elseif pcall(function()
|
||||
return require("resty.openssl.rand")
|
||||
end) then
|
||||
random_bytes = require("resty.openssl.rand").bytes
|
||||
else
|
||||
random_bytes = function()
|
||||
return error("Either luaossl or resty.openssl is required to generate random bytes")
|
||||
end
|
||||
end
|
||||
local x509_digest
|
||||
if pcall(function()
|
||||
return require("openssl.x509")
|
||||
end) then
|
||||
local x509 = require("openssl.x509")
|
||||
x509_digest = function(pem, hash_type)
|
||||
return x509.new(pem, "PEM"):digest(hash_type, "s")
|
||||
end
|
||||
elseif pcall(function()
|
||||
return require("resty.openssl.x509")
|
||||
end) then
|
||||
local x509 = require("resty.openssl.x509")
|
||||
x509_digest = function(pem, hash_type)
|
||||
return x509.new(pem, "PEM"):digest(hash_type)
|
||||
end
|
||||
else
|
||||
x509_digest = function()
|
||||
return error("Either luaossl or resty.openssl is required to calculate x509 digest")
|
||||
end
|
||||
end
|
||||
return {
|
||||
md5 = md5,
|
||||
hmac_sha256 = hmac_sha256,
|
||||
digest_sha256 = digest_sha256,
|
||||
kdf_derive_sha256 = kdf_derive_sha256,
|
||||
random_bytes = random_bytes,
|
||||
x509_digest = x509_digest
|
||||
}
|
||||
|
|
@ -1,72 +0,0 @@
|
|||
local PostgresHstore
|
||||
do
|
||||
local _class_0
|
||||
local _base_0 = { }
|
||||
_base_0.__index = _base_0
|
||||
_class_0 = setmetatable({
|
||||
__init = function() end,
|
||||
__base = _base_0,
|
||||
__name = "PostgresHstore"
|
||||
}, {
|
||||
__index = _base_0,
|
||||
__call = function(cls, ...)
|
||||
local _self_0 = setmetatable({}, _base_0)
|
||||
cls.__init(_self_0, ...)
|
||||
return _self_0
|
||||
end
|
||||
})
|
||||
_base_0.__class = _class_0
|
||||
PostgresHstore = _class_0
|
||||
end
|
||||
getmetatable(PostgresHstore).__call = function(self, t)
|
||||
return setmetatable(t, self.__base)
|
||||
end
|
||||
local encode_hstore
|
||||
do
|
||||
encode_hstore = function(tbl, escape_literal)
|
||||
if not (escape_literal) then
|
||||
local Postgres
|
||||
Postgres = require("pgmoon").Postgres
|
||||
local default_escape_literal
|
||||
default_escape_literal = function(v)
|
||||
return Postgres.escape_literal(nil, v)
|
||||
end
|
||||
escape_literal = default_escape_literal
|
||||
end
|
||||
local buffer = { }
|
||||
for k, v in pairs(tbl) do
|
||||
table.insert(buffer, '"' .. k .. '"=>"' .. v .. '"')
|
||||
end
|
||||
return escape_literal(table.concat(buffer, ", "))
|
||||
end
|
||||
end
|
||||
local decode_hstore
|
||||
do
|
||||
local P, R, S, V, Ct, C, Cs, Cg, Cf
|
||||
do
|
||||
local _obj_0 = require("lpeg")
|
||||
P, R, S, V, Ct, C, Cs, Cg, Cf = _obj_0.P, _obj_0.R, _obj_0.S, _obj_0.V, _obj_0.Ct, _obj_0.C, _obj_0.Cs, _obj_0.Cg, _obj_0.Cf
|
||||
end
|
||||
local g = P({
|
||||
"hstore",
|
||||
hstore = Cf(Ct("") * (V("pair") * (V("delim") * V("pair")) ^ 0) ^ -1, rawset) * -1,
|
||||
pair = Cg(V("value") * "=>" * (V("value") + V("null"))),
|
||||
value = V("invalid_char") + V("string"),
|
||||
string = P('"') * Cs((P([[\\]]) / [[\]] + P([[\"]]) / [["]] + (P(1) - P('"'))) ^ 0) * P('"'),
|
||||
null = C('NULL'),
|
||||
invalid_char = S(" \t\r\n") / function()
|
||||
return error("got unexpected whitespace")
|
||||
end,
|
||||
delim = P(", ")
|
||||
})
|
||||
decode_hstore = function(str, convert_fn)
|
||||
local out = (assert(g:match(str), "failed to parse postgresql hstore"))
|
||||
setmetatable(out, PostgresHstore.__base)
|
||||
return out
|
||||
end
|
||||
end
|
||||
return {
|
||||
encode_hstore = encode_hstore,
|
||||
decode_hstore = decode_hstore,
|
||||
PostgresHstore = PostgresHstore
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
|
|
@ -1,25 +0,0 @@
|
|||
local default_escape_literal = nil
|
||||
local encode_json
|
||||
encode_json = function(tbl, escape_literal)
|
||||
escape_literal = escape_literal or default_escape_literal
|
||||
local json = require("cjson")
|
||||
if not (escape_literal) then
|
||||
local Postgres
|
||||
Postgres = require("pgmoon").Postgres
|
||||
default_escape_literal = function(v)
|
||||
return Postgres.escape_literal(nil, v)
|
||||
end
|
||||
escape_literal = default_escape_literal
|
||||
end
|
||||
local enc = json.encode(tbl)
|
||||
return escape_literal(enc)
|
||||
end
|
||||
local decode_json
|
||||
decode_json = function(str)
|
||||
local json = require("cjson")
|
||||
return json.decode(str)
|
||||
end
|
||||
return {
|
||||
encode_json = encode_json,
|
||||
decode_json = decode_json
|
||||
}
|
||||
|
|
@ -1,109 +0,0 @@
|
|||
local create_luasocket
|
||||
do
|
||||
local flatten
|
||||
flatten = require("pgmoon.util").flatten
|
||||
local proxy_mt = {
|
||||
__index = function(self, key)
|
||||
local sock = self.sock
|
||||
local original = sock[key]
|
||||
if type(original) == "function" then
|
||||
local fn
|
||||
fn = function(_, ...)
|
||||
return original(sock, ...)
|
||||
end
|
||||
self[key] = fn
|
||||
return fn
|
||||
else
|
||||
return original
|
||||
end
|
||||
end
|
||||
}
|
||||
local method_overrides
|
||||
method_overrides = {
|
||||
send = function(self, ...)
|
||||
return self.sock:send(flatten(...))
|
||||
end,
|
||||
settimeout = function(self, t)
|
||||
if t then
|
||||
t = t / 1000
|
||||
end
|
||||
return self.sock:settimeout(t)
|
||||
end,
|
||||
setkeepalive = function(self)
|
||||
return error("You attempted to call setkeepalive on a LuaSocket socket. This method is only available for the ngx cosocket API for releasing a socket back into the connection pool")
|
||||
end,
|
||||
getreusedtimes = function(self, t)
|
||||
return 0
|
||||
end,
|
||||
sslhandshake = function(self, opts)
|
||||
if opts == nil then
|
||||
opts = { }
|
||||
end
|
||||
local ssl = require("ssl")
|
||||
local params = {
|
||||
mode = "client",
|
||||
protocol = "any",
|
||||
verify = "none",
|
||||
options = {
|
||||
"all",
|
||||
"no_sslv2",
|
||||
"no_sslv3",
|
||||
"no_tlsv1"
|
||||
}
|
||||
}
|
||||
for k, v in pairs(opts) do
|
||||
params[k] = v
|
||||
end
|
||||
local sec_sock, err = ssl.wrap(self.sock, params)
|
||||
if not (sec_sock) then
|
||||
return false, err
|
||||
end
|
||||
local success
|
||||
success, err = sec_sock:dohandshake()
|
||||
if not (success) then
|
||||
return false, err
|
||||
end
|
||||
for k, v in pairs(self) do
|
||||
if not method_overrides[k] and type(v) == "function" then
|
||||
self[k] = nil
|
||||
end
|
||||
end
|
||||
self.sock = sec_sock
|
||||
return true
|
||||
end
|
||||
}
|
||||
create_luasocket = function(...)
|
||||
local socket = require("socket")
|
||||
local proxy = {
|
||||
sock = socket.tcp(...)
|
||||
}
|
||||
for k, v in pairs(method_overrides) do
|
||||
proxy[k] = v
|
||||
end
|
||||
return setmetatable(proxy, proxy_mt)
|
||||
end
|
||||
end
|
||||
return {
|
||||
create_luasocket = create_luasocket,
|
||||
new = function(socket_type)
|
||||
if socket_type == nil then
|
||||
if ngx and ngx.get_phase() ~= "init" then
|
||||
socket_type = "nginx"
|
||||
else
|
||||
socket_type = "luasocket"
|
||||
end
|
||||
end
|
||||
local socket
|
||||
local _exp_0 = socket_type
|
||||
if "nginx" == _exp_0 then
|
||||
socket = ngx.socket.tcp()
|
||||
elseif "luasocket" == _exp_0 then
|
||||
socket = create_luasocket()
|
||||
elseif "cqueues" == _exp_0 then
|
||||
socket = require("pgmoon.cqueues").CqueuesSocket()
|
||||
else
|
||||
socket = error("got unknown or unset socket type: " .. tostring(socket_type))
|
||||
end
|
||||
return socket, socket_type
|
||||
end
|
||||
}
|
||||
|
|
@ -1,46 +0,0 @@
|
|||
local flatten
|
||||
do
|
||||
local __flatten
|
||||
__flatten = function(t, buffer)
|
||||
local _exp_0 = type(t)
|
||||
if "string" == _exp_0 then
|
||||
buffer[#buffer + 1] = t
|
||||
elseif "number" == _exp_0 then
|
||||
buffer[#buffer + 1] = tostring(t)
|
||||
elseif "table" == _exp_0 then
|
||||
for _index_0 = 1, #t do
|
||||
local thing = t[_index_0]
|
||||
__flatten(thing, buffer)
|
||||
end
|
||||
end
|
||||
end
|
||||
flatten = function(t)
|
||||
local buffer = { }
|
||||
__flatten(t, buffer)
|
||||
return table.concat(buffer)
|
||||
end
|
||||
end
|
||||
local encode_base64, decode_base64
|
||||
if ngx then
|
||||
do
|
||||
local _obj_0 = ngx
|
||||
encode_base64, decode_base64 = _obj_0.encode_base64, _obj_0.decode_base64
|
||||
end
|
||||
else
|
||||
local b64, unb64
|
||||
do
|
||||
local _obj_0 = require("mime")
|
||||
b64, unb64 = _obj_0.b64, _obj_0.unb64
|
||||
end
|
||||
encode_base64 = function(...)
|
||||
return (b64(...))
|
||||
end
|
||||
decode_base64 = function(...)
|
||||
return (unb64(...))
|
||||
end
|
||||
end
|
||||
return {
|
||||
flatten = flatten,
|
||||
encode_base64 = encode_base64,
|
||||
decode_base64 = decode_base64
|
||||
}
|
||||
|
|
@ -46,18 +46,16 @@ local function test()
|
|||
|
||||
ngx.say("Connection success")
|
||||
-- 执行查询
|
||||
local res, err = db:query("SELECT * FROM tbl_user")
|
||||
local res, err = db:query("SELECT * FROM \"T_Users\"")
|
||||
if not res then
|
||||
error("Query failed: " .. err)
|
||||
end
|
||||
ngx.say(err)
|
||||
for _, row in ipairs(res) do
|
||||
for key, value in pairs(row) do
|
||||
ngx.say(key .. ":" .. value)
|
||||
ngx.say(key .. ":" .. tostring(value))
|
||||
end
|
||||
end
|
||||
--local rest, err1 = db:query("SELECT version()")
|
||||
--ngx.say(rest)
|
||||
|
||||
--关闭数据库
|
||||
db:disconnect()
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user