基本概念
服务端状态码参照
链接
加载方式
plugins = bundled,my-custom-plugin # your plugin name here
插件组成
插件由两个必需模块组成:
simple-plugin
├── handler.lua
└── schema.lua
handler.lua
是插件的核心,它是一个需要实现的接口,其各功能都将在请求/连接生命周期中运行。
schema.lua
:由于插件可能必须保留用户输入的一些配置。此模块保存该配置。
进阶:
complete-plugin
├── api.lua
├── daos.lua
├── handler.lua
├── migrations
│ ├── init.lua
│ └── 000_base_complete_plugin.lua
└── schema.lua
handler.lua
handler.lua
文件中定义以下任何函数,将可以在Kong Gateway(OSS)执行生命周期的各个入口点实现自定义逻辑。
下面的示例handler.lua
文件定义了http和流通信中所有可能阶段的自定义函数。
两个需要注意的字段:
VERSION
是一个信息字段,不直接由Kong Gateway(OSS)使用。它通常与插件的Rockspec版本中定义的版本相匹配(如果存在)。
PRIORITY
用于在执行插件的每个阶段之前对插件进行排序,优先级较高的插件首先执行。
local CustomHandler = {
VERSION = "1.0.0"
PRIORITY = 10
}
function CustomHandler:init_worker()
-- Implement logic for the init_worker phase here (http/stream)
kong.log("init_worker")
end
function CustomHandler:preread(config)
-- Implement logic for the preread phase here (stream)
kong.log("preread")
end
function CustomHandler:certificate(config)
-- Implement logic for the certificate phase here (http/stream)
kong.log("certificate")
end
function CustomHandler:rewrite(config)
-- Implement logic for the rewrite phase here (http)
kong.log("rewrite")
end
function CustomHandler:access(config)
-- Implement logic for the rewrite phase here (http)
kong.log("access")
end
function CustomHandler:header_filter(config)
-- Implement logic for the header_filter phase here (http)
kong.log("header_filter")
end
function CustomHandler:body_filter(config)
-- Implement logic for the body_filter phase here (http)
kong.log("body_filter")
end
function CustomHandler:log(config)
-- Implement logic for the log phase here (http/stream)
kong.log("log")
end
-- return the created table, so that Kong can execute it
return CustomHandler
schema.lua
kong官方地址
主要用于配置参数
一些继承的默认field
示例
-- schema.lua
local typedefs = require "kong.db.schema.typedefs"
return {
name = "key-auth",
fields = {
{
consumer = typedefs.no_consumer
},
{
protocols = typedefs.protocols_http
},
{
config = {
type = "record",
fields = {
{
key_names = {
type = "array",
required = true,
elements = typedefs.header_name,
default = {
"apikey",
},
},
},
{
hide_credentials = {
type = "boolean",
default = false,
},
},
{
anonymous = {
type = "string",
uuid = true,
legacy = true,
},
},
{
key_in_body = {
type = "boolean",
default = false,
},
},
{
run_on_preflight = {
type = "boolean",
default = true,
},
},
},
},
},
},
}