Openresty编写Lua代码一例
生活随笔
收集整理的這篇文章主要介紹了
Openresty编写Lua代码一例
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1.前段時間糾結(jié)了很久,一直弄不清lua和tomcat的聯(lián)系。一直認(rèn)為是lua調(diào)用tomcat的接口才可使用,后面才明白過來,進(jìn)入了一個誤區(qū),lua本身就是一門獨立的腳本語言。在openresty里面配置好,即可編寫映射和響應(yīng)。
下面是自己編寫的lua代碼一例,僅供參考。還有些不完善,要開始忙項目了,等有空再繼續(xù)更新。
2.下面是lua代碼,記得在nginx.conf寫好配置。
local request_method = ngx.var.request_method local cjson = require("cjson") local mysql = require("resty.mysql") local quote = ngx.quote_sql_str local db,err = mysql:new() local function close_db(db) if not db then return end db:close() end if not db thennax.say("new mysql error",err) endlocal args = nil local username = nil local pwd = nil if "GET" == request_method thenargs = ngx.req.get_uri_args() elseif "POST" == ngx.request_method thenngx.req.read_body()args = ngx.req.get_post_args() end username = tostring(args["username"]) pwd = tostring(args["pwd"]) if username == nil thenngx.say("username not nil")return elseif username == '' thenngx.say("username not nil")return elseif pwd == nil thenngx.say("password not nil")return; elseif pwd == '' thenngx.say("password not nil")return; enddb:set_timeout(1000) local props = { host = "127.0.0.1", port = 3306, database = "hwc_hello", user = "root", password = "hwc123456" } local res, err, errno, sqlstate = db:connect(props) if not res then ngx.say("connect to mysql error : ", err, " , errno : ", errno, " , sqlstate : ", sqlstate) return close_db(db) end local select_name = "select username from user_table where username="..quote(username) res, err, errno, sqlstate = db:query(select_name) if not res then ngx.exec("/vi/404.html") return close_db(db) end local result = {} result.success = true; result.info = "登錄成功" ngx.say(cjson.encode(result))?
轉(zhuǎn)載于:https://www.cnblogs.com/dslx/p/9208622.html
總結(jié)
以上是生活随笔為你收集整理的Openresty编写Lua代码一例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 广发银行信用卡怎么申请?办卡渠道和方法大
- 下一篇: 在winform中使用wpf窗体