Files
warehouse-management-BE/configs/config.go
Tran Anh Tuan 6a4a96e0ca Base Project
2026-05-08 14:32:24 +07:00

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
}