-
Notifications
You must be signed in to change notification settings - Fork 0
/
threescale_utils.lua
136 lines (115 loc) · 2.79 KB
/
threescale_utils.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
-- threescale_utils.lua
local M = {} -- public interface
function string:split(delimiter)
local result = { }
local from = 1
local delim_from, delim_to = string.find( self, delimiter, from )
while delim_from do
table.insert( result, string.sub( self, from , delim_from-1 ) )
from = delim_to + 1
delim_from, delim_to = string.find( self, delimiter, from )
end
table.insert( result, string.sub( self, from ) )
return result
end
-- private
-- Logging Helpers
function M.show_table(t, ...)
local indent = 0 --arg[1] or 0
local indentStr=""
for i = 1,indent do indentStr=indentStr.." " end
for k,v in pairs(t) do
if type(v) == "table" then
msg = indentStr .. M.show_table(v or '', indent+1)
else
msg = indentStr .. k .. " => " .. v
end
M.log_message(msg)
end
end
function M.log_message(str)
ngx.log(0, str)
end
function M.newline()
ngx.log(0," --- ")
end
function M.log(content)
if type(content) == "table" then
M.log_message(M.show_table(content))
else
M.log_message(content)
end
M.newline()
end
-- End Logging Helpers
-- Table Helpers
function M.keys(t)
local n=0
local keyset = {}
for k,v in pairs(t) do
n=n+1
keyset[n]=k
end
return keyset
end
-- End Table Helpers
function M.dump(o)
if type(o) == 'table' then
local s = '{ '
for k,v in pairs(o) do
if type(k) ~= 'number' then k = '"'..k..'"' end
s = s .. '['..k..'] = ' .. M.dump(v) .. ','
end
return s .. '} '
else
return tostring(o)
end
end
function M.sha1_digest(s)
local str = require "resty.string"
return str.to_hex(ngx.sha1_bin(s))
end
-- returns true iif all elems of f_req are among actual's keys
function M.required_params_present(f_req, actual)
local req = {}
for k,v in pairs(actual) do
req[k] = true
end
for i,v in ipairs(f_req) do
if not req[v] then
return false
end
end
return true
end
function M.connect_redis(red)
redisurl = os.getenv("REDISTOGO_URL")
redisurl_connect = string.split(redisurl, ":")[3]
redisurl_user = string.split(redisurl_connect, "@")[1]
redisurl_host = string.split(redisurl_connect, "@")[2]
redisurl_port = string.sub(string.split(redisurl, ":")[4],1,-2)
local ok, err = red:connect(redisurl_host, tonumber(redisurl_port))
if not ok then
ngx.say("failed to connect: ", err)
ngx.exit(ngx.HTTP_OK)
end
local res, err = red:auth(redisurl_user)
if not res then
ngx.say("failed to authenticate: ", err)
return
end
return ok, err
end
-- error and exist
function M.error(text)
ngx.say(text)
ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
end
function M.missing_args(text)
ngx.say(text)
ngx.exit(ngx.HTTP_OK)
end
return M
-- -- Example usage:
-- local MM = require 'mymodule'
-- MM.bar()