summaryrefslogtreecommitdiffstats
path: root/openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib
diff options
context:
space:
mode:
Diffstat (limited to 'openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib')
-rw-r--r--openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib/tools/db_cache.lua54
-rw-r--r--openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib/tools/rr_cache.lua40
-rw-r--r--openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib/utils/error_handler.lua48
-rw-r--r--openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib/utils/log_util.lua28
-rw-r--r--openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib/utils/str_util.lua30
-rw-r--r--openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib/utils/svc_util.lua96
-rw-r--r--openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib/utils/table_util.lua30
7 files changed, 326 insertions, 0 deletions
diff --git a/openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib/tools/db_cache.lua b/openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib/tools/db_cache.lua
new file mode 100644
index 0000000..7f40590
--- /dev/null
+++ b/openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib/tools/db_cache.lua
@@ -0,0 +1,54 @@
+--[[
+
+ Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+--]]
+
+-- db cache over LRUCache, used within one worker processes
+local _M = {}
+_M._VERSION = '1.0.0'
+
+local lrucache = require "resty.lrucache"
+
+local EMPTY_DATA = '_EMPTY_'
+
+-- we need to initialize the cache on the lua module level so that
+-- it can be shared by all the requests served by each nginx worker process:
+local cache,err = lrucache.new(200) -- allow up to 200 items in the cache
+if not cache then
+ return ngx.log(ngx.ERR,"failed to create the cache: " .. (err or "unknown"))
+end
+
+function _M.get(key)
+ return cache:get(key)
+end
+
+function _M.set(key,value)
+ return _M:set(key, value, nil)
+end
+
+function _M.set(key,value,ttl)
+ if not value then
+ value = EMPTY_DATA
+ end
+ return cache:set(key, value, ttl)
+end
+
+-- check if the data returned by get() is considered empty
+function _M.is_empty(data)
+ return data == EMPTY_DATA
+end
+
+return _M \ No newline at end of file
diff --git a/openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib/tools/rr_cache.lua b/openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib/tools/rr_cache.lua
new file mode 100644
index 0000000..bfcc157
--- /dev/null
+++ b/openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib/tools/rr_cache.lua
@@ -0,0 +1,40 @@
+--[[
+
+ Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+--]]
+
+-- round-robin cache over LRUCache, used within one worker processes
+local _M = {}
+_M._VERSION = '1.0.0'
+
+local lrucache = require "resty.lrucache"
+
+-- we need to initialize the cache on the lua module level so that
+-- it can be shared by all the requests served by each nginx worker process:
+local rrcache,err = lrucache.new(500) -- allow up to 200 items in the cache
+if not rrcache then
+ return ngx.log(ngx.ERR,"failed to create the cache: " .. (err or "unknown"))
+end
+
+function _M.get(key)
+ return rrcache:get(key)
+end
+
+function _M.set(key,value)
+ rrcache:set(key, value)
+end
+
+return _M \ No newline at end of file
diff --git a/openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib/utils/error_handler.lua b/openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib/utils/error_handler.lua
new file mode 100644
index 0000000..784eb86
--- /dev/null
+++ b/openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib/utils/error_handler.lua
@@ -0,0 +1,48 @@
+--[[
+
+ Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+--]]
+
+local _M = {}
+_M._VERSION = '1.0.0'
+local msbConf = require('conf.msbinit')
+local enablefullsearch = msbConf.systemConf.enablefullsearch
+local ngx_var = ngx.var
+local error_page_head = '<html><head><title>502 Bad Gateway</title></head><body bgcolor="white"><center><h1>502 Bad Gateway</h1></center><center>error message:'
+local error_page_foot = '</center><hr><center>nginx</center></body></html>'
+local upstream_not_found_err = "service info is incorrect:using own upstream flag is on but upstream name is empty"
+
+function _M.svc_not_found(err_info,detail_info)
+ ngx.log(ngx.WARN, ngx.var.request_id.." "..(err_info or "").." detail_info:"..(detail_info or ""))
+ if enablefullsearch and ngx_var.svc_type ~= "custom" then
+ -- test against the custom services after the commonrewrite phase
+ --ngx.status = ngx.HTTP_GONE
+ return ngx.exec("@commonnotfound");
+ else
+ ngx.status = ngx.HTTP_BAD_GATEWAY
+ ngx.print(error_page_head..err_info..error_page_foot)
+ end
+ return ngx.exit(ngx.status)
+end
+
+function _M.upstream_not_found()
+ ngx.log(ngx.WARN, ngx.var.request_id.." "..upstream_not_found_err)
+ ngx.status = ngx.HTTP_BAD_GATEWAY
+ ngx.print(error_page_head..upstream_not_found_err..error_page_foot)
+ return ngx.exit(ngx.status)
+end
+
+return _M \ No newline at end of file
diff --git a/openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib/utils/log_util.lua b/openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib/utils/log_util.lua
new file mode 100644
index 0000000..67b0e26
--- /dev/null
+++ b/openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib/utils/log_util.lua
@@ -0,0 +1,28 @@
+--[[
+
+ Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+--]]
+
+local _M = {}
+_M._VERSION = '1.0.0'
+
+function _M.log(k, v)
+ --if empty,initialize it
+ if not ngx.ctx.logtbl then ngx.ctx.logtbl = {} end
+ ngx.ctx.logtbl[k] = v
+end
+
+return _M \ No newline at end of file
diff --git a/openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib/utils/str_util.lua b/openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib/utils/str_util.lua
new file mode 100644
index 0000000..6fa8d39
--- /dev/null
+++ b/openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib/utils/str_util.lua
@@ -0,0 +1,30 @@
+--[[
+
+ Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+--]]
+
+local _M = {}
+_M._VERSION = '1.0.0'
+
+function _M.mark_empty_as_nil(t)
+ if t == "" then
+ return nil
+ else
+ return t
+ end
+end
+
+return _M \ No newline at end of file
diff --git a/openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib/utils/svc_util.lua b/openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib/utils/svc_util.lua
new file mode 100644
index 0000000..226e31f
--- /dev/null
+++ b/openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib/utils/svc_util.lua
@@ -0,0 +1,96 @@
+--[[
+
+ Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+--]]
+
+local _M = {}
+_M._VERSION = '1.0.0'
+
+local msbConf= require('conf.msbinit')
+local svcConf = require('conf.svcconf')
+local log_util = require('lib.utils.log_util')
+
+local log = log_util.log
+local ngx_var = ngx.var
+
+local defaultport = msbConf.systemConf.defaultport
+local defaulthttpsport = msbConf.systemConf.defaulthttpsport
+local defaultprefix = msbConf.systemConf.defaultprefix
+local router_subdomain = msbConf.routerConf.subdomain
+local router_defaultprefix = msbConf.routerConf.defaultprefix
+local useconsultemplate = msbConf.systemConf.useconsultemplate
+local urlfieldMap = svcConf.urlfieldMap
+local apiRelatedTypes = svcConf.apiRelatedTypes
+
+function _M.isactive(svcinfo)
+ if svcinfo["status"] == "1" then
+ return true
+ else
+ return false
+ end
+end
+
+function _M.use_own_upstream(svcinfo)
+ if useconsultemplate and svcinfo.spec.useOwnUpstream == "1" then
+ log("useOwnUpstream",true)
+ return true
+ else
+ return false
+ end
+end
+
+function _M.get_url(svcinfo,svc_type)
+ return svcinfo.spec[urlfieldMap[svc_type]]
+end
+
+function _M.get_backend_protocol(svcinfo)
+ local svc_enable_ssl = svcinfo.spec["enable_ssl"]
+ if svc_enable_ssl then
+ return "https"
+ else
+ return "http"
+ end
+end
+
+function _M.get_key_prefix()
+ --now assemble the key prefix according the svc_name and server_port
+ local key_prefix = ""
+ local server_port = ngx_var.server_port
+ local svc_name = ngx_var.svc_name
+ if (svc_name == "microservices" or svc_name == "msdiscover") then
+ key_prefix = defaultprefix
+ elseif (server_port == defaultport or server_port == defaulthttpsport) then
+ local m, err = ngx.re.match(ngx_var.host, "(?<hostname>.+)\\."..router_subdomain,"o")
+ if m then
+ key_prefix = router_defaultprefix..":"..m["hostname"]
+ else
+ key_prefix = defaultprefix
+ end
+ else
+ key_prefix = "msb:"..server_port
+ end
+ return key_prefix
+end
+
+function _M.is_api_related_types(svc_type)
+ if(apiRelatedTypes[svc_type]) then
+ return true
+ else
+ return false
+ end
+end
+
+return _M \ No newline at end of file
diff --git a/openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib/utils/table_util.lua b/openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib/utils/table_util.lua
new file mode 100644
index 0000000..c1ddbfe
--- /dev/null
+++ b/openresty-ext/src/assembly/resources/openresty/nginx/luaext/lib/utils/table_util.lua
@@ -0,0 +1,30 @@
+--[[
+
+ Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+--]]
+
+local _M = {}
+_M._VERSION = '1.0.0'
+
+function _M.isempty(t)
+ if t == nil or next(t) == nil then
+ return true
+ else
+ return false
+ end
+end
+
+return _M \ No newline at end of file