Skip to content

Commit

Permalink
Issue #42: add support for 'weight=f' option on the url tag
Browse files Browse the repository at this point in the history
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
  • Loading branch information
magiconair committed Jun 28, 2017
1 parent f90eeb2 commit 7c8ce98
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions registry/consul/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 7c8ce98

Please sign in to comment.