-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathcoordinator.go
84 lines (74 loc) · 2.76 KB
/
coordinator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package config
import (
"encoding/json"
"fmt"
"log"
"os"
"strings"
"github.com/BurntSushi/toml"
"gopkg.in/yaml.v2"
)
var cfgCoordinator Coordinator
type Coordinator struct {
LogLevel string `json:"log_level" toml:"log_level" yaml:"log_level"`
QdbAddr string `json:"qdb_addr" toml:"qdb_addr" yaml:"qdb_addr"`
CoordinatorPort string `json:"coordinator_port" toml:"coordinator_port" yaml:"coordinator_port"`
GrpcApiPort string `json:"grpc_api_port" toml:"grpc_api_port" yaml:"grpc_api_port"`
Host string `json:"host" toml:"host" yaml:"host"`
Auth *AuthCfg `json:"auth" toml:"auth" yaml:"auth"`
FrontendTLS *TLSConfig `json:"frontend_tls" yaml:"frontend_tls" toml:"frontend_tls"`
ShardDataCfg string `json:"shard_data" toml:"shard_data" yaml:"shard_data"`
UseSystemdNotifier bool `json:"use_systemd_notifier" toml:"use_systemd_notifier" yaml:"use_systemd_notifier"`
SystemdNotifierDebug bool `json:"systemd_notifier_debug" toml:"systemd_notifier_debug" yaml:"systemd_notifier_debug"`
}
// LoadCoordinatorCfg loads the coordinator configuration from the specified file path.
//
// Parameters:
// - cfgPath (string): The path of the configuration file.
//
// Returns:
// - error: An error if any occurred during the loading process.
func LoadCoordinatorCfg(cfgPath string) error {
file, err := os.Open(cfgPath)
if err != nil {
return err
}
defer file.Close()
if err := initCoordinatorConfig(file, cfgPath); err != nil {
return err
}
configBytes, err := json.MarshalIndent(&cfgCoordinator, "", " ")
if err != nil {
return err
}
log.Println("Running config:", string(configBytes))
return nil
}
// initCoordinatorConfig initializes the coordinator configuration based on the file content and file format.
//
// Parameters:
// - file (*os.File): the file containing the configuration data.
// - filepath (string): the path of the configuration file.
//
// Returns:
// - error: an error if any occurred during the initialization process.
func initCoordinatorConfig(file *os.File, filepath string) error {
if strings.HasSuffix(filepath, ".toml") {
_, err := toml.NewDecoder(file).Decode(&cfgCoordinator)
return err
}
if strings.HasSuffix(filepath, ".yaml") {
return yaml.NewDecoder(file).Decode(&cfgCoordinator)
}
if strings.HasSuffix(filepath, ".json") {
return json.NewDecoder(file).Decode(&cfgCoordinator)
}
return fmt.Errorf("unknown config format type: %s. Use .toml, .yaml or .json suffix in filename", filepath)
}
// CoordinatorConfig returns a pointer to the Coordinator configuration.
//
// Returns:
// - *Coordinator: a pointer to the Coordinator configuration.
func CoordinatorConfig() *Coordinator {
return &cfgCoordinator
}