Files
nixcn-cms/config/config.go
2025-12-23 23:53:32 +08:00

38 lines
628 B
Go

package config
import (
"log"
"os"
"strings"
"github.com/spf13/viper"
)
func ConfigDir() string {
env := os.Getenv("CONFIG_PATH")
if env != "" {
return env
}
return "."
}
func Init() {
// Read global config
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(ConfigDir())
// Bind ENV
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()
conf := &config{}
if err := viper.ReadInConfig(); err != nil {
// Dont generate config when using dev mode
log.Fatalln("Can't read config!")
}
if err := viper.Unmarshal(conf); err != nil {
log.Fatal(err)
}
}