Skip to content
Snippets Groups Projects
config_url.go 2.68 KiB
Newer Older
vito.he's avatar
vito.he committed
package config
fangyincheng's avatar
fangyincheng committed

import (
	"fmt"
	"net"
	"net/url"
	"strconv"
	"strings"
	"time"
)

import (
	jerrors "github.com/juju/errors"
)

vito.he's avatar
vito.he committed
type baseUrl struct {
vito.he's avatar
vito.he committed
	Protocol     string `required:"true"  yaml:"protocol"  json:"protocol,omitempty"`
	Location     string // ip+port
	Ip           string
	Port         string
	Timeout      time.Duration
	Query        url.Values
vito.he's avatar
vito.he committed
	PrimitiveURL string
vito.he's avatar
vito.he committed
type ConfigURL struct {
	baseUrl
	Service string
vito.he's avatar
vito.he committed
	Path string `yaml:"path" json:"path,omitempty"` // like  /com.ikurento.dubbo.UserProvider3

	Version string `yaml:"version" json:"version,omitempty"`
	Group   string `yaml:"group" json:"group,omitempty"`
vito.he's avatar
vito.he committed
	Weight   int32
	Methods  string `yaml:"methods" json:"methods,omitempty"`
	Username string
	Password string
vito.he's avatar
vito.he committed
func NewConfigURL(urlString string) (*ConfigURL, error) {
fangyincheng's avatar
fangyincheng committed
	var (
		err          error
		rawUrlString string
		serviceUrl   *url.URL
vito.he's avatar
vito.he committed
		s            = &ConfigURL{}
	// new a null instance
	if urlString == "" {
		return s, nil
	}

fangyincheng's avatar
fangyincheng committed
	rawUrlString, err = url.QueryUnescape(urlString)
	if err != nil {
		return nil, jerrors.Errorf("url.QueryUnescape(%s),  error{%v}", urlString, err)
	}

	serviceUrl, err = url.Parse(rawUrlString)
	if err != nil {
		return nil, jerrors.Errorf("url.Parse(url string{%s}),  error{%v}", rawUrlString, err)
	}

vito.he's avatar
vito.he committed
	s.Query, err = url.ParseQuery(serviceUrl.RawQuery)
fangyincheng's avatar
fangyincheng committed
	if err != nil {
		return nil, jerrors.Errorf("url.ParseQuery(raw url string{%s}),  error{%v}", serviceUrl.RawQuery, err)
	}

vito.he's avatar
vito.he committed
	s.PrimitiveURL = urlString
	s.Protocol = serviceUrl.Scheme
	s.Location = serviceUrl.Host
	s.Path = serviceUrl.Path
	if strings.Contains(s.Location, ":") {
		s.Ip, s.Port, err = net.SplitHostPort(s.Location)
fangyincheng's avatar
fangyincheng committed
		if err != nil {
vito.he's avatar
vito.he committed
			return nil, jerrors.Errorf("net.SplitHostPort(Url.Host{%s}), error{%v}", s.Location, err)
vito.he's avatar
vito.he committed
	s.Group = s.Query.Get("group")
	s.Version = s.Query.Get("version")
	timeoutStr := s.Query.Get("timeout")
fangyincheng's avatar
fangyincheng committed
	if len(timeoutStr) == 0 {
vito.he's avatar
vito.he committed
		timeoutStr = s.Query.Get("default.timeout")
fangyincheng's avatar
fangyincheng committed
	}
	if len(timeoutStr) != 0 {
		timeout, err := strconv.Atoi(timeoutStr)
		if err == nil && timeout != 0 {
vito.he's avatar
vito.he committed
			s.Timeout = time.Duration(timeout * 1e6) // timeout unit is millisecond
fangyincheng's avatar
fangyincheng committed
		}
	}

	return s, nil
}

vito.he's avatar
vito.he committed
func (c *ConfigURL) Key() string {
	return fmt.Sprintf("%s@%s-%s-%s-%s-%s", c.Service, c.Protocol, c.Group, c.Location, c.Version, c.Methods)
vito.he's avatar
vito.he committed
}

vito.he's avatar
vito.he committed
func (c *ConfigURL) ConfigURLEqual(url ConfigURL) bool {
vito.he's avatar
vito.he committed
	if c.Key() != url.Key() {
		return false
	}
	return true
}
vito.he's avatar
vito.he committed
func (s ConfigURL) String() string {
fangyincheng's avatar
fangyincheng committed
	return fmt.Sprintf(
		"DefaultServiceURL{Protocol:%s, Location:%s, Path:%s, Ip:%s, Port:%s, "+
			"Timeout:%s, Version:%s, Group:%s, Weight_:%d, Query:%+v}",
vito.he's avatar
vito.he committed
		s.Protocol, s.Location, s.Path, s.Ip, s.Port,
		s.Timeout, s.Version, s.Group, s.Weight, s.Query)
fangyincheng's avatar
fangyincheng committed
}