61 lines
1.2 KiB
Lua
61 lines
1.2 KiB
Lua
local json = require 'json'
|
|
local redis = require 'redis'
|
|
local response = require 'response'
|
|
|
|
local module = {
|
|
['redis'] = {}
|
|
}
|
|
|
|
module.redis.connect = function()
|
|
local red = redis:new();
|
|
red:set_timeout(1000);
|
|
local ok, err = red:connect('redis', 6379)
|
|
if not ok then
|
|
ngx.log(ngx.ERR, err)
|
|
response.quit(ngx.HTTP_SERVICE_UNAVAILABLE)
|
|
return
|
|
end
|
|
|
|
return red
|
|
end
|
|
|
|
module.redis.checkDomain = function(red, category, domain)
|
|
local result, err = red:hmget(category, domain)
|
|
if err then
|
|
ngx.log(ngx.ERR, err)
|
|
response.quit(ngx.HTTP_SERVICE_UNAVAILABLE)
|
|
return
|
|
end
|
|
return type(result) == 'string' and true or false
|
|
end
|
|
|
|
local function split(str, pat)
|
|
local t = {}
|
|
local fpat = "(.-)" .. pat
|
|
local last_end = 1
|
|
local s, e, cap = str:find(fpat, 1)
|
|
while s do
|
|
if s ~= 1 or cap ~= "" then
|
|
table.insert(t,cap)
|
|
end
|
|
last_end = e+1
|
|
s, e, cap = str:find(fpat, last_end)
|
|
end
|
|
if last_end <= #str then
|
|
cap = str:sub(last_end)
|
|
table.insert(t, cap)
|
|
end
|
|
return t
|
|
end
|
|
|
|
module.parseURL = function(uri)
|
|
local uri = uri or ngx.var.request_uri
|
|
return split(uri, '[\\/]+')
|
|
end
|
|
|
|
module.getDomain = function(str)
|
|
return string.match(str, '%w+%.%w+$')
|
|
end
|
|
|
|
return module
|