
Developing plugins for the Kong API Gateway in Go is a straightforward process that can be completed in a few steps. You'll need to start by installing the Kong Go SDK, which can be done using the command `go get github.com/Kong/go-kong`.
The Kong Go SDK provides a set of tools and libraries that make it easy to interact with the Kong API Gateway. With the SDK installed, you can begin building your plugin.
A Kong plugin is essentially a Go package that exports a set of functions that can be used to extend the functionality of the API Gateway. To create a plugin, you'll need to create a new Go package and export the required functions.
Prerequisites
To get started with golang kong, you'll need to meet a few prerequisites.
First and foremost, you'll need Docker and Docker Compose installed on your machine. This will allow you to create and manage containers for your kong project.

You'll also need Go (version 1.18 or later) installed on your machine. This is a requirement for building and running golang kong.
Having a basic knowledge of Go and Docker is also essential for this project. Don't worry if you're new to these technologies – with some practice, you'll be up to speed in no time.
Here are the specific requirements in a concise list:
- Docker and Docker Compose installed on your machine
- Go (version 1.18 or later) installed
- Basic knowledge of Go and Docker
If you meet these prerequisites, you're ready to start building your golang kong project.
Writing the Plugin
To write a Kong plugin in Go, you'll need to create a file named handler.go with the plugin code.
This file should contain the plugin's logic, such as logging a message and setting a custom header for the response.
The plugin code should be written in Go and should be placed inside a directory named hello-plugin.
The plugin should be designed to work with the Kong API, which can be accessed at http://localhost:8000.
For another approach, see: Golang File
Once the plugin is written, you can build and run it using Docker Compose, which will start the Kong API and the plugin.
You can access the Kong API and the plugin's configuration at http://localhost:1337.
To write a custom plugin, you'll need to create a new Go file, such as key-checker.go, and import the Kong PDK file.
You'll also need to create a struct for configuration to represent the config parameters in the config.yaml file.
The plugin should have a function called New that returns an interface, and a function called Access that gets the requests and responds.
The plugin can be built into an image and loaded into the Kong image using the go-pluginserver.
Here is a list of the steps to write a Kong plugin in Go:
- Create a new Go file, such as handler.go, with the plugin code.
- Write the plugin's logic, such as logging a message and setting a custom header for the response.
- Import the Kong PDK file and create a struct for configuration.
- Create a function called New that returns an interface.
- Create a function called Access that gets the requests and responds.
- Build the plugin into an image and load it into the Kong image using the go-pluginserver.
By following these steps, you can write a custom Kong plugin in Go and enhance your API gateway's capabilities.
Building and Running
To build and run your Go plugin for Kong, you'll need to create a Dockerfile to containerize the plugin. This sets up the Go plugin and integrates it into the Kong container.
Next, you'll need to create a start script, such as a start.sh script, which automates the process of starting the services. This script starts the Kong and Konga services after setting up the database.
Once you've built your plugin, you can access Kong at http://localhost:8000 and Konga Dashboard at http://localhost:1337.
Create the Dockerfile
To create a Dockerfile, you need to place it in the root of your project. This Dockerfile sets up the Go plugin and integrates it into the Kong container.
A Dockerfile is essential for containerizing the plugin. It allows you to package your application and its dependencies into a single container that can be easily deployed and managed.
Create a Dockerfile in the root of your project to get started. This will enable you to containerize your plugin and set it up with Kong.
On a similar theme: Golang Dockerfile
Create Start Script
In the process of building and running your system, creating a start script is a crucial step. This script is used to automate the process of starting the services.
To create the start script, you'll need to create a start.sh script. This script starts the Kong and Konga services after setting up the database.
The start script is essential for streamlining your setup process and making it more efficient. It eliminates the need to manually start each service, saving you time and effort.
In this script, the Kong and Konga services are started, which is a key part of getting your system up and running.
Build and Run the Plugin
To build and run your Kong API plugin written in Go, you'll need to use Docker Compose. Run the following commands to get started: Once the services are up and running, your plugin should be live.
You can access Kong at http://localhost:8000 and Konga Dashboard at http://localhost:1337.
To build the plugin, create a Dockerfile in the root of your project. This Dockerfile sets up the Go plugin and integrates it into the Kong container.

