Files
cms-server/config/config.go
Asai Neko 79fbbd1862
All checks were successful
Server Check Build (NixCN CMS) TeamCity build finished
Use env vars when config.yaml not exist
Signed-off-by: Asai Neko <sugar@sne.moe>
2026-02-18 16:47:11 +08:00

42 lines
791 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()
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
log.Println("[Config] No config file found, using Env vars only.")
} else {
log.Fatalf("[Config] Fatal error reading config file: %s \n", err)
}
}
conf := &config{}
if err := viper.Unmarshal(conf); err != nil {
log.Fatalln("[Condig] Can't unmarshal config!")
}
}