claptrap-go/config/env.go

54 lines
1.2 KiB
Go
Raw Normal View History

2024-07-08 18:09:43 +02:00
package config
import (
"os"
"github.com/joho/godotenv"
)
type Config struct {
DiscordToken string
DatabaseHost string
DatabasePort string
DatabaseName string
DatabaseUser string
DatabasePassword string
}
func LoadEnv() *Config {
godotenv.Load()
missingEnv := make([]string, 0)
config := Config{}
config.DiscordToken = os.Getenv("DISCORD_TOKEN")
if config.DiscordToken == "" {
missingEnv = append(missingEnv, "DISCORD_TOKEN")
}
config.DatabaseHost = os.Getenv("DB_HOST")
if config.DatabaseHost == "" {
missingEnv = append(missingEnv, "DB_HOST")
}
config.DatabasePort = os.Getenv("DB_PORT")
if config.DatabasePort == "" {
config.DatabasePort = "5432"
}
config.DatabaseName = os.Getenv("DB_NAME")
if config.DatabaseName == "" {
missingEnv = append(missingEnv, "DB_NAME")
}
config.DatabaseUser = os.Getenv("DB_USER")
if config.DatabaseUser == "" {
missingEnv = append(missingEnv, "DB_USER")
}
config.DatabasePassword = os.Getenv("DB_PASSWORD")
if config.DatabasePassword == "" {
missingEnv = append(missingEnv, "DB_PASSWORD")
}
if len(missingEnv) != 0 {
InitLogger()
Logger.Sugar().Fatalw("Env var are missing, please add the fallowing missing env var", "MissingEnvVar", missingEnv)
}
return &config
}