Fixed a nasty bug related to lat/lon parsing
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user