Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

README.md

README

Lua Scripting in KrakenD API Gateway Configuration

This repository contains a KrakenD configuration (krakend.json) focusing on demonstrating Lua scripting to manipulate HTTP request headers dynamically.

Key Feature: Lua Header Manipulation

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 the Camera header. If the segment cannot be determined, it defaults to "unknown".

Usage

To test this configuration, you can use Docker for a straightforward setup:

  1. Clone the Repository: Ensure the krakend.json is in your working directory.
  2. 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.

Example

Testing the Configuration

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/test

This 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.

Expected Response

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.

Note

  • 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.