local BasePlugin = require "kong.plugins.base_plugin"
-- The actual logic is implemented in those modules local access = require "kong.plugins.my-custom-plugin.access" local body_filter = require "kong.plugins.my-custom-plugin.body_filter"
local CustomHandler = BasePlugin:extend()
function CustomHandler:new() CustomHandler.super.new(self, "my-custom-plugin") end
function CustomHandler:access(config) CustomHandler.super.access(self)
-- Execute any function from the module loaded in `access`, -- for example, `execute()` and passing it the plugin's configuration. access.execute(config) end
function CustomHandler:body_filter(config) CustomHandler.super.body_filter(self)
-- Execute any function from the module loaded in `body_filter`, -- for example, `execute()` and passing it the plugin's configuration. body_filter.execute(config) end
return { no_consumer = true, -- this plugin will only be applied to Services or Routes, fields = { -- Describe your plugin's configuration's schema here. }, self_check = function(schema, plugin_t, dao, is_updating) -- perform any custom verification return true end }
local BasePlugin = require "kong.plugins.base_plugin" local plugin = BasePlugin:extend()
-- 插件构造函数 function plugin:new() plugin.super.new(self, "first") end
function split(input, delimiter) input = tostring(input) delimiter = tostring(delimiter) if (delimiter=='') then return false end local pos,arr = 0, {} for st,sp in function() return string.find(input, delimiter, pos, true) end do table.insert(arr, string.sub(input, pos, st - 1)) pos = sp + 1 end table.insert(arr, string.sub(input, pos)) return arr end
function plugin:access(plugin_conf) plugin.super.access(self) local socket = require('socket') local host = kong.request.get_host() local hostName = socket.dns.gethostname() --本机名 local list = split(hostName,'.') local hostroom = list[3] local ok, err = kong.service.set_upstream(hostroom) if not ok then kong.log.err(err) return end end return plugin