2022-07-28 11:00:05 -04:00
|
|
|
package node
|
2022-06-01 13:35:41 -04:00
|
|
|
|
|
|
|
import (
|
2023-06-09 00:36:49 -04:00
|
|
|
"fmt"
|
2022-08-16 01:04:33 -04:00
|
|
|
"github.com/Yuzuki616/V2bX/api/panel"
|
2022-07-28 11:00:05 -04:00
|
|
|
"github.com/Yuzuki616/V2bX/conf"
|
2023-06-07 13:18:56 -04:00
|
|
|
vCore "github.com/Yuzuki616/V2bX/core"
|
2022-06-01 13:35:41 -04:00
|
|
|
)
|
|
|
|
|
2022-07-28 11:00:05 -04:00
|
|
|
type Node struct {
|
2023-05-15 21:15:29 -04:00
|
|
|
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-06-07 13:18:56 -04:00
|
|
|
func (n *Node) Start(nodes []*conf.NodeConfig, core vCore.Core) error {
|
2023-05-15 21:15:29 -04:00
|
|
|
n.controllers = make([]*Controller, len(nodes))
|
2022-10-09 23:31:15 -04:00
|
|
|
for i, c := range nodes {
|
2022-12-18 10:31:06 -05:00
|
|
|
p, err := panel.New(c.ApiConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-09 23:31:15 -04:00
|
|
|
// Register controller service
|
2023-05-15 21:15:29 -04:00
|
|
|
n.controllers[i] = NewController(core, p, c.ControllerConfig)
|
2022-12-18 10:31:06 -05:00
|
|
|
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-09-07 11:02:02 -04:00
|
|
|
}
|
|
|
|
}
|
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()
|
2022-10-09 20:59:00 -04:00
|
|
|
if err != nil {
|
2022-10-09 23:31:15 -04:00
|
|
|
panic(err)
|
2022-10-09 20:59:00 -04:00
|
|
|
}
|
|
|
|
}
|
2022-10-09 23:31:15 -04:00
|
|
|
n.controllers = nil
|
2022-06-01 13:35:41 -04:00
|
|
|
}
|