Added wind route and started forecast endpoint
This commit is contained in:
@@ -34,6 +34,19 @@ func fmtTemperature(temp string, isImperial bool) string {
|
||||
return fmt.Sprintf("%d°C", int(math.Round(parsedTemp)))
|
||||
}
|
||||
|
||||
func fmtWind(windSpeed string, isImperial bool) string {
|
||||
// Convert wind speed to mph or km/s from m/s
|
||||
// 1 m/s = 2.23694 mph
|
||||
// 1 m/s = 3.6 km/h
|
||||
parsedSpeed, _ := strconv.ParseFloat(windSpeed, 64)
|
||||
|
||||
if isImperial {
|
||||
return fmt.Sprintf("%.1f mph", (parsedSpeed * 2.23694))
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%.1f km/h", (parsedSpeed * 3.6))
|
||||
}
|
||||
|
||||
func GetWeather(res http.ResponseWriter, req *http.Request, cache *types.Cache[types.Weather], vars *types.Variables) {
|
||||
if req.Method != http.MethodGet {
|
||||
jsonError(res, "error", "method not allowed", http.StatusMethodNotAllowed)
|
||||
@@ -47,13 +60,13 @@ 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.GetEntry(cityName, vars.TimeToLive)
|
||||
cachedValue, found := cache.GetEntry(cityName, vars.TimeToLive)
|
||||
if found {
|
||||
// Format weather object and then return it
|
||||
weather.Temperature = fmtTemperature(weather.Temperature, isImperial)
|
||||
weather.FeelsLike = fmtTemperature(weather.FeelsLike, isImperial)
|
||||
cachedValue.Temperature = fmtTemperature(cachedValue.Temperature, isImperial)
|
||||
cachedValue.FeelsLike = fmtTemperature(cachedValue.FeelsLike, isImperial)
|
||||
|
||||
jsonValue(res, weather)
|
||||
jsonValue(res, cachedValue)
|
||||
} else {
|
||||
// Get city coordinates
|
||||
city, err := model.GetCoordinates(cityName, vars.Token)
|
||||
@@ -93,15 +106,15 @@ func GetMetrics(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")
|
||||
|
||||
metrics, found := cache.GetEntry(cityName, vars.TimeToLive)
|
||||
cachedValue, found := cache.GetEntry(cityName, vars.TimeToLive)
|
||||
if found {
|
||||
// Format metrics object and then return it
|
||||
metrics.Humidity = fmt.Sprintf("%s%%", metrics.Humidity)
|
||||
metrics.Pressure = fmt.Sprintf("%s hPa", metrics.Pressure)
|
||||
metrics.DewPoint = fmtTemperature(metrics.DewPoint, isImperial)
|
||||
metrics.Visibility = fmt.Sprintf("%skm", metrics.Visibility)
|
||||
cachedValue.Humidity = fmt.Sprintf("%s%%", cachedValue.Humidity)
|
||||
cachedValue.Pressure = fmt.Sprintf("%s hPa", cachedValue.Pressure)
|
||||
cachedValue.DewPoint = fmtTemperature(cachedValue.DewPoint, isImperial)
|
||||
cachedValue.Visibility = fmt.Sprintf("%skm", cachedValue.Visibility)
|
||||
|
||||
jsonValue(res, metrics)
|
||||
jsonValue(res, cachedValue)
|
||||
} else {
|
||||
// Get city coordinates
|
||||
city, err := model.GetCoordinates(cityName, vars.Token)
|
||||
@@ -129,3 +142,105 @@ func GetMetrics(res http.ResponseWriter, req *http.Request, cache *types.Cache[t
|
||||
jsonValue(res, metrics)
|
||||
}
|
||||
}
|
||||
|
||||
func GetWind(res http.ResponseWriter, req *http.Request, cache *types.Cache[types.Wind], vars *types.Variables) {
|
||||
if req.Method != http.MethodGet {
|
||||
jsonError(res, "error", "method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
// Extract city name from '/wind/:city'
|
||||
path := strings.TrimPrefix(req.URL.Path, "/wind/")
|
||||
cityName := strings.Trim(path, "/") // Remove trailing slash if present
|
||||
|
||||
// Check whether the 'i' parameter(imperial mode) is specified
|
||||
isImperial := req.URL.Query().Has("i")
|
||||
|
||||
cachedValue, found := cache.GetEntry(cityName, vars.TimeToLive)
|
||||
if found {
|
||||
// Format wind object and then return it
|
||||
cachedValue.Speed = fmtWind(cachedValue.Speed, isImperial)
|
||||
|
||||
jsonValue(res, cachedValue)
|
||||
} else {
|
||||
// Get city coordinates
|
||||
city, err := model.GetCoordinates(cityName, vars.Token)
|
||||
if err != nil {
|
||||
jsonError(res, "error", err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Get city wind
|
||||
wind, err := model.GetWind(&city, vars.Token)
|
||||
if err != nil {
|
||||
jsonError(res, "error", err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Add result to cache
|
||||
cache.AddEntry(wind, cityName)
|
||||
|
||||
// Format wind object and then return it
|
||||
wind.Speed = fmtWind(wind.Speed, isImperial)
|
||||
|
||||
jsonValue(res, wind)
|
||||
}
|
||||
}
|
||||
|
||||
func GetForecast(res http.ResponseWriter, req *http.Request, cache *types.Cache[types.Forecast], vars *types.Variables) {
|
||||
if req.Method != http.MethodGet {
|
||||
jsonError(res, "error", "method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
// Extract city name from '/forecast/:city'
|
||||
path := strings.TrimPrefix(req.URL.Path, "/forecast/")
|
||||
cityName := strings.Trim(path, "/") // Remove trailing slash if present
|
||||
|
||||
// Check whether the 'i' parameter(imperial mode) is specified
|
||||
isImperial := req.URL.Query().Has("i")
|
||||
|
||||
cachedValue, found := cache.GetEntry(cityName, vars.TimeToLive)
|
||||
if found {
|
||||
// Format forecast object and then return it
|
||||
for idx := range cachedValue.Forecast {
|
||||
cachedValue.Forecast[idx].Min = fmtTemperature(cachedValue.Forecast[idx].Min, isImperial)
|
||||
cachedValue.Forecast[idx].Max = fmtTemperature(cachedValue.Forecast[idx].Max, isImperial)
|
||||
cachedValue.Forecast[idx].FeelsLike = fmtTemperature(cachedValue.Forecast[idx].FeelsLike, isImperial)
|
||||
cachedValue.Forecast[idx].Wind.Speed = fmtWind(cachedValue.Forecast[idx].Wind.Speed, isImperial)
|
||||
}
|
||||
|
||||
jsonValue(res, cachedValue)
|
||||
} else {
|
||||
// Get city coordinates
|
||||
city, err := model.GetCoordinates(cityName, vars.Token)
|
||||
if err != nil {
|
||||
jsonError(res, "error", err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Get city forecast
|
||||
forecast, err := model.GetForecast(&city, vars.Token)
|
||||
if err != nil {
|
||||
jsonError(res, "error", err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Add result to cache
|
||||
cache.AddEntry(forecast, cityName)
|
||||
|
||||
// *****************
|
||||
// FIXME: formatting 'forecast' alters cached value
|
||||
// *****************
|
||||
|
||||
// Format forecast object and then return it
|
||||
for idx := range forecast.Forecast {
|
||||
forecast.Forecast[idx].Min = fmtTemperature(forecast.Forecast[idx].Min, isImperial)
|
||||
forecast.Forecast[idx].Max = fmtTemperature(forecast.Forecast[idx].Max, isImperial)
|
||||
forecast.Forecast[idx].FeelsLike = fmtTemperature(forecast.Forecast[idx].FeelsLike, isImperial)
|
||||
forecast.Forecast[idx].Wind.Speed = fmtWind(forecast.Forecast[idx].Wind.Speed, isImperial)
|
||||
}
|
||||
|
||||
jsonValue(res, forecast)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user