Skip to content

Commit

Permalink
demo5
Browse files Browse the repository at this point in the history
  • Loading branch information
git committed Jan 3, 2017
1 parent 99794b3 commit 0a91c3f
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

[openresty 前端开发入门四之Redis篇](http://blog.csdn.net/qq362228416/article/details/53948961)

openresty 前端开发入门五之Mysql篇
[openresty 前端开发入门五之Mysql篇](http://blog.csdn.net/qq362228416/article/details/53999565)

openresty 前端开发入门六之调试篇

Expand Down
21 changes: 21 additions & 0 deletions demo5/conf/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

worker_processes 1;

error_log logs/error.log;

events {
worker_connections 1024;
}

http {
lua_package_path "/Users/john/opensource/openresty-web-dev/demo5/lua/?.lua;/usr/local/openresty/lualib/?.lua";
server {
listen 80;
server_name localhost;
lua_code_cache off;
location ~ /lua/(.+) {
default_type text/html;
content_by_lua_file lua/$1.lua;
}
}
}
29 changes: 29 additions & 0 deletions demo5/lua/hello.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
local cjson = require "cjson"
local mysql = require "mysql"
local req = require "req"

local args = req.getArgs()

local name = args['name']

if name == nil or name == "" then
name = "root"
end

name = ngx.quote_sql_str(name) -- SQL 转义,将 ' 转成 \', 防SQL注入,并且转义后的变量包含了引号,所以可以直接当成条件值使用

local db = mysql:new()

local sql = "select * from user where User = " .. name

ngx.say(sql)
ngx.say("<br/>")

local res, err, errno, sqlstate = db:query(sql)
db:close()
if not res then
ngx.say(err)
return {}
end

ngx.say(cjson.encode(res))
41 changes: 41 additions & 0 deletions demo5/lua/mysql.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
local mysql = require "resty.mysql"

local config = {
host = "114.215.222.190",
port = 3306,
database = "mysql",
user = "root",
password = "testdbadmin123"
}

local _M = {}


function _M.new(self)
local db, err = mysql:new()
if not db then
return nil
end
db:set_timeout(1000) -- 1 sec

local ok, err, errno, sqlstate = db:connect(config)

if not ok then
return nil
end
db.close = close
return db
end

function close(self)
local sock = self.sock
if not sock then
return nil, "not initialized"
end
if self.subscribed then
return nil, "subscribed state"
end
return sock:setkeepalive(10000, 50)
end

return _M
20 changes: 20 additions & 0 deletions demo5/lua/req.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
local _M = {}

-- 获取http get/post 请求参数
function _M.getArgs()
local request_method = ngx.var.request_method
local args = ngx.req.get_uri_args()
-- 参数获取
if "POST" == request_method then
ngx.req.read_body()
local postArgs = ngx.req.get_post_args()
if postArgs then
for k, v in pairs(postArgs) do
args[k] = v
end
end
end
return args
end

return _M

0 comments on commit 0a91c3f

Please sign in to comment.