Base Project
This commit is contained in:
42
configs/config.go
Normal file
42
configs/config.go
Normal file
@@ -0,0 +1,42 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user