Share
Explore

Kong Plugin development

KONG PLUGIN DEVELOPMENT

Step 1: Set up the Development Environment
Install Lua and Kong on your local machine.
Set up a Kong development environment or use Kong Docker image.
Step 2: Create the Plugin Directory Structure
Create a directory for your plugin, e.g., "etta-uri-changer-plugin".
Inside the plugin directory, create the following structure:
etta-uri-changer-plugin/
handler.lua
schema.lua
Step 3: Implement the Plugin Logic in handler.lua
Open handler.lua in a text editor and add the following code:
luaCopy code
local BasePlugin = require "kong.plugins.base_plugin"

local EttaUriChangerHandler = BasePlugin:extend()

EttaUriChangerHandler.VERSION = "1.0.0"
EttaUriChangerHandler.PRIORITY = 10

function EttaUriChangerHandler:new()
EttaUriChangerHandler.super.new(self, "etta-uri-changer")
end

function EttaUriChangerHandler:access(config)
EttaUriChangerHandler.super.access(self)

-- Retrieve the request object
local request = ngx.var.request

-- Modify the URI to "etta.uri.test"
ngx.req.set_uri("/etta.uri.test")

-- Log the modified URI
ngx.log(ngx.NOTICE, "Modified URI: ", ngx.var.request_uri)
end

return EttaUriChangerHandler

Step 4: Define the Plugin Schema in schema.lua
Open schema.lua in a text editor and add the following code:
luaCopy code
return {
no_consumer = true,
fields = {},
self_check = function(schema, plugin_t, dao, is_updating)
return true
end
}

Step 5: Register the Plugin with Kong
Add the plugin to the Kong configuration file (kong.conf or kong.yml) under the custom_plugins property:
makefileCopy code
custom_plugins: - etta-uri-changer
Step 6: Test the Plugin
Start Kong Gateway and ensure it's running.
Send a request through Kong Gateway, and the plugin will modify the URI to "etta.uri.test".
Note: Make sure to restart Kong Gateway or reload the configuration for the changes to take effect.
That's it! You have created a custom Kong plugin that changes the URI to "etta.uri.test". You can further enhance the plugin by adding validation, error handling, or other functionalities as per your requirements.



SET UP ENVIRONMENT

Launch Script

Run this command in the terminal to set up your environment (~30 seconds).
./launch-dev-portal_summit.sh

Script Description

The launch script configures your environment with the following:
Creates a Docker Network called kong-net
Installs and starts the database
Prepares your database (Kong migration)
Installs and starts Kong with environment variables and configurations

Verify Setup

This learning lab utilizes the Kong Enterprise command line.
Verify that your environment is up and running.
Response
The results should be a HTTP/1.1 200 OK response from Kong.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.