Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 25 additions & 15 deletions application/controller/LiveRedisCacheController.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* |------------------------------------------------------------------------
--]]
local template = require "resty.template"
local helper = require "vendor.helper"
local redis = require "resty.redis"
local cjson = require("cjson")
local resty_lock = require "resty.lock"
Expand All @@ -25,7 +26,7 @@ local live_room = ngx.shared.live_room;

local redis_host = "121.41.88.209"
local redis_port = 63789
local redis_auth = "tinywanredisamaistream"
local redis_auth = "tinywanredisamaistream123456789"
local redis_timeout = 1000

----------------- set ngx.cache
Expand Down Expand Up @@ -211,9 +212,9 @@ end
-------------- read_http 大并发采用 resty.http ,对于:ngx.location.capture 慎用
local function read_http(id)
local httpc = http.new()
local resp, err = httpc:request_uri("http://sewise.amai8.com", {
local resp, err = httpc:request_uri("http://testwww.baidu.com", {
method = "GET",
path = "/openapi/luaJson?id=" .. id,
path = "/api/Api?liveId=" .. id,
headers = {
["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36"
}
Expand All @@ -236,19 +237,27 @@ local function read_http(id)
return
end

-- 缓存到 Redis 数据缓存 -- 这里要做优化
local live_info_key = "LIVE_TABLE:" .. id
local live_value = cjson_decode(resp.body) -- 解析的Lua自己的然后存储到Redis 数据库中去
local live_live_str = write_redis(redis_host,redis_port,redis_auth, { live_info_key }, cjson_encode(live_value))
if not live_live_str then
log(ERR, "redis set info error: ")
-- backend not data 判断api返回的状态码
local status_code = helper.cjson_decode(resp.body)['code']
if tonumber(status_code) ~= 404 then
-- 正常数据缓存到 Redis 数据缓存
local live_info_key = "LIVE_TABLE:" .. id
local live_value = helper.cjson_decode(resp.body) -- 解析的Lua自己的然后存储到Redis 数据库中去
local live_live_str = write_redis(redis_host, redis_port, redis_auth, { live_info_key }, helper.cjson_encode(live_value))
if not live_live_str then
log(ERR, "redis set info error: ")
end
log(ERR, " [read_http] content from backend API id : " .. id) -- tag data origin
return helper.cjson_encode(live_value)
else
-- 后端没有数据直接返回 nil
log(ERR, " [read_http] backend API is not content return error_msg : " ..helper.cjson_decode(resp.body)['msg']) -- tag data origin
return
end
log(ERR, " [read_http] content from backend API id : " .. id) -- tag data origin
return cjson_encode(live_value)
end

----------------------------------------------------------------------业务逻辑处理
--get var id
---------------------------------------------------------------------- 业务逻辑处理
-- get var id
local id = ngx_var.id
local live_info_key = "LIVE_TABLE:" .. id

Expand All @@ -262,14 +271,15 @@ if not content then
log(ERR, "redis not found content, back to backend API , id : ", id)
content = read_http(id)
end

-------------------------------- 主要就是这里要根据后端服务没有数据的情况处理,最好不要出现500错误就可以啦
-- if backend API not exit 404
-- if backend API not success data , page show 404
if not content then
log(ERR, "backend API not found content, id : ", id)
return ngx.exit(ngx.HTTP_NOT_FOUND)
end

-- if backend API response result is false 403
-- if backend API response result is false page show 403
if tostring(content) == "false" then
log(ERR, "backend API content is false ", id)
return ngx.exit(ngx.HTTP_FORBIDDEN)
Expand Down
6 changes: 4 additions & 2 deletions application/controller/WebSocketRedisController.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ if not wb then
return ngx.exit(444)
end


local push = function()
-- --create redis
local red = redis:new()
Expand All @@ -45,7 +46,7 @@ local push = function()

ok, err = red:auth(redis_auth)
if not ok then
log(ERR, "failed to auth: ", err)
ngx.log(ngx.ERR, "failed to auth: ", err)
wb:send_close()
end
--sub
Expand All @@ -61,7 +62,7 @@ local push = function()
local res, err = red:read_reply()
if res then
local item = res[3]
-- local bytes, err = wb:send_text(tostring(msg_id).." "..item)
--local bytes, err = wb:send_text(tostring(msg_id).." "..item)
local bytes, err = wb:send_text('[ '..tostring(msg_id).." ] "..ngx.localtime().." "..item)
if not bytes then
-- better error handling
Expand All @@ -73,6 +74,7 @@ local push = function()
end
end


local co = ngx.thread.spawn(push) -- create thread 1

--main loop
Expand Down
33 changes: 33 additions & 0 deletions application/demo/company_branch/cookie_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--
-- Created by IntelliJ IDEA.
-- User: Administrator
-- Date: 2017/6/19
-- Time: 10:05
-- To change this template use File | Settings | File Templates.
--
local redis = require "resty.redis"
local log = ngx.log
local redis_host = "121.41.88.209"
local redis_port = "63789"
local redis_auth = "tinywanredisamaistream"
local redis_timeout = 1000

local red = redis:new()
red:set_timeout(5000) -- 1 sec
local ok, err = red:connect(redis_host, redis_port)
if not ok then
ngx.log(ngx.ERR, "failed to connect redis: ", err)
wb:send_close()
return
end

ok, err = red:auth(redis_auth)
if not ok then
log(ERR, "failed to auth: ", err)
end

resp, err = red:set("LATEST_COMMENT::12345", "LATEST_COMMENT VALUES")
if not resp then
ngx.log(ngx.ERR, "set redis live error : ", err)
end

35 changes: 35 additions & 0 deletions application/demo/company_branch/header_filter.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
--
-- Created by IntelliJ IDEA.
-- User: Administrator
-- Date: 2017/6/19
-- Time: 10:14
-- To change this template use File | Settings | File Templates.
--
local match = string.match

local function get_cookies()
-- local cookies = ngx.header["Set-Cookie"] or {}
local cookies = ngx.header.set_cookie or {}
if type(cookies) == "string" then
cookies = {cookies}
end
return cookies
end

local function remove_cookie(cookie_name)
local cookies = get_cookies()

ngx.log(ngx.ERR, "source cookies ", table.concat(cookies, " "))
for key, value in ipairs(cookies) do
local name = match(value, "(.-)=")
ngx.log(ngx.ERR, key.."<=>", value)
if name == cookie_name then
table.remove(cookies, key)
end
end

ngx.header['Set-Cookie'] = cookies or {}
ngx.log(ngx.ERR, "new cookies ", table.concat(cookies, " "))
end

remove_cookie("Foo")
11 changes: 11 additions & 0 deletions application/demo/wechat_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--
-- Created by IntelliJ IDEA.
-- User: Administrator
-- Date: 2017/7/11
-- Time: 17:03
-- To change this template use File | Settings | File Templates.
--
local wechat = require "vendor.wechat"
ngx.say("_VERSION == "..wechat._VERSION)
ngx.say("_VERSION == "..wechat.index())

36 changes: 0 additions & 36 deletions conf/domains/api.conf

This file was deleted.

Loading