local rabbitmq = require "resty.rabbitmqstomp"
local cjson = require "cjson"

local data = {}
data['ip'] = ngx.var.remote_addr
data['time'] = ngx.var.time_iso8601
data['args'] = ngx.var.args
data['host'] = ngx.var.host
data['status'] = ngx.var.status
data['ua'] = ngx.var.http_user_agent
data['uri'] = ngx.var.uri
data['method'] = ngx.var.request_method
data['refer'] = ngx.var.http_referer

function send_msg()
	local opts = {
	host = '127.0.0.1',
	posrt = 5672,
	username = 'loger',
	password = 'loger',
	vhost = 'log'
}

local mq, err = rabbitmq:new(opts)

--if not mq then
--	ngx.say('cannot new mq')
--	ngx.say(err)
--	return
--end

mq:set_timeout(10000)

local ok, err = mq:connect('127.0.0.1', 61613)
--if not ok then
--	ngx.say('cannot connect mq' .. err)
--	return
--end

local headers = {}
headers["destination"] = "/exchange/log/log"
headers["persistent"] = "true"
headers["content-type"] = "application/json"

local ok, err = mq:send(cjson.encode(data), headers)
--if not ok then
--	ngx.say('cannot send mq')
--	return
--end

--local ok, err = mq:set_keepalive(10000, 500)
--if not ok then
	--ngx.say(err)
	--return
	--end
end

ngx.timer.at(0, send_msg)

