NodeController.go 2.1 KB
Newer Older
duanjinfei's avatar
duanjinfei committed
1 2
package controllers

3
import (
duanjinfei's avatar
duanjinfei committed
4 5 6
	"encoding/json"
	"example.com/m/conf"
	"example.com/m/models"
7
	"example.com/m/nm"
duanjinfei's avatar
duanjinfei committed
8
	"example.com/m/operate"
9
	nodeManagerV1 "github.com/odysseus/odysseus-protocol/gen/proto/go/nodemanager/v1"
duanjinfei's avatar
duanjinfei committed
10
	"io"
11
)
duanjinfei's avatar
duanjinfei committed
12 13 14 15 16 17

type NodeController struct {
	BaseController
}

func (c *NodeController) SetNmSeed() {
duanjinfei's avatar
duanjinfei committed
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
	bodyReq, err := io.ReadAll(c.Ctx.Request.Body)
	if err != nil || bodyReq == nil {
		c.ResponseInfo(500, "param error", "")
		return
	}
	req := &models.SeedUrl{}
	err = json.Unmarshal(bodyReq, req)
	if err != nil {
		c.ResponseInfo(500, "param error", "")
		return
	}
	serviceClient := operate.ConnNmGrpc(req.Seed)
	if serviceClient == nil {
		c.ResponseInfo(500, "seed is not connected", "")
	}
	conf.GetConfig().SetNmSeed(req.Seed)
	c.ResponseInfo(200, "set seed successful", "")
}

func (c *NodeController) SetRewardAddress() {
	bodyReq, err := io.ReadAll(c.Ctx.Request.Body)
	if err != nil || bodyReq == nil {
		c.ResponseInfo(500, "param error", "")
		return
	}
	req := &models.RewardAddress{}
	err = json.Unmarshal(bodyReq, req)
	if err != nil {
		c.ResponseInfo(500, "param error", "")
		return
	}
	if !conf.GetConfig().SetRewardAddress(req.Address) {
		c.ResponseInfo(500, "param is not address", "")
	}
duanjinfei's avatar
duanjinfei committed
52 53 54
	c.ResponseInfo(200, "sign successful", "")
}

55
func (c *NodeController) AddNodeManager() {
duanjinfei's avatar
duanjinfei committed
56 57 58 59 60 61 62 63 64 65 66
	bodyReq, err := io.ReadAll(c.Ctx.Request.Body)
	if err != nil || bodyReq == nil {
		c.ResponseInfo(500, "param error", "")
		return
	}
	req := &models.NodeManagerReq{}
	err = json.Unmarshal(bodyReq, req)
	if err != nil || req.PublicKey == "" || req.EndPoint == "" {
		c.ResponseInfo(500, "param error", "")
		return
	}
67
	nodeManager := &nodeManagerV1.NodeManagerInfo{
duanjinfei's avatar
duanjinfei committed
68 69
		Publickey: req.PublicKey,
		Endpoint:  req.EndPoint,
70 71
	}
	nm.AddNodeManager(nodeManager)
duanjinfei's avatar
duanjinfei committed
72 73
	c.ResponseInfo(200, "sign successful", "")

74 75
}

duanjinfei's avatar
duanjinfei committed
76
func (c *NodeController) GetNodeManagers() {
77
	manager := nm.GetNodeManagers()
duanjinfei's avatar
duanjinfei committed
78 79 80 81 82 83 84 85 86 87
	res := make([]*nm.NodeManager, 0)
	for _, nodeManager := range manager {
		if !nodeManager.IsExist {
			continue
		}
		if nodeManager.IsUsed {
			res = append(res, nodeManager)
		}
	}
	c.ResponseInfo(200, "Get used node manager successful", res)
duanjinfei's avatar
duanjinfei committed
88
}