From 1f2e2bd221a954c95c44fb89534e2df875be1564 Mon Sep 17 00:00:00 2001 From: Jan Date: Sat, 2 Nov 2024 19:11:56 +0100 Subject: [PATCH] fixes + Dockerfile --- Dockerfile | 12 ++++++++++ configs/config.go | 58 +++++++++++++++++++++++++++++++++++++++++++++++ main.go | 18 ++------------- models/cache.go | 2 -- models/env.go | 4 ++++ server/server.go | 11 ++++----- 6 files changed, 80 insertions(+), 25 deletions(-) create mode 100644 Dockerfile create mode 100644 configs/config.go create mode 100644 models/env.go diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..cbbe9b2 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +FROM golang:1.22.2 + +WORKDIR /usr/src/app + +# pre-copy/cache go.mod for pre-downloading dependencies and only redownloading them in subsequent builds if they change +COPY go.mod ./ +RUN go mod download && go mod verify + +COPY . . +RUN go build -v -o /usr/local/bin/humiditycalc ./... + +CMD ["humiditycalc"] \ No newline at end of file diff --git a/configs/config.go b/configs/config.go new file mode 100644 index 0000000..21c4028 --- /dev/null +++ b/configs/config.go @@ -0,0 +1,58 @@ +package configs + +import ( + "fmt" + "os" + + "github.com/joho/godotenv" +) + +func Load() map[string]string { + var env map[string]string = make(map[string]string) + + envpath := "./.env" + + if _, err := os.Stat(envpath); err == nil { + + dotenv, err := godotenv.Read(envpath) + if err != nil { + fmt.Println("Error loading .env file: ", err) + } + + env = dotenv + } else { + 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 + } + + if len(env) == 0 { + fmt.Println("no environment variables are set") + os.Exit(1) + } + + return env +} diff --git a/main.go b/main.go index 26bbc51..79f15ea 100644 --- a/main.go +++ b/main.go @@ -5,12 +5,12 @@ import ( "os" "strings" + "github.com/LeRoid-hub/humiditycalc/configs" "github.com/LeRoid-hub/humiditycalc/server" - "github.com/joho/godotenv" ) func main() { - env := loadEnv() + env := configs.Load() if val, ok := env["MODE"]; ok { if strings.ToLower(val) == "both" { checkEnv(env) @@ -28,20 +28,6 @@ func main() { } } -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 { diff --git a/models/cache.go b/models/cache.go index 57e8c27..a6b2402 100644 --- a/models/cache.go +++ b/models/cache.go @@ -1,7 +1,6 @@ package models import ( - "fmt" "time" ) @@ -13,7 +12,6 @@ type WeatherCache struct { } func (w *WeatherCache) IsExpired() bool { - fmt.Println(time.Since(w.timestamp), time.Duration(w.duration)*time.Second) return time.Since(w.timestamp) > time.Duration(w.duration)*time.Second } diff --git a/models/env.go b/models/env.go new file mode 100644 index 0000000..588fdb8 --- /dev/null +++ b/models/env.go @@ -0,0 +1,4 @@ +package models + +type Config struct { +} diff --git a/server/server.go b/server/server.go index 784e5a6..c565114 100644 --- a/server/server.go +++ b/server/server.go @@ -61,17 +61,14 @@ func Run(env map[string]string) { // Get Weather data humidity, temperature := internal.Weather(env) - // Calculate absolute weather humidity - absoluteWeatherHumidity = internal.AbsoluteHumidity(temperature, humidity) - // Update cache cacheWeather.SetData(humidity, temperature) - } else { - // Use cached data - humidity, temperature := cacheWeather.GetData() - absoluteWeatherHumidity = internal.AbsoluteHumidity(temperature, humidity) } + // Use cached data + humidity, temperature = cacheWeather.GetData() + absoluteWeatherHumidity = internal.AbsoluteHumidity(temperature, humidity) + // Create response WData := WeatherData{Temperature: FormatFloat(temperature, 2), RelativeHumidity: FormatFloat(humidity, 4), AbsoluteHumidity: FormatFloat(absoluteWeatherHumidity, 4)}