package main import ( "fmt" "gitea.iceberg-gaming.com/17th-Ranger-Battalion-ORG/17th-UnitTracker-API/logger" "github.com/spf13/viper" ) var Config *viper.Viper func LoadConfig() error { setConfigDefaults() // Read in environment variables viper.AutomaticEnv() // read in environment variables that match // Use config file (like json or yaml) // viper.SetConfigName("config") // name of config file (without extension) // viper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name // Use .env file viper.SetConfigFile(".env") // name of config file (without extension // viper.AddConfigPath("/etc/appname/") // path to look for the config file in // viper.AddConfigPath("$HOME/.appname") // call multiple times to add many search paths viper.AddConfigPath(".") // optionally look for config in the working directory err := viper.ReadInConfig() // Find and read the config file if err != nil { // Handle errors reading the config file return fmt.Errorf("fatal error config file: %w", err) } if err := viper.ReadInConfig(); err != nil { if _, ok := err.(viper.ConfigFileNotFoundError); ok { // Config file not found; ignore error if desired return fmt.Errorf("config file not found: %w", err) } else { // Config file was found but another error was produced return fmt.Errorf("fatal error config file: %w", err) } } // Config file found and successfully parsed Config = viper.GetViper() logger.Log.Debug().Interface("config", Config.AllSettings()).Msg("Configuration loaded") return nil } func setConfigDefaults() { viper.SetDefault("MARIADB_CONNSTRING", "user:password@tcp(localhost:3306)/dbname?parseTime=true") viper.SetDefault("API_PREFIX", "/api/v1") viper.SetDefault("API_PORT", "1323") }