实现pgmoon库操作数据库并读取数据进行显示

This commit is contained in:
wanglei 2025-09-27 23:43:35 +08:00
parent 5aea1f82fa
commit b31079fcdc
5 changed files with 118 additions and 93 deletions

41
build.xml Normal file
View File

@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="OpenResty" default="dist" basedir="/home/frankly/work/AuthPlatform">
<description>
run pic-server
</description>
<!-- set global properties for this build -->
<property name="openresty-home" location="/usr/local/openresty"/>
<property name="conf" location="${basedir}/conf"/>
<property name="src" location="${basedir}/src"/>
<property name="target-conf" location="${basedir}/conf"/>
<property name="target-src" location="${basedir}/src"/>
<echo>######开发版的ant配置#####</echo>
<target name="clean" depends="">
<echo>清理openresty目录${dist}下的conf,logs,janus,januslib</echo>
<!-- <delete dir="${target-conf}"/>
<delete dir="${target-src}"/> -->
<delete>
<fileset dir="${basedir}/logs" includes="*.log">
</fileset>
</delete>
</target>
<target name="init" depends="clean">
<echo>创建安装目录</echo>
<mkdir dir="${target-conf}"/>
<mkdir dir="${target-src}"/>
</target>
<target name="dist" depends="init" description="generate the distribution" >
<echo>复制安装目录</echo>
<copy todir="${target-conf}">
<fileset dir="${conf}"></fileset>
</copy>
<copy todir="${target-src}">
<fileset dir="${src}"></fileset>
</copy>
</target>
</project>

View File

@ -1,29 +1,27 @@
worker_processes 1;
worker_rlimit_nofile 65535;
#worker_rlimit_nofile 65535;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
limit_req_zone $binary_remote_addr zone=one:50m rate=20r/s;
lua_package_path 'D:/ZeroBraneStudio-1.90/lualibs/?/?.lua;D:/ZeroBraneStudio-1.90/lualibs/?.lua;;';
lua_package_cpath 'D:/ZeroBraneStudio-1.90/bin/clibs/?.dll;;';
lua_package_path 'src/?/?.lua;src/?.lua;src/share/?/?.lua;src/share/?.lua;;';
lua_package_cpath 'src/share/lib/?.so;;';
server {
listen 9080;
server_name localhost;
server_name 127.0.0.1;
default_type text/html;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location /test {
default_type 'text/plain';
content_by_lua_file 'src/test.lua';
location /api {
content_by_lua_file '/home/frankly/work/AuthPlatform/src/test.lua';
#content_by_lua_file 'src/api/api.lua';
}
}
}

View File

