/
config.go
69 lines (59 loc) · 1.28 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
package main
import (
"os"
"regexp"
"github.com/pkg/errors"
)
const (
DefaultInterface = "127.0.0.1:9545"
DefaultLogLevel = "debug"
)
type Config struct {
Interface string
CertFile string
KeyFile string
LogFile string
LogLevel string
CacheDir string
Backends map[string]*Backend
Routes []*Route
}
var reTokens = regexp.MustCompile(`[,\s]+`)
func NewConfig() *Config {
return &Config{
Interface: any(os.Getenv("LISTEN"), DefaultInterface),
CertFile: os.Getenv("CERTFILE"),
KeyFile: os.Getenv("KEYFILE"),
LogFile: os.Getenv("LOGFILE"),
LogLevel: any(os.Getenv("LOGLEVEL"), DefaultLogLevel),
CacheDir: os.Getenv("CACHEDIR"),
Backends: map[string]*Backend{},
Routes: nil,
}
}
func any(vals ...string) string {
for _, s := range vals {
if s != "" {
return s
}
}
return ""
}
func (c *Config) Validate() error {
switch {
case c == nil:
return errors.New("is nil")
case c.CertFile != "" && c.KeyFile == "":
return errors.New("private key not set")
case c.CertFile == "" && c.KeyFile != "":
return errors.New("certificate not set")
case len(c.Backends) == 0:
return errors.New("backend not set")
}
for _, b := range c.Backends {
if err := b.Validate(); err != nil {
return errors.Wrap(err, "backend")
}
}
return nil
}