feat(conf/new.go): 添加配置文件不存在时的处理逻辑及环境变量替换
在`New`函数中增加了对配置文件不存在时的处理逻辑,如果指定的服务配置文件不存在,则尝试读取基于工作空间的配置文件。此外,在加载YAML配置之前,新增了对环境变量的替换步骤,确保配置中的环境变量能够被正确解析。 这些改动提高了配置加载过程的灵活性与适应性,使得应用能够在缺少特定服务配置的情况下也能正常启动,并且支持通过环境变量动态调整配置内容。
This commit is contained in:
11
conf/new.go
11
conf/new.go
@@ -30,6 +30,12 @@ func New(srvKey string, cfg any) {
|
||||
cfp := fmt.Sprintf("%s_%s.yaml", strings.ToLower(srvKey), env.Runtime.Mode)
|
||||
cfp = filepath.Join(env.Runtime.Prefix, "etc", cfp)
|
||||
|
||||
// 配置文件不存在则读取Workspace配置文件
|
||||
if !utils.PathExists(cfp) {
|
||||
cfp = fmt.Sprintf("workspace_%s_%s.yaml", strings.ToLower(env.Runtime.Workspace), env.Runtime.Mode)
|
||||
cfp = filepath.Join(env.Runtime.Prefix, "etc", cfp)
|
||||
}
|
||||
|
||||
print.Info("[BSM - %s] Config File: %s", srvKey, cfp)
|
||||
print.Info("[BSM - %s] Check Configure ...", vars.ServiceKey)
|
||||
|
||||
@@ -39,8 +45,11 @@ func New(srvKey string, cfg any) {
|
||||
log.Fatalf("ERROR: %v", err)
|
||||
}
|
||||
|
||||
// 替换环境变量
|
||||
yamlString := os.ExpandEnv(string(yamlFile))
|
||||
|
||||
// 检查配置文件中是否存在Service和Port字段
|
||||
if !strings.Contains(string(yamlFile), "Service:") {
|
||||
if !strings.Contains(yamlString, "Service:") {
|
||||
log.Fatalln("ERROR: Service Not Nil", cfp)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user