mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-11-05 21:02:11 +08:00
28 lines
518 B
Go
28 lines
518 B
Go
package config
|
|
|
|
import (
|
|
"github.com/mitchellh/mapstructure"
|
|
"github.com/spf13/viper"
|
|
"strings"
|
|
)
|
|
|
|
func LoadConfig(path string, prefix string, config any) error {
|
|
v := viper.New()
|
|
v.SetConfigFile(path)
|
|
v.SetEnvPrefix(prefix)
|
|
v.AutomaticEnv()
|
|
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
|
|
|
if err := v.ReadInConfig(); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := v.Unmarshal(config, func(config *mapstructure.DecoderConfig) {
|
|
config.TagName = "mapstructure"
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|