Tyk Session Stickiness and Custom Plugins Integration

To answer my question. This can be done by combining a custom pre-request plugin, header or session metadata injection, and URL rewriting.

API Definition:

        "use_extended_paths": true,
        "extended_paths": {
          "url_rewrites": [
            {
              "path": "/v1/{.*}",
              "match_pattern": "/(.*)",
              "method": "GET",
              "rewrite_to": "http://greeter-lb:8080/$1", // fallback
              "triggers": [
                {
                  "on": "all",
                  "options": {
                    "header_matches": {
                      "Service-Address": {
                        "match_rx": ".*"
                      }
                    }
                  },
                  "rewrite_to": "$tyk_context.trigger-0-Service-Address-0/$1" // override
                }
              ]
            }
          ]
        },

In the Go middleware you can simply call

func GetGreeterAddress(w http.ResponseWriter, r *http.Request) {
  // logic
  // ... 
  r.Header.Set("Service-Address", serviceAddress)
}
1 Like