From bf5dfb06b024fc807a543a0ebf62ec0bc54647cc Mon Sep 17 00:00:00 2001 From: Jan Date: Sat, 14 Dec 2024 15:51:29 +0100 Subject: [PATCH] Config checks if DB vars are set --- config/config.go | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/config/config.go b/config/config.go index 53af835..ca87717 100644 --- a/config/config.go +++ b/config/config.go @@ -3,7 +3,6 @@ package config import ( "fmt" "os" - "strings" "github.com/joho/godotenv" ) @@ -38,8 +37,26 @@ func Load() map[string]string { fmt.Println("no environment variables are set") os.Exit(1) } - - // TODO: check if all the required environment variables are set + + checkDB(env) return env } +func checkDB(env map[string]string) { + required := []string{"DB_USER", "DB_PASSWORD", "DB_NAME", "DB_HOST", "DB_PORT"} + for _, item := range required { + checkEnv(item, env) + } +} + +func checkEnv(check string, env map[string]string) { + if val, ok := env[check]; ok { + if val == "" { + fmt.Println(check, "is not set") + os.Exit(1) + } + } else { + fmt.Println(check, "is not set") + os.Exit(1) + } +}