config.go 1.3 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3 4 5 6
package cmd

import (
	"bytes"
	"encoding/json"
	"fmt"
黄孟柱 已提交
7 8 9
	log "github.com/eolinker/goku-api-gateway/goku-log"
	node_common "github.com/eolinker/goku-api-gateway/goku-node/node-common"
	"github.com/eolinker/goku-api-gateway/server/entity"
E
eoLinker API Management 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
	"io/ioutil"
	"net/http"
	"time"
)

// 获取节点配置
func GetConfig(listenPort int) (bool, *entity.ClusterInfo) {
	client := &http.Client{
		Timeout: time.Second * 10,
	}
	addr := node_common.GetAdminUrl(fmt.Sprintf("/register?port=%d", listenPort))
	reader := bytes.NewReader([]byte(""))
	request, err := http.NewRequest("GET", addr, reader)
	if err != nil {
		return false, nil
	}
	// request.Header.Set("Content-Type", "application/json;charset=UTF-8")
	resp, err := client.Do(request)
	if err != nil {
		log.Info(err)
		return false, nil
	}
	defer resp.Body.Close()
	if resp.StatusCode != 200 {
		errReason := "[Error] Fail to match code!"
		log.Info(errReason)
		return false, nil
	}
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Info(err)
		return false, nil
	}
	content := new(ClusterConfig)
	err = json.Unmarshal(body, &content)
	if err != nil {
		log.Info(err)
		return false, nil
	}
	if content.StatusCode != "000000" {
		errReason := "[Error] Fail to match status code!"
		log.Info(errReason)
		return false, nil
	}
	return true, content.Cluster
}