Skip to content

Suggest to refactor the code to read and watch some config files #1675

@sykim-etri

Description

@sykim-etri

config 파일을 읽는 코드와 config 파일의 변경을 인식하는 코드가 init()와 main()으로 구분되어 있고, config 파일의 종류가 많아져서 혼란이 발생할 가능성이 높습니다.
최근 commit에 새롭게 추가된 jwt_auth config로 인해(viper의 기본 객체 v.configName을 덮어쓰는 것으로 추정) cloud_conf.yaml 내용 변경을 인식하지 못하고 있습니다.

read 파트와 watch 파트를 config 파일별로 합치는 등의 방안이 있을 것 같은데, 좋은 의견 부탁드립니다.

cb-tumblebug/src/main.go

Lines 139 to 155 in c1d8afa

fileName := "cloud_conf"
viper.AddConfigPath(".")
viper.AddConfigPath("./conf/")
viper.AddConfigPath("../conf/")
viper.SetConfigName(fileName)
viper.SetConfigType("yaml")
err := viper.ReadInConfig()
if err != nil {
log.Error().Err(err).Msg("")
panic(fmt.Errorf("fatal error reading cloud_conf: %w", err))
}
log.Info().Msg(viper.ConfigFileUsed())
err = viper.Unmarshal(&common.RuntimeConf)
if err != nil {
log.Error().Err(err).Msg("")
panic(err)
}

cb-tumblebug/src/main.go

Lines 207 to 213 in c1d8afa

jwtConfigfileName := "jwt_auth"
viper.SetConfigName(jwtConfigfileName)
err = viper.MergeInConfig()
if err != nil {
log.Error().Err(err).Msgf("fatal error reading jwt_auth info from file: %w", err)
panic(err)
}

cb-tumblebug/src/main.go

Lines 371 to 386 in c1d8afa

go func() {
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
log.Info().Msgf("Config file changed: %s", e.Name)
err := viper.ReadInConfig()
if err != nil { // Handle errors reading the config file
log.Error().Err(err).Msg("")
panic(fmt.Errorf("fatal error config file: %w", err))
}
err = viper.Unmarshal(&common.RuntimeConf)
if err != nil {
log.Error().Err(err).Msg("")
panic(err)
}
})
}()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions