-
Notifications
You must be signed in to change notification settings - Fork 114
/
handlers.go
173 lines (149 loc) · 4.66 KB
/
handlers.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"github.com/genuinetools/weather/forecast"
"github.com/genuinetools/weather/geocode"
"github.com/sirupsen/logrus"
)
// JSONResponse is a map[string]string response from the web server.
type JSONResponse map[string]string
// String returns the string representation of the JSONResponse object.
func (j JSONResponse) String() string {
str, err := json.MarshalIndent(j, "", " ")
if err != nil {
return fmt.Sprintf(`{
"error": "%v"
}`, err)
}
return string(str)
}
// forecastHandler takes a forecast.Request object and passes it to the darksky API.
func (cmd *serverCommand) forecastHandler(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
var f forecast.Request
if err := decoder.Decode(&f); err != nil {
writeError(w, fmt.Sprintf("parsing request body for forecast failed: %v", err))
return
}
// data to send to the API
exclude, err := json.Marshal(f.Exclude)
if err != nil {
writeError(w, fmt.Sprintf("marshal forecast exclude failed: %v", err))
return
}
data := url.Values{"units": {f.Units}, "exclude": {string(exclude)}}
// request the darksky.net API
url := fmt.Sprintf("%s/%s/%g,%g?%s", darkskyAPIURI, cmd.darkskyAPIKey, f.Latitude, f.Longitude, data.Encode())
resp, err := http.Get(url)
if err != nil {
writeError(w, fmt.Sprintf("request to %s failed: %v", url, err))
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
writeError(w, fmt.Sprintf("reading response body from %s failed: %v", url, err))
return
}
// write the response from the API to our client
w.WriteHeader(resp.StatusCode)
if _, err := w.Write(body); err != nil {
writeError(w, fmt.Sprintf("writing response from %s failed: %v", url, err))
return
}
}
// geocodeHandler takes a geocode.Request object and passes it to the Google Geocode API.
func (cmd *serverCommand) geocodeHandler(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
var g geocode.Request
if err := decoder.Decode(&g); err != nil {
writeError(w, fmt.Sprintf("parsing request body for geocode failed: %v", err))
return
}
if g.Location == "" {
writeError(w, "Location was not sent.")
return
}
// data to send to the API
data := url.Values{"address": {g.Location}, "key": {cmd.geocodeAPIKey}}
// request the geocode API
url := fmt.Sprintf("%s?%s", geocodeAPIURI, data.Encode())
resp, err := http.Get(url)
if err != nil {
writeError(w, fmt.Sprintf("request to %s failed: %v", url, err))
return
}
defer resp.Body.Close()
decoder = json.NewDecoder(resp.Body)
var geoResp geocode.Response
if err := decoder.Decode(&geoResp); err != nil {
writeError(w, fmt.Sprintf("parsing response body for geocode failed: %v", err))
return
}
// These messages come from Google Geocoding API server
if geoResp.ErrorMessage != "" {
writeError(w, fmt.Sprintf("Google Geocode API response error: %s - %s", geoResp.Status, geoResp.ErrorMessage))
return
}
// check if we have results
if len(geoResp.Results) <= 0 {
writeError(w, "No results found.")
return
}
result := geoResp.Results[0]
geo := geocode.Geocode{
Latitude: result.Geometry.Location.Latitude,
Longitude: result.Geometry.Location.Longitude,
}
// parse each address for information to add to the geocode struct
for _, addr := range result.AddressComponents {
for _, t := range addr.Types {
switch t {
case "postal_code":
geo.PostalCode = addr.LongName
case "country":
geo.Country = addr.LongName
geo.CountryCode = addr.ShortName
geo.CountryCode3 = addr.ShortName
case "locality":
geo.City = addr.LongName
case "administrative_area_level_1":
geo.Region = addr.LongName
geo.RegionCode = addr.ShortName
}
}
}
// marshal the geo object
body, err := json.Marshal(geo)
if err != nil {
writeError(w, fmt.Sprintf("marshal geo body failed: %v", err))
return
}
// write the response from the API to our client
w.WriteHeader(resp.StatusCode)
if _, err := w.Write(body); err != nil {
writeError(w, fmt.Sprintf("writing response from %s failed: %v", url, err))
return
}
}
// failHandler returns not a valid endpoint
func failHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusNotFound)
fmt.Fprint(w, JSONResponse{
"error": fmt.Sprintf("Not a valid endpoint: %s", r.URL.Path),
})
}
// writeError sends an error back to the requester
// and also logrus. the error
func writeError(w http.ResponseWriter, msg string) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, JSONResponse{
"error": msg,
})
logrus.Printf("writing error: %s", msg)
}