finished and have a git file for it, is modified with .env
This commit is contained in:
1 parent
0bcaf223b0
commit
198cd4ff43
6 files changed
+141
-5
No files matched your search
@@ -0,0 +1,5 @@
|
||||
# Weather API
|
||||
API_ADDRESS="https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/"
|
||||
LOCAL_ADDRESS="Forestville,Cincinnati"
|
||||
# LOCAL_ADDRESS="New_York,America"
|
||||
EXTENSION="?unitGroup=us"
|
||||
+2
-3
@@ -3,7 +3,6 @@
|
||||
.vscode/
|
||||
.idea/
|
||||
|
||||
.env
|
||||
.env.*
|
||||
bin/
|
||||
|
||||
bin/
|
||||
redis/
|
||||
@@ -0,0 +1,5 @@
|
||||
FROM redis/redis-stack:latest
|
||||
|
||||
EXPOSE 6379 8001
|
||||
|
||||
CMD ["/entrypoint.sh"]
|
||||
@@ -0,0 +1,11 @@
|
||||
module com.DarthMooMancer.weather_api
|
||||
|
||||
go 1.27.3
|
||||
|
||||
require (
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
github.com/joho/godotenv v1.5.1 // indirect
|
||||
github.com/redis/go-redis/v9 v9.23.0 // indirect
|
||||
go.uber.org/atomic v1.12.0 // indirect
|
||||
golang.org/x/sys v0.48.0 // indirect
|
||||
)
|
||||
@@ -0,0 +1,10 @@
|
||||
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||
github.com/redis/go-redis/v9 v9.23.0 h1:/TTVdJa8BRNsybwwGGnmiYktHNlUXtbOQxlWpHBPL3s=
|
||||
github.com/redis/go-redis/v9 v9.23.0/go.mod h1:EWP2UUk+XDaI1s5nQB5mablZN6vccXlmBQTyfnF87D0=
|
||||
go.uber.org/atomic v1.12.0 h1:BvcXdFKuviU4fTL/f+SxdQ5qJX/Jix8pAkgdUcb3XOE=
|
||||
go.uber.org/atomic v1.12.0/go.mod h1:I6c4cg+6HCxRjfjSsYtApoFILnpc0CGUdGkXVqbYVNk=
|
||||
golang.org/x/sys v0.48.0 h1:bbX/i/6MgT9BVLM9RT1thmxL04yeTAhbEz4SyadbXoo=
|
||||
golang.org/x/sys v0.48.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
|
||||
@@ -1,7 +1,113 @@
|
||||
package main
|
||||
|
||||
import ("fmt")
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
type CurrentConditions struct {
|
||||
DateTime string `json:"datetime" redis:"datetime"`
|
||||
Temp float64 `json:"temp" redis:"temp"`
|
||||
Feelslike float64 `json:"feelslike" redis:"feelslike"`
|
||||
Humidity float64 `json:"humidity" redis:"humidity"`
|
||||
Precip float64 `json:"precip" redis:"precip"`
|
||||
Precipprob float64 `json:"precipprob" redis:"precipprob"`
|
||||
Conditions string `json:"conditions" redis:"conditions"`
|
||||
}
|
||||
|
||||
type Weather struct {
|
||||
ResolvedAddress string `json:"resolvedAddress" redis:"resolvedAddress"`
|
||||
Address string `json:"address" redis:"address"`
|
||||
Timezone string `json:"timezone" redis:"timezone"`
|
||||
CurrentConditions CurrentConditions `json:"currentConditions" redis:"currentConditions"`
|
||||
}
|
||||
|
||||
func getNewWeatherStruct(ctx context.Context, rdb *redis.Client) (Weather, error) {
|
||||
var cachedWeather Weather
|
||||
|
||||
raw, err := rdb.Get(ctx, "weather").Bytes()
|
||||
if err != nil {
|
||||
return cachedWeather, err
|
||||
}
|
||||
if err := json.Unmarshal(raw, &cachedWeather); err != nil {
|
||||
return cachedWeather, err
|
||||
}
|
||||
return cachedWeather, nil
|
||||
}
|
||||
|
||||
func refreshRedisCache(ctx context.Context, rdb *redis.Client) error {
|
||||
_ = godotenv.Load()
|
||||
|
||||
address := os.Getenv("LOCAL_ADDRESS")
|
||||
|
||||
cachedWeather, err := getNewWeatherStruct(ctx, rdb)
|
||||
if err != nil && !errors.Is(err, redis.Nil) {
|
||||
return err
|
||||
} else if errors.Is(err, redis.Nil) || cachedWeather.Address != address {
|
||||
log.Println("Fetching New Data...")
|
||||
resp, err := http.Get(os.Getenv("API_ADDRESS") + address + os.Getenv("EXTENSION"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
log.Println("Caching data to redis...")
|
||||
|
||||
var obj Weather
|
||||
if err := json.NewDecoder(resp.Body).Decode(&obj); err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := rdb.Set(ctx, "weather", data, 30*time.Minute).Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
log.Println("Successfully fetched and cached data...")
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println("Hello World")
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: "localhost:6379",
|
||||
Password: "",
|
||||
DB: 0,
|
||||
Protocol: 2,
|
||||
})
|
||||
defer rdb.Close()
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
exists := refreshRedisCache(ctx, rdb)
|
||||
if exists != nil {
|
||||
log.Fatal(exists)
|
||||
}
|
||||
|
||||
w, exists := getNewWeatherStruct(ctx, rdb)
|
||||
if exists != nil {
|
||||
log.Fatal(exists)
|
||||
}
|
||||
|
||||
fmt.Printf("%s @ %s\n\nConditions: %s\nTemp: %.1f\nFeels Like: %.1f\nHumidity: %.1f\nHeight of Rain: %.1f\nRain Chance: %.1f\n",
|
||||
w.ResolvedAddress,
|
||||
w.CurrentConditions.DateTime,
|
||||
w.CurrentConditions.Conditions,
|
||||
w.CurrentConditions.Temp,
|
||||
w.CurrentConditions.Feelslike,
|
||||
w.CurrentConditions.Humidity,
|
||||
w.CurrentConditions.Precip,
|
||||
w.CurrentConditions.Precipprob)
|
||||
}
|
||||
Reference in new issue
Block a user