-
Notifications
You must be signed in to change notification settings - Fork 6
/
default_config.go
263 lines (249 loc) · 6.76 KB
/
default_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
256
257
258
259
260
261
262
263
package core
import (
"fmt"
stdlog "log"
"net"
"github.com/DoNewsCode/core/config"
"github.com/DoNewsCode/core/contract"
"github.com/DoNewsCode/core/contract/lifecycle"
"github.com/DoNewsCode/core/di"
"github.com/DoNewsCode/core/events"
"github.com/DoNewsCode/core/logging"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/rawbytes"
"go.uber.org/dig"
)
const defaultConfig = `
name: app
env: local
http:
addr: :8080
disable: false
readTimeout: 2s
writeTimeout: 10s
idleTimeout: 10s
grpc:
addr: :9090
disable: false
cron:
disable: false
log:
level: debug
format: logfmt
redis:
default:
addrs:
- 127.0.0.1:6379
db: 0
gorm:
default:
database: sqlite
dsn: ":memory:"
`
// ProvideConfig is the default ConfigProvider for package Core.
func ProvideConfig(configStack []config.ProviderSet, configWatcher contract.ConfigWatcher) contract.ConfigUnmarshaler {
var (
stack []config.Option
err error
cfg contract.ConfigAccessor
)
for _, layer := range configStack {
stack = append(stack, config.WithProviderLayer(layer.Provider, layer.Parser))
}
stack = append(stack, config.WithProviderLayer(rawbytes.Provider([]byte(defaultConfig)), yaml.Parser()))
if configWatcher != nil {
stack = append(stack, config.WithWatcher(configWatcher))
}
cfg, err = config.NewConfig(stack...)
if err != nil {
stdlog.Fatal(err)
}
return cfg
}
// ProvideEnv is the default EnvProvider for package Core.
func ProvideEnv(conf contract.ConfigUnmarshaler) contract.Env {
return config.NewEnvFromConf(conf)
}
// ProvideAppName is the default AppNameProvider for package Core.
func ProvideAppName(conf contract.ConfigUnmarshaler) contract.AppName {
var appName config.AppName
err := conf.Unmarshal("name", &appName)
if err != nil {
return config.AppName("default")
}
return appName
}
// ProvideLogger is the default LoggerProvider for package Core.
func ProvideLogger(conf contract.ConfigUnmarshaler, appName contract.AppName, env contract.Env) log.Logger {
var (
lvl string
format string
err error
)
err = conf.Unmarshal("log.level", &lvl)
if err != nil {
lvl = "debug"
}
err = conf.Unmarshal("log.format", &format)
if err != nil {
format = "logfmt"
}
logger := logging.NewLogger(format)
logger = level.NewFilter(logger, logging.LevelFilter(lvl))
logger = level.NewInjector(logger, level.DebugValue())
return logger
}
// ProvideDi is the default DiProvider for package Core.
func ProvideDi(conf contract.ConfigUnmarshaler) *dig.Container {
return dig.New()
}
type lifecycleOut struct {
di.Out
lifecycle.ConfigReload
lifecycle.HTTPServerStart
lifecycle.HTTPServerShutdown
lifecycle.GRPCServerStart
lifecycle.GRPCServerShutdown
}
func provideLifecycle() lifecycleOut {
return lifecycleOut{
ConfigReload: &events.Event[contract.ConfigUnmarshaler]{},
HTTPServerStart: &events.Event[lifecycle.HTTPServerStartPayload]{},
HTTPServerShutdown: &events.Event[lifecycle.HTTPServerShutdownPayload]{},
GRPCServerStart: &events.Event[lifecycle.GRPCServerStartPayload]{},
GRPCServerShutdown: &events.Event[lifecycle.GRPCServerShutdownPayload]{},
}
}
// provideDefaultConfig exports config for "name", "version", "env", "http", "grpc".
func provideDefaultConfig() []config.ExportedConfig {
return []config.ExportedConfig{
{
Owner: "core",
Data: map[string]any{
"name": "app",
},
Comment: "The name of the application",
Validate: func(data map[string]any) error {
_, err := getString(data, "name")
if err != nil {
return fmt.Errorf("the name field is not valid: %w", err)
}
return nil
},
},
{
Owner: "core",
Data: map[string]any{
"env": "local",
},
Comment: "The environment of the application, one of production, development, staging, testing or local",
Validate: func(data map[string]any) error {
str, err := getString(data, "env")
if err != nil {
return fmt.Errorf("the env field is not valid: %w", err)
}
if config.NewEnv(str) != config.EnvUnknown {
return nil
}
return fmt.Errorf(
"the env field must be one of \"production\", \"development\", \"staging\", \"testing\" or \"local\", got %s", str)
},
},
{
Owner: "core",
Data: map[string]any{
"http": map[string]any{
"addr": ":8080",
"disable": false,
},
},
Comment: "The http address",
Validate: func(data map[string]any) error {
disable, err := getBool(data, "http", "disable")
if err != nil {
return fmt.Errorf("the http.disable field is not valid: %w", err)
}
if disable {
return nil
}
str, err := getString(data, "http", "addr")
if err != nil {
return fmt.Errorf("the http.addr field is not valid: %w", err)
}
if _, err := net.ResolveTCPAddr("tcp", str); err != nil {
return fmt.Errorf("the http.addr field must be an valid address like :8080, got %s", str)
}
return nil
},
},
{
Owner: "core",
Data: map[string]any{
"grpc": map[string]any{
"addr": ":9090",
"disable": false,
},
},
Comment: "The gRPC address",
Validate: func(data map[string]any) error {
disable, err := getBool(data, "grpc", "disable")
if err != nil {
return fmt.Errorf("the grpc.disable field is not valid: %w", err)
}
if disable {
return nil
}
str, err := getString(data, "grpc", "addr")
if err != nil {
return fmt.Errorf("the grpc.addr field is not valid: %w", err)
}
if _, err := net.ResolveTCPAddr("tcp", str); err != nil {
return fmt.Errorf("the grpc.addr field must be an valid address like :9090, got %s", str)
}
return nil
},
},
{
Owner: "core",
Data: map[string]any{
"cron": map[string]any{
"disable": false,
},
},
Comment: "The cron job runner",
Validate: func(data map[string]any) error {
_, err := getBool(data, "cron", "disable")
if err != nil {
return fmt.Errorf("the cron.disable field is not valid: %w", err)
}
return nil
},
},
{
Owner: "core",
Data: map[string]any{
"log": map[string]any{"level": "debug", "format": "logfmt"},
},
Comment: "The global logging level and format",
Validate: func(data map[string]any) error {
lvl, err := getString(data, "log", "level")
if err != nil {
return fmt.Errorf("the log.level field is not valid: %w", err)
}
if !isValidLevel(lvl) {
return fmt.Errorf("allowed levels are \"debug\", \"info\", \"warn\", \"error\", or \"none\", got \"%s\"", lvl)
}
format, err := getString(data, "log", "format")
if err != nil {
return fmt.Errorf("the log.format field is not valid: %w", err)
}
if !isValidFormat(format) {
return fmt.Errorf("the log format is not supported")
}
return nil
},
},
}
}