forked from 362228416/openresty-web-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
git
committed
Jan 3, 2017
1 parent
99794b3
commit 0a91c3f
Showing
5 changed files
with
112 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |