register.go 1.0 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3 4
package admin

import (
	"fmt"
黄孟柱 已提交
5 6 7 8
	"github.com/eolinker/goku-api-gateway/console/controller"
	"github.com/eolinker/goku-api-gateway/console/module/node"
	cluster2 "github.com/eolinker/goku-api-gateway/server/cluster"
	"github.com/eolinker/goku-api-gateway/server/entity"
E
eoLinker API Management 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
	"net/http"
	"strconv"
)

func Register(w http.ResponseWriter, r *http.Request) {

	ip, port, err := GetIpPort(r)
	if err != nil {
		controller.WriteError(w, "700000", "cluster", err.Error(), err)
		return
	}
	cluster, err := regedister(ip, port)
	if err != nil {
		controller.WriteError(w, "700001", "cluster", err.Error()+ip, err)
		return
	}
Y
Your Name 已提交
25
	node.Refresh(ip, strconv.Itoa(port))
E
eoLinker API Management 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
	controller.WriteResultInfo(w, "cluster", "cluster", cluster)
}

func regedister(ip string, port int) (*entity.ClusterInfo, error) {

	has, node, err := node.GetNodeInfoByIpPort(ip, port)
	if err != nil {
		return nil, err
	}
	if !has {
		return nil, err
	}
	cName := node.Cluster
	info, has := cluster2.Get(cName)
	if has {
		return info, nil
	}
	return nil, fmt.Errorf("not has that node[%s:%d]", ip, port)

}