This repository provides an example of how to build and inject a Client Plugin into the KrakenD API Gateway. Follow the steps below to understand how to set up and use the client plugin.
The client plugin, named my-client-plugin
, demonstrates how to create a custom HTTP client that can be integrated into KrakenD. The plugin replaces the default HTTP client by adding custom logic before and after the request is processed.
client.go
: The main plugin code that implements theRegisterClient
interface.go.mod
: The Go module file.Makefile
: Instructions for building the plugin using Docker.
- Go
- Docker
- KrakenD API Gateway (version 2.x)
You should build the plugin using the KrakenD Docker builder image. The Makefile
includes targets for different architectures.
-
Clone the Repository:
git clone https://github.com/krakend/examples.git cd examples/plugins/client
-
Build for Different Architectures:
-
For amd64:
make amd64
-
For arm64:
make arm64
-
For linux_amd64 (non-docker):
make linux_amd64
-
For linux_arm64 (non-docker):
make linux_arm64
-
To use the plugin in your KrakenD configuration, add it under the extra_config
section of your krakend.json
file.
{
"version": 3,
"plugin": {
"pattern": ".so",
"folder": "/etc/krakend/plugins"
},
"host": ["http://localhost:8080/"],
"debug_endpoint": true,
"endpoints": [
{
"endpoint": "/example",
"backend": [
{
"url_pattern": "/__debug/",
"extra_config": {
"plugin/http-client": {
"name": "my-client-plugin",
"my-client-plugin": {
"option": "/some-path"
}
}
}
}
]
}
]
}
The plugin supports a logger interface to help with debugging and logging messages.
Debug(v ...interface{})
Info(v ...interface{})
Warning(v ...interface{})
Error(v ...interface{})
Critical(v ...interface{})
Fatal(v ...interface{})
The my-client-plugin
adds custom logic by wrapping the default HTTP client. It logs the request details and copies headers, status codes, and body from the backend response to the client response.
- Build the plugin using the appropriate make target. The plugin will be generated under the
plugins
folder. - Test your KrakenD using Docker, for instance:
docker run --rm -it --name krakend -p 8080:8080 -v "$PWD:/etc/krakend" devopsfaith/krakend
- Send an example call
curl -iG 'http://localhost:8080/example'
For more details on client plugins and extending KrakenD, refer to the official documentation.