diff --git a/balance/tl_ops_balance_core_api.lua b/balance/tl_ops_balance_core_api.lua index f850592..d5acd06 100644 --- a/balance/tl_ops_balance_core_api.lua +++ b/balance/tl_ops_balance_core_api.lua @@ -8,11 +8,12 @@ local cjson = require("cjson.safe"); local cache_api = require("cache.tl_ops_cache_core"):new("tl-ops-balance-api"); local tl_ops_utils_func = require("utils.tl_ops_utils_func"); local tl_ops_constant_balance_api = require("constant.tl_ops_constant_balance_api"); -local tl_ops_match_mode = require("constant.tl_ops_constant_comm").tl_ops_match_mode; -local tl_ops_constant_health = require("constant.tl_ops_constant_health") -local shared = ngx.shared.tlopsbalance -local find = ngx.re.find - +local tl_ops_constant_comm = require("constant.tl_ops_constant_comm"); +local tl_ops_constant_health = require("constant.tl_ops_constant_health"); +local tl_ops_match_mode = tl_ops_constant_comm.tl_ops_match_mode; +local tl_ops_api_type = tl_ops_constant_comm.tl_ops_api_type; +local shared = ngx.shared.tlopsbalance; +local find = ngx.re.find; -- 处理匹配逻辑 local tl_ops_balance_api_matcher_mode = function (matcher, request_uri, obj) @@ -81,6 +82,7 @@ end local tl_ops_balance_api_service_matcher = function(service_list_table) + local matcher = nil local node = nil @@ -153,15 +155,26 @@ local tl_ops_balance_api_service_matcher = function(service_list_table) local node_state , _ = shared:get(key) + -- 静态页面代理路径 + local api_type = matcher.api_type + if api_type and api_type == tl_ops_api_type.page then + ngx.req.set_uri_args({ + url = request_uri + }) + ngx.req.set_uri("/pageproxy", true) + return + end + -- 需要重写url local rewrite_url = matcher.rewrite_url if rewrite_url and rewrite_url ~= '' then ngx.req.set_uri(rewrite_url, false) + return end - -- 需要转发到服务具体路径 + -- 需要转发到服务下的具体路径 local fake_prefix = matcher.fake_prefix - if fake_prefix and matcher.fake_prefix ~= '' then + if fake_prefix and fake_prefix ~= '' then -- 通过虚拟前缀截取后缀 local fake_sub = string.sub(request_uri, #fake_prefix + 1, #request_uri) if fake_sub then diff --git a/bin/install_centeros.sh b/bin/install_centeros.sh index 7d82d3b..51b84e2 100644 --- a/bin/install_centeros.sh +++ b/bin/install_centeros.sh @@ -6,7 +6,7 @@ TL_OPS_PATH="/usr/local/tl-ops-manage/" TL_OPS_CONF_PATH="/usr/local/tl-ops-manage/conf/tl_ops_manage.conf" TL_OPS_LUA_PATH="/usr/local/openresty/lualib/?.lua;;/usr/local/tl-ops-manage/?.lua;;" TL_OPS_LUAC_PATH="/usr/local/openresty/lualib/?.so;;" -TL_OPS_VER="v3.2.0" +TL_OPS_VER="v3.3.0" echo_msg(){ cur_time=$(date "+%Y-%m-%d %H:%M:%S") diff --git a/bin/install_ubuntu.sh b/bin/install_ubuntu.sh index 9df4b9d..a48ec23 100644 --- a/bin/install_ubuntu.sh +++ b/bin/install_ubuntu.sh @@ -6,7 +6,7 @@ TL_OPS_PATH="/usr/local/tl-ops-manage/" TL_OPS_CONF_PATH="/usr/local/tl-ops-manage/conf/tl_ops_manage.conf" TL_OPS_LUA_PATH="/usr/local/openresty/lualib/?.lua;;/usr/local/tl-ops-manage/?.lua;;" TL_OPS_LUAC_PATH="/usr/local/openresty/lualib/?.so;;" -TL_OPS_VER="v3.2.0" +TL_OPS_VER="v3.3.0" echo_msg(){ cur_time=$(date "+%Y-%m-%d %H:%M:%S") diff --git a/conf/tl_ops_manage.conf b/conf/tl_ops_manage.conf index 707613c..213fad2 100644 --- a/conf/tl_ops_manage.conf +++ b/conf/tl_ops_manage.conf @@ -66,6 +66,14 @@ server { } } + location = /pageproxy { + set $pageproxy ""; + alias $pageproxy; + rewrite_by_lua_block { + tlops:tl_ops_process_init_rewrite('onlyplugin'); + } + } + location /balanceerr/ { content_by_lua_block { tlops:tl_ops_process_init_content(); diff --git a/constant/tl_ops_constant_balance_api.lua b/constant/tl_ops_constant_balance_api.lua index 56b377a..dc744ee 100644 --- a/constant/tl_ops_constant_balance_api.lua +++ b/constant/tl_ops_constant_balance_api.lua @@ -1,4 +1,5 @@ local match_mode = require("constant.tl_ops_constant_comm").tl_ops_match_mode; +local api_type = require("constant.tl_ops_constant_comm").tl_ops_api_type; -- api 默认列表 local tl_ops_constant_balance_api = { @@ -23,6 +24,7 @@ local tl_ops_constant_balance_api = { host = "tlops1.com", -- 当前url处理的域名范围 rewrite_url = "", -- 当前url重写后的url fake_prefix = "", -- 当前uri规则的虚拟前缀 + api_type = api_type.api, -- 当前uri规则的类型 }, random = { id = 1, @@ -32,6 +34,7 @@ local tl_ops_constant_balance_api = { host = "tlops1.com", -- 当前url处理的域名范围 rewrite_url = "", -- 当前url重写后的url fake_prefix = "", -- 当前uri规则的虚拟前缀 + api_type = api_type.api, -- 当前uri规则的类型 } }, rule = {-- api rule 策略 diff --git a/constant/tl_ops_constant_comm.lua b/constant/tl_ops_constant_comm.lua index 997aeea..2593b60 100644 --- a/constant/tl_ops_constant_comm.lua +++ b/constant/tl_ops_constant_comm.lua @@ -1,14 +1,22 @@ local tl_ops_constant_comm = { tl_ops_status = {-- service status 状态 + -- 在线 online = 0, + -- 下线 offline = 1, + -- 错误 error = 2 }, tl_ops_rt = {-- rt 返回值 + -- 成功 ok = 0, + -- 错误 error = -1, + -- 参数有误 args_error = -2, + -- 找不到 not_found = -3, + -- 解析错误 parse_error = -4 }, tl_ops_match_mode = {-- api匹配模式 @@ -20,6 +28,12 @@ local tl_ops_constant_comm = { regi = "joi", -- 最长字符串匹配 regid = "joid" + }, + tl_ops_api_type = {-- api类型 + -- 接口类型 + api = "api", + -- 文件资源 + page = "page" } }; diff --git a/doc/change.md b/doc/change.md index 791d532..5f93c17 100644 --- a/doc/change.md +++ b/doc/change.md @@ -1,6 +1,17 @@ ## 事务更新日程 +2023-03-05 + + 1. 支持静态资源路由配置 + + 2. 同步补充文档 + + 3. 优化部分代码 + + 4. 事务日程调整 + + 2023-02-27 1. 支持api路由指定虚拟前缀 diff --git a/err/tl_ops_err_content.lua b/err/tl_ops_err_content.lua index 347acd6..d64d77c 100644 --- a/err/tl_ops_err_content.lua +++ b/err/tl_ops_err_content.lua @@ -4,7 +4,7 @@ -- @author iamtsm -- @email 1905333456@qq.com -local tl_ops_err_content_core = require(".err.tl_ops_err_content_core"); +local tl_ops_err_content_core = require("err.tl_ops_err_content_core"); local _M = {} diff --git a/gitbook/SUMMARY.md b/gitbook/SUMMARY.md index cca3ec4..edc480f 100644 --- a/gitbook/SUMMARY.md +++ b/gitbook/SUMMARY.md @@ -49,8 +49,6 @@ * [多规则匹配](code/balance/README_RULE.md) - * [多模式匹配](code/balance/README_RULE.md) - * [路由统计](code/balancecount/README.md) * [动态配置](code/dynamic/README.md) diff --git a/gitbook/code/pageproxy/README.md b/gitbook/code/pageproxy/README.md index f7e362e..6cd2271 100644 --- a/gitbook/code/pageproxy/README.md +++ b/gitbook/code/pageproxy/README.md @@ -30,8 +30,7 @@ location xxx { #### 动态调整路径 -还有一个好处就是支持动态调整静态资源路径,当然,前提是你已经支持了可视化操作修改路径 - +还有一个好处就是支持动态调整静态资源路径,v-3.3.0以上已支持动态配置静态资源路由 ## 实现 diff --git a/gitbook/usage/balance/README_RULE.md b/gitbook/usage/balance/README_RULE.md index 2700fbc..a20bab6 100644 --- a/gitbook/usage/balance/README_RULE.md +++ b/gitbook/usage/balance/README_RULE.md @@ -48,6 +48,28 @@ tl_ops_match_mode = {-- api匹配模式 } ``` +## API路由虚拟前缀 + + ![图片](https://qnproxy.iamtsm.cn/WechatIMG50.png "图片") + +这个场景也是有小伙伴反馈的,目前已支持在路由后将请求转发到具体服务下的具体路径,比如有这样几个规则 + +`/open-api/order-api/*` - `订单服务节点1` + +`/open-api/product-api/*` - `产品服务节点1` + +`/open-api/member-api/*` - `会员服务节点1` + +那么对应的 /open-api 作为虚拟前缀, /order-api/* 作为订单服务下的所有接口,这样可以方便的统一多个服务的对外统一接口 + + +## 静态资源路由 + +目前支持静态文件资源路由配置,需要结合page_proxy插件开启使用 + +首先需要配置好api路由策略,其次在插件配置页面设置好对应路由需要分发的静态资源路径即可。 + + ## 在文件中的配置 在文件中的配置需注意的是,如果`sync`插件为开启状态时,会有后台任务同步文件中的配置数据至store中,且是根据 `id` 来判定是否需要执行同步逻辑。所以需要保证文件中的配置的数据的id是具有唯一性的标识字段。 @@ -67,6 +89,8 @@ point = { node = 0, -- 当前url路由到的service下的node的索引 host = "tlops1.com", -- 当前url处理的域名范围 rewrite_url = "", -- 当前url重写后的url + fake_prefix = "", -- 当前uri规则的虚拟前缀 + api_type = api_type.api, -- 当前uri规则的类型 } }, random = { @@ -77,6 +101,8 @@ random = { service = "tlops-demo", -- 当前url路由到的service host = "tlops1.com", -- 当前url处理的域名范围 rewrite_url = "", -- 当前url重写后的url + fake_prefix = "", -- 当前uri规则的虚拟前缀 + api_type = api_type.api, -- 当前uri规则的类型 } }, ``` diff --git a/plugins/tl_ops_page_proxy/export_set_router.lua b/plugins/tl_ops_page_proxy/export_set_router.lua index fec2c10..63c661a 100644 --- a/plugins/tl_ops_page_proxy/export_set_router.lua +++ b/plugins/tl_ops_page_proxy/export_set_router.lua @@ -1,6 +1,6 @@ -- tl_ops_set_page_proxy_export -- en : set export page_proxy config --- zn : 更新log_analyze插件配置管理 +-- zn : 更新page_proxy插件配置管理 -- @author iamtsm -- @email 1905333456@qq.com diff --git a/plugins/tl_ops_page_proxy/get_router.lua b/plugins/tl_ops_page_proxy/get_router.lua new file mode 100644 index 0000000..c846f11 --- /dev/null +++ b/plugins/tl_ops_page_proxy/get_router.lua @@ -0,0 +1,30 @@ +-- tl_ops_page_proxy_get +-- en : get page_proxy config list +-- zn : 获取page_proxy配置列表 +-- @author iamtsm +-- @email 1905333456@qq.com + +local cache = require("cache.tl_ops_cache_core"):new("tl-ops-page-proxy"); +local constant = require("plugins.tl_ops_page_proxy.tl_ops_plugin_constant"); +local tl_ops_rt = tlops.constant.comm.tl_ops_rt; +local tl_ops_utils_func = tlops.utils +local cjson = require("cjson.safe"); +cjson.encode_empty_table_as_object(false) + + +local Router = function() + + local list_str, _ = cache:get(constant.cache_key.list); + if not list_str or list_str == nil then + tl_ops_utils_func:set_ngx_req_return_ok(tl_ops_rt.not_found, "not found list", _); + return; + end + + + local res_data = {} + res_data[constant.cache_key.list] = cjson.decode(list_str) + + tl_ops_utils_func:set_ngx_req_return_ok(tl_ops_rt.ok, "success", res_data); +end + +return Router \ No newline at end of file diff --git a/plugins/tl_ops_page_proxy/page_proxy.lua b/plugins/tl_ops_page_proxy/page_proxy.lua new file mode 100644 index 0000000..82c6bd2 --- /dev/null +++ b/plugins/tl_ops_page_proxy/page_proxy.lua @@ -0,0 +1,83 @@ +-- tl_ops_page_proxy +-- en : page_proxy +-- zn : 静态页面代理实现 +-- @author iamtsm +-- @email 1905333456@qq.com + +local constant = require("plugins.tl_ops_page_proxy.tl_ops_plugin_constant"); +local cache = require("cache.tl_ops_cache_core"):new("tl-ops-page-proxy"); +local tlog = require("utils.tl_ops_utils_log"):new("tl_ops_plugin_page_proxy"); +local env = tlops.env +local cjson = require("cjson.safe") +cjson.encode_empty_table_as_object(false) + +local _M = { + _VERSION = '0.01', +} +local mt = { __index = _M } + + +-- 加载静态页面路由配置 +local get_page_proxy_cache = function( request_uri, api ) + local list_str, _ = cache:get(constant.cache_key.list); + if not list_str or list_str == nil then + return nil + end + + local list = cjson.decode(list_str) + if not list then + return nil + end + + for i = 1, #list do + local data = list[i] + if ngx.re.find(api, data.api, 'jo') then + return data + end + end + + return nil +end + +-- 核心逻辑 +function _M:page_proxy_core(ctx) + + -- 请求uri + local request_uri = ctx.request_uri + -- 参数 + local args = ngx.req.get_uri_args() + + if ngx.re.find(request_uri, "/tlopsmanage/", 'jo') then + ngx.var.tlopsmanage = env.path.tlopsmanage + return true, "ok" + end + + if ngx.re.find(request_uri, "/website/", 'jo') then + ngx.var.website = env.path.website + return true, "ok" + end + + -- 静态页面代理路径 + if args.url then + local match = get_page_proxy_cache(request_uri, args.url); + if match and match.path then + ngx.var.pageproxy = match.path .. args.url; + tlog:dbg("page_proxy match : ",match, ", args : ", args, ",request_uri : ",request_uri) + else + tlog:dbg("page_proxy not match : ",match, ", args : ", args, ",request_uri : ",request_uri) + ngx.exit(404) + return true, "ok" + end + end + + tlog:dbg("page_proxy plugin ok ") + + return true, "ok" +end + + +function _M:new() + return setmetatable({}, mt) +end + +return _M \ No newline at end of file diff --git a/plugins/tl_ops_page_proxy/set_router.lua b/plugins/tl_ops_page_proxy/set_router.lua new file mode 100644 index 0000000..b3eb913 --- /dev/null +++ b/plugins/tl_ops_page_proxy/set_router.lua @@ -0,0 +1,50 @@ +-- tl_ops_page_proxy_set +-- en : set page_proxy config list +-- zn : 更新page_proxy配置列表 +-- @author iamtsm +-- @email 1905333456@qq.com + +local snowflake = require("lib.snowflake"); +local cache = require("cache.tl_ops_cache_core"):new("tl-ops-page-proxy"); +local constant = require("plugins.tl_ops_page_proxy.tl_ops_plugin_constant"); +local tl_ops_rt = tlops.constant.comm.tl_ops_rt; +local tl_ops_utils_func = tlops.utils +local cjson = require("cjson.safe"); +cjson.encode_empty_table_as_object(false) + + +local Router = function() + + local list, _ = tl_ops_utils_func:get_req_post_args_by_name(constant.cache_key.list, 1); + if not list or list == nil then + tl_ops_utils_func:set_ngx_req_return_ok(tl_ops_rt.args_error ,"page_proxy args err1", _); + return; + end + + -- 更新生成id + for _, page in ipairs(list) do + if not page.id or page.id == nil or page.id == '' then + page.id = snowflake.generate_id( 100 ) + end + if not page.updatetime or page.updatetime == nil or page.updatetime == '' then + page.updatetime = ngx.localtime() + end + if page.change and page.change == true then + page.updatetime = ngx.localtime() + page.change = nil + end + end + + local cache_list, _ = cache:set(constant.cache_key.list, cjson.encode(list)); + if not cache_list then + tl_ops_utils_func:set_ngx_req_return_ok(tl_ops_rt.error, "set list err ", _) + return; + end + + local res_data = {} + res_data[constant.cache_key.list] = list + + tl_ops_utils_func:set_ngx_req_return_ok(tl_ops_rt.ok, "ok", res_data) + end + +return Router diff --git a/plugins/tl_ops_page_proxy/sync.lua b/plugins/tl_ops_page_proxy/sync.lua index b788ee7..a4b8878 100644 --- a/plugins/tl_ops_page_proxy/sync.lua +++ b/plugins/tl_ops_page_proxy/sync.lua @@ -1,24 +1,139 @@ -- tl_ops_page_proxy_sync --- en : sync page_proxy config --- zn : 同步、预热日志分析相关数据 --- @corsor iamtsm +-- en : sync page_proxy config list +-- zn : 同步、预热page_proxy数据 +-- @author iamtsm -- @email 1905333456@qq.com -local tlog = require("utils.tl_ops_utils_log"):new("tl_ops_plugin_page_proxy") -local cache = require("cache.tl_ops_cache_core"):new("tl-ops-page-proxy") -local constant = require("plugins.tl_ops_page_proxy.tl_ops_plugin_constant") -local tl_ops_rt = tlops.constant.comm.tl_ops_rt -local cjson = require("cjson.safe") +local tlog = require("utils.tl_ops_utils_log"):new("tl_ops_plugin_page_proxy") +local cache = require("cache.tl_ops_cache_core"):new("tl-ops-page-proxy") +local constant = require("plugins.tl_ops_page_proxy.tl_ops_plugin_constant") +local tl_ops_rt = tlops.constant.comm.tl_ops_rt +local cjson = require("cjson.safe") cjson.encode_empty_table_as_object(false) --- 同步静态数据 -local sync_data = function() +-- 静态文件中的未同步到store的配置数据 +local sync_data_need_sync = function (constant_data, store_data) + local add = {} + for i = 1, #constant_data do + local synced = false + for j = 1, #store_data do + if constant_data[i]['id'] == store_data[j]['id'] then + synced = true + break + end + end + if not synced then + table.insert(add, constant_data[i]) + end + end + return add +end + + +-- 静态配置数据同步 +local sync_data = function () + local cache_key_list = constant.cache_key.list + + local data_str, _ = cache:get(cache_key_list); + if not data_str then + local res, _ = cache:set(cache_key_list, cjson.encode(constant.list)) + if not res then + tlog:err("page_proxy sync_data new store data err, res=",res) + return tl_ops_rt.error + end + + tlog:dbg("page_proxy sync_data new store data, res=",res) + return tl_ops_rt.ok + end + + local data = cjson.decode(data_str); + if not data and type(data) ~= 'table' then + tlog:err("page_proxy sync_data err, old=",data) + return tl_ops_rt.error + end + + tlog:dbg("page_proxy sync_data start, old=",data) + + -- 静态配置 + local constant_data = constant.list + + -- 获取需要同步的配置 + local add = sync_data_need_sync(constant_data, data) + for i = 1, #add do + table.insert(data, add[i]) + end + + local res = cache:set(cache_key_list, cjson.encode(data)) + if not res then + tlog:err("page_proxy sync_data err, res=",res,",new=",data) + return tl_ops_rt.error + end + + tlog:dbg("page_proxy sync_data done, new=",data) + + return tl_ops_rt.ok +end + + + + +-- 字段数据同步 +local sync_fields_list = function () + local cache_key_list = constant.cache_key.list; + + local demo = constant.demo + + local data_str, _ = cache:get(cache_key_list); + if not data_str then + local res, _ = cache:set(cache_key_list, cjson.encode(constant.list)) + if not res then + tlog:err("page_proxy sync_fields new store data err, res=",res) + return tl_ops_rt.error + end + + data_str, _ = cache:get(cache_key_list) + + tlog:dbg("page_proxy sync_fields new store data, res=",res) + end + + local data = cjson.decode(data_str); + if not data and type(data) ~= 'table' then + tlog:err("page_proxy sync_fields err, old=",data) + return tl_ops_rt.error + end + + tlog:dbg("page_proxy sync_fields start, old=",data) + + local add_keys = {} + + -- demo fileds check + for key , _ in pairs(demo) do + -- data fileds check + for i = 1, #data do + -- add keys + if data[i][key] == nil then + data[i][key] = demo[key] + table.insert(add_keys , { + key = data[i][key] + }) + end + end + end + + local res = cache:set(cache_key_list, cjson.encode(data)) + if not res then + tlog:err("page_proxy sync_fields err, res=",res,",new=",data) + return tl_ops_rt.error + end + + tlog:dbg("page_proxy sync_fields done, new=",data,",add_keys=",add_keys) return tl_ops_rt.ok end + -- 同步插件对外数据 local sync_fields_export = function () local cache_key = constant.export.cache_key.page_proxy @@ -70,18 +185,14 @@ local sync_fields_export = function () end +local sync_fields = function() + sync_fields_list() --- 字段数据同步 -local sync_fields = function () - - -- 对外配置 数据 sync_fields_export() return tl_ops_rt.ok end - - return { sync_data = sync_data, sync_fields = sync_fields diff --git a/plugins/tl_ops_page_proxy/tl_ops_plugin_api.lua b/plugins/tl_ops_page_proxy/tl_ops_plugin_api.lua index 056503a..945da88 100644 --- a/plugins/tl_ops_page_proxy/tl_ops_plugin_api.lua +++ b/plugins/tl_ops_page_proxy/tl_ops_plugin_api.lua @@ -7,10 +7,17 @@ local constant = require("plugins.tl_ops_page_proxy.tl_ops_plugin_constant") local export_get_router = require("plugins.tl_ops_page_proxy.export_get_router") local export_set_router = require("plugins.tl_ops_page_proxy.export_set_router") +local set_router = require("plugins.tl_ops_page_proxy.set_router"); +local get_router = require("plugins.tl_ops_page_proxy.get_router"); -- 插件管理对外管理接口 return function(ctx) + -- 插件 + ctx.tlops_api[constant.tlops_api.get] = get_router + + ctx.tlops_api[constant.tlops_api.set] = set_router + -- 插件管理对外管理接口 ctx.tlops_api[constant.export.tlops_api.get] = export_get_router diff --git a/plugins/tl_ops_page_proxy/tl_ops_plugin_constant.lua b/plugins/tl_ops_page_proxy/tl_ops_plugin_constant.lua index 1efcc36..6be22e3 100644 --- a/plugins/tl_ops_page_proxy/tl_ops_plugin_constant.lua +++ b/plugins/tl_ops_page_proxy/tl_ops_plugin_constant.lua @@ -1,6 +1,18 @@ local tl_ops_plugin_constant_page_proxy = { cache_key = { + list = "tl_ops_plugin_page_proxy_list" + }, + tlops_api = { -- 对外API + get = "/tlops/page_proxy/list", + set = "/tlops/page_proxy/set" + }, + list = { + }, + demo = { + id = 1, + api = "", -- 对应的api路由路径,在api路由规则中定义的 + path = "", -- 路由对应的静态资源的路径 }, export = { cache_key = { @@ -12,7 +24,7 @@ local tl_ops_plugin_constant_page_proxy = { }, page_proxy = { zname = '页面代理插件', - page = "", + page = "page_proxy/tl_ops_web_page_proxy.html", name = 'page_proxy', open = true, scope = "tl_ops_process_before_init_rewrite", diff --git a/plugins/tl_ops_page_proxy/tl_ops_plugin_core.lua b/plugins/tl_ops_page_proxy/tl_ops_plugin_core.lua index 5287e11..2a056c1 100644 --- a/plugins/tl_ops_page_proxy/tl_ops_plugin_core.lua +++ b/plugins/tl_ops_page_proxy/tl_ops_plugin_core.lua @@ -4,9 +4,8 @@ -- @author iamtsm -- @email 1905333456@qq.com -local utils = tlops.utils -local env = tlops.env local sync = require("plugins.tl_ops_page_proxy.sync"); +local page_proxy = require("plugins.tl_ops_page_proxy.page_proxy"); local _M = { _VERSION = '0.01' @@ -25,18 +24,7 @@ end -- 页面转发插件实现 function _M:tl_ops_process_before_init_rewrite(ctx) - - local request_uri = utils:get_req_uri() - - if ngx.re.find(request_uri, "/tlopsmanage/", 'jo') then - ngx.var.tlopsmanage = env.path.tlopsmanage - end - - if ngx.re.find(request_uri, "/website/", 'jo') then - ngx.var.website = env.path.website - end - - return true, "ok" + return page_proxy:page_proxy_core(ctx) end diff --git a/plugins/tl_ops_ssl/get_router.lua b/plugins/tl_ops_ssl/get_router.lua index ef06a1d..4ea0206 100644 --- a/plugins/tl_ops_ssl/get_router.lua +++ b/plugins/tl_ops_ssl/get_router.lua @@ -5,7 +5,7 @@ -- @email 1905333456@qq.com local cache = require("cache.tl_ops_cache_core"):new("tl-ops-ssl"); -local constant_ssl = require("plugins.tl_ops_ssl.tl_ops_plugin_constant"); +local constant = require("plugins.tl_ops_ssl.tl_ops_plugin_constant"); local tl_ops_rt = tlops.constant.comm.tl_ops_rt; local tl_ops_utils_func = tlops.utils local cjson = require("cjson.safe"); @@ -14,7 +14,7 @@ cjson.encode_empty_table_as_object(false) local Router = function() - local list_str, _ = cache:get(constant_ssl.cache_key.list); + local list_str, _ = cache:get(constant.cache_key.list); if not list_str or list_str == nil then tl_ops_utils_func:set_ngx_req_return_ok(tl_ops_rt.not_found, "not found list", _); return; @@ -22,7 +22,7 @@ local Router = function() local res_data = {} - res_data[constant_ssl.cache_key.list] = cjson.decode(list_str) + res_data[constant.cache_key.list] = cjson.decode(list_str) tl_ops_utils_func:set_ngx_req_return_ok(tl_ops_rt.ok, "success", res_data); end diff --git a/plugins/tl_ops_ssl/set_router.lua b/plugins/tl_ops_ssl/set_router.lua index f84c131..e292245 100644 --- a/plugins/tl_ops_ssl/set_router.lua +++ b/plugins/tl_ops_ssl/set_router.lua @@ -6,7 +6,7 @@ local snowflake = require("lib.snowflake"); local cache = require("cache.tl_ops_cache_core"):new("tl-ops-ssl"); -local constant_ssl = require("plugins.tl_ops_ssl.tl_ops_plugin_constant"); +local constant = require("plugins.tl_ops_ssl.tl_ops_plugin_constant"); local tl_ops_rt = tlops.constant.comm.tl_ops_rt; local tl_ops_utils_func = tlops.utils local cjson = require("cjson.safe"); @@ -15,7 +15,7 @@ cjson.encode_empty_table_as_object(false) local Router = function() - local list, _ = tl_ops_utils_func:get_req_post_args_by_name(constant_ssl.cache_key.list, 1); + local list, _ = tl_ops_utils_func:get_req_post_args_by_name(constant.cache_key.list, 1); if not list or list == nil then tl_ops_utils_func:set_ngx_req_return_ok(tl_ops_rt.args_error ,"ssl args err1", _); return; @@ -35,14 +35,14 @@ local Router = function() end end - local cache_list, _ = cache:set(constant_ssl.cache_key.list, cjson.encode(list)); + local cache_list, _ = cache:set(constant.cache_key.list, cjson.encode(list)); if not cache_list then tl_ops_utils_func:set_ngx_req_return_ok(tl_ops_rt.error, "set list err ", _) return; end local res_data = {} - res_data[constant_ssl.cache_key.list] = list + res_data[constant.cache_key.list] = list tl_ops_utils_func:set_ngx_req_return_ok(tl_ops_rt.ok, "ok", res_data) end diff --git a/plugins/tl_ops_ssl/ssl.lua b/plugins/tl_ops_ssl/ssl.lua index e1eeb65..54a3e2a 100644 --- a/plugins/tl_ops_ssl/ssl.lua +++ b/plugins/tl_ops_ssl/ssl.lua @@ -4,9 +4,9 @@ -- @author iamtsm -- @email 1905333456@qq.com -local constant_ssl = require("plugins.tl_ops_ssl.tl_ops_plugin_constant"); +local constant = require("plugins.tl_ops_ssl.tl_ops_plugin_constant"); local tlog = require("utils.tl_ops_utils_log"):new("tl_ops_plugin_ssl"); -local cache_ssl = require("cache.tl_ops_cache_core"):new("tl-ops-ssl"); +local cache = require("cache.tl_ops_cache_core"):new("tl-ops-ssl"); local utils = tlops.utils local ssl = require("ngx.ssl") local cjson = require("cjson.safe") @@ -21,7 +21,7 @@ local mt = { __index = _M } -- 加载host的证书配置 local get_pem_key_cache = function(host) - local list_str, _ = cache_ssl:get(constant_ssl.cache_key.list); + local list_str, _ = cache:get(constant.cache_key.list); if not list_str or list_str == nil then return nil end diff --git a/tl_ops_manage.lua b/tl_ops_manage.lua index 8a24dce..cd67893 100644 --- a/tl_ops_manage.lua +++ b/tl_ops_manage.lua @@ -19,7 +19,6 @@ local utils = require("utils.tl_ops_utils_func"); local env = require("tl_ops_manage_env") local constant = require("constant.tl_ops_constant") local cache = require("cache.tl_ops_cache") -local tlops_api = require("api.tl_ops_api_core") local m_err_content = require("err.tl_ops_err_content") local balance_shared = ngx.shared.tlopsbalance local plugin_shared = ngx.shared.tlopsplugin diff --git a/web/balance/tl_ops_web_api.js b/web/balance/tl_ops_web_api.js index f2aac66..dd4c1d6 100644 --- a/web/balance/tl_ops_web_api.js +++ b/web/balance/tl_ops_web_api.js @@ -49,7 +49,9 @@ const tl_ops_web_api_point_cols = function () { { type:'checkbox',fixed : 'left', width: "5%" }, { - field: 'id', title: 'ID',width:"10%" + field: 'id', title: 'ID',width:"5%" + }, { + field: 'api_type', title: '类型',width:"8%" }, { field: 'host', title: '域名',width:"10%" }, { @@ -57,13 +59,13 @@ const tl_ops_web_api_point_cols = function () { }, { field: 'match_mode', title: '匹配模式',width:"10%" }, { - field: 'service', title: '所属服务',width:"10%" + field: 'service', title: '服务',width:"7%" }, { - field: 'node', title: '节点索引',width:"5%" + field: 'node', title: '节点',width:"5%" }, { field: 'rewrite_url', title: '重写地址',width:"10%" }, { - field: 'fake_prefix', title: '节点路径',width:"10%" + field: 'fake_prefix', title: '虚拟前缀',width:"10%" }, { field: 'updatetime', title: '更新时间',width:"10%", }, { @@ -82,7 +84,9 @@ const tl_ops_web_api_random_cols = function () { { type:'checkbox',fixed : 'left', width: "5%" }, { - field: 'id', title: 'ID', width:"10%" + field: 'id', title: 'ID', width:"5%" + }, { + field: 'api_type', title: '类型',width:"7%" }, { field: 'host', title: '域名',width:"10%" }, { @@ -90,11 +94,11 @@ const tl_ops_web_api_random_cols = function () { }, { field: 'match_mode', title: '匹配模式',width:"10%" }, { - field: 'service', title: '所属服务',width:"10%" + field: 'service', title: '服务',width:"8%" }, { field: 'rewrite_url', title: '重写地址',width:"10%" },{ - field: 'fake_prefix', title: '节点路径',width:"10%" + field: 'fake_prefix', title: '虚拟前缀',width:"10%" }, { field: 'updatetime', title: '更新时间',width:"10%", }, { diff --git a/web/balance/tl_ops_web_api_form.html b/web/balance/tl_ops_web_api_form.html index a279815..b749213 100644 --- a/web/balance/tl_ops_web_api_form.html +++ b/web/balance/tl_ops_web_api_form.html @@ -30,14 +30,12 @@ margin-top: 10px; text-overflow: ellipsis; overflow: hidden; - padding-left: 10px; color: #918576; font-weight: bold; - padding-right: 10px; } #url{ width: 100%; - min-width: 60%; + margin-left: 1%; } @@ -49,6 +47,13 @@ +
+ +
+ API接口 + 静态资源 +
+
diff --git a/web/lib/axios.js b/web/lib/axios.js index 8d6b7d6..a221dc7 100644 --- a/web/lib/axios.js +++ b/web/lib/axios.js @@ -633,5 +633,4 @@ } } }]) -}); -//# sourceMappingURL=axios.min.map +}); \ No newline at end of file diff --git a/web/plugins/page_proxy/tl_ops_web_page_proxy.html b/web/plugins/page_proxy/tl_ops_web_page_proxy.html new file mode 100644 index 0000000..b389c18 --- /dev/null +++ b/web/plugins/page_proxy/tl_ops_web_page_proxy.html @@ -0,0 +1,42 @@ + + + + + tl-ops-manage后台 + + + + + + + + + +
+
+
+
+ + +
+ +
+ +
+
+
+ + + + + + diff --git a/web/plugins/page_proxy/tl_ops_web_page_proxy.js b/web/plugins/page_proxy/tl_ops_web_page_proxy.js new file mode 100644 index 0000000..9935668 --- /dev/null +++ b/web/plugins/page_proxy/tl_ops_web_page_proxy.js @@ -0,0 +1,311 @@ +const _table_id_name = "tl-ops-web-page-proxy-table"; +const _search_id_name = "tl-ops-web-page-proxy-search"; +const _add_form_btn_id_name = "tl-ops-web-page-proxy-form-submit"; +const _add_form_id_name = "tl-ops-web-page-proxy-form"; + +let res_data = {}; + +const tl_ops_web_page_proxy_main = async function (){ + window.$ = layui.$; + window.form = layui.form; + window.table = layui.table; + window.layedit = layui.layedit; + + tl_ops_web_page_proxy_render(); + + //事件操作 + $('.layui-btn.layuiadmin-btn-useradmin').on('click', function(){ + let type = $(this).data('type'); + tl_ops_web_page_proxy_event()[type] ? tl_ops_web_page_proxy_event()[type].call(this) : ''; + }); + + //搜索 + form.on('submit('+_search_id_name+')', function(data){ + tl_ops_web_page_proxy_reload(data.field); + }); + + //行事件操作 + table.on('tool('+_table_id_name+')', function(obj) { + let type = obj.event; + let data = obj.data; + tl_ops_web_page_proxy_event()[type] ? tl_ops_web_page_proxy_event()[type].call(this, data) : ''; + }); +}; + +//事件监听定义 +const tl_ops_web_page_proxy_event = function () { + return { + add: tl_ops_web_page_proxy_add, + edit : tl_ops_web_page_proxy_edit, + delete : tl_ops_web_page_proxy_delete, + } +}; + +//表格cols +const tl_ops_web_page_proxy_cols = function () { + return [[ + { + type:'checkbox',fixed : 'left', width: "10%" + }, { + field: 'id', title: 'ID',width:"15%" + }, { + field: 'api', title: 'API',width:"25%" + }, { + field: 'path', title: '资源路径', width:"20%" + }, { + field: 'updatetime', title: '更新时间',width:"15%", + }, { + width: "15%", + align: 'center', + fixed: 'right', + title: '操作', + toolbar: '#tl-ops-web-page-proxy-operate' + } + ]]; +}; + + +//表格render +const tl_ops_web_page_proxy_render = function () { + table.render(tl_ajax_data({ + elem: '#'+_table_id_name, + url: '/tlops/page_proxy/list', + cols: tl_ops_web_page_proxy_cols(), + page:true, + needReloadMsg : false, + toolbar: '#tl-ops-web-page-proxy-toolbar', + defaultToolbar: ['filter', 'print', 'exports'], + totalRow: true, //开启合计行 + parseData: function(res){ + if (res.code !== 0){ + return { + "code": res.code, + "msg": res.msg, + "count": 0, + "data": [] + }; + } + res_data = res.data; + let datas = res_data.tl_ops_plugin_page_proxy_list; + if (datas === undefined){ datas = []; } + datas = datas.sort(function(a, b){return b.id - a.id}) + return { + "code": res.code, + "msg": res.msg, + "count": datas.length, + "data": datas + }; + } + })); +}; + +//表格reload +const tl_ops_web_page_proxy_reload = function (matcher) { + table.render(tl_ajax_data({ + elem: '#'+_table_id_name, + url: '/tlops/page_proxy/list', + where : matcher, + cols: tl_ops_web_page_proxy_cols(), + page:true, + needReloadMsg : false, + toolbar: '#tl-ops-web-page-proxy-toolbar', + defaultToolbar: ['filter', 'print', 'exports'], + totalRow: true, //开启合计行 + parseData: function(res){ + if (res.code !== 0){ + return { + "code": res.code, + "msg": res.msg, + "count": 0, + "data": [] + }; + } + res_data = res.data; + let datas = res_data.tl_ops_plugin_page_proxy_list; + if (datas === undefined){ datas = []; } + datas = datas.sort(function(a, b){return b.id - a.id}) + return { + "code": res.code, + "msg": res.msg, + "count": datas.length, + "data": datas + }; + } + })); +}; + + +//删除ssl +const tl_ops_web_page_proxy_delete = function () { + let checkStatus = table.checkStatus(_table_id_name) + ,checkData = checkStatus.data; //得到选中的数据 + + if(checkData.length === 0){ + layer.msg('请选删除择数据'); + return; + } + + let idList = []; + for(let i = 0; i < checkData.length; i++){ + idList.push(checkData[i].id); + } + + let new_list = res_data.tl_ops_plugin_page_proxy_list.filter(item=>{ + return !idList.includes(item.id); + }) + + $.ajax(tl_ajax_data({ + url: '/tlops/page_proxy/set', + data : JSON.stringify({ + tl_ops_plugin_page_proxy_list : new_list + }), + contentType : "application/json", + success : (res)=>{ + tl_ops_web_page_proxy_reload() + } + })); +} + + +//添加 +const tl_ops_web_page_proxy_add = function () { + let index = layer.open({ + type: 2 + ,title: '添加静态页面代理' + ,content: 'tl_ops_web_page_proxy_form.html' + ,maxmin: true + ,minStack:false + ,area: ['700px', '600px'] + ,btn: ['确定', '取消'] + ,yes: function(sub_index, layero){ + let iframeWindow = window['layui-layer-iframe'+ sub_index] + ,submit = layero.find('iframe').contents().find('#'+ _add_form_btn_id_name); + + iframeWindow.layui.form.on('submit('+ _add_form_btn_id_name +')', function(data){ + if(!tl_ops_page_proxy_data_add_filter(data)){ + return; + } + $.ajax(tl_ajax_data({ + url: '/tlops/page_proxy/set', + data : JSON.stringify({ + tl_ops_plugin_page_proxy_list : res_data.tl_ops_plugin_page_proxy_list + }), + contentType : "application/json", + success : (res)=>{ + tl_ops_web_page_proxy_reload() + } + })); + layer.close(sub_index); + }); + submit.trigger('click'); + } + }); + layer.full(index) +}; + + +//编辑 +const tl_ops_web_page_proxy_edit = function (evtdata) { + let index = layer.open({ + type: 2 + ,title: '编辑静态页面代理' + ,content: 'tl_ops_web_page_proxy_form.html' + ,maxmin: true + ,minStack:false + ,area: ['700px', '600px'] + ,btn: ['确定', '取消'] + ,yes: function(sub_index, dom){ + let iframeWindow = window['layui-layer-iframe'+ sub_index] + ,submit = dom.find('iframe').contents().find('#'+ _add_form_btn_id_name); + iframeWindow.layui.form.on('submit('+ _add_form_btn_id_name +')', function(data){ + if(!tl_ops_page_proxy_data_edit_filter(data)){ + return; + } + $.ajax(tl_ajax_data({ + url: '/tlops/page_proxy/set', + data : JSON.stringify({ + tl_ops_plugin_page_proxy_list : res_data.tl_ops_plugin_page_proxy_list + }), + contentType : "application/json", + success : (res)=>{ + tl_ops_web_page_proxy_reload() + } + })); + layer.close(sub_index); + }); + submit.trigger('click'); + }, + success: function(dom, index) { + let editForm = dom.find('iframe')[0].contentWindow; + // 延时执行,编辑页面存在同步请求,执行较慢 + if(!editForm.tl_ops_web_page_proxy_form_render){ + setTimeout(() => { + let editForm = dom.find('iframe')[0].contentWindow; + editForm.tl_ops_web_page_proxy_form_render(evtdata); + }, 500); + return + } + editForm.tl_ops_web_page_proxy_form_render(evtdata); + }, + }); + layer.full(index) +}; + + +//过滤新增数据 +const tl_ops_page_proxy_data_add_filter = function( data ) { + delete data.field.file; + for(let key in data.field){ + if(key === 'id'){ + continue; + } + if(data.field[key] === undefined || data.field[key] === null || data.field[key] === ''){ + layer.msg(key + "未填写") + return false; + } + } + res_data.tl_ops_plugin_page_proxy_list.push(data.field) + + res_data.tl_ops_plugin_page_proxy_list.forEach(item=>{ + if( item.LAY_TABLE_INDEX !== undefined){ + delete item.LAY_TABLE_INDEX + } + if( item.LAY_CHECKED !== undefined){ + delete item.LAY_CHECKED + } + }) + + return true +} + + +//过滤编辑数据 +const tl_ops_page_proxy_data_edit_filter = function( data ) { + delete data.field.file; + for(let key in data.field){ + if(data.field[key] === undefined || data.field[key] === null || data.field[key] === ''){ + layer.msg(key + "未填写") + return false; + } + } + let cur_list = [] + res_data.tl_ops_plugin_page_proxy_list.forEach((item)=>{ + if(parseInt(item.id) === parseInt(data.field.id)){ + data.field.change = true; + item = data.field; + } + cur_list.push(item) + }) + res_data.tl_ops_plugin_page_proxy_list = cur_list; + + res_data.tl_ops_plugin_page_proxy_list.forEach(item=>{ + if( item.LAY_TABLE_INDEX !== undefined){ + delete item.LAY_TABLE_INDEX + } + if( item.LAY_CHECKED !== undefined){ + delete item.LAY_CHECKED + } + }) + + return true +} \ No newline at end of file diff --git a/web/plugins/page_proxy/tl_ops_web_page_proxy_form.html b/web/plugins/page_proxy/tl_ops_web_page_proxy_form.html new file mode 100644 index 0000000..aaf5d6a --- /dev/null +++ b/web/plugins/page_proxy/tl_ops_web_page_proxy_form.html @@ -0,0 +1,101 @@ + + + + + + tl-spb-blog-后台添加配置 + + + + + + + + + + +
+ +
+ + +
+ +
+ +
+
+
+ +
+
+ + + + + + \ No newline at end of file diff --git a/web/tl_ops_web_index.html b/web/tl_ops_web_index.html index c9bd6aa..ff118e7 100644 --- a/web/tl_ops_web_index.html +++ b/web/tl_ops_web_index.html @@ -196,7 +196,7 @@ -
+
Copyright @ 2021 iamtsm