Bläddra i källkod

增加服务配置参数默认值

qdy 3 månader sedan
förälder
incheckning
f45693c2f4
1 ändrade filer med 19 tillägg och 9 borttagningar
  1. 19
    9
      config/config.go

+ 19
- 9
config/config.go Visa fil

36
 
36
 
37
 // Service 微服务配置
37
 // Service 微服务配置
38
 type Service struct {
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
 
108
 
109
 // loadConfig 加载配置文件
109
 // loadConfig 加载配置文件
110
 func loadConfig() (*Config, error) {
110
 func loadConfig() (*Config, error) {
111
+
111
 	configFile, err := findConfigFile()
112
 	configFile, err := findConfigFile()
112
 	if err != nil {
113
 	if err != nil {
113
 		return nil, err
114
 		return nil, err
115
 
116
 
116
 	fmt.Printf("✅ Using config file: %s\n", configFile)
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
 	data, err := os.ReadFile(configFile)
130
 	data, err := os.ReadFile(configFile)
120
 	if err != nil {
131
 	if err != nil {
121
 		return nil, fmt.Errorf("failed to read config file %s: %v", configFile, err)
132
 		return nil, fmt.Errorf("failed to read config file %s: %v", configFile, err)
122
 	}
133
 	}
123
 
134
 
124
-	var config Config
125
 	err = yaml.Unmarshal(data, &config)
135
 	err = yaml.Unmarshal(data, &config)
126
 	if err != nil {
136
 	if err != nil {
127
 		return nil, fmt.Errorf("failed to parse config file: %v", err)
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
 // findConfigFile 查找配置文件
143
 // findConfigFile 查找配置文件
157
 		exeDir = filepath.Dir(exePath)
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
 Tried locations:
172
 Tried locations:
163
 1. Executable directory: %s/db.yaml
173
 1. Executable directory: %s/db.yaml
164
 2. Environment variable: DB_CONFIG_PATH
174
 2. Environment variable: DB_CONFIG_PATH
165
 
175
 
166
 Solutions:
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
 - Or set DB_CONFIG_PATH environment variable to config file path
178
 - Or set DB_CONFIG_PATH environment variable to config file path
169
 
179
 
170
 Example:
180
 Example:

Loading…
Avbryt
Spara