V2bX/node/node.go

49 lines
903 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
}
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
2023-07-29 06:47:47 -04:00
n.controllers[i] = NewController(core, p, c.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",
c.ApiConfig.APIHost,
2023-08-16 01:46:00 -04:00
c.ApiConfig.NodeType,
2023-06-09 00:36:49 -04:00
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
}