Added weather route and embedded cache system

This commit is contained in:
2025-06-16 16:19:18 +02:00
commit dffec37d90
9 changed files with 385 additions and 0 deletions

81
controller/controller.go Normal file
View File

@@ -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)
}
}

3
go.mod Normal file
View File

@@ -0,0 +1,3 @@
module github.com/ceticamarco/zephyr
go 1.24.4

41
main.go Normal file
View File

@@ -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)
}

155
model/weatherModel.go Normal file
View File

@@ -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
}

12
types/Weather.go Normal file
View File

@@ -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"`
}

66
types/cache.go Normal file
View File

@@ -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,
}
}
}
}

9
types/city.go Normal file
View File

@@ -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"`
}

11
types/metrics.go Normal file
View File

@@ -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"`
}

7
types/variables.go Normal file
View File

@@ -0,0 +1,7 @@
package types
// Variables type, representing values read from environment variables
type Variables struct {
Token string
TimeToLive int8
}