From afea4a5d263f8c9a07473e8466f11781f35cf8e4 Mon Sep 17 00:00:00 2001 From: Jan Barfuss Date: Thu, 7 Nov 2024 10:17:33 +0100 Subject: [PATCH] Added Ports to compose.yml + moved the env check to the config file --- configs/config.go | 86 +++++++++++++++++++++++++++++++++------------ example/compose.yml | 4 ++- main.go | 34 +++++------------- 3 files changed, 75 insertions(+), 49 deletions(-) diff --git a/configs/config.go b/configs/config.go index 21c4028..4c78e89 100644 --- a/configs/config.go +++ b/configs/config.go @@ -10,6 +10,8 @@ import ( func Load() map[string]string { var env map[string]string = make(map[string]string) + validEnv := []string{"MODE", "OPENWEATHERMAP_API_KEY", "LATITUDE", "LONGITUDE", "PORT"} + envpath := "./.env" if _, err := os.Stat(envpath); err == nil { @@ -24,35 +26,73 @@ func Load() map[string]string { fmt.Println("No .env file found", err) } - mode := os.Getenv("MODE") - if mode != "" { - env["MODE"] = os.Getenv("MODE") - } - - openweathermapAPIKey := os.Getenv("OPENWEATHERMAP_API_KEY") - if openweathermapAPIKey != "" { - env["OPENWEATHERMAP_API_KEY"] = openweathermapAPIKey - } - - latitude := os.Getenv("LATITUDE") - if latitude != "" { - env["LATITUDE"] = latitude - } - - longitude := os.Getenv("LONGITUDE") - if longitude != "" { - env["LONGITUDE"] = longitude - } - - port := os.Getenv("PORT") - if port != "" { - env["PORT"] = port + for _, key := range validEnv { + tempenv := os.Getenv(key) + if tempenv != "" { + env[key] = tempenv + } } if len(env) == 0 { fmt.Println("no environment variables are set") os.Exit(1) } + + if val, ok := env["MODE"]; ok { + if val == "" { + env["MODE"] = "calc" + } + } else { + env["MODE"] = "calc" + } + + env["MODE"] = strings.ToLower(env["MODE"]) + switch env["MODE"] { + case "both": + checkEnvWeather(env) + checkEnvCalc(env) + case "weather": + checkEnvWeather(env) + default: + env["MODE"] = "calc" + } return env } + +func checkEnvWeather(env map[string]string) { + // Is there an API key for openweathermap? + if val, ok := env["OPENWEATHERMAP_API_KEY"]; ok { + if val == "" { + print("OPENWEATHERMAP_API_KEY is not set") + os.Exit(1) + } + } else { + fmt.Println("OPENWEATHERMAP_API_KEY is not set") + os.Exit(1) + } + + // Is there a LATITUDE and LONGITUDE? + if val, ok := env["LATITUDE"]; ok { + if val == "" { + print("LATITUDE is not set") + os.Exit(1) + } + } else { + fmt.Println("LATITUDE is not set") + os.Exit(1) + } + if val, ok := env["LONGITUDE"]; ok { + if val == "" { + print("LONGITUDE is not set") + os.Exit(1) + } + } else { + fmt.Println("LONGITUDE is not set") + os.Exit(1) + } +} + +func checkEnvCalc(env map[string]string) { + // Check for calc variables +} diff --git a/example/compose.yml b/example/compose.yml index 99ec742..e801f91 100644 --- a/example/compose.yml +++ b/example/compose.yml @@ -3,8 +3,10 @@ services: restart: always container_name: humiditycalc2 image: ghcr.io/leroid-hub/humiditycalc:latest + ports: + - 80:8080 environment: OPENWEATHERMAP_API_KEY: "YOURAPIKEY" LATITUDE: "52.51" LONGITUDE: "13.404" - MODE: "BOTH" \ No newline at end of file + MODE: "BOTH" diff --git a/main.go b/main.go index 79f15ea..9fdee1b 100644 --- a/main.go +++ b/main.go @@ -11,32 +11,16 @@ import ( func main() { env := configs.Load() - if val, ok := env["MODE"]; ok { - if strings.ToLower(val) == "both" { - checkEnv(env) + + switch env["MODE"] { + case "both": + server.Run(env) + case "weather": server.Run(env) - } else if strings.ToLower(val) == "weather" { - checkEnv(env) // weather.Run() - } else if strings.ToLower(val) == "calc" { - // calc.Run() + case "calc": + server.Run(env) + // calc.Run() + default: server.Run(env) - } - } else { - // calc.Run() - server.Run(env) - } -} -func checkEnv(env map[string]string) { - // Is there an API key for openweathermap? - if val, ok := env["OPENWEATHERMAP_API_KEY"]; ok { - if val == "" { - print("OPENWEATHERMAP_API_KEY is not set") - os.Exit(1) - } - } else { - fmt.Println("OPENWEATHERMAP_API_KEY is not set") - os.Exit(1) - } -}