将cluster.yaml修改为api_cluster.yaml

This commit is contained in:
GoEdgeLab
2023-08-12 18:53:32 +08:00
parent cb4d2ab7d7
commit a791630d31
5 changed files with 81 additions and 15 deletions

View File

@@ -2,4 +2,5 @@ node.json
api.yaml api.yaml
api_node.yaml api_node.yaml
cluster.yaml cluster.yaml
api_cluster.yaml
*.cache *.cache

View File

@@ -37,9 +37,13 @@ func main() {
// validate config // validate config
_, err := configs.LoadAPIConfig() _, err := configs.LoadAPIConfig()
if err != nil { if err != nil {
// validate cluster config
_, clusterErr := configs.LoadClusterConfig()
if clusterErr != nil { // fail again
fmt.Println("[ERROR]start failed: load api config from '" + Tea.ConfigFile(configs.ConfigFileName) + "' failed: " + err.Error()) fmt.Println("[ERROR]start failed: load api config from '" + Tea.ConfigFile(configs.ConfigFileName) + "' failed: " + err.Error())
os.Exit(0) os.Exit(0)
} }
}
}) })
app.On("uninstall", func() { app.On("uninstall", func() {
// service // service

View File

@@ -1,11 +1,57 @@
package configs package configs
import (
"github.com/iwind/TeaGo/Tea"
"gopkg.in/yaml.v3"
"os"
)
// ClusterConfig 集群配置 // ClusterConfig 集群配置
type ClusterConfig struct { type ClusterConfig struct {
RPC struct { OldRPC struct {
Endpoints []string `yaml:"endpoints" json:"endpoints"` Endpoints []string `yaml:"endpoints" json:"endpoints"`
DisableUpdate bool `yaml:"disableUpdate" json:"disableUpdate"` DisableUpdate bool `yaml:"disableUpdate" json:"disableUpdate"`
} `yaml:"rpc" json:"rpc"` } `yaml:"rpc,omitempty" json:"rpc"`
RPCEndpoints []string `yaml:"rpc.endpoints,flow" json:"rpc.endpoints"`
RPCDisableUpdate bool `yaml:"rpc.disableUpdate" json:"rpc.disableUpdate"`
ClusterId string `yaml:"clusterId" json:"clusterId"` ClusterId string `yaml:"clusterId" json:"clusterId"`
Secret string `yaml:"secret" json:"secret"` Secret string `yaml:"secret" json:"secret"`
} }
func (this *ClusterConfig) Init() error {
// compatible with old
if len(this.RPCEndpoints) == 0 && len(this.OldRPC.Endpoints) > 0 {
this.RPCEndpoints = this.OldRPC.Endpoints
this.RPCDisableUpdate = this.OldRPC.DisableUpdate
}
return nil
}
func LoadClusterConfig() (*ClusterConfig, error) {
for _, filename := range []string{"api_cluster.yaml", "cluster.yaml"} {
data, err := os.ReadFile(Tea.ConfigFile(filename))
if err != nil {
if os.IsNotExist(err) {
continue
}
return nil, err
}
var config = &ClusterConfig{}
err = yaml.Unmarshal(data, config)
if err != nil {
return config, err
}
err = config.Init()
if err != nil {
return nil, err
}
return config, nil
}
return nil, os.ErrNotExist
}

View File

@@ -0,0 +1,23 @@
// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
package configs_test
import (
"github.com/TeaOSLab/EdgeNode/internal/configs"
"gopkg.in/yaml.v3"
"testing"
)
func TestLoadClusterConfig(t *testing.T) {
config, err := configs.LoadClusterConfig()
if err != nil {
t.Fatal(err)
}
t.Logf("%+v", config)
configData, err := yaml.Marshal(config)
if err != nil {
t.Fatal(err)
}
t.Log(string(configData))
}

View File

@@ -36,7 +36,6 @@ import (
"github.com/iwind/TeaGo/maps" "github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types" "github.com/iwind/TeaGo/types"
"github.com/iwind/gosock/pkg/gosock" "github.com/iwind/gosock/pkg/gosock"
"gopkg.in/yaml.v3"
"log" "log"
"os" "os"
"os/exec" "os/exec"
@@ -524,20 +523,13 @@ func (this *Node) startSyncTimer() {
// 检查集群设置 // 检查集群设置
func (this *Node) checkClusterConfig() error { func (this *Node) checkClusterConfig() error {
var configFile = Tea.ConfigFile("cluster.yaml") config, err := configs.LoadClusterConfig()
data, err := os.ReadFile(configFile)
if err != nil { if err != nil {
return err return err
} }
config := &configs.ClusterConfig{}
err = yaml.Unmarshal(data, config)
if err != nil {
return err
}
rpcClient, err := rpc.NewRPCClient(&configs.APIConfig{ rpcClient, err := rpc.NewRPCClient(&configs.APIConfig{
RPCEndpoints: config.RPC.Endpoints, RPCEndpoints: config.RPCEndpoints,
RPCDisableUpdate: config.RPC.DisableUpdate, RPCDisableUpdate: config.RPCDisableUpdate,
NodeId: config.ClusterId, NodeId: config.ClusterId,
Secret: config.Secret, Secret: config.Secret,
}) })