魏长东

weichangdong

lua api

本来这个功能使用php实现了,但是想尝试下lua,暂时在win测试的,因为md5的原因,就终止了。代码先放上来,以后有时间在改。

location ~ /api {
		content_by_lua_file html/lua.lua;
}
curl的时候 一定用双引号 开始用单引号 折腾了半天 奶奶的。
curl  -d "{\"news\":{\"id\":\"175e45e26c5d10a78dd42e613ba74c57\",\"title\":\"Put a ring on it: 
Mariko Mori to hang symbol of unity above Rio waterfall ahead of OlympicGames\",\"timestamp\":\"\",
\"news_entry_id\":\"5f89783e\",\"category\":\"others\",
\"url\":\"http://theartnewspaper.com/news/news/put-a-ring-on-it-mariko-mori-to-hang-symbol-
of-unity-above-rio-waterfall-ahead-of-olympic-games/\"},
\"from\":{\"avatar\":\"http://pbs.twimg.com/profile_images/633675712319242240/9Vri2Jh7_bigger.jpg\",
\"screen_name\":\"TheArtNewspaper\",\"openid\":\"38681183\"},
\"to\":[\"cd21b5bac2317d41c32ae2713836b2824455bafc\"],
\"time\":146414554125,\"key\":\"754942a90ecd1227a2ace4c32c7d36af11\"}"  http://localhost/api

local str = require "resty.string"
local cjson  = require("cjson.safe")
local redis  = require("resty.redis")
local resty_md5 = require "resty.md5"
local md5 = resty_md5:new()
local API_KEY = 'wcd'
local M         = {}

--[[
    获取POST请求的Body
]]--
M.get_post_body = function ()
    ngx.req.read_body()
    --[[ngx.req.get_body_data 可能会得到 nil 的情况如下
        the request body has not been read
        the request body has been read into disk temporary files,
        the request body has zero size
    ]]--
    local data = ngx.req.get_body_data()
    if not data then
        local temp_body_filename = ngx.req.get_body_file()
        if temp_body_filename then
            local f1 = io.open(temp_body_filename, 'r')
            data = f1:read('*a')
            f1:close()
        end
    end
    return data
end

M.trim=function (s)
  return s:match "^%s*(.-)%s*$"
end
M.main = function ()
    local post_data = M.get_post_body()
	if not post_data then
		 ngx.say('{"ret":1,"msg":"post empty or error 1"}')
		 return
	end
	--local post_data='{"wcd":123}'
	-- local post_data = M.trim(post_data)
	-- local post_data=tostring(post_data)
	--ngx.say(post_data)
    local data,err = cjson.decode(post_data)
    if err or not data then
        ngx.say(err..'fuck')
		return
    end
	--ngx.say('wcd')
	--ngx.exit(200)
    local post_key = data.key
	local post_time = data.time
	
	--ngx.say(post_key)
	--local wcd=md5:update(post_time..API_KEY)
	-- ngx.say(ngx.md5('wcd'))
	--ngx.exit(200)

	if post_key == ngx.md5(post_time..API_KEY) then
		if not data.news or not data.from or not data.to then
			ngx.say('{"ret":1,"msg":"info error"}')
			return
		end
		local red = redis:new()
		red:set_timeout(1000)  --1 seconds
		local ok,err = red:connect('127.0.0.1', 6379)
		if not ok then
		  ngx.say("failed to connect redis: ", err)
		  return 
		end
		red:select(0)
		data.time=nil
		data.key=nil
		local redis_value = cjson.encode(data);
		red:lpush('push_list_friends',redis_value);
		ngx.say('{"ret":0,"msg":""}')
	else
		ngx.say('{"ret":1,"msg":"key error"}')
		return
	end
end
M.main()

此程序成功运行。中间的md5一直通不过,最后换用ngx.md5 一下子ok了。