-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
71 lines (57 loc) · 1.43 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
67
68
69
70
71
package main
import (
"fmt"
"gopkg.in/gcfg.v1"
"net"
"strings"
)
type ConfigError interface {
error
}
func newConfigError(format string, args ...interface{}) ConfigError {
return fmt.Errorf(format, args...)
}
type Config struct {
Main struct {
Primary string
Follower string
Backends []string
}
Health struct {
Username string
Password string
Database string
}
}
func ConfigFromFile(filename string) (c *Config, err error) {
c = &Config{}
if err := gcfg.ReadFileInto(c, filename); err != nil {
return nil, err
}
_, _, err = net.SplitHostPort(c.Main.Primary)
if err != nil {
return nil, newConfigError("Main.Primary: %s", c.Main.Primary, err)
}
_, _, err = net.SplitHostPort(c.Main.Follower)
if err != nil {
return nil, newConfigError("Main.Follower: %s", c.Main.Follower, err)
}
c.Main.Backends = strings.Split(c.Main.Backends[0], ",")
if len(c.Main.Backends) < 1 {
return nil, newConfigError("Main.Backends contains no backend definitions")
}
for i := range c.Main.Backends {
c.Main.Backends[i] = strings.TrimSpace(c.Main.Backends[i])
_, _, err = net.SplitHostPort(c.Main.Backends[i])
if err != nil {
return nil, newConfigError("Invalid backend '%s': %s", c.Main.Backends[i], err)
}
}
if c.Health.Username == "" {
return nil, newConfigError("No health-check username defined")
}
if c.Health.Database == "" {
return nil, newConfigError("No health-check database defined")
}
return c, nil
}