-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.go
94 lines (84 loc) · 3.09 KB
/
weather.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"fmt"
"net/http"
"log"
"os"
"io/ioutil"
"strings"
"github.com/tidwall/gjson"
"time"
"strconv"
)
var (
//This environment variable is fed from the downward API. See the deployment configuration for details
ENVIRONMENT string = os.Getenv("NAMESPACE")
)
type WeatherSession struct {
WeatherAPIKey string
CurrentTemp float64
FetchIntervalMinutes uint32
}
func NewWeatherSession() (*WeatherSession) {
log.Println("Creating new weather session")
weather_session := &WeatherSession{}
log.Println("Fetching key..")
weather_session.fetch_key()
log.Println("Updating weather store..")
weather_session.update_weather()
fetch_update_env := os.Getenv("UPDATE_INTERVAL_MINUTES")
if fetch_update, err := strconv.Atoi(fetch_update_env); err == nil && fetch_update > 0 {
weather_session.FetchIntervalMinutes = uint32(fetch_update)
log.Println("Setting update interval to ", fetch_update_env, " minutes")
} else {
weather_session.FetchIntervalMinutes = 60
log.Println("No update interval set. Defaulting to an hour")
}
return weather_session
}
func (session *WeatherSession) fetch_key() {
const KEY_FILE string = "/data/weather/key"
log.Println("Reading weather key file..")
key, err := ioutil.ReadFile(KEY_FILE)
if err != nil {
log.Fatal(fmt.Sprint("Could not read the weather api key from the secret", KEY_FILE))
os.Exit(1)
}
session.WeatherAPIKey = strings.TrimSpace(string(key))
}
func (session *WeatherSession) update_weather() {
log.Println("Requesting current weather..")
//Note we use the external service endpoint here in the form open-weathermap-service.<namespace>.svc
resp, err := http.Get(fmt.Sprintf("http://open-weathermap-service.%s.svc/data/2.5/weather?q=Raleigh&units=imperial&appid=%s", ENVIRONMENT, session.WeatherAPIKey))
if err != nil {
log.Fatal(fmt.Sprintf("Could not get the weather: %s", err.Error()))
os.Exit(1)
}
if resp.StatusCode != 200 {
log.Fatal(fmt.Sprintf("Something went wrong when attempting to get the weather, go status code %d", resp.StatusCode))
os.Exit(1)
}
data, _ := ioutil.ReadAll(resp.Body)
session.CurrentTemp = gjson.Get(string(data), "main.temp").Num
log.Println("Weather updated to ", session.CurrentTemp)
}
func serve_weather(weather *WeatherSession) {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
log.Println(fmt.Sprint("Received request from ", r.RemoteAddr, " at ", time.Now().String()))
fmt.Fprintf(w, "Current Raleigh temperature: %f \n", weather.CurrentTemp)
})
log.Print("Ready for weather requests..")
log.Fatal(http.ListenAndServe(":8080", nil))
}
func update_timer(session *WeatherSession) {
for {
sleep_time := time.Duration(session.FetchIntervalMinutes) * time.Minute
time.Sleep(sleep_time)
session.update_weather()
}
}
func main() {
weather_session := NewWeatherSession()
go update_timer(weather_session)
serve_weather(weather_session)
}