-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
592 lines (542 loc) · 17.3 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
package eudore
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"reflect"
"regexp"
"runtime"
"strings"
"sync"
"time"
)
// The Config interface defines config read-write and parsing functions.
//
// Use [ConfigParseFunc] to implement custom parsing.
type Config interface {
// The Get method implements getting data,
// and uses the RLock method to lock the data,
//
// if key is empty string return metadata.
Get(key string) any
// The Set method implements setting data,
// and uses the Lock method to lock the data,
//
// If key is empty string set metadata.
Set(key string, val any) error
// ParseOption method adds [ConfigParseFunc],
// If it is empty, clear the current func list
ParseOption(fn ...ConfigParseFunc)
// The Parse method executes all [ConfigParseFunc].
// If the parsing funcs returns error, it stops parsing and returns error.
Parse(ctx context.Context) error
}
// ConfigParseFunc defines the [Config] parsing function.
//
// [context.Context] will trigger [DefaultConfigParseTimeout] when parsing.
//
// You can use [Config] to modify the configuration when parsing.
type ConfigParseFunc func(context.Context, Config) error
// configStd uses a structure or map to save configurations,
// and reads and writes properties through attributes or [reflect].
type configStd struct {
Data any `alias:"data"`
Map map[string]any `alias:"map"`
Funcs []ConfigParseFunc `alias:"funcs"`
Err error `alias:"err"`
Lock rwLocker `alias:"lock"`
}
type rwLocker interface {
sync.Locker
RLock()
RUnlock()
}
type MetadataConfig struct {
Health bool `json:"health" protobuf:"1,name=health" yaml:"health"`
Name string `json:"name" protobuf:"2,name=name" yaml:"name"`
Data any `json:"data,omitempty" protobuf:"3,name=data,omitempty" yaml:"data,omitempty"`
Error error `json:"error,omitempty" protobuf:"4,name=error,omitempty" yaml:"error,omitempty"`
}
// The NewConfig function creates a [Config] implementation.
//
// The data variable is the configuration data and
// is read and written using [GetAnyByPath]/[SetAnyByPath].
//
// If the data type is map[string]any, it is read and written using map key.
//
// If the data variable implements the [rwLocker] read-write lock interface,
// it will be used during [Config.Get]/[Config.Set].
//
// The default [ConfigParseFunc] is [DefaultConfigAllParseFunc].
func NewConfig(data any) Config {
if data == nil {
data = make(map[string]any)
}
mu, ok := data.(rwLocker)
if !ok {
mu = &sync.RWMutex{}
}
m, _ := data.(map[string]any)
return &configStd{
Data: data,
Map: m,
Funcs: append([]ConfigParseFunc{}, DefaultConfigAllParseFunc...),
Lock: mu,
}
}
func (c *configStd) Metadata() any {
return MetadataConfig{
Health: c.Err == nil,
Name: "eudore.configStd",
Data: anyMetadata(c.Data),
Error: c.Err,
}
}
// The Get method implements getting data,
// and uses the RLock method to lock the data,
//
// if key is empty string return metadata.
func (c *configStd) Get(key string) any {
c.Lock.RLock()
defer c.Lock.RUnlock()
if len(key) == 0 {
if c.Map != nil {
return &c.Map
}
return c.Data
}
if c.Map != nil {
return c.Map[key]
}
return GetAnyByPath(c.Data, key)
}
// The Set method implements setting data,
// and uses the Lock method to lock the data,
//
// If key is empty string set metadata.
func (c *configStd) Set(key string, val any) error {
c.Lock.Lock()
defer c.Lock.Unlock()
if len(key) == 0 {
c.Data = val
c.Map, _ = val.(map[string]any)
return nil
}
if c.Map != nil {
c.Map[key] = val
return nil
}
return SetAnyByPath(c.Data, key, val)
}
// ParseOption method adds [ConfigParseFunc],
// If it is empty, clear the current func list
//
// The default [ConfigParseFunc] is [eudore.ConfigAllParseFunc].
func (c *configStd) ParseOption(fn ...ConfigParseFunc) {
if fn == nil {
c.Funcs = nil
c.Err = nil
} else {
c.Funcs = append(c.Funcs, fn...)
}
}
// The Parse method executes all [ConfigParseFunc].
// If the parsing function returns error, it stops parsing and returns error.
func (c *configStd) Parse(ctx context.Context) error {
if c.Err != nil {
return c.Err
}
ctx, cancel := context.WithTimeout(ctx, DefaultConfigParseTimeout)
defer cancel()
for _, fn := range c.Funcs {
c.Err = fn(ctx, c)
if c.Err != nil {
if !errors.Is(c.Err, context.Canceled) {
// replace logger in parsing
name := runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name()
c.Err = fmt.Errorf(ErrConfigParseError, name, c.Err)
NewLoggerWithContext(ctx).Error(c.Err.Error())
}
return c.Err
}
}
NewLoggerWithContext(ctx).Info("config parse done")
return nil
}
// MarshalJSON implements the [json.Marshaler] interface.
func (c *configStd) MarshalJSON() ([]byte, error) {
c.Lock.RLock()
defer c.Lock.RUnlock()
return json.Marshal(c.Data)
}
// UnmarshalJSON implements the [json.Unmarshaler] interface.
func (c *configStd) UnmarshalJSON(data []byte) error {
c.Lock.Lock()
defer c.Lock.Unlock()
return json.Unmarshal(data, &c.Data)
}
// The NewConfigParseJSON function creates [ConfigParseFunc] to parse the json
// configuration file.
//
// refer: [NewConfigParseDecoder].
func NewConfigParseJSON(key string) ConfigParseFunc {
fn := NewConfigParseDecoder(key, "json",
func(reader io.Reader, data any) error {
return json.NewDecoder(reader).Decode(data)
},
)
// update func name
return func(ctx context.Context, conf Config) error {
return fn(ctx, conf)
}
}
// The NewConfigParseDecoder function creates [ConfigParseFunc]
// to parse the specified decoder configuration file.
//
// Get the configuration file path from the command line --{key}=,
// environment variable EUDORE_{KEY}, and conf.Get(key) in order.
//
// The allowed type is string or []string.
//
// Will try to load {key} and workdir from [os.Args] and [os.Getenv],
// and the env prefix uses [DefaultConfigEnvPrefix].
func NewConfigParseDecoder(key, format string,
decode func(io.Reader, any) error,
) ConfigParseFunc {
return func(ctx context.Context, conf Config) error {
log := NewLoggerWithContext(ctx)
log.Infof("config read %s file by key: %s", format, key)
for _, path := range getConfigPath(conf, key) {
path = strings.TrimSpace(path)
file, err := os.Open(path)
if err != nil {
if !os.IsNotExist(err) {
log.Warningf("config ignored file: %s", err)
}
continue
}
defer file.Close()
if format == "json" {
err = decode(file, conf)
} else {
err = decode(file, conf.Get(""))
}
if err != nil {
err = fmt.Errorf(ErrConfigParseDecoder, format, path, err)
log.Info(err)
return err
}
log.Infof("config load %s file: %s", format, path)
}
return nil
}
}
func getConfigPath(conf Config, key string) []string {
config := getValueFromArgsAndEnv(key)
if config != "" {
workdir := getValueFromArgsAndEnv("workdir")
workabs, _ := filepath.Abs(workdir)
wd, _ := os.Getwd()
if strings.EqualFold(wd, workdir) || strings.EqualFold(wd, workabs) {
workdir = ""
}
if !filepath.IsAbs(config) {
config = filepath.Join(workdir, config)
}
return strings.Split(config, ";")
}
switch val := conf.Get(key).(type) {
case string:
return strings.Split(val, ";")
case []string:
return val
default:
return nil
}
}
func getValueFromArgsAndEnv(key string) string {
keyarg := fmt.Sprintf("--%s=", key)
for _, str := range os.Args {
if strings.HasPrefix(str, keyarg) {
return str[3+len(key):]
}
}
return os.Getenv(DefaultConfigEnvPrefix + strings.ToUpper(key))
}
// The NewConfigParseEnvs function creates [ConfigParseFunc] to parse
// [os.Environ] into [Config].
//
// Environment variables will remove prefixes and convert to lowercase paths,
// converting '_' to '.'.
//
// exmapel: 'ENV_EUDORE_NAME=eudore' => 'eudore.name=eudore'.
//
// Note: The path after env conversion is all lowercase, and the structure needs
// to specify an 'alias' tag.
func NewConfigParseEnvs(prefix string) ConfigParseFunc {
if prefix == "" {
prefix = DefaultConfigEnvPrefix
}
l := len(prefix)
return func(ctx context.Context, conf Config) error {
log := NewLoggerWithContext(ctx)
for _, env := range os.Environ() {
if strings.HasPrefix(env, prefix) {
log.Infof("set os environment: %s", env)
k, v, _ := strings.Cut(env, "=")
if k != "" {
_ = conf.Set(strings.ToLower(strings.ReplaceAll(k[l:], "_", ".")), v)
}
}
}
return nil
}
}
// The NewConfigParseArgs function creates [ConfigParseFunc] to parse [os.Args]
// into [Config].
//
// Command line parameters use '--{key}.{sub}={value}' format,
// short parameters use '-{key}={value}'.
//
// if the struct has 'flag' tag, will be used as an abbreviation for the path.
func NewConfigParseArgs() ConfigParseFunc {
return func(ctx context.Context, conf Config) error {
log := NewLoggerWithContext(ctx)
args := []string{}
// Initialize shorts using struct flag tag
shorts := newStructShorts(conf.Get(""))
for _, str := range os.Args[1:] {
key, val, _ := strings.Cut(str, "=")
switch {
case strings.HasPrefix(key, "--"): // full param
log.Info("set os argument: " + str)
_ = conf.Set(key[2:], val)
case len(key) > 1 && key[0] == '-' && key[1] != '-': // short param
for _, lkey := range shorts[key[1:]] {
log.Infof("set os short argument: %s --%s=%s",
key[1:], lkey, val,
)
_ = conf.Set(lkey, val)
}
default:
args = append(args, str)
}
}
_ = conf.Set("args", args)
return nil
}
}
// The NewConfigParseWorkdir function creates [ConfigParseFunc] to initializes
// the workspace,
// usually using the key as string("workdir") to get the workspace directory and
// changes.
func NewConfigParseWorkdir(key string) ConfigParseFunc {
return func(ctx context.Context, conf Config) error {
dir, ok := conf.Get(key).(string)
if ok && dir != "" {
NewLoggerWithContext(ctx).Info(
"changes working directory to: " + dir,
)
return os.Chdir(dir)
}
return nil
}
}
/*
The NewConfigParseEnvFile function creates the [ConfigParseFunc] parsing
environment files.
The default ENV file is [DefaultConfigEnvFiles], using ; as a separator.
If the line Env is in the env format, use [os.Setenv] to set it to the process.
If the Env value is an empty string, use [os.Unsetenv] to delete this Env.
If the first character of the Env value is ', it is a multi-line value until the
end of a line also has ',
Multi-line values replace \r\n to \n and exec TrimSpace.
example:
EUDORE_NAME=eudore
EUDORE_DEBUG=
EUDORE_KEY='
-----BEGIN RSA PRIVATE KEY-----
-----END RSA PRIVATE KEY-----
'
*/
func NewConfigParseEnvFile(files ...string) ConfigParseFunc {
if files == nil {
files = strings.Split(DefaultConfigEnvFiles, ";")
}
return func(ctx context.Context, _ Config) error {
log := NewLoggerWithContext(ctx)
for _, file := range files {
data, err := os.ReadFile(file)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
log.Info("confif load env file", file)
parseEnvFile(log, data)
}
return nil
}
}
var regEnvLine = regexp.MustCompile(`[a-zA-Z]\w*`)
func parseEnvFile(log Logger, data []byte) {
lines := strings.Split(
strings.ReplaceAll(string(data), "\r\n", "\n"),
"\n",
)
char := "'"
for i := range lines {
key, val, ok := strings.Cut(lines[i], "=")
if ok && regEnvLine.MatchString(key) {
if strings.HasPrefix(val, char) {
if strings.HasSuffix(val, char) {
val = strings.TrimSuffix(val[1:], char)
val = strings.TrimSpace(val)
} else if i+1 < len(lines) {
lines[i+1] = lines[i] + "\n" + lines[i+1]
continue
}
}
log.Infof("set file environment: %s=%s", key, val)
if val != "" {
os.Setenv(key, val)
} else {
os.Unsetenv(key)
}
}
}
}
/*
The NewConfigParseJSON function creates the [ConfigParseFunc] parsing
environment and changes it to the global default configuration.
env to keys:
ENV_CONFIG_PARSE_TIMEOUT => DefaultConfigParseTimeout
ENV_CONTEXT_MAX_APPLICATION_FORM_SIZE => DefaultContextMaxApplicationFormSize
ENV_CONTEXT_MAX_MULTIPART_FORM_MEMORY => DefaultContextMaxMultipartFormMemory
ENV_HANDLER_DATA_TEMPLATE_RELOAD => DefaultHandlerDataTemplateReload
ENV_HANDLER_EMBED_CACHE_CONTROL => DefaultHandlerEmbedCacheControl
ENV_HANDLER_EMBED_TIME => DefaultHandlerEmbedTime
ENV_HANDLER_EXTENDER_SHOW_NAME => DefaultHandlerExtenderShowName
ENV_LOGGER_ENTRY_BUFFER_LENGTH => DefaultLoggerEntryBufferLength
ENV_LOGGER_ENTRY_FIELDS_LENGTH => DefaultLoggerEntryFieldsLength
ENV_LOGGER_FORMATTER => DefaultLoggerFormatter
ENV_LOGGER_FORMATTER_FORMAT_TIME => DefaultLoggerFormatterFormatTime
ENV_LOGGER_FORMATTER_KEY_LEVEL => DefaultLoggerFormatterKeyLevel
ENV_LOGGER_FORMATTER_KEY_MESSAGE => DefaultLoggerFormatterKeyMessage
ENV_LOGGER_FORMATTER_KEY_TIME => DefaultLoggerFormatterKeyTime
ENV_LOGGER_HOOK_FATAL => DefaultLoggerHookFatal
ENV_LOGGER_WRITER_STDOUT => DefaultLoggerWriterStdout
ENV_LOGGER_WRITER_STDOUT_COLOR => DefaultLoggerWriterStdoutColor
ENV_ROUTER_LOGGER_KIND => DefaultRouterLoggerKind
ENV_SERVER_READ_TIMEOUT => DefaultServerReadTimeout
ENV_SERVER_READ_HEADER_TIMEOUT => DefaultServerReadHeaderTimeout
ENV_SERVER_WRITE_TIMEOUT => DefaultServerWriteTimeout
ENV_SERVER_IDLE_TIMEOUT => DefaultServerIdleTimeout
ENV_SERVER_SHUTDOWN_WAIT => DefaultServerShutdownWait
ENV_DAEMON_PIDFILE => DefaultDaemonPidfile
ENV_GODOC_SERVER => DefaultGodocServer
*/
func NewConfigParseDefault() ConfigParseFunc {
return func(_ context.Context, _ Config) error {
parseEnvDefault(&DefaultConfigParseTimeout, "CONFIG_PARSE_TIMEOUT")
parseEnvDefault(&DefaultContextMaxApplicationFormSize, "CONTEXT_MAX_APPLICATION_FORM_SIZE")
parseEnvDefault(&DefaultContextMaxMultipartFormMemory, "CONTEXT_MAX_MULTIPART_FORM_MEMORY")
parseEnvDefault(&DefaultHandlerDataTemplateReload, "HANDLER_DATA_TEMPLATE_RELOAD")
parseEnvDefault(&DefaultHandlerEmbedCacheControl, "HANDLER_EMBED_CACHE_CONTROL")
parseEnvDefault(&DefaultHandlerEmbedTime, "HANDLER_EMBED_TIME")
parseEnvDefault(&DefaultHandlerExtenderShowName, "HANDLER_EXTENDER_SHOW_NAME")
parseEnvDefault(&DefaultLoggerEntryBufferLength, "LOGGER_ENTRY_BUFFER_LENGTH")
parseEnvDefault(&DefaultLoggerEntryFieldsLength, "LOGGER_ENTRY_FIELDS_LENGTH")
parseEnvDefault(&DefaultLoggerFormatter, "LOGGER_FORMATTER")
parseEnvDefault(&DefaultLoggerFormatterFormatTime, "LOGGER_FORMATTER_FORMAT_TIME")
parseEnvDefault(&DefaultLoggerFormatterKeyLevel, "LOGGER_FORMATTER_KEY_LEVEL")
parseEnvDefault(&DefaultLoggerFormatterKeyMessage, "LOGGER_FORMATTER_KEY_MESSAGE")
parseEnvDefault(&DefaultLoggerFormatterKeyTime, "LOGGER_FORMATTER_KEY_TIME")
parseEnvDefault(&DefaultLoggerHookFatal, "LOGGER_HOOK_FATAL")
parseEnvDefault(&DefaultLoggerWriterStdout, "LOGGER_WRITER_STDOUT")
parseEnvDefault(&DefaultLoggerWriterStdoutColor, "LOGGER_WRITER_STDOUT_COLOR")
parseEnvDefault(&DefaultRouterLoggerKind, "ROUTER_LOGGER_KIND")
parseEnvDefault(&DefaultServerReadTimeout, "SERVER_READ_TIMEOUT")
parseEnvDefault(&DefaultServerReadHeaderTimeout, "SERVER_READ_HEADER_TIMEOUT")
parseEnvDefault(&DefaultServerWriteTimeout, "SERVER_WRITE_TIMEOUT")
parseEnvDefault(&DefaultServerIdleTimeout, "SERVER_IDLE_TIMEOUT")
parseEnvDefault(&DefaultServerShutdownWait, "SERVER_SHUTDOWN_WAIT")
parseEnvDefault(&DefaultDaemonPidfile, "DAEMON_PIDFILE")
parseEnvDefault(&DefaultGodocServer, "GODOC_SERVER")
return nil
}
}
func parseEnvDefault[T string | bool | time.Time | time.Duration |
typeNumber](val *T, key string) {
*val = GetAnyByString(os.Getenv(DefaultConfigEnvPrefix+key), *val)
}
type eachTags struct {
shorts map[string][]string
repeat map[uintptr]string
}
func newStructShorts(data any) map[string][]string {
each := &eachTags{
shorts: make(map[string][]string),
repeat: make(map[uintptr]string),
}
each.Each("", reflect.ValueOf(data))
return each.shorts
}
func (each *eachTags) Each(prefix string, v reflect.Value) {
switch v.Kind() {
case reflect.Chan, reflect.Func, reflect.Map,
reflect.Ptr, reflect.Slice, reflect.UnsafePointer:
if !v.IsNil() {
_, ok := each.repeat[v.Pointer()]
if ok {
return
}
each.repeat[v.Pointer()] = prefix
}
}
switch v.Kind() {
case reflect.Ptr, reflect.Interface:
if !v.IsNil() {
each.Each(prefix, v.Elem())
}
case reflect.Struct:
iType := v.Type()
for i := 0; i < iType.NumField(); i++ {
if v.Field(i).CanSet() {
flag := iType.Field(i).Tag.Get("flag")
name := iType.Field(i).Tag.Get("alias")
if name == "" {
name = iType.Field(i).Name
}
if flag != "" && getEachValueKind(iType.Field(i).Type) {
val := strings.TrimPrefix(prefix+"."+name, ".")
each.shorts[flag] = append(each.shorts[flag], val)
} else {
each.Each(prefix+"."+name, v.Field(i))
}
}
}
}
}
func getEachValueKind(iType reflect.Type) bool {
switch iType.Kind() {
case reflect.String, reflect.Bool, reflect.Float32, reflect.Float64,
reflect.Int, reflect.Uint,
reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return true
case reflect.Ptr:
return getEachValueKind(iType.Elem())
default:
if iType.Kind() == reflect.Slice &&
iType.Elem().Kind() == reflect.Uint8 {
return true
}
return false
}
}