-
Notifications
You must be signed in to change notification settings - Fork 1
/
user.lua
134 lines (119 loc) · 2.84 KB
/
user.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
local cjson = require "cjson"
local mysql = require "resty.mysql"
local MYSQL_HOST = "123.57.41.242"
local MYSQL_POST = 3306
local MYSQL_DATABASE = "fm_appserver"
local MYSQL_USER = "lingbanfm"
local MYSQL_PASSWD = "lingban2014"
local DB_TIMEOUT = 2000 --2 sec
local MAX_SIZE = 1024*1024
--初始化mysql连接
function init_mysql()
db = mysql:new()
if not db then
return 10001
end
db:set_timeout(DB_TIMEOUT)
local ok, err, errno, sqlstate = db:connect{
host = MYSQL_HOST,
port = MYSQL_POST,
database = MYSQL_DATABASE,
user = MYSQL_USER,
password = MYSQL_PASSWD,
max_packet_size = MAX_SIZE
}
if not ok then
--ngx.say("failed to connect: ", err, ": ", errno, " ", sqlstate)
return 10001
end
--ngx.say("connected to mysql.")
return 0
end
function parse_postargs()
ngx.req.read_body()
args = ngx.req.get_post_args()
if not args then
ngx.say("failed to get post args: ", err)
return 10002
end
--解析翻页参数
start = args["start"]
page = args["page"]
if not start then start = "0" end
if not page then page = "20" end
opname = args["opName"]
if not opname then
--ngx.say("optype error")
return 10002
end
return 0
end
function close_mysql()
--关闭连接
-- local ok, err = db:close()
-- if not ok then
-- ngx.say("failed to close: ", err)
-- return 10010
-- end
-- put it into the connection pool of size 100,
-- with 10 seconds max idle timeout
local ok, err = db:set_keepalive(30000, 100)
if not ok then
ngx.say("failed to set keepalive: ", err)
return
end
return 0
end
function user_register(userId, password, nickname)
if not userId or not password or not nickname then
ngx.say("args == nil")
return -1
end
if (userId == "" or password == "" or nickname == "") then
ngx.say("args == ''")
return -1
end
local register_sql = string.format("insert into u_userInfo (userId,password,nickname) values('%s','%s','%s')", userId, password, nickname)
ngx.say(register_sql)
local res, err, errno, sqlstate = db:query(register_sql)
if not res then
ngx.say("err: ", err)
return -1
end
end
function error_res(err_code)
local describe = "describe"
local res_json = {
errorId = err_code, desc = describe
}
close_mysql()
ngx.say(cjson.encode(res_json))
end
--函数入口
function main()
ngx.say("hello,world")
local res_code
local res_code = init_mysql()
if ( res_code ~= 0 ) then
error_res(res_code)
return
end
--解析post参数
res_code = parse_postargs()
if( res_code ~= 0) then
error_res(res_code)
return
end
--用户注册
if (opname == "register") then
local password = args["password"]
local nickname = args["nickname"]
local userId = args["userId"]
ngx.say("password= ", password)
ngx.say("nickname= ", nickname)
ngx.say("userId= ", userId)
user_register(userId, password, nickname)
end
close_mysql()
end
main()