krakend / krakend-ce

KrakenD Community Edition: High-performance, stateless, declarative, API Gateway written in Go.
https://www.krakend.io
Apache License 2.0
1.98k stars 452 forks source link

Backend manipulation based on condition #201

Closed rast1025 closed 4 years ago

rast1025 commented 4 years ago

I'm trying to configure my krakend.json using sequential proxy so that based on my backend's status code I can make one more request to another backend. Ex: If my 1st backend answers 201, I want to make request to 2nd backed, but if it answers 200, to another. Can I do it using krakend?

"backend": [
        {
          "url_pattern": "/api/verify",
          "encoding": "json",
          "host": "127.0.0.1:8081",
          "method": "GET",
          "group": "limits",
          "extra_config":{
            "github.com/devopsfaith/krakend-cel": [
              {
                "check_expr": "resp_data.limits.is_valid"
              }
            ]
          }
        },
        {
          "url_pattern": "/increase",
          "encoding": "json",
          "host": "127.0.0.1:8081",
          "method": "PUT",
          "group": "response"
        },
        {
          "url_pattern": "/decrease",
          "encoding": "json",
          "host": "127.0.0.1:8081",
          "method": "PUT",
          "group": "limits"
        }

      ]
kpacha commented 4 years ago

@rast1025 the usage of the status code as you described is not what your example shows.

the sequential response allows you to use elements of the previous response, so maybe you could combine it with some lua.

you can use no-op pipes for checking the status code and take a decision as your described or you can implement your example with this config:

    {
        "endpoint": "/your-endpoint",
        "extra_config": {
            "github.com/devopsfaith/krakend/proxy": { "sequential": true }
        },
        "backend": [
            {
                "url_pattern": "//api/verify",
                "host": ["http://host1.tld"],
                "extra_config": {
                    "github.com/devopsfaith/krakend-lua/proxy/backend": {
                        "sources": [ "verify.lua" ],
                        "post": "check_response(response.load())",
                        "allow_open_libs": true
                    }
                }
            },
            {
                "url_pattern": "/{resp0_action}",
                "host": ["http://host2.tld"]
            }
        ]
    }

and this lua snippet:

function check_response( resp )
    local responseData = resp:data()
    local limits = responseData:get("limits")
    if limits:get("is_valid")
    then
        responseData:set("action", "increase")
    else
        responseData:set("action", "decrease")
    end
end
github-actions[bot] commented 2 years ago

This issue was marked as resolved a long time ago and now has been automatically locked as there has not been any recent activity after it. You can still open a new issue and reference this link.