Skip to content

Commit

Permalink
add mysql support
Browse files Browse the repository at this point in the history
  • Loading branch information
leandre committed Feb 22, 2016
1 parent 355081a commit 3bbda4a
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 23 deletions.
2 changes: 1 addition & 1 deletion app.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ app_path = "app."
local kernel = require(app_path .. "kernel")

local status, error = pcall(function()
kernel:run();
kernel:run()
end)

if status == false then
Expand Down
5 changes: 4 additions & 1 deletion app/bootstrap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
local _B = {}

function _B:initDb()
local mysql = require(app_config["lib_path"] .. "db.mysql")
mysql:init()
container["mysql"] = mysql; -- put mysql to container
-- ngx.say("Db init")
local testPlugin = require(app_config["plugin_path"] .. "test")
testPlugin:register()
Expand All @@ -18,4 +21,4 @@ function _B:initTest()
-- ngx.say("Test init")
end

return _B;
return _B
26 changes: 26 additions & 0 deletions app/config/mysql.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--
-- mysql config
-- User: leandre
-- Date: 16/2/1
-- Time: 上午11:21
--

local _P = {
alpha = {
master = {
host = "127.0.0.1",
port = "3306",
user = "root",
password = "root",
database = "test"
},
slave = {
host = "127.0.0.1",
port = "3306",
user = "root",
password = "root",
database = "test"
}
}
}
return _P
2 changes: 1 addition & 1 deletion app/controllers/abstract.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ function _CTR:error(code, msg, data)
return true
end

return _CTR;
return _CTR
4 changes: 2 additions & 2 deletions app/controllers/index.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ local _M = CTR
--- test init action
--
function _M:init()
-- ngx.say("controller init");
-- ngx.say("controller init")
end

--- test index action
--
function _M:index()
ngx.say("<h1>Hello SingleLua</h1>");
ngx.say("<h1>Hello SingleLua</h1>")
-- self:succ("", "", { 1, 2, 3 })
-- self:error("", "", { 4, 5, 6 })
end
Expand Down
20 changes: 20 additions & 0 deletions app/controllers/mysql.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--
-- mysql test
-- User: leandre
-- Date: 16/2/22
-- Time: 下午3:22
--

local CTR = require(app_config["ctr_path"] .. "abstract")
local _M = CTR

--- test index action
--
function _M:index()
local model = require(app_path .. "model.test")
local result = model:getTest()
self:succ("", "", result)
end

return _M

6 changes: 3 additions & 3 deletions app/kernel.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ end
--
function _K:run()
local uri_arr = tools.array_filter(tools.lua_string_split("/", ngx.var.uri))
local ctr_file = tools.lua_string_merge(".", uri_arr);
local ctr_file = tools.lua_string_merge(".", uri_arr)

-- default index
if ctr_file == "" then
Expand All @@ -61,7 +61,7 @@ function _K:run()
self:bootstrap()

self:hook_run(container['routerStartup'])
ctr_file = app_config['ctr_path'] .. container['ctr_file'];
ctr_file = app_config['ctr_path'] .. container['ctr_file']
self:hook_run(container['routerShutdown'])

local ctr_cache = ngx.shared.ctrs:get(ctr_file)
Expand All @@ -87,4 +87,4 @@ function _K:run()
self:hook_run(container['postDispatch'])
end

return _K;
return _K
2 changes: 1 addition & 1 deletion app/library/container.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
--
local _C = {}

return _C;
return _C

86 changes: 86 additions & 0 deletions app/library/db/mysql.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
--
-- mysql
-- User: leandre
-- Date: 16/2/1
-- Time: 上午11:18
--

local _M = {}
local _config = {}

--- init db config
--
function _M:init()
_config = require(app_path .. "config.mysql")
return _config
end

function _M:connect(pool_name, is_slave)
local mysql = require("resty.mysql")
local db, err = mysql:new()
if not db then
ngx.say("failed to instantiate mysql: ", err)
return
end

db:set_timeout(1000) -- 1 sec

local conf = {}
if is_slave == true then
conf = _config[pool_name]["slave"]
else
conf = _config[pool_name]["master"]
end

local ok, err, errno, sqlstate = db:connect {
host = conf["host"],
port = conf["port"],
database = conf["database"],
user = conf["user"],
password = conf["password"],
max_packet_size = 1024 * 1024
}

if not ok then
ngx.say("failed to connect: ", err, ": ", errno, " ", sqlstate)
return false
end

return db
end

function _M:query(poolname, sql)
if poolname == "" or sql == "" then
return false
end

local db
if string.find(sql, "select") == 1 then
db = self:connect(poolname, true) -- slave db
else
db = self:connect(poolname, false) -- master db
end

if not db then
return false
end


local res, err, errno, sqlstate = db:query(sql)
if not res then
ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".")
return false
end

-- put it into the connection pool of size 100,
-- with 10 seconds max idle timeout
local ok, err = db:set_keepalive(10000, 100)
if not ok then
ngx.say("failed to set keepalive: ", err)
return false
end

return res
end

return _M
28 changes: 14 additions & 14 deletions app/library/tools.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,26 @@ local _M = {}
-- @param str
--
function _M.lua_string_split(split_char, str)
local sub_str_tab = {};
local i = 0;
local j = 0;
local sub_str_tab = {}
local i = 0
local j = 0
while true do
j = string.find(str, split_char, i + 1);
j = string.find(str, split_char, i + 1)
if j == nil then
table.insert(sub_str_tab, string.sub(str, i + 1));
break;
end;
table.insert(sub_str_tab, string.sub(str, i + 1, j - 1));
i = j;
table.insert(sub_str_tab, string.sub(str, i + 1))
break
end
table.insert(sub_str_tab, string.sub(str, i + 1, j - 1))
i = j
end
return sub_str_tab;
return sub_str_tab
end

--- print array
--- print table
-- @param arr
--
function _M.print_arr(arr)
for key, val in ipairs(arr) do
function _M.print_table(arr)
for key, val in pairs(arr) do
if type(val) == "table" then
ngx.say(key, ": ", table.concat(val, ", "))
else
Expand All @@ -44,7 +44,7 @@ end
-- @param arr
--
function _M.array_filter(arr)
local sub_tab = {};
local sub_tab = {}
for key, val in ipairs(arr) do
if val ~= "" then
table.insert(sub_tab, val)
Expand Down
19 changes: 19 additions & 0 deletions app/model/test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--
-- test model
-- User: leandre
-- Date: 16/2/22
-- Time: 下午3:24
--

local _M = {}
local mysql = container["mysql"];

function _M:getTest()
local res = mysql:query("alpha", "select * from users;")
if res then
return res
end
return false
end

return _M

0 comments on commit 3bbda4a

Please sign in to comment.