Kong Plugin Development

오픈소스·2020년 6월 21일
1

Kong API Gateway

목록 보기
2/2
post-thumbnail

Plugin Development Guide

https://docs.konghq.com/2.0.x/plugin-development/

File Structure

<plugin-name>
├── INSTALL.txt
├── README.md
├── kong
│   └── plugins
│       └── <plugin-name>
│           ├── handler.lua
│           └── schema.lua
└── <plugin-name>-<version>.rockspec

schema.lua

Property nameLua type
namestring
fieldstable
entity_checksfunction
local typedefs = require "kong.db.schema.typedefs"

return {
  name = "<plugin-name>",
  fields = {
    {
      -- this plugin will only be applied to Services or Routes
      consumer = typedefs.no_consumer
    },
    {
      -- this plugin will only run within Nginx HTTP module
      protocols = typedefs.protocols_http
    },
    {
      config = {
        type = "record",
        fields = {
          -- Describe your plugin's configuration's schema here.        
        },
      },
    },
  },
  entity_checks = {
    -- Describe your plugin's entity validation rules
  },
}

handler.lua

Function name
:init_worker()
:certificate()
:rewrite()
:access()
:header_filter()
:body_filter()
:log()
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()

CustomHandler.VERSION  = "1.0.0"
CustomHandler.PRIORITY = 10 

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 CustomHandler

Installing the plugin

$ cd <plugin-name>
$ luarocks make

Load the plugin

$ vi /etc/kong/kong.conf
plugins = bundled,<plugin-name>
$ kong restart

Verify loading the plugin

$ vi /etc/kong/kong.conf
log_level = debug
$ tail -f /usr/local/kong/logs/error.log

Plugin Development Kit

https://docs.konghq.com/2.0.x/pdk/

  • kong.client
  • kong.ctx
  • kong.ip
  • kong.log
  • kong.nginx
  • kong.node
  • kong.request
  • kong.response
  • kong.router
  • kong.service
  • kong.service.request
  • kong.service.response
  • kong.table

참고자료

Buy Me A Coffee

0개의 댓글