@ -4,10 +4,6 @@
--- DateTime: 2025/9/24 15:29
---
-- local user = require('api.system.user')--- 启动调试
-- local mobdebug = require('src.share.initial.mobdebug');
-- mobdebug.start();
local function say_hello(req)
ngx.say("Hello, World!")
end
@ -38,56 +34,8 @@ local function test(req)
ngx.req.read_body()
args = ngx.req.get_post_args()
end
--2、组合url请求Get/Post请求 并获取参数
local http = require "resty.http"
local httpc = http.new()
local url = "http://xxxxx/user/login/"..args["userid"].."/"..args["pass"]
local resStr --响应结果
local res, err = httpc:request_uri(url, {
method = "GET",
--args = str,
body = "a=1&b=2",
headers = {
["Content-Type"] = "application/json",
}
})
--3、开始重新组合参数 例子 可根据返回的JSON自己处理
local cjson = require "cjson"
local sampleJson = [[{"age":"23","testArray":{"array":[8,9,11,14,25]},"Himi":"himigame.com"}]];
--解析json字符串
local data = cjson.decode(sampleJson);
--打印json字符串中的age字段
ngx.say(data["age"]);
--打印数组中的第一个值(lua默认是从0开始计数)
ngx.say(data["testArray"]["array"][1]);
--4、打印输出新返回值
ngx.say(res.body)
--获取url中a的值
ngx.say(ngx.var.arg_a);
--获取主机名
ngx.say(ngx.var.remote_addr);
--获取get和post参数
local arg = ngx.req.get_uri_args()
for k,v in pairs(arg) do
ngx.say("[GET ] key:", k, " v:", v)
end
ngx.req.read_body() -- 解析 body 参数之前一定要先读取 body
local arg = ngx.req.get_post_args()
for k,v in pairs(arg) do
ngx.say("[POST] key:", k, " v:", v)
end
end
---local function conn()
--- local conn1 = user.conn
--- conn1:connect(...)
---end
local routes = {
["/hello"] = say_hello,
["/user"] = get_user,

View File

@ -116,7 +116,6 @@ local PG_TYPES = {
[1016] = "array_number",
[1021] = "array_number",
[1022] = "array_number",
[1041] = "array_inet",
[1231] = "array_number",
[1009] = "array_string",
[1015] = "array_string",
@ -178,11 +177,6 @@ do
bytea = function(self, val, name)
return self:decode_bytea(val)
end,
array_inet = function(self, val, name)
local decode_array
decode_array = require("pgmoon.arrays").decode_array
return decode_array(val, nil, self)
end,
array_boolean = function(self, val, name)
local decode_array
decode_array = require("pgmoon.arrays").decode_array
@ -347,7 +341,7 @@ do
end
end,
cleartext_auth = function(self, msg)
assert(self.config.password, "the database is requesting a password for authentication but you did not provide a password")
assert(self.config.password, "missing password, required for connect")
self:send_message(MSG_TYPE_F.password, {
self.config.password,
NULL
@ -355,7 +349,7 @@ do
return self:check_auth()
end,
scram_sha_256_auth = function(self, msg)
assert(self.config.password, "the database is requesting a password for authentication but you did not provide a password")
assert(self.config.password, "missing password, required for connect")
local random_bytes, x509_digest
do
local _obj_0 = require("pgmoon.crypto")

View File

@ -1,22 +1,66 @@
require("mobdebug").start("127.0.0.1")
local cjson = require('cjson')
local pgmoon = require('pgmoon');
--用于接收前端数据的对象
local args = nil
--获取前端的请求方式 并获取传递的参数
local request_method = ngx.var.request_method
--判断是get请求还是post请求并分别拿出相应的数据
if "GET" == request_method then
args = ngx.req.get_uri_args()
elseif "POST" == request_method then
ngx.req.read_body()
args = ngx.req.get_post_args()
--兼容请求使用post请求但是传参以get方式传造成的无法获取到数据的bug
if (args == nil or args.data == null) then
args = ngx.req.get_uri_args()
-- 配置数据库连接
local config = {
host = "127.0.0.1", -- postgres host
port = 5432, -- postgres port
user = "postgres",
password = "1qaz2wsx", -- postgres password
database = "postgres"
}
-- 创建一个新的连接
local conn = pgmoon.new(config)
-- 连接到数据库
conn:connect(function(err)
if err then
print("Error connecting to database:", err)
else
print("Connected to the PostgreSQL server.")
-- 执行一个简单的查询
conn:query("SELECT version()")
:on_data(function(row)
print("Database Version:", row[1])
end)
:on_error(function(err)
print("Query Error:", err)
end)
:on_finish(function()
print("Query finished.")
-- 关闭连接
conn:close()
end)
end
end)
local function test()
-- 创建数据库连接对象
local db = pgmoon.new(config)
local ok, err = db:connect()
if not ok then
error("Connection failed: " .. err)
end
ngx.say("Connection success")
-- 执行查询
local res, err = db:query("SELECT * FROM tbl_user")
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)
end
end
--local rest, err1 = db:query("SELECT version()")
--ngx.say(rest)
--关闭数据库
db:disconnect()
end
--获取前端传递的name值
local name = args.name
--响应前端
ngx.say("hello:"..name)
test()