示例工程 32-openresty-and-redis

32-openresty-and-redis/01-ngx-handler/main.lua

lua · 0.6 KB

-- @runtimes lua54
--- 演示:用 stub ngx 测试 handler 逻辑。
local M = {}
 
local function handler(ngx)
	ngx.say("hello " .. ngx.var.arg_name)
	return 200
end
 
--- @return table { body = string, status = integer }
function M.run(log)
	log = log or print
	local output = {}
	local ngx = {
		var = { arg_name = "Ada" },
		say = function(text)
			output[#output + 1] = text
		end,
	}
	local status = handler(ngx)
	local body = table.concat(output)
	log(("status=%d body=%s"):format(status, body))
	return { body = body, status = status }
end
if not ... then
	M.run()
end
return M