config.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package config
  2. import (
  3. "github.com/spf13/viper"
  4. )
  5. type any interface{}
  6. func init() {
  7. //引入viper配置文件
  8. viper.SetConfigName("iot-master") //name := lib.AppName()
  9. viper.SetConfigType("yaml")
  10. viper.AddConfigPath(".")
  11. //viper.SetEnvPrefix("database")
  12. //绑定命令行参数
  13. //_ = viper.BindPFlags(pflag.CommandLine)
  14. //数据目录
  15. viper.SetDefault("data", "data")
  16. }
  17. func Load() error {
  18. return viper.ReadInConfig()
  19. }
  20. func Store() error {
  21. return viper.SafeWriteConfig()
  22. }
  23. func Register(module string, key string, value any) {
  24. viper.SetDefault(module+"."+key, value)
  25. }
  26. func GetBool(module string, key string) bool {
  27. return viper.GetBool(module + "." + key)
  28. }
  29. func GetString(module string, key string) string {
  30. return viper.GetString(module + "." + key)
  31. }
  32. func GetInt(module string, key string) int {
  33. return viper.GetInt(module + "." + key)
  34. }
  35. func GetFloat(module string, key string) float64 {
  36. return viper.GetFloat64(module + "." + key)
  37. }
  38. func GetStringSlice(module string, key string) []string {
  39. return viper.GetStringSlice(module + "." + key)
  40. }