-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathconfig.go
255 lines (207 loc) · 6.53 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package gogo
import (
"fmt"
"io/ioutil"
"path"
"strings"
"github.com/dolab/gogo/pkgs/interceptors"
"github.com/dolab/logger"
yaml "gopkg.in/yaml.v2"
)
// default configurations
var (
DefaultServerConfig = &ServerConfig{
Addr: "127.0.0.1",
Port: 9090,
RTimeout: 5, // 5s
WTimeout: 10, // 10s
Ssl: false,
Healthz: true,
RequestID: DefaultRequestIDKey,
}
DefaultLoggerConfig = &LoggerConfig{
Output: "stderr",
LevelName: "info",
FilterFields: []string{"password", "token"},
}
DefaultSectionConfig = &SectionConfig{
Server: DefaultServerConfig,
Logger: DefaultLoggerConfig,
}
DefaultInterceptorConfig = new(InterceptorConfig)
)
// AppConfig defines config component of gogo.
// It implements Configer interface.
type AppConfig struct {
Mode RunMode `yaml:"mode"`
Name string `yaml:"name"`
Sections map[RunMode]interface{} `yaml:"sections"`
interceptors *InterceptorConfig
filename string
}
// NewAppConfig returns *AppConfig by parsing application.yml
func NewAppConfig(filename string) (*AppConfig, error) {
if strings.HasPrefix(filename, GogoSchema) {
return NewAppConfigFromDefault()
}
b, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
config, err := NewAppConfigFromString(string(b))
if err == nil {
config.filename = filename
}
return config, nil
}
// NewAppConfigFromString returns *AppConfig by unmarshaling yaml string passed
func NewAppConfigFromString(s string) (*AppConfig, error) {
var config *AppConfig
err := yaml.Unmarshal([]byte(s), &config)
if err != nil {
return nil, err
}
// default to development mode
if !config.Mode.IsValid() {
config.SetMode(Development)
}
// adjust app name
if config.Name == "" {
config.Name = "GOGO"
}
return config, nil
}
// NewAppConfigFromDefault returns *AppConfig of defaults
func NewAppConfigFromDefault() (*AppConfig, error) {
data, _ := yaml.Marshal(map[string]interface{}{
"mode": Development,
"name": "gogo",
"sections": map[RunMode]interface{}{
Development: DefaultSectionConfig,
Test: DefaultSectionConfig,
Production: DefaultSectionConfig,
},
})
return NewAppConfigFromString(string(data))
}
// RunMode returns the current mode of *AppConfig
func (config *AppConfig) RunMode() RunMode {
return config.Mode
}
// RunName returns the application name
func (config *AppConfig) RunName() string {
return config.Name
}
// Filename returns the /path/to/application.yml
func (config *AppConfig) Filename() string {
return config.filename
}
// SetMode changes config mode
func (config *AppConfig) SetMode(mode RunMode) {
if !mode.IsValid() {
return
}
config.Mode = mode
}
// Section is shortcut of retreving app config by section at once.
// It returns SectionConfig if exists, otherwise returns DefaultSectionConfig instead.
func (config *AppConfig) Section() *SectionConfig {
var section *SectionConfig
err := config.UnmarshalYAML(§ion)
if err != nil {
return DefaultSectionConfig
}
return section
}
// UnmarshalJSON parses JSON-encoded data of section and stores the result in the value pointed to by v.
// It returns ErrConfigSection error if section of the current mode does not exist.
func (config *AppConfig) UnmarshalJSON(v interface{}) error {
return fmt.Errorf("DEPRECATED!!! Please use yaml format")
}
// UnmarshalYAML parses YAML-encoded data of section and stores the result in the value pointed to by v.
// It returns ErrConfigSection error if section of the current mode does not exist.
func (config *AppConfig) UnmarshalYAML(v interface{}) error {
section, ok := config.Sections[config.Mode]
if !ok {
return ErrConfigSection
}
data, err := yaml.Marshal(section)
if err != nil {
return err
}
return yaml.Unmarshal(data, v)
}
// Interceptors returns a InterceptorConfiger wrapped with parsed YAML-encoded data of all middlewares.
func (config *AppConfig) Interceptors() interceptors.Configer {
return config.interceptors
}
// LoadInterceptors reads all config of interceptors
func (config *AppConfig) LoadInterceptors() error {
mode := config.RunMode().String()
cfgfile := path.Dir(config.Filename())
cfgfile = strings.TrimSuffix(cfgfile, "/config")
filename := FindInterceptorConfigFile(mode, cfgfile)
if strings.HasPrefix(filename, GogoSchema) {
config.interceptors = DefaultInterceptorConfig
return nil
}
b, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
err = yaml.Unmarshal(b, &config.interceptors)
if err != nil {
config.interceptors = DefaultInterceptorConfig
}
return err
}
// SectionConfig defines config spec for internal usage
type SectionConfig struct {
Server *ServerConfig `yaml:"server"`
Logger *LoggerConfig `yaml:"logger"`
}
// ServerConfig defines config spec of AppServer
type ServerConfig struct {
Addr string `yaml:"addr"` // listen address
Port int `yaml:"port"` // listen port
RTimeout int `yaml:"request_timeout"` // unit in second
WTimeout int `yaml:"response_timeout"` // unit in second
MaxHeaderBytes int `yaml:"max_header_bytes"` // unit in byte
RequestID string `yaml:"request_id"`
// ssl support
Ssl bool `yaml:"ssl"`
SslCert string `yaml:"ssl_cert"`
SslKey string `yaml:"ssl_key"`
HTTP2 bool `yaml:"http2"` // enable http2
Healthz bool `yaml:"healthz"` // enable /-/healthz
Throttle int `yaml:"throttle"` // in time.Second/throttle ms
Demotion int `yaml:"demotion"` // concurrency
}
// InterceptorConfig defines config spec of middleware
type InterceptorConfig map[string]interface{}
// Unmarshal parses YAML-encoded data of defined with name and stores the result in the
// value pointed to by v. It returns error if there is no config data for the name.
func (config *InterceptorConfig) Unmarshal(name string, v interface{}) error {
if config == nil {
return nil
}
idata, ok := (*config)[name]
if !ok {
return fmt.Errorf("no config data for middleware %q", name)
}
b, err := yaml.Marshal(idata)
if err != nil {
return err
}
return yaml.Unmarshal(b, v)
}
// LoggerConfig defines config spec of AppLogger
type LoggerConfig struct {
Output string `yaml:"output"` // valid values [stdout|stderr|null|nil|path/to/file]
LevelName string `yaml:"level"` // valid values [debug|info|warn|error]
FilterFields []string `yaml:"filter_fields"` // sensitive fields which should be filtered out when logging
}
// Level returns logger.Level by its name
func (l *LoggerConfig) Level() logger.Level {
return logger.ResolveLevelByName(l.LevelName)
}