From dffec37d905674fd9ac5bd5f61582fdcb2962010 Mon Sep 17 00:00:00 2001 From: Marco Cetica Date: Mon, 16 Jun 2025 16:19:18 +0200 Subject: [PATCH] Added weather route and embedded cache system --- controller/controller.go | 81 ++++++++++++++++++++ go.mod | 3 + main.go | 41 +++++++++++ model/weatherModel.go | 155 +++++++++++++++++++++++++++++++++++++++ types/Weather.go | 12 +++ types/cache.go | 66 +++++++++++++++++ types/city.go | 9 +++ types/metrics.go | 11 +++ types/variables.go | 7 ++ 9 files changed, 385 insertions(+) create mode 100644 controller/controller.go create mode 100644 go.mod create mode 100644 main.go create mode 100644 model/weatherModel.go create mode 100644 types/Weather.go create mode 100644 types/cache.go create mode 100644 types/city.go create mode 100644 types/metrics.go create mode 100644 types/variables.go diff --git a/controller/controller.go b/controller/controller.go new file mode 100644 index 0000000..5f828d1 --- /dev/null +++ b/controller/controller.go @@ -0,0 +1,81 @@ +package controller + +import ( + "encoding/json" + "fmt" + "math" + "net/http" + "strconv" + "strings" + + "github.com/ceticamarco/zephyr/model" + "github.com/ceticamarco/zephyr/types" +) + +func jsonError(res http.ResponseWriter, key string, value string, status int) { + res.Header().Set("Content-Type", "application/json") + res.WriteHeader(status) + json.NewEncoder(res).Encode(map[string]string{key: value}) +} + +func jsonValue(res http.ResponseWriter, val any) { + res.Header().Set("Content-Type", "application/json") + res.WriteHeader(http.StatusOK) + json.NewEncoder(res).Encode(val) +} + +func fmtTemperature(temp string, isImperial bool) string { + parsedTemp, _ := strconv.ParseFloat(temp, 64) + + if isImperial { + return fmt.Sprintf("%d°F", int(math.Round(parsedTemp*(9/5)+32))) + } + + return fmt.Sprintf("%d°C", int(math.Round(parsedTemp))) +} + +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) + return + } + + // Extract city name from '/weather/:city' + path := strings.TrimPrefix(req.URL.Path, "/weather/") + cityName := strings.Trim(path, "/") // Remove trailing slash if present + + // Check whether the 'i' parameter(imperial mode) is specified + isImperial := req.URL.Query().Has("i") + + weather, found := cache.GetCache(cityName, vars.TimeToLive) + if found { + // Format weather values and then return it + weather.Temperature = fmtTemperature(weather.Temperature, isImperial) + weather.FeelsLike = fmtTemperature(weather.FeelsLike, isImperial) + + jsonValue(res, weather) + } else { + // Get city coordinates + city, err := model.GetCoordinates(cityName, vars.Token) + if err != nil { + jsonError(res, "error", err.Error(), http.StatusBadRequest) + return + } + + // Get city weather + weather, err := model.GetWeather(&city, vars.Token) + if err != nil { + jsonError(res, "error", err.Error(), http.StatusBadRequest) + return + } + + // Add result to cache + cache.AddEntry(weather, cityName) + + // Format weather values and then return it + weather.Temperature = fmtTemperature(weather.Temperature, isImperial) + weather.FeelsLike = fmtTemperature(weather.FeelsLike, isImperial) + + jsonValue(res, weather) + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..925d5b8 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/ceticamarco/zephyr + +go 1.24.4 diff --git a/main.go b/main.go new file mode 100644 index 0000000..05ded3e --- /dev/null +++ b/main.go @@ -0,0 +1,41 @@ +package main + +import ( + "fmt" + "log" + "net/http" + "os" + "strconv" + + "github.com/ceticamarco/zephyr/controller" + "github.com/ceticamarco/zephyr/types" +) + +func main() { + // Retrieve listening port, API token and cache time-to-live from environment variables + var ( + port = os.Getenv("ZEPHYR_PORT") + token = os.Getenv("ZEPHYR_TOKEN") + ttl, _ = strconv.ParseInt(os.Getenv("ZEPHYR_CACHE_TTL"), 10, 8) + ) + + if port == "" || token == "" || ttl == 0 { + log.Fatalf("Environment variables not set") + } + + // Initialize cache and vars + cache := types.InitCache() + vars := types.Variables{ + Token: token, + TimeToLive: int8(ttl), + } + + // API endpoints + http.HandleFunc("/weather/", func(res http.ResponseWriter, req *http.Request) { + controller.GetWeather(res, req, &cache.WeatherCache, &vars) + }) + + listenAddr := fmt.Sprintf(":%s", port) + log.Printf("Server listening on %s", listenAddr) + http.ListenAndServe(listenAddr, nil) +} diff --git a/model/weatherModel.go b/model/weatherModel.go new file mode 100644 index 0000000..4a66c26 --- /dev/null +++ b/model/weatherModel.go @@ -0,0 +1,155 @@ +package model + +import ( + "encoding/json" + "errors" + "net/http" + "net/url" + "strconv" + "strings" + "time" + + "github.com/ceticamarco/zephyr/types" +) + +const ( + GEO_URL = "https://api.openweathermap.org/geo/1.0/direct" + WTR_URL = "https://api.openweathermap.org/data/3.0/onecall" +) + +func getEmoji(condition string, isNight bool) string { + switch condition { + case "Thunderstorm": + return "⛈️" + case "Drizzle": + return "🌦️" + case "Rain": + return "🌧️" + case "Snow": + return "☃️" + case "Mist", "Smoke", "Haze", "Dust", "Fog", "Sand", "Ash", "Squall": + return "🌫️" + case "Tornado": + return "🌪️" + case "Clear": + { + if isNight { + return "🌙" + } else { + return "☀️" + } + } + case "Clouds": + return "☁️" + case "SunWithCloud": + return "🌤️" + case "CloudWithSun": + return "🌥️" + } + + return "❓" +} + +func GetCoordinates(cityName string, apiKey string) (types.City, error) { + url, err := url.Parse(GEO_URL) + if err != nil { + return types.City{}, err + } + + params := url.Query() + params.Set("q", cityName) + params.Set("limit", "1") + params.Set("appid", apiKey) + + url.RawQuery = params.Encode() + + res, err := http.Get(url.String()) + if err != nil { + return types.City{}, err + } + + var geoArr []types.City + if err := json.NewDecoder(res.Body).Decode(&geoArr); err != nil { + return types.City{}, err + } + + if len(geoArr) == 0 { + return types.City{}, errors.New("Cannot find this city") + } + + return types.City{ + Name: geoArr[0].Name, + Lat: geoArr[0].Lat, + Lon: geoArr[0].Lon, + }, nil +} + +func GetWeather(city *types.City, apiKey string) (types.Weather, error) { + url, err := url.Parse(WTR_URL) + if err != nil { + return types.Weather{}, err + } + + 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("appid", apiKey) + params.Set("units", "metric") + params.Set("exclude", "minutely,hourly,daily,alerts") + + url.RawQuery = params.Encode() + + res, err := http.Get(url.String()) + if err != nil { + return types.Weather{}, err + } + + // Structures representing the JSON response + type InfoRes struct { + Title string `json:"main"` + Description string `json:"description"` + Icon string `json:"icon"` + } + type CurrentRes struct { + FeelsLike float64 `json:"feels_like"` + Temperature float64 `json:"temp"` + Timestamp int64 `json:"dt"` + Weather []InfoRes `json:"weather"` + } + type WeatherRes struct { + Current CurrentRes `json:"current"` + } + + var weather WeatherRes + if err := json.NewDecoder(res.Body).Decode(&weather); err != nil { + return types.Weather{}, err + } + + // Format UNIX timestamp as 'YYYY-MM-DD' + // unixTs, _ := strconv.Atoi(weather.Current.Timestamp) + utcTime := time.Unix(int64(weather.Current.Timestamp), 0) + weatherDate := utcTime.UTC() + + // Set condition accordingly to weather description + var condition string + switch weather.Current.Weather[0].Description { + case "few clouds": + condition = "SunWithCloud" + case "broken clouds": + condition = "CloudWithSun" + default: + condition = weather.Current.Weather[0].Title + } + + // Get emoji from weather condition + isNight := strings.HasSuffix(weather.Current.Weather[0].Icon, "n") + emoji := getEmoji(condition, isNight) + + return types.Weather{ + Date: weatherDate, + Temperature: strconv.FormatFloat(weather.Current.Temperature, 'E', -1, 64), + FeelsLike: strconv.FormatFloat(weather.Current.FeelsLike, 'E', -1, 64), + Condition: weather.Current.Weather[0].Title, + Emoji: emoji, + }, nil +} diff --git a/types/Weather.go b/types/Weather.go new file mode 100644 index 0000000..cb3ca66 --- /dev/null +++ b/types/Weather.go @@ -0,0 +1,12 @@ +package types + +import "time" + +// The Weather data type, representing the weather of a certain city +type Weather struct { + Date time.Time `json:"date"` + Temperature string `json:"temperature"` + Condition string `json:"condition"` + FeelsLike string `json:"feelsLike"` + Emoji string `json:"emoji"` +} diff --git a/types/cache.go b/types/cache.go new file mode 100644 index 0000000..67a4bdd --- /dev/null +++ b/types/cache.go @@ -0,0 +1,66 @@ +package types + +import ( + "time" +) + +// CacheType, representing the abstract value of a CacheEntity +type CacheType interface { + Weather | Metrics +} + +// CacheEntity, representing the value of the cache +type CacheEntity[T CacheType] struct { + Element T + Timestamp time.Time +} + +// Cache, representing a mapping between a key(str) and a CacheEntity +type Cache[T CacheType] struct { + Data map[string]CacheEntity[T] +} + +// Caches, representing a grouping of the various caches +type Caches struct { + WeatherCache Cache[Weather] + MetricsCache Cache[Metrics] +} + +func InitCache() *Caches { + return &Caches{ + WeatherCache: Cache[Weather]{Data: make(map[string]CacheEntity[Weather])}, + MetricsCache: Cache[Metrics]{Data: make(map[string]CacheEntity[Metrics])}, + } +} + +func (cache *Cache[T]) GetCache(key string, ttl int8) (T, bool) { + val, isPresent := cache.Data[key+"_weather"] + + // If key is not present, return a zero value + if !isPresent { + return val.Element, false + } + + // Otherwise check whether cache element is expired + currentTime := time.Now() + expired := currentTime.Sub(val.Timestamp) > (time.Duration(ttl) * time.Hour) + if expired { + return val.Element, false + } + + return val.Element, true +} + +func (cache *Cache[T]) AddEntry(entry T, cityName string) { + currentTime := time.Now() + + switch any(entry).(type) { + case Weather: + { + cache.Data[cityName+"_weather"] = CacheEntity[T]{ + Element: entry, + Timestamp: currentTime, + } + } + } +} diff --git a/types/city.go b/types/city.go new file mode 100644 index 0000000..ff2c36e --- /dev/null +++ b/types/city.go @@ -0,0 +1,9 @@ +package types + +// The City data type, representing the name, the latitude and the longitude +// of a location +type City struct { + Name string `json:"name"` + Lat float64 `json:"lat"` + Lon float64 `json:"lon"` +} diff --git a/types/metrics.go b/types/metrics.go new file mode 100644 index 0000000..a142f5b --- /dev/null +++ b/types/metrics.go @@ -0,0 +1,11 @@ +package types + +// The Metrics data type, representing the humidity, pressure and +// similar miscellaneous values +type Metrics struct { + Humidity string `json:"humidity"` + Pressure string `json:"pressure"` + DewPoint string `json:"dewPoint"` + UvIndex int8 `json:"uvIndex"` + Visibility string `json:"visibility"` +} diff --git a/types/variables.go b/types/variables.go new file mode 100644 index 0000000..d6ab9ed --- /dev/null +++ b/types/variables.go @@ -0,0 +1,7 @@ +package types + +// Variables type, representing values read from environment variables +type Variables struct { + Token string + TimeToLive int8 +}