From 7c8ce98cb864deb55cc29f0f239a94ce0bb278b0 Mon Sep 17 00:00:00 2001 From: Frank Schroeder Date: Mon, 19 Jun 2017 11:48:24 +0200 Subject: [PATCH] Issue #42: add support for 'weight=f' option on the url tag This patch adds support for a 'weight=f' option on the url tag which gets translated to a 'weight f' option on the 'route add' command. This should eventually move into the options to unify the config option syntax. Fixes #42 --- registry/consul/service.go | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/registry/consul/service.go b/registry/consul/service.go index 2b4b42f6a..7b6d787b3 100644 --- a/registry/consul/service.go +++ b/registry/consul/service.go @@ -112,21 +112,33 @@ func serviceConfig(client *api.Client, name string, passing map[string]bool, tag } // build route command + weight := "" + ropts := []string{} + tags := strings.Join(svctags, ",") addr = net.JoinHostPort(addr, strconv.Itoa(port)) dst := "http://" + addr + "/" - if strings.Contains(opts, "proto=tcp") { - dst = "tcp://" + addr - } else if strings.Contains(opts, "proto=https") { - dst = "https://" + addr + for _, o := range strings.Fields(opts) { + switch { + case o == "proto=tcp": + dst = "tcp://" + addr + case o == "proto=https": + dst = "https://" + addr + case strings.HasPrefix(o, "weight="): + weight = o[len("weight="):] + default: + ropts = append(ropts, o) + } } - tags := strings.Join(svctags, ",") cfg := "route add " + name + " " + route + " " + dst + if weight != "" { + cfg += " weight " + weight + } if tags != "" { cfg += " tags " + strconv.Quote(tags) } - if opts != "" { - cfg += " opts " + strconv.Quote(opts) + if len(ropts) > 0 { + cfg += " opts " + strconv.Quote(strings.Join(ropts, " ")) } config = append(config, cfg) }