Divide the MSB source codes into two repos
[msb/apigateway.git] / openresty-ext / src / assembly / resources / openresty / nginx / luaext / msb.lua
1 --[[\r
2 \r
3     Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)\r
4 \r
5     Licensed under the Apache License, Version 2.0 (the "License");\r
6     you may not use this file except in compliance with the License.\r
7     You may obtain a copy of the License at\r
8 \r
9             http://www.apache.org/licenses/LICENSE-2.0\r
10 \r
11     Unless required by applicable law or agreed to in writing, software\r
12     distributed under the License is distributed on an "AS IS" BASIS,\r
13     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14     See the License for the specific language governing permissions and\r
15     limitations under the License.\r
16 \r
17 --]]\r
18 local _M = {\r
19 }\r
20 _M._VERSION = '1.0.0'\r
21 _M._DESCRIPTION = 'msb plugins controller'\r
22 \r
23 local default_conf   =  require('plugins.config_default')\r
24 local custom_conf   =  require('plugins.config_custom')\r
25 local table_insert = table.insert\r
26 local string_find = string.find\r
27 local str_low = string.lower\r
28 \r
29 --- Borrowed from Kong\r
30 --- Try to load a module.\r
31 -- Will not throw an error if the module was not found, but will throw an error if the\r
32 -- loading failed for another reason (eg: syntax error).\r
33 -- @param module_name Path of the module to load (ex: kong.plugins.keyauth.api).\r
34 -- @return success A boolean indicating wether the module was found.\r
35 -- @return module The retrieved module.\r
36 local load_module_if_exists = function(module_name)\r
37         local status, res = pcall(require, module_name)\r
38         if status then\r
39                 return true, res\r
40                 -- Here we match any character because if a module has a dash '-' in its name, we would need to escape it.\r
41         elseif type(res) == "string" and string_find(res, "module '"..module_name.."' not found", nil, true) then\r
42                 return false\r
43         else\r
44                 error(res)\r
45         end\r
46 end\r
47 \r
48 \r
49 function _M.load_plugins()\r
50         local pluginnames = {}\r
51         for _, plugin in ipairs(default_conf.plugins_default) do\r
52                 if(str_low(plugin.status) =="on") then\r
53                         table_insert(pluginnames,plugin.name)\r
54                 end\r
55         end\r
56         for _, plugin in ipairs(custom_conf.plugins_custom) do\r
57                 if(str_low(plugin.status) =="on") then\r
58                         table_insert(pluginnames,plugin.name)\r
59                 end\r
60         end\r
61         local plugins = {}\r
62         for _, v in ipairs(pluginnames) do\r
63                 local loaded, plugin_handler_mod = load_module_if_exists("plugins."..v..".handler")\r
64                 if not loaded then\r
65                         error("The following plugin has been enabled in the configuration but it is not installed on the system: "..v)\r
66                 end\r
67                 ngx.log(ngx.DEBUG, "Loading plugin: "..v)\r
68                 table_insert(plugins, {\r
69                                 name = v,\r
70                                 handler = plugin_handler_mod()\r
71                         })\r
72         end\r
73         package.loaded.plugins = plugins\r
74 end\r
75 \r
76 function _M.access()\r
77         local plugins = package.loaded.plugins\r
78         for _, plugin in ipairs(plugins) do\r
79                 plugin.handler:access()\r
80         end\r
81 end\r
82 \r
83 function _M.header_filter()\r
84         local plugins = package.loaded.plugins\r
85         for _, plugin in ipairs(plugins) do\r
86                 plugin.handler:header_filter()\r
87         end\r
88 end\r
89 \r
90 return _M