Openresty编写Lua代码一例
1.前段时间纠结了很久,一直弄不清lua和tomcat的联系。一直认为是lua调用tomcat的接口才可使用,后面才明白过来,进入了一个误区,lua本身就是一门独立的脚本语言。在openresty里面配置好,即可编写映射和响应。
下面是自己编写的lua代码一例,仅供参考。还有些不完善,要开始忙项目了,等有空再继续更新。
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
then
nax.say("new mysql error",err)
end local args = nil
local username = nil
local pwd = nil
if "GET" == request_method
then
args = ngx.req.get_uri_args()
elseif "POST" == ngx.request_method
then
ngx.req.read_body()
args = ngx.req.get_post_args()
end
username = tostring(args["username"])
pwd = tostring(args["pwd"])
if username == nil then
ngx.say("username not nil")
return
elseif username == '' then
ngx.say("username not nil")
return
elseif pwd == nil then
ngx.say("password not nil")
return;
elseif pwd == '' then
ngx.say("password not nil")
return;
end db: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))
Openresty编写Lua代码一例的更多相关文章
- 用 openresty 编写 lua
""" #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/erro ...
- [转]编写高性能的Lua代码
昨天晚上闲来无事,看室友在电脑上挂机玩游戏,用的一个辅助脚本,以为是lua写的脚本在跑,实际调查发现是按键精灵的脚本. 于是在网上找相关Lua开发游戏脚本的案例,看到一个人的博客,内容很不错,学到了很 ...
- openresty源码剖析——lua代码的加载
##Openresty是什么 OpenResty是一个基于 Nginx 与 Lua 的高性能 Web 平台,通过把lua嵌入到Nginx中,使得我们可以用轻巧的lua语言进行nginx的相关开发,处理 ...
- 编写高性能的Lua代码
编写高性能的Lua代码 Posted on2014/04/18· 10 Comments 前言 Lua是一门以其性能著称的脚本语言,被广泛应用在很多方面,尤其是游戏.像<魔兽世界>的插件, ...
- 编写高性能的 Lua 代码
前言 Lua是一门以其性能著称的脚本语言,被广泛应用在很多方面,尤其是游戏.像<魔兽世界>的插件,手机游戏<大掌门><神曲><迷失之地>等都是用Lua来 ...
- lua代码的加载
lua代码的加载 Openresty是什么 OpenResty是一个基于 Nginx 与 Lua 的高性能 Web 平台,通过把lua嵌入到Nginx中,使得我们可以用轻巧的lua语言进行nginx的 ...
- openresty源码剖析——lua代码的执行
上一篇文章中我们讨论了openresty是如何加载lua代码的 那么加载完成之后的lua代码又是如何执行的呢 ##代码的执行 在init_by_lua等阶段 openresty是在主协程中通过lu ...
- 【openresty】向lua代码中传递参数
前面介绍FormInputNginxModule模块时,明白了openresty如何获取post提交的数据. 然后,如果需要通过lua处理这些数据,需要把数据作为参数传递到lua中,lua获取了这些数 ...
- 编写高效Lua代码的方法
编写高效Lua代码的方法 翻译自<Lua Programming Gems>Chapter 2:Lua Performance Tips:Basic fact By Roberto Ier ...
随机推荐
- HTML5 CSS3 诱人的实例 :canvas 模拟实现电子彩票刮刮乐
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/34089553 今天给大家带来一个刮刮乐的小例子~基于HTML5 canvas的, ...
- 最短寻道优先算法----SSTF算法
请珍惜小编劳动成果,该文章为小编原创,转载请注明出处. 该算法选择这样的进程,其要求访问的磁道与当前磁头所在的磁道距离最近,以使每次的寻道时间最短 java代码实现如下: import java.ut ...
- fixed 和 absolute 定位的区别
fixed:固定定位 absolute:绝对定位 在没有滚动条的情况下两者其实没有差异.但是在有滚动条后,fixed始终会在定好的位置不动,而absolute会随参照对象元素的宽高 ...
- https://blog.csdn.net/u011489043/article/details/68488459
转自https://blog.csdn.net/u011489043/article/details/68488459 String 字符串常量 StringBuffer 字符串变量(线程安全) ...
- 使用画圆的方法来标注指定Id的实体
//entId为指定实体的Id void SignEnt(AcDbObjectId entId){ AcDbEntity *pEnt = NULL; AcDbBlockReference *pRef; ...
- bzoj 3343 教主的魔法 分块
修改直接对整块打标记,两边暴力. 查询需要保证每个整块有序,所以在修改时排序就好啦 #include<cstdio> #include<cstring> #include< ...
- 拿Proxy可以做哪些有意思的事儿
Proxy是什么 首先,我们要清楚,Proxy是什么意思,这个单词翻译过来,就是 代理.可以理解为,有一个很火的明星,开通了一个微博账号,这个账号非常活跃,回复粉丝.到处点赞之类的,但可能并不是真的由 ...
- linux目录详细介绍
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://yangrong.blog.51cto.com/6945369/1288072 目 ...
- POI读取excel文件。
1) poi读取现成.xls文件,不需要自己建立.xls ====ReadExcel类==== package cust.com.excelToDataTest; import java.io.F ...
- istio添加Fluentd
这个教程展示了istio如何自定义日志格式,并且将其发送给fluent.Fluentd 是一个开源的日志收集器,支持多种数据输出并且有一个可插拔架构.Elasticsearch是一个流行的后端日志记录 ...