This commit is contained in:
kzkzzzz
2025-12-13 15:16:31 +08:00
parent 07ea247096
commit 4553fc8466
10 changed files with 423 additions and 338 deletions

View File

@@ -29,30 +29,31 @@ type target struct {
Dc string `form:"dc"`
AllowStale bool `form:"allow-stale"`
RequireConsistent bool `form:"require-consistent"`
// TODO(mbobakov): custom parameters for the http-transport
// TODO(mbobakov): custom parameters for the TLS subsystem
}
func (t *target) String() string {
return fmt.Sprintf("service='%s' healthy='%t' tag='%s'", t.Service, t.Healthy, t.Tag)
str := t.Service
if t.Tag != "" {
str = fmt.Sprintf("%s (tag:%s)", str, t.Tag)
}
return str
}
// parseURL with parameters
// see README.md for the actual format
// URL schema will stay stable in the future for backward compatibility
func parseURL(u string) (target, error) {
func parseURL(u string) (*target, error) {
rawURL, err := url.Parse(u)
if err != nil {
return target{}, errors.Wrap(err, "Malformed URL")
return nil, errors.Wrap(err, "Malformed URL")
}
if rawURL.Scheme != schemeName ||
len(rawURL.Host) == 0 || len(strings.TrimLeft(rawURL.Path, "/")) == 0 {
return target{},
return nil,
errors.Errorf("Malformed URL('%s'). Must be in the next format: 'consul://[user:passwd]@host/service?param=value'", u)
}
var tgt target
tgt := &target{}
tgt.User = rawURL.User.Username()
tgt.Password, _ = rawURL.User.Password()
tgt.Addr = rawURL.Host
@@ -64,7 +65,7 @@ func parseURL(u string) (target, error) {
err = decoder.Decode(&tgt, rawURL.Query())
if err != nil {
return target{}, errors.Wrap(err, "Malformed URL parameters")
return nil, errors.Wrap(err, "Malformed URL parameters")
}
if len(tgt.Near) == 0 {
tgt.Near = "_agent"