43 lines
858 B
Go
43 lines
858 B
Go
package configs
|
|
|
|
import (
|
|
"log"
|
|
"os"
|
|
"wm-backend/configs/constants"
|
|
"wm-backend/internal/models"
|
|
|
|
"github.com/joho/godotenv"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
func LoadConfig(path string) (config models.Config, err error) {
|
|
// Load environment variables from .env file
|
|
err = godotenv.Load()
|
|
if err != nil {
|
|
log.Fatalf("Error loading .env file: %v", err)
|
|
}
|
|
|
|
viper.AddConfigPath(path)
|
|
env := os.Getenv("ENV")
|
|
if env == constants.ProdEnvironment {
|
|
viper.SetConfigName("yaml/config.prod")
|
|
} else {
|
|
viper.SetConfigName("yaml/config.dev")
|
|
}
|
|
viper.SetConfigType("yaml")
|
|
|
|
viper.AutomaticEnv()
|
|
|
|
// Read the configuration file
|
|
if err := viper.ReadInConfig(); err != nil {
|
|
return config, err
|
|
}
|
|
|
|
// Unmarshal the configuration into the config struct
|
|
if err := viper.Unmarshal(&config); err != nil {
|
|
return config, err
|
|
}
|
|
|
|
return config, nil
|
|
}
|