From bac8c693826f112d7bc1b38ed591a19c1d0ef153 Mon Sep 17 00:00:00 2001 From: Marco Cetica Date: Tue, 17 Jun 2025 09:35:59 +0200 Subject: [PATCH] Fixed a nasty bug related to lat/lon parsing --- controller/controller.go | 2 +- model/weatherModel.go | 10 ++++++---- types/cache.go | 6 +++--- types/{Weather.go => weather.go} | 0 4 files changed, 10 insertions(+), 8 deletions(-) rename types/{Weather.go => weather.go} (100%) diff --git a/controller/controller.go b/controller/controller.go index 5f828d1..5f95131 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -47,7 +47,7 @@ func GetWeather(res http.ResponseWriter, req *http.Request, cache *types.Cache[t // Check whether the 'i' parameter(imperial mode) is specified isImperial := req.URL.Query().Has("i") - weather, found := cache.GetCache(cityName, vars.TimeToLive) + weather, found := cache.GetEntry(cityName, vars.TimeToLive) if found { // Format weather values and then return it weather.Temperature = fmtTemperature(weather.Temperature, isImperial) diff --git a/model/weatherModel.go b/model/weatherModel.go index 4a66c26..0709991 100644 --- a/model/weatherModel.go +++ b/model/weatherModel.go @@ -67,6 +67,7 @@ func GetCoordinates(cityName string, apiKey string) (types.City, error) { if err != nil { return types.City{}, err } + defer res.Body.Close() var geoArr []types.City if err := json.NewDecoder(res.Body).Decode(&geoArr); err != nil { @@ -91,8 +92,8 @@ func GetWeather(city *types.City, apiKey string) (types.Weather, error) { } params := url.Query() - params.Set("lat", strconv.FormatFloat(city.Lat, 'E', -1, 64)) - params.Set("lon", strconv.FormatFloat(city.Lon, 'E', -1, 64)) + params.Set("lat", strconv.FormatFloat(city.Lat, 'f', -1, 64)) + params.Set("lon", strconv.FormatFloat(city.Lon, 'f', -1, 64)) params.Set("appid", apiKey) params.Set("units", "metric") params.Set("exclude", "minutely,hourly,daily,alerts") @@ -103,6 +104,7 @@ func GetWeather(city *types.City, apiKey string) (types.Weather, error) { if err != nil { return types.Weather{}, err } + defer res.Body.Close() // Structures representing the JSON response type InfoRes struct { @@ -147,8 +149,8 @@ func GetWeather(city *types.City, apiKey string) (types.Weather, error) { return types.Weather{ Date: weatherDate, - Temperature: strconv.FormatFloat(weather.Current.Temperature, 'E', -1, 64), - FeelsLike: strconv.FormatFloat(weather.Current.FeelsLike, 'E', -1, 64), + Temperature: strconv.FormatFloat(weather.Current.Temperature, 'f', -1, 64), + FeelsLike: strconv.FormatFloat(weather.Current.FeelsLike, 'f', -1, 64), Condition: weather.Current.Weather[0].Title, Emoji: emoji, }, nil diff --git a/types/cache.go b/types/cache.go index 67a4bdd..34bfb34 100644 --- a/types/cache.go +++ b/types/cache.go @@ -33,8 +33,8 @@ func InitCache() *Caches { } } -func (cache *Cache[T]) GetCache(key string, ttl int8) (T, bool) { - val, isPresent := cache.Data[key+"_weather"] +func (cache *Cache[T]) GetEntry(key string, ttl int8) (T, bool) { + val, isPresent := cache.Data[key] // If key is not present, return a zero value if !isPresent { @@ -57,7 +57,7 @@ func (cache *Cache[T]) AddEntry(entry T, cityName string) { switch any(entry).(type) { case Weather: { - cache.Data[cityName+"_weather"] = CacheEntity[T]{ + cache.Data[cityName] = CacheEntity[T]{ Element: entry, Timestamp: currentTime, } diff --git a/types/Weather.go b/types/weather.go similarity index 100% rename from types/Weather.go rename to types/weather.go