Compare commits
No commits in common. "a8b3fb9cfb5f6063998fe4fd8c2419d447826fc1" and "7529cb9114315965dc83e4e2fd095bdb3ddaeced" have entirely different histories.
a8b3fb9cfb
...
7529cb9114
|
|
@ -1,5 +1,4 @@
|
||||||
worker_processes auto; #nginx worker 数量
|
worker_processes 1;
|
||||||
error_log /home/frankly/work/AuthPlatform/logs/error.log info; #指定错误日志文件路径
|
|
||||||
|
|
||||||
#worker_rlimit_nofile 65535;
|
#worker_rlimit_nofile 65535;
|
||||||
|
|
||||||
|
|
@ -8,10 +7,6 @@ events {
|
||||||
}
|
}
|
||||||
|
|
||||||
http {
|
http {
|
||||||
##lua_need_request_body on; #开启读取请求体数据
|
|
||||||
client_max_body_size 1024M; #允许最大100k的请求体
|
|
||||||
client_body_buffer_size 1024M; #设置缓冲区大小
|
|
||||||
|
|
||||||
lua_package_path '$prefix/src/?/?.lua;$prefix/src/?.lua;/home/frankly/work/AuthPlatform/src/?.lua;/home/frankly/work/AuthPlatform/src/?/?.lua;;';
|
lua_package_path '$prefix/src/?/?.lua;$prefix/src/?.lua;/home/frankly/work/AuthPlatform/src/?.lua;/home/frankly/work/AuthPlatform/src/?/?.lua;;';
|
||||||
lua_package_cpath '$prefix/src/share/lib/?.so;;';
|
lua_package_cpath '$prefix/src/share/lib/?.so;;';
|
||||||
|
|
||||||
|
|
@ -31,7 +26,5 @@ http {
|
||||||
}
|
}
|
||||||
|
|
||||||
include 'system/user.conf';
|
include 'system/user.conf';
|
||||||
|
|
||||||
include 'system/test.conf';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,8 +0,0 @@
|
||||||
#测试接口文件
|
|
||||||
location /testSQL {
|
|
||||||
content_by_lua_file '/home/frankly/work/AuthPlatform/src/test/testPostgres.lua';
|
|
||||||
}
|
|
||||||
|
|
||||||
location /cjson {
|
|
||||||
content_by_lua_file '/home/frankly/work/AuthPlatform/src/test/test.lua';
|
|
||||||
}
|
|
||||||
1
logs/nginx.pid
Normal file
1
logs/nginx.pid
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
11428
|
||||||
|
|
@ -6,40 +6,46 @@
|
||||||
local radix = require("resty.radixtree")
|
local radix = require("resty.radixtree")
|
||||||
local userApi = require("api.system.user")
|
local userApi = require("api.system.user")
|
||||||
|
|
||||||
--定义相关路由,前端接口url地址
|
|
||||||
local routes = {
|
local routes = {
|
||||||
{
|
{
|
||||||
paths = { "/api/user" },
|
paths = { "/api/user" },
|
||||||
metadata = { "metadata user" },
|
metadata = { "metadata /user" },
|
||||||
methods = { "GET", "POST" },
|
methods = { "GET", "POST" },
|
||||||
handler = userApi.get_allusers,
|
handler = userApi.get_allusers,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
paths = { "/api/user/:id" },
|
paths = { "/api/user/:id" },
|
||||||
metadata = { "metadata /api/user/id" },
|
metadata = { "metadata /user/id" },
|
||||||
methods = { "GET", "PUT", "DELETE" },
|
methods = { "GET", "PUT", "DELETE" },
|
||||||
handler = userApi.get_user,
|
handler = userApi.get_users,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
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)
|
local rx, err = radix.new(routes)
|
||||||
if not rx then
|
if not rx then
|
||||||
|
ngx.say("-----")
|
||||||
|
ngx.status = 404
|
||||||
ngx.say("Not Found")
|
ngx.say("Not Found")
|
||||||
ngx.exit(ngx.HTTP_NOT_FOUND)
|
--ngx.exit()
|
||||||
end
|
end
|
||||||
|
|
||||||
--获取访问的uri地址
|
|
||||||
local uri = ngx.var.uri
|
local uri = ngx.var.uri
|
||||||
local opts = {
|
local opts = {
|
||||||
method = ngx.var.request_method,
|
method = ngx.var.request_method,
|
||||||
--vars = ngx.var,
|
|
||||||
matched = {}
|
matched = {}
|
||||||
}
|
}
|
||||||
|
|
||||||
-- 进行路由匹配和相关函数调用
|
|
||||||
local ok = rx:dispatch(uri, opts, opts.matched)
|
local ok = rx:dispatch(uri, opts, opts.matched)
|
||||||
if not ok then
|
if not ok then
|
||||||
|
ngx.status = 404
|
||||||
ngx.say("Not Found")
|
ngx.say("Not Found")
|
||||||
ngx.exit(ngx.HTTP_NOT_FOUND)
|
--ngx.exit()
|
||||||
end
|
end
|
||||||
|
|
@ -6,22 +6,14 @@
|
||||||
|
|
||||||
local _M = {}
|
local _M = {}
|
||||||
|
|
||||||
local dao = require("service.system.user")
|
|
||||||
local resp = require("util.response")
|
|
||||||
|
|
||||||
--获取所有用户信息
|
--获取所有用户信息
|
||||||
function _M.get_allusers()
|
function _M.get_allusers()
|
||||||
local ret = dao.getAllUser()
|
|
||||||
local result = resp:json(0, 'ok', ret)
|
|
||||||
resp:send(result)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--根据用户id获取用户信息
|
--获取所有用户信息
|
||||||
function _M.get_user(m)
|
function _M.get_users(req)
|
||||||
local id = m.id
|
|
||||||
local ret = dao.getUser(id)
|
|
||||||
local result = resp:json(0, 'ok', ret)
|
|
||||||
resp:send(result)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return _M
|
return _M
|
||||||
|
|
@ -7,11 +7,6 @@
|
||||||
return {
|
return {
|
||||||
APP_ENV = "dev", -- dev/prod
|
APP_ENV = "dev", -- dev/prod
|
||||||
|
|
||||||
locale = 'en',
|
|
||||||
fallback_locale = 'zh',
|
|
||||||
|
|
||||||
time_zone = "+8:00", -- UTC + 8
|
|
||||||
|
|
||||||
-- 配置redis数据库连接
|
-- 配置redis数据库连接
|
||||||
REDIS = {
|
REDIS = {
|
||||||
HOST = "127.0.0.1", -- redis host
|
HOST = "127.0.0.1", -- redis host
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
local env = require('config.config')
|
local env = require('env')
|
||||||
|
|
||||||
return {
|
return {
|
||||||
redis_prefix = 'Auth:',
|
redis_prefix = 'Auth:',
|
||||||
|
|
@ -11,8 +11,8 @@ return {
|
||||||
postgres = {
|
postgres = {
|
||||||
host = env.POSTGRES.HOST,
|
host = env.POSTGRES.HOST,
|
||||||
port = env.POSTGRES.PORT,
|
port = env.POSTGRES.PORT,
|
||||||
user = env.POSTGRES.USERNAME,
|
username = env.POSTGRES.USERNAME,
|
||||||
password = env.POSTGRES.PASSWORD,
|
password = env.POSTGRES.PASSWORD,
|
||||||
database = env.POSTGRES.DATABASE
|
dbname = env.POSTGRES.DATABASE
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
@ -1,58 +0,0 @@
|
||||||
return {
|
|
||||||
zh = {
|
|
||||||
-- 系统状态码
|
|
||||||
[0x000000] = 'ok',
|
|
||||||
[0x000001] = '验证错误',
|
|
||||||
[0x000002] = '数据不存在',
|
|
||||||
[0x000003] = '密码错误',
|
|
||||||
[0x000004] = '未授权访问',
|
|
||||||
[0x000005] = '系统错误,数据库错误',
|
|
||||||
[0x000006] = '请求太频繁,请稍后访问',
|
|
||||||
[0x000007] = '系统错误,系统数据异常',
|
|
||||||
[0x000008] = '系统错误,共享内存错误',
|
|
||||||
[0x000009] = '系统错误,发起 Http 请求错误',
|
|
||||||
[0x00000A] = '系统错误, Cookie 错误',
|
|
||||||
[0x00000B] = '系统错误,定时器错误',
|
|
||||||
[0x00000C] = '系统异常,用户未登录',
|
|
||||||
|
|
||||||
-- user module
|
|
||||||
[0x010001] = '注册失败,手机号已存在',
|
|
||||||
[0x010002] = '登录失败,手机号或密码错误',
|
|
||||||
[0x010003] = '登录失败,用户不存在',
|
|
||||||
[0x010004] = '短信验证失败,短信验证码错误',
|
|
||||||
[0x010005] = '重置密码失败,旧密码错误',
|
|
||||||
[0x010006] = '重置密码失败,系统异常',
|
|
||||||
[0x010007] = '重置密码失败,新密码不能和旧密码相同',
|
|
||||||
[0x010008] = '获取用户信息失败,系统错误',
|
|
||||||
[0x010009] = '重置密码失败,用户不存在',
|
|
||||||
[0x01000A] = '获取用户信息失败,用户未登录',
|
|
||||||
[0x01000B] = '获取用户信息失败,用户不存在',
|
|
||||||
|
|
||||||
},
|
|
||||||
en = {
|
|
||||||
-- system code
|
|
||||||
[0x000000] = 'ok',
|
|
||||||
[0x000001] = 'validate error',
|
|
||||||
[0x000002] = 'data not found',
|
|
||||||
[0x000003] = 'password error',
|
|
||||||
[0x000004] = 'no authorization',
|
|
||||||
[0x000005] = 'database error',
|
|
||||||
[0x000006] = 'request frequency please be gentle',
|
|
||||||
[0x000007] = 'system error,data cache error',
|
|
||||||
[0x000008] = 'shared memory error',
|
|
||||||
[0x000009] = 'http request err',
|
|
||||||
[0x00000A] = 'system error, cookie error',
|
|
||||||
[0x00000B] = 'system error, timer error',
|
|
||||||
[0x00000C] = 'system error,user not authenticat',
|
|
||||||
|
|
||||||
-- user module
|
|
||||||
[0x010001] = 'phone number already exits',
|
|
||||||
[0x010002] = 'phone no or password error',
|
|
||||||
[0x010003] = 'user not exits',
|
|
||||||
[0x010004] = 'SMS verification failed, SMS code error',
|
|
||||||
[0x010005] = 'fail to reset password, old password error',
|
|
||||||
[0x010006] = 'fail to reset password, unknow error',
|
|
||||||
[0x010007] = 'fail to reset password, new password cannot equal to old password',
|
|
||||||
[0x010008] = 'fail to get user info, system error',
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -3,76 +3,25 @@
|
||||||
--- 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 _M = {}
|
local _M = {}
|
||||||
|
|
||||||
local function get_con(cfg)
|
-- 创建一个新的连接
|
||||||
-- 创建一个新的连接
|
local conn = pgmoon.new(db_config.postgres)
|
||||||
local conn = pgmoon.new(cfg);
|
|
||||||
---- 连接到数据库
|
-- 连接到数据库
|
||||||
local ok, err = conn:connect()
|
conn:connect(function(err)
|
||||||
if not ok then
|
if err then
|
||||||
error("Connection failed: " .. err)
|
print("Error connecting to database: ", err)
|
||||||
ngx.exit(ngx.HTTP_NOT_FOUND)
|
else
|
||||||
end
|
print("Connected to the PostgreSQL server.")
|
||||||
--ngx.say("Connection success")
|
end
|
||||||
return conn
|
end)
|
||||||
end
|
|
||||||
|
|
||||||
-- 查询数据表中的所有用户信息
|
|
||||||
function _M.getAllUser()
|
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()
|
|
||||||
return res
|
|
||||||
end
|
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
|
|
||||||
-- ngx.say(cjson.encode(res))
|
|
||||||
--关闭数据库
|
|
||||||
conn:disconnect()
|
|
||||||
|
|
||||||
return res
|
|
||||||
end
|
|
||||||
return _M
|
return _M
|
||||||
1
src/share/pgmoon.lua
Normal file
1
src/share/pgmoon.lua
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
return require('pgmoon.init')
|
||||||
196
src/share/pgmoon/arrays.lua
Normal file
196
src/share/pgmoon/arrays.lua
Normal file
|
|
@ -0,0 +1,196 @@
|
||||||
|
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
|
||||||
|
}
|
||||||
67
src/share/pgmoon/bit.lua
Normal file
67
src/share/pgmoon/bit.lua
Normal file
|
|
@ -0,0 +1,67 @@
|
||||||
|
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
|
||||||
|
}
|
||||||
75
src/share/pgmoon/cqueues.lua
Normal file
75
src/share/pgmoon/cqueues.lua
Normal file
|
|
@ -0,0 +1,75 @@
|
||||||
|
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
|
||||||
|
}
|
||||||
178
src/share/pgmoon/crypto.lua
Normal file
178
src/share/pgmoon/crypto.lua
Normal file
|
|
@ -0,0 +1,178 @@
|
||||||
|
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
|
||||||
|
}
|
||||||
72
src/share/pgmoon/hstore.lua
Normal file
72
src/share/pgmoon/hstore.lua
Normal file
|
|
@ -0,0 +1,72 @@
|
||||||
|
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
|
||||||
|
}
|
||||||
1112
src/share/pgmoon/init.lua
Normal file
1112
src/share/pgmoon/init.lua
Normal file
File diff suppressed because it is too large
Load Diff
25
src/share/pgmoon/json.lua
Normal file
25
src/share/pgmoon/json.lua
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
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
|
||||||
|
}
|
||||||
109
src/share/pgmoon/socket.lua
Normal file
109
src/share/pgmoon/socket.lua
Normal file
|
|
@ -0,0 +1,109 @@
|
||||||
|
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
|
||||||
|
}
|
||||||
46
src/share/pgmoon/util.lua
Normal file
46
src/share/pgmoon/util.lua
Normal file
|
|
@ -0,0 +1,46 @@
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
@ -3,43 +3,3 @@
|
||||||
--- Created by admin.
|
--- Created by admin.
|
||||||
--- DateTime: 2025/10/15 09:12
|
--- DateTime: 2025/10/15 09:12
|
||||||
---
|
---
|
||||||
|
|
||||||
--读取请求体的数据
|
|
||||||
ngx.req.read_body()
|
|
||||||
|
|
||||||
--获取请求数据
|
|
||||||
local body_data = ngx.req.get_body_data()
|
|
||||||
|
|
||||||
--if not body_data then
|
|
||||||
-- ngx.say("read file error:", err)
|
|
||||||
-- return ngx.exit(400)
|
|
||||||
--end
|
|
||||||
local len = #body_data
|
|
||||||
|
|
||||||
--local file_name = ngx.req.get_body_file()
|
|
||||||
ngx.say("file length:", len)
|
|
||||||
|
|
||||||
--ngx.req.read_body_in_buffer(ngx.var.request_body_file)
|
|
||||||
|
|
||||||
--ngx.say(body_data)
|
|
||||||
|
|
||||||
|
|
||||||
--local cjson = require("cjson")
|
|
||||||
--local file_path = "/home/frankly/work/test.dat"
|
|
||||||
--local file_length = 1024 * 1024 * 400
|
|
||||||
--local f, err = io.input(file_path, "r")
|
|
||||||
--if not f then
|
|
||||||
-- ngx.say("read file error:"..err)
|
|
||||||
-- return
|
|
||||||
--end
|
|
||||||
--local content = f:read(file_length) --读取文件内容
|
|
||||||
--f:close() --关闭文件
|
|
||||||
----ngx.say(content)
|
|
||||||
--local res = {
|
|
||||||
-- key = "data",
|
|
||||||
-- value = content
|
|
||||||
--}
|
|
||||||
----ngx.header["Length"] = #content
|
|
||||||
--ngx.header["Content-Type"] = 'application/json; charset=UTF-8'
|
|
||||||
--ngx.say(cjson.encode(res))
|
|
||||||
--ngx.log(ngx.INFO, "send data success")
|
|
||||||
|
|
@ -46,16 +46,18 @@ local function test()
|
||||||
|
|
||||||
ngx.say("Connection success")
|
ngx.say("Connection success")
|
||||||
-- 执行查询
|
-- 执行查询
|
||||||
local res, err = db:query("SELECT * FROM \"T_Users\"")
|
local res, err = db:query("SELECT * FROM tbl_user")
|
||||||
if not res then
|
if not res then
|
||||||
error("Query failed: " .. err)
|
error("Query failed: " .. err)
|
||||||
end
|
end
|
||||||
ngx.say(err)
|
ngx.say(err)
|
||||||
for _, row in ipairs(res) do
|
for _, row in ipairs(res) do
|
||||||
for key, value in pairs(row) do
|
for key, value in pairs(row) do
|
||||||
ngx.say(key .. ":" .. tostring(value))
|
ngx.say(key .. ":" .. value)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
--local rest, err1 = db:query("SELECT version()")
|
||||||
|
--ngx.say(rest)
|
||||||
|
|
||||||
--关闭数据库
|
--关闭数据库
|
||||||
db:disconnect()
|
db:disconnect()
|
||||||
|
|
|
||||||
|
|
@ -1,61 +0,0 @@
|
||||||
local cjson = require('cjson')
|
|
||||||
local conf = require('config.config')
|
|
||||||
local error_code = require('config.status')
|
|
||||||
local ngx = ngx
|
|
||||||
|
|
||||||
local _M = {}
|
|
||||||
|
|
||||||
function _M:json(status, message, data, http_status)
|
|
||||||
-- you can modify this response struct as you favor
|
|
||||||
local msg = message
|
|
||||||
local response_status = http_status or ngx.OK
|
|
||||||
if msg == nil or msg == '' then
|
|
||||||
local locale = ngx.ctx.locale or conf.locale
|
|
||||||
if error_code[locale] ~= nil then
|
|
||||||
msg = error_code[locale][status]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
local response = {status=status, msg=msg, data=data}
|
|
||||||
if not response.status then
|
|
||||||
response.status = -1
|
|
||||||
response.message = 'not find status code'
|
|
||||||
end
|
|
||||||
return {
|
|
||||||
status = response_status,
|
|
||||||
headers = {content_type = 'application/json; charset=UTF-8'},
|
|
||||||
body = cjson.encode(response)
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
function _M:raw(http_status, http_body)
|
|
||||||
return {
|
|
||||||
status = http_status,
|
|
||||||
headers = {},
|
|
||||||
body = http_body
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
function _M:error(http_status, http_headers, http_body)
|
|
||||||
return {
|
|
||||||
status = http_status,
|
|
||||||
headers = http_headers,
|
|
||||||
body = http_body
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
function _M:send(response)
|
|
||||||
ngx.status = response.status
|
|
||||||
if response.headers ~= nil then
|
|
||||||
for name, value in pairs(response.headers) do
|
|
||||||
ngx.header[name] = value
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if response.body ~= nil then
|
|
||||||
ngx.say(response.body)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
return _M
|
|
||||||
Loading…
Reference in New Issue
Block a user