33 lines
753 B
Go
33 lines
753 B
Go
package globals
|
|
|
|
import (
|
|
"fmt"
|
|
"log/slog"
|
|
"strings"
|
|
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
func InitConfig() error {
|
|
viper.SetConfigName("kontor-gin")
|
|
viper.SetConfigType("yaml")
|
|
viper.AddConfigPath(".")
|
|
viper.AddConfigPath("$HOME/.config")
|
|
viper.AddConfigPath("/etc/kontor")
|
|
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
|
viper.AutomaticEnv()
|
|
|
|
// Read the config file
|
|
if err := viper.ReadInConfig(); err != nil {
|
|
// Handle the case where config file is not found
|
|
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
|
slog.Info("No config file found, using defaults")
|
|
return nil
|
|
}
|
|
return fmt.Errorf("error reading config file: %w", err)
|
|
}
|
|
|
|
slog.Info("initConfig", "Using config file: ", viper.ConfigFileUsed())
|
|
return nil
|
|
}
|