func()

in state/manager.go [124:168]


func (m *Mgr) getDBAddr(cfg *config.AppConfig) (*db.Addr, error) {
	var err error
	var dbAddr *db.Addr

	if cfg.StateConnectURL != "" {
		cs := cfg.StateConnectURL

		// url.Parse requires scheme in the beginning of the URL, just prepend
		// with random scheme if it wasn't in the config file URL
		if !strings.Contains(cfg.StateConnectURL, "://") {
			cs = "dsn://" + cfg.StateConnectURL
		}
		u, err := url.Parse(cs)
		if err != nil {
			return nil, err
		}

		var host, port = u.Host, ""
		if strings.Contains(u.Host, ":") {
			host, port, _ = net.SplitHostPort(u.Host)
		}
		if u.User.Username() == "" || host == "" {
			return nil, fmt.Errorf("host and username required in DB db URL")
		}

		if port == "" {
			port = "3306"
		}
		uPort, err := strconv.ParseUint(port, 10, 16)
		if err != nil {
			return nil, err
		}

		pwd, _ := u.User.Password()
		dbAddr = &db.Addr{Host: host, Port: uint16(uPort), User: u.User.Username(), Pwd: pwd, DB: types.MyDBName}
	} else {
		dbAddr, err = db.GetConnInfo(&db.Loc{Service: types.MySvcName, Name: types.MyDBName, Cluster: types.MyClusterName}, db.Master,
			types.InputMySQL)
		if err != nil {
			return nil, err
		}
	}

	return dbAddr, nil
}