-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
config.go
141 lines (127 loc) · 3.32 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package extconfig
import (
"fmt"
"os"
"path"
"beryju.io/gravity/pkg/storage"
env "github.com/Netflix/go-env"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type ExtConfig struct {
Debug bool `env:"DEBUG,default=false"`
LogLevel string `env:"LOG_LEVEL,default=info"`
DataPath string `env:"DATA_PATH,default=./data"`
Etcd struct {
Prefix string `env:"ETCD_PREFIX,default=/gravity"`
Endpoint string `env:"ETCD_ENDPOINT,default=localhost:2379"`
JoinCluster string `env:"ETCD_JOIN_CLUSTER"`
}
BootstrapRoles string `env:"BOOTSTRAP_ROLES,default=dns;dhcp;api;etcd;discovery;backup;monitoring;tsdb"`
Sentry struct {
Enabled bool `env:"SENTRY_ENABLED,default=false"`
DSN string `env:"SENTRY_DSN,default=https://731a93aa4a1a42a2960ac9eecee628c5@sentry.beryju.org/2"`
}
Instance struct {
Identifier string `env:"INSTANCE_IDENTIFIER"`
IP string `env:"INSTANCE_IP"`
Listen string `env:"INSTANCE_LISTEN"`
}
ListenOnlyMode bool `env:"LISTEN_ONLY,default=false"`
FallbackDNS string `env:"FALLBACK_DNS,default=1.1.1.1:53"`
logger *zap.Logger
}
type ExtConfigDirs struct {
EtcdDir string `json:"etcdDir"`
CertDir string `json:"certDir"`
BackupDir string `json:"backupDir"`
}
var globalExtConfig *ExtConfig
func Get() *ExtConfig {
if globalExtConfig != nil {
return globalExtConfig
}
var cfg ExtConfig
_, err := env.UnmarshalFromEnviron(&cfg)
if err != nil {
panic(err)
}
cfg.load()
globalExtConfig = &cfg
return &cfg
}
func (e *ExtConfig) Dirs() *ExtConfigDirs {
return &ExtConfigDirs{
EtcdDir: path.Join(e.DataPath, "etcd/"),
CertDir: path.Join(e.DataPath, "cert/"),
BackupDir: path.Join(e.DataPath, "backup/"),
}
}
func (e *ExtConfig) EtcdClient() *storage.Client {
return storage.NewClient(
e.Etcd.Prefix,
e.BuildLoggerWithLevel(zap.WarnLevel).Named("etcd.client"),
e.Debug,
e.Etcd.Endpoint,
)
}
func (e *ExtConfig) Listen(port int32) string {
listen := e.Instance.IP
if e.Instance.Listen != "" {
listen = e.Instance.Listen
}
return fmt.Sprintf("%s:%d", listen, port)
}
func (e *ExtConfig) Logger() *zap.Logger {
return e.logger
}
func (e *ExtConfig) BuildLogger() *zap.Logger {
l, err := zapcore.ParseLevel(e.LogLevel)
if err != nil {
l = zapcore.InfoLevel
}
if e.Debug {
l = zapcore.DebugLevel
}
return e.BuildLoggerWithLevel(l)
}
func (e *ExtConfig) BuildLoggerWithLevel(l zapcore.Level) *zap.Logger {
config := zap.Config{
Encoding: "json",
Development: false,
OutputPaths: []string{"stderr"},
ErrorOutputPaths: []string{"stderr"},
EncoderConfig: zap.NewProductionEncoderConfig(),
}
config.Level = zap.NewAtomicLevelAt(l)
config.DisableCaller = !e.Debug
if e.Debug {
config.Development = true
config.Encoding = "console"
config.EncoderConfig = zap.NewDevelopmentEncoderConfig()
config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
}
config.EncoderConfig.EncodeDuration = zapcore.MillisDurationEncoder
log, err := config.Build()
if err != nil {
panic(err)
}
return log
}
func (e *ExtConfig) load() {
e.logger = e.BuildLogger()
if e.Instance.Identifier == "" {
h, err := os.Hostname()
if err != nil {
panic(err)
}
e.Instance.Identifier = h
}
if e.Instance.IP == "" {
instIp, err := e.GetIP()
if err != nil {
panic(err)
}
e.Instance.IP = instIp.String()
}
}