V2bX/node/node.go

49 lines
936 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"
2023-07-29 06:47:47 -04:00
2023-07-29 07:27:15 -04:00
"github.com/InazumaV/V2bX/api/panel"
"github.com/InazumaV/V2bX/conf"
vCore "github.com/InazumaV/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
}
2023-08-20 03:13:52 -04:00
func (n *Node) Start(nodes []conf.NodeConfig, core vCore.Core) error {
n.controllers = make([]*Controller, len(nodes))
2023-08-20 03:13:52 -04:00
for i := range nodes {
p, err := panel.New(&nodes[i].ApiConfig)
if err != nil {
return err
}
2022-10-09 23:31:15 -04:00
// Register controller service
2023-08-20 03:13:52 -04:00
n.controllers[i] = NewController(core, p, &nodes[i].Options)
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",
2023-08-20 03:13:52 -04:00
nodes[i].ApiConfig.APIHost,
nodes[i].ApiConfig.NodeType,
nodes[i].ApiConfig.NodeID,
2023-06-09 00:36:49 -04:00
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
}