You'll also need to use MultiStage to build the container of your plugin. This involves building a golang image and building the plugin into an executable binary, and then building the kong image and copying the plugin's binary from the builder into the kong image.
Here's a simple example of how you can do this:
By following these steps, you'll be able to build and run your Kong API plugin written in Go.
Why Go?
Go is a great choice for building Kong API plugins, and here's why.
Go performs better than Lua, especially for resource-intensive operations, thanks to its statically typed, compiled nature.
If you're already familiar with Go, it makes sense to use it for your Kong plugins, as opposed to learning Lua from scratch.
Go has native support for concurrency with Goroutines, making it easier to handle multiple API requests simultaneously.
Go's static typing, clearer syntax, and better error handling make it easier to maintain large codebases compared to Lua.
A rich ecosystem with libraries that can simplify integration and scaling is just one of the many benefits of choosing Go for your Kong plugins.
Consider reading: Golang Lua
Gateway Plugins
Kong Gateway Go Plugins are a powerful tool for extending the functionality of the Kong Gateway. At Kong Summit 2019, Kong announced that users could develop plugins for Kong Gateway with the Go programming language.
To create a plugin, you'll need to write the plugin code, which involves creating a file named handler.go with a specific content. This file will contain the logic for your plugin.
Developing a plugin from scratch can be a challenge, but it's not impossible. I used declarative configuration (decK) to create my custom Go plugin for Kong Gateway, which involved creating a custom plugin that checks the API key before redirecting the URL.
To start writing your plugin, you'll need to import the Go PDK file, which allows your Go plugin to access functionality provided by the Kong PDK. You'll also need to create a struct for configuration to represent the config parameters in the config.yaml config file.
Your plugin will need to define a function called "New", which returns an interface. You'll also need to create a func that figures out the request and response handlers for the processing lifecycle. This func should be named "Access" and should get the requests and respond.
To build an image into the Kong image, you'll need to get the files to build an image. The Go plugin server's role is to dynamically load the Go plugins and execute their code on demand.
Development and Deployment
Development and deployment of a Go plugin for Kong is relatively straightforward. To start, you'll need to define constants for version and priority, which determine the plugin's execution order.
The version and priority constants are essential, as they dictate the plugin's execution order. The higher the priority, the earlier it will be executed.
You'll also need to define a struct called Config to accept parameters, such as the field message as a string. This is where you specify the plugin's configuration.
To deploy your plugin, you'll need to build an executable and add it to the Kong image. This involves adding environment variables to help Kong find and load the customized plugin.
Here's a brief overview of the possible phase access for Kong plugins: CertificateRewriteAccessResponsePrereadLog
A fresh viewpoint: S Golang
Development
Development is a crucial part of any project, and building a Go plugin is no exception. It's easy to follow the signature proposed by Kong's plugin development kit or PDK.
The first step is to define both constants: version and priority. You'll need to configure the current version of the plugin and its execution priority within the other plugins. The higher the priority, the earlier it will be executed.
You can define these constants with a simple line of code, like this: `var Version="0.0.1" var Priority=1`.
To define which params the plugin accepts, you'll need to build a struct called Config. This is where you'll receive the field message as a string, like this: `type Config struct { Message string `json:"message"` }`.
Kong plugins work based on which phase access they need to run. The possible phase access are: Certificate, Rewrite, Access, Response, Preread, and Log. You'll need to choose one based on your plugin's requirements.
In this example, we're using Access because we need to update the response before forwarding the request to the responsible microservice. You can find more information about all phase access in the documentation.
The function uses the PDK to read the available headers by using `kong.Request.GetHeader`, and later we add a response header using `kong.Response.SetHeader`. You can find more information about all available functions in the Go PDK.
The main function initializes the Kong server, sending the Config, Version, and Priority defined before.
Deploy
Deploying a plugin requires building an executable and adding it to the Kong image. This involves using a Docker image, specifically the golang:1.20 image, to build the plugin.
To build the plugin, we need to create a Dockerfile that copies the plugin code into the image and builds the executable. The Dockerfile uses the find command to locate the plugin directories and build the executable for each one.
The Dockerfile also uses a while loop to iterate over the plugin directories and build the executable for each one. This is done using the go build command with the -o option to specify the output file name.
Once the plugin is built, we need to add it to the Kong image. This is done by copying the built plugin into the Kong image.
To make the plugin work with Kong, we need to add some environment variables. These variables provide Kong with the necessary information to find and load the customized plugin.
Here are the environment variables that need to be added:
Code
You can create a simple project structure for your GoLang Kong plugin, as shown in the example. This structure is a good starting point for exploring more complex architectures like hexagonal or clean-architecture.
To organize your code, create a file named handler.go inside your plugin directory. This file will contain the plugin's code.
In this code, you can define a struct that represents the plugin's configuration. This struct can be customized using JSON tags to suit your needs.
A constructor is also a must-have for your plugin, which is used to instantiate the Config and return it as an interface. In the example code, the constructor is named New.
When it comes to defining the phases of the request lifecycle, you can choose from several options based on the Kong documentation. In the example code, the Access phase is used to add header validation.
You can also define the version and priority of your plugin's execution by declaring constant variables. These variables can be passed to the server.StartServer function, which executes plugins from highest priority to lowest.
Testing Tests
Testing is easy with the PDK, thanks to the tools it provides. You can test your Go plugins with just a few lines of code.
The PDK offers a `test` package that makes it straightforward to test your plugins. You can import it into your code and use its functions to create test environments.
To test your plugin, you can use the `test.New` function to create a new test environment. This function takes a `testing.T` object and a `test.Request` object as arguments. The `test.Request` object defines the request you want to send to the server.
Here's an example of how you can use `test.New` to create a test environment:
```html
```
In the example, the `test.Request` object is used to define a GET request to `http://example.com?q=search&x=9` with a `Host` header set to `localhost`. The `test.New` function returns a new test environment, which is then used to send the request and verify the response.
The PDK also provides an `assert` package from `github.com/stretchr/testify`, which you can use to assert that the response was successful and that the expected headers were returned.
In the example, the `assert.NoError` function is used to check that there was no error sending the request, and the `assert.Equal` function is used to check that the response status code was 200 and that the expected header was returned.
Intriguing read: Example Golang
Featured Images: pexels.com


