/
config_yaml.go
91 lines (75 loc) · 1.87 KB
/
config_yaml.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
85
86
87
88
89
90
91
package gosql
import (
"os"
"path/filepath"
)
type Decoder interface {
Decode(interface{}) error
}
func ProvideYamlConfigFile(path string) (*os.File, func(), error) {
abs, err := filepath.Abs(path)
if nil != err {
return nil, nil, err
}
file, err := os.Open(abs)
if nil != err {
return nil, nil, err
}
cleanup := func() {
file.Close()
}
return file, cleanup, nil
}
func ProvideYamlConfig(dec Decoder) (*YamlConfig, error) {
conf := &YamlConfig{}
err := dec.Decode(conf)
if nil != err {
return nil, err
}
return conf, nil
}
type YamlConfig struct {
Cluster YamlClusterConfig `yaml:"cluster"`
Shards []*YamlShardsConfig `yaml:"shards"`
}
type YamlClusterConfig struct {
YamlDBConfig `yaml:",inline"`
Sharding YamlShardingConfig `yaml:"sharding"`
}
type YamlDBConfig struct {
Driver string `yaml:"driver"`
Dbname string `yaml:"dbname"`
Type string `yaml:"type"`
Conn YamlConnConfig `yaml:"conn"`
}
type YamlConnConfig struct {
MaxLifetime int `yaml:"max_life_time"`
MaxIdleConns int `yaml:"max_idle_conns"`
MaxOpenConns int `yaml:"max_open_conns"`
}
type YamlShardingConfig struct {
DbTotal int `yaml:"total"`
Table []*YamlTableShardingConfig `yaml:"table"`
}
type YamlTableShardingConfig struct {
Name string `yaml:"name"`
Total int `yaml:"total"`
}
type YamlShardsConfig struct {
Master YamlMasterConfig `yaml:"master"`
Replicas YamlReplicasConfig `yaml:"replicas"`
}
type YamlMasterConfig struct {
Host string `yaml:"host"`
YamlNodeConfig `yaml:",inline"`
}
type YamlReplicasConfig struct {
Hosts []string `yaml:"hosts"`
YamlNodeConfig `yaml:",inline"`
}
type YamlNodeConfig struct {
User string `yaml:"user"`
Password string `yaml:"password"`
Protocol string `yaml:"protocol"`
Params map[string]string `yaml:"params"`
}