Is there a way to send metadata in krakend endpoint configuration?

Viewed 231

I'm using Krakend as API-Gateway, and my configuration looks like this :

{
  "plugin": {
    "folder": "/etc/krakend/plugins/authenticator/",
    "pattern":".so"
  },
  "port": 8080,
  "extra_config": {
    "github_com/devopsfaith/krakend/transport/http/server/handler": {
      "name": "authenticator"
    }
  },

  "endpoints": [
    {
      "output_encoding": "no-op",
      "backend": [
        {
          "encoding": "no-op",
          "host": [
            "127.0.0.1:8080"
          ],
          "url_pattern": "/api/v1/address/{id}",
          "method": "GET"
        }
      ],
      "endpoint": "/api/v1/addresses/{id}",
      "method": "GET"
    }
  ],

  "name": "gateway",
  "timeout": "30s",
  "version": 2
}

I want to pass some metadata per end point and access it in my predefined plugin . In this case authenticator plugin.

1 Answers

What you are trying to achieve is perfectly possible, and is the way all components work in KrakenD. Your plugin can access the KrakenD configuration using the namespace you define. For instance, you could set your metadata like this (I am assuming you have in your Go code a pluginName = "slifer2015-authenticator" ):

{
    "endpoints": [
        {
            "output_encoding": "no-op",
            "backend": [
                {
                    "encoding": "no-op",
                    "host": [
                        "127.0.0.1:8080"
                    ],
                    "url_pattern": "/api/v1/address/{id}"
                }
            ],
            "endpoint": "/api/v1/addresses/{id}",
            "extra_config": {
                "github_com/devopsfaith/krakend/transport/http/server/handler": {
                    "name": [
                        "slifer2015-authenticator",
                        "some-other-plugin-here"
                    ],
                    "slifer2015-authenticator": {
                        "Metadata1": "value1",
                        "Metadata2": {
                            "Some": 10,
                            "Thing": 100,
                            "Here": "60s"
                        }
                    }
                }
            }
        }
    ]
}

Then your metada is available in the extra parameter when the registerer kicks in, inside the key you have chosen.

func (r registerer) registerHandlers(ctx context.Context, extra map[string]interface{}, h http.Handler) (http.Handler, error) {
``
Related