Added weather route and embedded cache system
This commit is contained in:
12
types/Weather.go
Normal file
12
types/Weather.go
Normal 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
66
types/cache.go
Normal 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
9
types/city.go
Normal 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
11
types/metrics.go
Normal 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
7
types/variables.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package types
|
||||
|
||||
// Variables type, representing values read from environment variables
|
||||
type Variables struct {
|
||||
Token string
|
||||
TimeToLive int8
|
||||
}
|
||||
Reference in New Issue
Block a user