config.go 1.1 KB

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