-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
150 lines (129 loc) · 3.64 KB
/
main.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"time"
"html/template"
"github.com/apex/log"
"github.com/gorilla/mux"
"github.com/pkg/errors"
)
type Reading struct {
Value int // >100 is unhealthy by either measurement
Timestamp time.Time
}
var views = template.Must(template.New("").Funcs(template.FuncMap{
"since": func(t time.Time) string {
return fmt.Sprintf("%dminutes ago", int(time.Since(t).Minutes()))
}}).ParseGlob("templates/*.html"))
func main() {
addr := ":" + os.Getenv("PORT")
app := mux.NewRouter()
app.HandleFunc("/", handleIndex).Methods("GET")
if err := http.ListenAndServe(addr, app); err != nil {
log.WithError(err).Fatal("error listening")
}
}
func johorReading() (pasirgudang Reading, err error) {
// Air Pollutant Index of Malaysia
// http://apims.doe.gov.my/public_v2/api_table.html
type MalaysiaAPI struct {
Two4HourAPI [][]string `json:"24hour_api"`
}
var aq MalaysiaAPI
resp, err := http.Get("http://apims.doe.gov.my/data/public/CAQM/last24hours.json")
if err != nil {
return
}
err = json.NewDecoder(resp.Body).Decode(&aq)
if err != nil {
return pasirgudang, err
}
defer resp.Body.Close()
// log.Infof("%v", aq)
loc, _ := time.LoadLocation("Asia/Singapore")
// This feels daft, but whatever
for _, v := range aq.Two4HourAPI {
if v[1] == "Location" {
latest := v[len(v)-1]
log.Infof("Timestamp: %s", latest)
latest = time.Now().Format("2006-01-02") + " " + latest
pasirgudang.Timestamp, err = time.ParseInLocation("2006-01-02 3:04PM", latest, loc)
if err != nil {
return
}
}
if v[1] == "Pasir Gudang" {
latest := v[len(v)-1]
log.Infof("Latest: %s", latest)
_, err = fmt.Sscanf(latest, "%d**", &pasirgudang.Value)
if err != nil {
return pasirgudang, errors.Wrap(err, "Pasir Gudang reading is not available")
}
break
}
}
return pasirgudang, err
}
func singaporeReading() (northSingapore Reading, err error) {
type SingaporePM25 struct {
RegionMetadata []struct {
Name string `json:"name"`
LabelLocation struct {
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
} `json:"label_location"`
} `json:"region_metadata"`
Items []struct {
Timestamp time.Time `json:"timestamp"`
UpdateTimestamp time.Time `json:"update_timestamp"`
Readings struct {
Pm25OneHourly struct {
West int `json:"west"`
East int `json:"east"`
Central int `json:"central"`
South int `json:"south"`
North int `json:"north"`
} `json:"pm25_one_hourly"`
} `json:"readings"`
} `json:"items"`
APIInfo struct {
Status string `json:"status"`
} `json:"api_info"`
}
resp, err := http.Get("https://api.data.gov.sg/v1/environment/pm25")
if err != nil {
return northSingapore, err
}
var aq SingaporePM25
err = json.NewDecoder(resp.Body).Decode(&aq)
defer resp.Body.Close()
log.Infof("%v", aq)
northSingapore.Value = aq.Items[0].Readings.Pm25OneHourly.North
northSingapore.Timestamp = aq.Items[0].UpdateTimestamp
return
}
func handleIndex(w http.ResponseWriter, r *http.Request) {
if os.Getenv("UP_STAGE") != "production" {
w.Header().Set("X-Robots-Tag", "none")
}
northSingapore, err := singaporeReading()
if err != nil {
log.WithError(err).Error("failed to get singapore reading")
}
johor, err := johorReading()
if err != nil {
log.WithError(err).Error("failed to get johor reading")
}
err = views.ExecuteTemplate(w, "index.html", map[string]Reading{
"Singapore": northSingapore,
"Johor": johor,
})
if err != nil {
log.WithError(err).Fatal("template failed to parse")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}