Skip to content

Commit b6e76bd

Browse files
committed
feat(配置): 支持从多个默认路径加载配置文件
1 parent 271763e commit b6e76bd

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

internal/prometheus_adapter/config/config.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,27 @@ type ServerConfig struct {
4242

4343
// LoadConfig 加载配置文件
4444
func LoadConfig(configPath string) (*PrometheusAdapterConfig, error) {
45-
// 如果没有指定配置文件,使用默认路径
45+
// 如果没有指定配置文件,尝试多个默认路径
4646
if configPath == "" {
47-
configPath = "internal/prometheus_adapter/config/prometheus_adapter.yml"
47+
// 尝试的路径列表(按优先级)
48+
possiblePaths := []string{
49+
"config/prometheus_adapter.yml", // 部署环境:相对于工作目录
50+
"internal/prometheus_adapter/config/prometheus_adapter.yml", // 开发环境:源码目录
51+
"./prometheus_adapter.yml", // 当前目录
52+
}
53+
54+
for _, path := range possiblePaths {
55+
if _, err := os.Stat(path); err == nil {
56+
configPath = path
57+
log.Info().Str("path", path).Msg("Found config file")
58+
break
59+
}
60+
}
61+
62+
// 如果都找不到,使用第一个路径(稍后会返回默认配置)
63+
if configPath == "" {
64+
configPath = possiblePaths[0]
65+
}
4866
}
4967

5068
// 读取配置文件

0 commit comments

Comments
 (0)