From 80cdd9de4f9a16bea9b1f4ec62985014800bfe71 Mon Sep 17 00:00:00 2001 From: Jan Date: Sat, 2 Nov 2024 13:24:36 +0100 Subject: [PATCH] env --- README.md | 8 +++++++- go.mod | 2 ++ go.sum | 2 ++ internal/weather.go | 1 + main.go | 47 ++++++++++++++++++++++++++++++++++++++++++++- server/server.go | 2 +- 6 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 go.sum create mode 100644 internal/weather.go diff --git a/README.md b/README.md index 5023029..2cd7e0b 100644 --- a/README.md +++ b/README.md @@ -1 +1,7 @@ -# humiditycalc \ No newline at end of file +# humiditycalc + +## ENV +MODES: CALC, WEATHER, BOTH +BOTH is standard + +OPENWEATHERMAP_API_KEY is required in WEATHER and BOTH mode \ No newline at end of file diff --git a/go.mod b/go.mod index 1ee8d69..c932426 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/LeRoid-hub/humiditycalc go 1.22.2 + +require github.com/joho/godotenv v1.5.1 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..d61b19e --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= diff --git a/internal/weather.go b/internal/weather.go new file mode 100644 index 0000000..5bf0569 --- /dev/null +++ b/internal/weather.go @@ -0,0 +1 @@ +package internal diff --git a/main.go b/main.go index 653b6fe..0896e87 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,54 @@ package main import ( + "fmt" + "os" + "strings" + "github.com/LeRoid-hub/humiditycalc/server" + "github.com/joho/godotenv" ) func main() { - server.Run() + env := loadEnv() + if val, ok := env["MODE"]; ok { + if strings.ToLower(val) == "both" { + checkEnv(env) + server.Run() + } else if strings.ToLower(val) == "weather" { + checkEnv(env) + // weather.Run() + } else if strings.ToLower(val) == "calc" { + // calc.Run() + server.Run() + } + } +} + +func loadEnv() map[string]string { + var env map[string]string + env, err := godotenv.Read() + if err != nil { + fmt.Println("Error loading .env file: ", err) + os.Exit(1) + } + if len(env) == 0 { + fmt.Println(".env file is empty") + os.Exit(1) + } + return env +} + +func checkEnv(env map[string]string) { + // Is there an API key for openweathermap? + if val, ok := env["OPENWEATHERMAP_API_KEY"]; ok { + fmt.Println(val) + if val == "" { + print("OPENWEATHERMAP_API_KEY is not set") + os.Exit(1) + } + } else { + fmt.Println("OPENWEATHERMAP_API_KEY is not set") + os.Exit(1) + } } diff --git a/server/server.go b/server/server.go index 3131d30..f6aaf07 100644 --- a/server/server.go +++ b/server/server.go @@ -12,7 +12,7 @@ type result struct { AbsoluteHumidity float64 } -// StartServer starts the HTTP server. +// Run starts the HTTP server. func Run() { tmpl := template.Must(template.ParseFiles("./web/templates/index.html"))