V2bX/node/node.go

48 lines
914 B
Go
Raw Normal View History

package node
2022-06-01 13:35:41 -04:00
import (
2023-06-09 00:36:49 -04:00
"fmt"
"github.com/Yuzuki616/V2bX/api/panel"
"github.com/Yuzuki616/V2bX/conf"
vCore "github.com/Yuzuki616/V2bX/core"
2022-06-01 13:35:41 -04:00
)
type Node struct {
controllers []*Controller
2022-06-01 13:35:41 -04:00
}
2022-10-09 23:31:15 -04:00
func New() *Node {
return &Node{}
2022-06-01 13:35:41 -04:00
}
func (n *Node) Start(nodes []*conf.NodeConfig, core vCore.Core) error {
n.controllers = make([]*Controller, len(nodes))
2022-10-09 23:31:15 -04:00
for i, c := range nodes {
p, err := panel.New(c.ApiConfig)
if err != nil {
return err
}
2022-10-09 23:31:15 -04:00
// Register controller service
n.controllers[i] = NewController(core, p, c.ControllerConfig)
err = n.controllers[i].Start()
2022-10-09 23:31:15 -04:00
if err != nil {
2023-06-09 00:36:49 -04:00
return fmt.Errorf("start node controller [%s-%s-%d] error: %s",
c.ApiConfig.NodeType,
c.ApiConfig.APIHost,
c.ApiConfig.NodeID,
err)
}
}
2022-06-01 13:35:41 -04:00
return nil
}
2022-10-09 23:31:15 -04:00
func (n *Node) Close() {
for _, c := range n.controllers {
err := c.Close()
if err != nil {
2022-10-09 23:31:15 -04:00
panic(err)
}
}
2022-10-09 23:31:15 -04:00
n.controllers = nil
2022-06-01 13:35:41 -04:00
}