-
Notifications
You must be signed in to change notification settings - Fork 0
/
env_config.go
107 lines (97 loc) · 3.28 KB
/
env_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
/*
* Copyright 2024 hopeio. All rights reserved.
* Licensed under the MIT License that can be found in the LICENSE file.
* @Created by jyb
*/
package initialize
import (
"github.com/hopeio/initialize/conf_center"
"github.com/hopeio/utils/log"
"github.com/hopeio/utils/reflect/mtos"
"github.com/spf13/viper"
"os"
"reflect"
"strings"
"unsafe"
)
const (
fixedFieldNameEnvConfig = "EnvConfig"
fixedFieldNameBasicConfig = "RootConfig"
fixedFieldNameConfigCenter = "ConfigCenter"
fixedFieldNameEnv = "Env"
fixedFieldNameEncoderRegistry = "encoderRegistry"
prefixConfigTemplate = "config.template."
prefixLocalTemplate = "local.template."
skipTypeTlsConfig = "tls.Config"
)
func (gc *globalConfig) setEnvConfig() {
if gc.RootConfig.Env == "" {
log.Warn("lack of env configuration, try single config file mode")
return
}
format := gc.RootConfig.ConfigCenter.Format
defer func() {
if gc.RootConfig.EnvConfig.ConfigTemplateDir != "" {
// template
confMap := make(map[string]any)
struct2Map(&gc.RootConfig.BasicConfig, confMap)
envMap := make(map[string]any)
struct2Map(&gc.RootConfig.EnvConfig, envMap)
confMap[gc.RootConfig.Env] = envMap
ccMap := make(map[string]any)
struct2Map(&gc.RootConfig.EnvConfig.ConfigCenter, ccMap)
envMap[fixedFieldNameConfigCenter] = ccMap
for name, v := range conf_center.GetRegisteredConfigCenter() {
cc := make(map[string]any)
struct2Map(v.Config(), cc)
ccMap[name] = cc
}
// unsafe
encoderRegistry := reflect.ValueOf(gc.Viper).Elem().FieldByName(fixedFieldNameEncoderRegistry).Elem()
fieldValue := reflect.NewAt(encoderRegistry.Type(), unsafe.Pointer(encoderRegistry.UnsafeAddr()))
data, err := fieldValue.Interface().(Encoder).Encode(format, confMap)
dir := gc.RootConfig.EnvConfig.ConfigTemplateDir
if dir[len(dir)-1] != '/' {
dir += "/"
}
err = os.WriteFile(dir+prefixConfigTemplate+format, data, 0644)
if err != nil {
log.Fatal(err)
}
}
}()
envConfig, ok := gc.Viper.Get(gc.RootConfig.Env).(map[string]any)
if !ok {
log.Warn("lack of environment configuration, try single config file")
return
}
err := mtos.Unmarshal(&gc.RootConfig.EnvConfig, envConfig)
if err != nil {
log.Fatal(err)
}
applyFlagConfig(nil, &gc.RootConfig.EnvConfig)
if gc.RootConfig.EnvConfig.ConfigCenter.Format == "" {
log.Fatalf("lack of configCenter format, support format:%v", viper.SupportedExts)
}
gc.Viper.SetConfigType(gc.RootConfig.EnvConfig.ConfigCenter.Format)
if gc.RootConfig.EnvConfig.ConfigCenter.Type == "" {
log.Warn("lack of configCenter type, try single config file")
return
}
configCenter, ok := conf_center.GetRegisteredConfigCenter()[strings.ToLower(gc.RootConfig.EnvConfig.ConfigCenter.Type)]
if !ok {
log.Warn("lack of registered configCenter, try single config file")
return
}
applyFlagConfig(gc.Viper, configCenter.Config())
gc.RootConfig.EnvConfig.ConfigCenter.ConfigCenter = configCenter
configCenterConfig, ok := gc.Viper.Get(gc.RootConfig.Env + ".configCenter." + gc.RootConfig.EnvConfig.ConfigCenter.Type).(map[string]any)
if !ok {
log.Warn("lack of configCenter config, try single config file")
return
}
err = mtos.Unmarshal(configCenter.Config(), configCenterConfig)
if err != nil {
log.Fatal(err)
}
}