-
Notifications
You must be signed in to change notification settings - Fork 489
/
config.go
75 lines (61 loc) · 1.73 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
package server
import (
"flag"
"github.com/weaveworks/common/logging"
)
// LogLevel wraps the logging.Level type to allow defining IsZero, which is required to make omitempty work when marshalling YAML.
type LogLevel struct {
logging.Level `yaml:",inline"`
}
func (l LogLevel) IsZero() bool {
return l.Level.String() == ""
}
// Config holds dynamic configuration options for a Server.
type Config struct {
LogLevel LogLevel `yaml:"log_level,omitempty"`
LogFormat logging.Format `yaml:"log_format,omitempty"`
GRPC GRPCConfig `yaml:",inline"`
HTTP HTTPConfig `yaml:",inline"`
}
// UnmarshalYAML unmarshals the server config with defaults applied.
func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
*c = DefaultConfig()
type config Config
return unmarshal((*config)(c))
}
// HTTPConfig holds dynamic configuration options for the HTTP server.
type HTTPConfig struct {
TLSConfig TLSConfig `yaml:"http_tls_config,omitempty"`
}
// GRPCConfig holds dynamic configuration options for the gRPC server.
type GRPCConfig struct {
TLSConfig TLSConfig `yaml:"grpc_tls_config,omitempty"`
}
// Default configuration structs.
var (
emptyFlagSet = flag.NewFlagSet("", flag.ExitOnError)
DefaultLogLevel = func() LogLevel {
var lvl LogLevel
lvl.RegisterFlags(emptyFlagSet)
return lvl
}()
DefaultLogFormat = func() logging.Format {
var fmt logging.Format
fmt.RegisterFlags(emptyFlagSet)
return fmt
}()
)
func DefaultConfig() Config {
DefaultHTTPConfig := HTTPConfig{
// No non-zero defaults yet
}
DefaultGRPCConfig := GRPCConfig{
// No non-zero defaults yet
}
return Config{
GRPC: DefaultGRPCConfig,
HTTP: DefaultHTTPConfig,
LogLevel: DefaultLogLevel,
LogFormat: DefaultLogFormat,
}
}