-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
148 lines (136 loc) · 3.36 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/ant0ine/go-json-rest/rest"
)
//------------------------------------
// TYPES
//------------------------------------
// WeatherResponse
type WeatherResponse struct {
Location string
Temperature float64
}
//Weather is
type Weather struct {
Coord struct {
Longitude float64 `json:"lon"`
Latitude float64 `json:"lat"`
} `json:"coord"`
Weather []struct {
ID int `json:"id"`
Main string `json:"main"`
Description string `json:"description"`
Icon string `json:"icon"`
} `json:"weather"`
Base string `json:"base"`
Main struct {
Temp float64 `json:"temp"`
Pressure int `json:"pressure"`
Humidity int `json:"humidity"`
TempMin float64 `json:"temp_min"`
TempMax float64 `json:"temp_max"`
} `json:"main"`
Wind struct {
Speed float64 `json:"speed"`
Direction int `json:"deg"`
} `json:"wind"`
Clouds struct {
Percent int `json:"all"`
} `json:"clouds"`
Rain struct {
LastThreeHours int `json:"3h"`
} `json:"rain"`
Snow struct {
LastThreeHours int `json:"3h"`
} `json:"snow"`
Dt int `json:"dt"`
Sys struct {
Type int `json:"type"`
ID int `json:"id"`
Message float64 `json:"message"`
CountryCode string `json:"country"`
Sunrise int `json:"sunrise"`
Sunset int `json:"sunset"`
} `json:"sys"`
CityID int `json:"id"`
CityName string `json:"name"`
Cod int `json:"cod"`
}
//------------------------------------
// MAIN
//------------------------------------
func main() {
api := rest.NewApi()
api.Use(rest.DefaultDevStack...)
router, err := rest.MakeRouter(
rest.Get("/weather/#location", func(w rest.ResponseWriter, req *rest.Request) {
weather, err := GetWeather(req.PathParam("location"))
if err != nil {
rest.Error(w, err.Error(), http.StatusInternalServerError)
return
}
//w.WriteJson(&weather)
//
w.WriteJson(
&WeatherResponse{
Location: fmt.Sprintf("%s", weather.CityName),
Temperature: KelvinToFarenheit(weather.Main.Temp),
},
)
}),
)
if err != nil {
log.Fatal(err)
}
api.SetApp(router)
log.Fatal(http.ListenAndServe(":8080", api.MakeHandler()))
}
//------------------------------------
// FUNCS
//------------------------------------
// getContent is a generic URL get function
func getContent(url string) ([]byte, error) {
// Build the request
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
// Send the request via a client
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
// Defer the closing of the body
defer resp.Body.Close()
// Read the content into a byte array
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}
// GetWeather calls the openweathermap API
func GetWeather(location string) (*Weather, error) {
content, err := getContent(
fmt.Sprintf("http://api.openweathermap.org/data/2.5/weather?q=%s", location))
if err != nil {
return nil, err
}
// Fill the record with the data from the JSON
var weather Weather
err = json.Unmarshal(content, &weather)
if err != nil {
return nil, err
}
return &weather, err
}
// KelvinToFarenheit calculates fahrenheit values
func KelvinToFarenheit(temp float64) float64 {
return (temp-273.15)*1.8000 + 32.00
}