config.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package config
  2. import (
  3. "github.com/spf13/viper"
  4. )
  5. type any interface{}
  6. var (
  7. defaultName = "config"
  8. defaultConfigType = "yaml"
  9. defaultPath = "."
  10. )
  11. //func init() {
  12. // Init()
  13. //}
  14. func Init() {
  15. //引入viper配置文件
  16. viper.SetConfigName(defaultName) //defaultName := lib.AppName()
  17. viper.SetConfigType(defaultConfigType)
  18. viper.AddConfigPath(defaultPath)
  19. }
  20. func SetConfigInfo(name, suffix, path string) {
  21. viper.SetConfigName(name) //defaultName := lib.AppName()
  22. viper.SetConfigType(suffix)
  23. viper.AddConfigPath(path)
  24. }
  25. func Load() error {
  26. return viper.ReadInConfig()
  27. }
  28. func Store() error {
  29. return viper.SafeWriteConfig()
  30. }
  31. func Register(module string, key string, value any) {
  32. viper.SetDefault(module+"."+key, value)
  33. }
  34. func GetBool(module string, key string) bool {
  35. return viper.GetBool(module + "." + key)
  36. }
  37. func GetString(module string, key string) string {
  38. return viper.GetString(module + "." + key)
  39. }
  40. func GetInt(module string, key string) int {
  41. return viper.GetInt(module + "." + key)
  42. }
  43. func GetFloat(module string, key string) float64 {
  44. return viper.GetFloat64(module + "." + key)
  45. }
  46. func GetStringSlice(module string, key string) []string {
  47. return viper.GetStringSlice(module + "." + key)
  48. }
  49. func Set(module, key string, value any) {
  50. viper.Set(module+"."+key, key)
  51. }