Files
be_absensi_sas/backend/pkg/config/config.go
2024-01-10 16:23:04 +07:00

48 lines
739 B
Go

package config
import (
"log"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
type Reader interface {
Get(key string) string
}
type viperConfigReader struct {
viper *viper.Viper
}
var Data *viperConfigReader
func (v viperConfigReader) Get(key string) string {
return v.viper.GetString(key)
}
func (v viperConfigReader) GetInt(key string) int {
return v.viper.GetInt(key)
}
func Load() {
v := viper.New()
v.AddConfigPath(".")
v.SetConfigName("config")
v.SetConfigType("yaml")
v.AutomaticEnv()
err := v.ReadInConfig()
if err != nil {
return
}
Data = &viperConfigReader{
viper: v,
}
v.WatchConfig()
v.OnConfigChange(func(e fsnotify.Event) {
log.Println("config file changed", e.Name)
})
return
}