-
Notifications
You must be signed in to change notification settings - Fork 0
/
initialize.lua
116 lines (100 loc) · 3.21 KB
/
initialize.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
local secret = {APP1010TEST="s32k98d5102309"}
local db_config = {host = "127.0.0.1",port = 3306,database = "apidb",user = "readeruser",password = "pxqects123021Nbp"}
local sql_reload = "SELECT method_name,request_api_url FROM api_method"
local sql_prefix = "SELECT request_api_url FROM api_method WHERE method_name = "
err_100 = '{"code":100,"message":"无鉴权用户信息","data":{},"request_id":"%s"}'
err_101 = '{"code":101,"message":"鉴权验证失败","data":{},"request_id":"%s"}'
err_102 = '{"code":102,"message":"鉴权认证失败(参数异常)","data":{},"request_id":"%s"}'
err_103 = '{"code":103,"message":"内部接口调用失败","data":{},"request_id":"%s"}'
err_104 = '{"code":104,"message":"无接口信息","data":{},"request_id":"%s"}'
err_105 = '{"code":105,"message":"缺少关键参数","data":{},"request_id":"%s"}'
global = ngx.shared.global
local ffi = require("ffi")
ffi.cdef[[
struct timeval {
long int tv_sec;
long int tv_usec;
};
int gettimeofday(struct timeval *tv, void *tz);
]]
--获取时间 年月日时分秒毫秒
local tm = ffi.new("struct timeval")
function RequestId()
ffi.C.gettimeofday(tm,nil)
local sec = tonumber(tm.tv_sec)
local usec = tonumber(tm.tv_usec)
return os.date("%Y%m%d%H%M")..sec..usec
end
--输出错误信息
function Err(err_code)
return string.format(err_code,RequestId())
end
--获取secret_key
function SecretKey(key)
return secret[key]
end
--获取代理的地址
function Dispatch(key)
local value = global:get(key)
if value ~= nil then
return value
end
return FromDatabase(key)
end
--数据不存在时,从mysql中查询
function FromDatabase(key)
local mysql = require "resty.mysql"
local db,err = mysql:new()
if not db then
ngx.log(ngx.ERR,"failed to instantiate mysql: "..err)
return nil
end
db:set_timeout(1000)
local ok, err, errno, sqlstate = db:connect(db_config)
if not ok then
ngx.log(ngx.ERR,"failed to connect: "..err..": ", errno.." "..sqlstate)
return nil
end
local res, err, errno, sqlstate = db:query(sql_prefix..ngx.quote_sql_str(key),1)
if not res then
ngx.log(ngx.ERR,"bad result: "..err..": "..errno..": "..sqlstate..".")
return nil
end
if res ~= nil and res[1] ~= nil then
local value = res[1].request_api_url
global:set(key, value)
return value
end
return nil
end
--数据重新加载
function DispatchReload()
local mysql = require "resty.mysql"
local db,err = mysql:new()
if not db then
ngx.log(ngx.ERR,"failed to instantiate mysql: "..err)
return nil
end
db:set_timeout(1000)
local ok, err, errno, sqlstate = db:connect(db_config)
if not ok then
ngx.log(ngx.ERR,"failed to connect: "..err..": ", errno.." "..sqlstate)
return nil
end
local res, err, errno, sqlstate = db:query(sql_reload)
if not res then
ngx.log(ngx.ERR,"bad result: "..err..": "..errno..": "..sqlstate..".")
return nil
end
for k,v in pairs(res) do
local key = v.method_name
local value = v.request_api_url
local exist_value = global:get(key)
if exist_value ~= nil then
global:replace(key, value)
else
global:set(key, value)
end
end
ngx.log(ngx.ERR,'数据重新加载成功。')
end