-
Notifications
You must be signed in to change notification settings - Fork 123
/
config.go
66 lines (53 loc) · 1.4 KB
/
config.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
package config
import (
"github.com/siddontang/go-yaml/yaml"
"io/ioutil"
)
type NodeConfig struct {
Name string `yaml:"name"`
DownAfterNoAlive int `yaml:"down_after_noalive"`
IdleConns int `yaml:"idle_conns"`
RWSplit bool `yaml:"rw_split"`
User string `yaml:"user"`
Password string `yaml:"password"`
Master string `yaml:"master"`
Slave string `yaml:"slave"`
}
type SchemaConfig struct {
DB string `yaml:"db"`
Nodes []string `yaml:"nodes"`
RulesConifg RulesConfig `yaml:"rules"`
}
type RulesConfig struct {
Default string `yaml:"default"`
ShardRule []ShardConfig `yaml:"shard"`
}
type ShardConfig struct {
Table string `yaml:"table"`
Key string `yaml:"key"`
Nodes []string `yaml:"nodes"`
Type string `yaml:"type"`
Range string `yaml:"range"`
}
type Config struct {
Addr string `yaml:"addr"`
User string `yaml:"user"`
Password string `yaml:"password"`
LogLevel string `yaml:"log_level"`
Nodes []NodeConfig `yaml:"nodes"`
Schemas []SchemaConfig `yaml:"schemas"`
}
func ParseConfigData(data []byte) (*Config, error) {
var cfg Config
if err := yaml.Unmarshal([]byte(data), &cfg); err != nil {
return nil, err
}
return &cfg, nil
}
func ParseConfigFile(fileName string) (*Config, error) {
data, err := ioutil.ReadFile(fileName)
if err != nil {
return nil, err
}
return ParseConfigData(data)
}