|
|
@@ -36,11 +36,11 @@ type Auth struct {
|
|
36
|
36
|
|
|
37
|
37
|
// Service 微服务配置
|
|
38
|
38
|
type Service struct {
|
|
39
|
|
- Port int `yaml:"port"`
|
|
40
|
|
- ReadTimeout int `yaml:"read_timeout"`
|
|
41
|
|
- WriteTimeout int `yaml:"write_timeout"`
|
|
42
|
|
- IdleTimeout int `yaml:"idle_timeout"`
|
|
43
|
|
- TrustedProxies []string `yaml:"trusted_proxies"`
|
|
|
39
|
+ Port int `yaml:"port"`
|
|
|
40
|
+ ReadTimeout int `yaml:"read_timeout"`
|
|
|
41
|
+ WriteTimeout int `yaml:"write_timeout"`
|
|
|
42
|
+ IdleTimeout int `yaml:"idle_timeout"`
|
|
|
43
|
+ TrustedProxies string `yaml:"trusted_proxies"`
|
|
44
|
44
|
}
|
|
45
|
45
|
|
|
46
|
46
|
// 导出接口,防止外部修改
|
|
|
@@ -108,6 +108,7 @@ func GetInitError() error {
|
|
108
|
108
|
|
|
109
|
109
|
// loadConfig 加载配置文件
|
|
110
|
110
|
func loadConfig() (*Config, error) {
|
|
|
111
|
+
|
|
111
|
112
|
configFile, err := findConfigFile()
|
|
112
|
113
|
if err != nil {
|
|
113
|
114
|
return nil, err
|
|
|
@@ -115,19 +116,28 @@ func loadConfig() (*Config, error) {
|
|
115
|
116
|
|
|
116
|
117
|
fmt.Printf("✅ Using config file: %s\n", configFile)
|
|
117
|
118
|
|
|
|
119
|
+ // 先创建带默认值的配置
|
|
|
120
|
+ config := &Config{
|
|
|
121
|
+ Service: Service{
|
|
|
122
|
+ Port: 8080,
|
|
|
123
|
+ ReadTimeout: 30,
|
|
|
124
|
+ WriteTimeout: 30,
|
|
|
125
|
+ IdleTimeout: 60,
|
|
|
126
|
+ TrustedProxies: "",
|
|
|
127
|
+ },
|
|
|
128
|
+ }
|
|
118
|
129
|
// 读取配置文件
|
|
119
|
130
|
data, err := os.ReadFile(configFile)
|
|
120
|
131
|
if err != nil {
|
|
121
|
132
|
return nil, fmt.Errorf("failed to read config file %s: %v", configFile, err)
|
|
122
|
133
|
}
|
|
123
|
134
|
|
|
124
|
|
- var config Config
|
|
125
|
135
|
err = yaml.Unmarshal(data, &config)
|
|
126
|
136
|
if err != nil {
|
|
127
|
137
|
return nil, fmt.Errorf("failed to parse config file: %v", err)
|
|
128
|
138
|
}
|
|
129
|
139
|
|
|
130
|
|
- return &config, nil
|
|
|
140
|
+ return config, nil
|
|
131
|
141
|
}
|
|
132
|
142
|
|
|
133
|
143
|
// findConfigFile 查找配置文件
|
|
|
@@ -157,14 +167,14 @@ func findConfigFile() (string, error) {
|
|
157
|
167
|
exeDir = filepath.Dir(exePath)
|
|
158
|
168
|
}
|
|
159
|
169
|
|
|
160
|
|
- return "", fmt.Errorf(`No configuration file found!
|
|
|
170
|
+ return "", fmt.Errorf(`no configuration file found!
|
|
161
|
171
|
|
|
162
|
172
|
Tried locations:
|
|
163
|
173
|
1. Executable directory: %s/db.yaml
|
|
164
|
174
|
2. Environment variable: DB_CONFIG_PATH
|
|
165
|
175
|
|
|
166
|
176
|
Solutions:
|
|
167
|
|
-- Place db.yaml in the same directory as the executable
|
|
|
177
|
+- Place db.yaml in the same directory as the executable
|
|
168
|
178
|
- Or set DB_CONFIG_PATH environment variable to config file path
|
|
169
|
179
|
|
|
170
|
180
|
Example:
|