This repository contains a KrakenD configuration (krakend.json) focusing on demonstrating Lua scripting to manipulate HTTP request headers dynamically.
The configuration applies a Lua script at the /test endpoint, which performs the following:
- Context Loading: Loads the current request context.
- Hostname Segmentation and Header Modification: Extracts the first segment of the
host(before the first dot) and sets this value in theCameraheader. If the segment cannot be determined, it defaults to"unknown".
To test this configuration, you can use Docker for a straightforward setup:
- Clone the Repository: Ensure the
krakend.jsonis in your working directory. - Run KrakenD in Docker:
docker run -it -p 8080:8080 -v $PWD:/etc/krakend/ devopsfaith/krakend:watch run -d -c krakend.json
This command runs KrakenD in a Docker container, mapping port 8080 and using the configuration file from your current directory.
To test the functionality of the Lua scripting in the KrakenD configuration, you can use the following curl command:
curl http://my-camera-1.prx.myhostname.localhost:8080/testThis command simulates a request to the KrakenD API Gateway with a custom hostname. The Lua script will extract the first segment of the hostname and set it in the Camera header.
Upon executing the above curl command, you should receive a response similar to the following:
{
"req_body": "",
"req_headers": {
"Camera": [
"my-camera-1"
],
"User-Agent": [
"KrakenD Version 2.5.0"
]
},
"req_method": "GET",
"req_querystring": {},
"req_uri": "/__echo/",
"req_uri_details": {
"fragment": "",
"host": "localhost:8080",
"path": "/__echo/",
"query": "",
"user": ""
}
}This response indicates that the Lua script successfully extracted "my-camera-1" from the host and set it as the value for the Camera header.
- The Lua script is executed before the request is processed by the backend.
- This setup is ideal for testing and development purposes. Adjustments might be necessary for production environments.
Refer to KrakenD documentation for more details on configuration and Lua scripting capabilities.