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代码一例的更多相关文章

  1. 用 openresty 编写 lua

    """ #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/erro ...

  2. [转]编写高性能的Lua代码

    昨天晚上闲来无事,看室友在电脑上挂机玩游戏,用的一个辅助脚本,以为是lua写的脚本在跑,实际调查发现是按键精灵的脚本. 于是在网上找相关Lua开发游戏脚本的案例,看到一个人的博客,内容很不错,学到了很 ...

  3. openresty源码剖析——lua代码的加载

    ##Openresty是什么 OpenResty是一个基于 Nginx 与 Lua 的高性能 Web 平台,通过把lua嵌入到Nginx中,使得我们可以用轻巧的lua语言进行nginx的相关开发,处理 ...

  4. 编写高性能的Lua代码

    编写高性能的Lua代码 Posted on2014/04/18· 10 Comments 前言 Lua是一门以其性能著称的脚本语言,被广泛应用在很多方面,尤其是游戏.像<魔兽世界>的插件, ...

  5. 编写高性能的 Lua 代码

    前言 Lua是一门以其性能著称的脚本语言,被广泛应用在很多方面,尤其是游戏.像<魔兽世界>的插件,手机游戏<大掌门><神曲><迷失之地>等都是用Lua来 ...

  6. lua代码的加载

    lua代码的加载 Openresty是什么 OpenResty是一个基于 Nginx 与 Lua 的高性能 Web 平台,通过把lua嵌入到Nginx中,使得我们可以用轻巧的lua语言进行nginx的 ...

  7. openresty源码剖析——lua代码的执行

    上一篇文章中我们讨论了openresty是如何加载lua代码的 那么加载完成之后的lua代码又是如何执行的呢 ##代码的执行  在init_by_lua等阶段  openresty是在主协程中通过lu ...

  8. 【openresty】向lua代码中传递参数

    前面介绍FormInputNginxModule模块时,明白了openresty如何获取post提交的数据. 然后,如果需要通过lua处理这些数据,需要把数据作为参数传递到lua中,lua获取了这些数 ...

  9. 编写高效Lua代码的方法

    编写高效Lua代码的方法 翻译自<Lua Programming Gems>Chapter 2:Lua Performance Tips:Basic fact By Roberto Ier ...

随机推荐

  1. fixed 和 absolute 定位的区别

    fixed:固定定位           absolute:绝对定位 在没有滚动条的情况下两者其实没有差异.但是在有滚动条后,fixed始终会在定好的位置不动,而absolute会随参照对象元素的宽高 ...

  2. 安装scrapy出错Failed building wheel for Twisted

    用64位windows10的CMD命令安装pip install scrapy出错: Running setup.py bdist_wheel for Twisted ... error Failed ...

  3. QM

    答案: C 解题: 1. PV = 1,2 / 11% = 10.91 NPV = PV(inflow)-PV(outflow) = 10.91 - 8 = 2.91 2. IRR : NPV = 0 ...

  4. ConcurrentHashMap简介

    ConcurrentHashMap为了高并发而设计,相比于HashTable和HashMap有更多优势.HashTable是同步的,在多线程环境下,能保证程序执行的正确性,每次同步执行的时候都要锁住整 ...

  5. java.lang.IllegalArgumentException异常 数据库别名问题

    java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Path expect ...

  6. 「WC 2018」州区划分

    题目大意: 给一个无向图$G(V,E)$满足$|V|<=21$,对于某一种将$G(V,E)$划分为k个的有序集合方案,若每一个子集$G_i(V_i,E_i)$,$E_i=\{(x,y)|x\in ...

  7. [NOI赛前训练]——专项测试3·数学

    由于并不想写T1和T2的题解……所有只有T3的题解了. T3 由于内部题就只写题解了. 好吧,我是一点都不想写…… 说一下这zz题解哪里写错了吧…… ……不想写…… 就说一个吧…… $n-\frac{ ...

  8. 硬木地板 JDFZ1667

    Description 举行计算机科学家盛宴的大厅的地板为M×N (1<=M<=9, 1<=N<=9)的矩形.现在必须要铺上硬木地板砖.可以使用的地板砖形状有两种:1) 2×1 ...

  9. 原生wcPop.js消息提示框(移动端)、内含仿微信弹窗效果

    wcPop.js移动端消息对话框插件是之前的wxPop.js的升级版,优化了js和css,并且新增了仿微信弹窗效果, 是一款含有多种情景模式的原生模态消息对话框代码,可用于替代浏览器默认的alert弹 ...

  10. ASP.Net Core MVC+Ajax 跨域

    要求 C端:用户端(http://www.b.com) A端:管理端(http://admin.b.com) 问题:A端上传图片到C端指定文件夹内保存,供C端使用. 方案 ① C端从nuget引入Mi ...