-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.lua
More file actions
188 lines (160 loc) · 4.03 KB
/
Copy pathindex.lua
File metadata and controls
188 lines (160 loc) · 4.03 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
-- CMS 推荐接口
-- @author chenweichuan@baofeng.net
--
-- 调试工具
local dump = require "dump"
dump.html = true
--[[ 函数缩写 ]]
local null = ngx.null
local print = ngx.print
local time = ngx.time
local exit = ngx.exit
local error = ngx.error
local log = ngx.log
local capture = ngx.location.capture
local getn = table.getn
local concat = table.concat
local insert = table.insert
local remove = table.remove
local sub = string.sub
local find = string.find
local len = string.len
local ceil = math.ceil
local pairs = pairs
local tonumber = tonumber
local unpack = unpack
-- 文本类型和字符集
ngx.header['Content-Type'] = "text/html;charset=utf-8"
-- 配置
local CFG = {
memcached = {
cache = {
host = "127.0.0.1",
port = 11211,
timeout = 2000,
pool_size = 100,
keepalive_timeout = 120
}
},
redis = {
cache = {
host = "127.0.0.1",
port = 6379,
timeout = 2000,
pool_size = 100,
keepalive_timeout = 120
}
}
}
--[[ Import lib ]]
local memcached = require "resty.memcached"
local redis = require "resty.redis"
-- [[ 自定义函数 ]]
-- 初始化memcached
local function connect_memcache( name )
-- key 不做特殊处理
local _memcached = memcached:new( {
key_transform = {
function( key )
return key
end,
function( key )
return key
end
}
} )
local cfg = CFG.memcached[name]
_memcached:set_timeout( cfg.timeout )
local ok, err = _memcached:connect( cfg.host, cfg.port )
if not ok then
error( "memcached " .. name )
end
-- 附加名称
_memcached._name = name
return _memcached
end
-- 关闭memcached
local function close_memcache( _memcached )
local cfg = CFG.memcached[_memcached._name]
local ok, err = _memcached:set_keepalive( cfg.keepalive_timeout, cfg.pool_size )
end
-- 初始化redis
local function connect_redis( name )
local _redis = redis:new()
local cfg = CFG.redis[name]
_redis:set_timeout(cfg.timeout)
local ok, err = _redis:connect(cfg.host, cfg.port)
if not ok then
error("redis " .. name)
end
if cfg.password then
_redis:auth(cfg.password)
end
if cfg.dbname then
_redis:select(cfg.dbname)
end
-- 附加名称
_redis._name = name
return _redis
end
-- 关闭redis
local function close_redis( _redis )
local cfg = CFG.redis[_redis._name]
local ok, err = _redis:set_keepalive( cfg.keepalive_timeout, cfg.pool_size )
end
-- 参数
local DOCUMENT_ROOT = ngx.var.document_root
local URI = ngx.var.uri
local QUERY_STRING = ngx.var.query_string or ""
local _GET = ngx.req.get_uri_args()
local PARAMS = {}
PARAMS.type = sub( URI, 2, ( find( URI, "/", 2 ) or 0 ) - 1 )
-- 若请求同时带有多个callback 参数,默认会转换为table,可通过callback[1] 判断
PARAMS.callback = _GET.callback and "" ~= ( _GET.callback[1] or _GET.callback ) and ( _GET.callback[1] or _GET.callback )
-- 读取memcache 缓存
local function get_cache_from_memcache()
local cache_memcache = connect_memcache( "cache" )
local res, flags, err = cache_memcache:get( URI )
close_memcache( cache_memcache )
return res
end
-- 读取文件缓存
local function read_cache_from_file()
local file = io.open( DOCUMENT_ROOT .. URI, "r" )
local contents = nil
if file then
contents = file:read( "*a" )
file:close()
end
return contents
end
-- 读取各种推荐的缓存的方法
local switch = {}
switch["api1"] = read_cache_from_file
switch["api2"] = get_cache_from_memcache
switch["api3"] = function ()
local cmsgpack = require "cmsgpack"
-- do anything...
res = {}
return res
end
-- 验证推荐类型,并从缓存中读取结果
local result = not switch[PARAMS.type] and '["十万个冷笑话"]' or switch[PARAMS.type]()
-- 请求PHP
if not result then
local request = capture( "/index.php?router=" .. URI .. "&" .. QUERY_STRING )
-- 合并由php 返回的header
for i, v in pairs( request.header ) do
ngx.header[i] = v
end
result = request.body
end
-- 处理callback
if PARAMS.callback then
result = PARAMS.callback .. "(" .. result .. ");"
end
-- 内容长度
ngx.header["Content-Length"] = len( result or "" )
-- 输出结果
print( result )
exit( 200 )