LUA利用第三方API访问数据库

===========数据库访问--第三方

http {
upstream backend {
drizzle_server 192.168.4.119:3306 protocol=mysql
dbname=igirl user=root password=123456;
drizzle_keepalive max=10 overflow=ignore mode=single;
}
server {
listen 8080;
location /lua_content {
# MIME type determined by default_type:
default_type 'text/plain';
content_by_lua_block {
ngx.say('Hello,world!')
}
}
location /mysql {
drizzle_query $echo_request_body;
drizzle_pass backend;
}
location /api {
content_by_lua '
local sql = "select * from p_user"
local resp = ngx.location.capture("/mysql", {
method = ngx.HTTP_POST, body = sql
})
if resp.status ~= ngx.HTTP_OK or not resp.body then
error("failed to query mysql")
end

 

local parser = require "rds.parser"
local res, err = parser.parse(resp.body)
if res == nil then
error("failed to parse RDS: " .. err)
end

 

local rows = res.resultset
if not rows or #rows == 0 then
ngx.say("empty resultset")
ngx.exit(0)
end

 

for i, row in ipairs(rows) do
ngx.print("row ", i, ": ")
for col, val in pairs(row) do
if val ~= parser.null then
ngx.print(col, "=", val, " ")
else
ngx.print(col, "=null ")
end
end
ngx.say()
end
';
}
}

}

posted @ 2020-02-27 21:55  FromScratch  阅读(388)  评论(0编辑  收藏  举报