-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
117 lines (98 loc) · 3.01 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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"github.com/Parallels/acme-weather-demo-backend/controllers"
"github.com/Parallels/acme-weather-demo-backend/entities"
"github.com/Parallels/acme-weather-demo-backend/services"
"github.com/go-resty/resty/v2"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/joho/godotenv"
)
type WeatherResponse struct {
Weather []struct {
Main string `json:"main"`
} `json:"weather"`
Main struct {
Temp float64 `json:"temp"`
} `json:"main"`
}
func getWeather(city string, apiKey string) (*WeatherResponse, error) {
client := resty.New()
resp, err := client.R().
SetQueryParams(map[string]string{
"q": city,
"appid": apiKey,
"units": "metric",
}).
Get("https://api.openweathermap.org/data/2.5/weather")
if err != nil {
return nil, err
}
var weatherResponse WeatherResponse
body := resp.Body()
if !resp.IsSuccess() {
var apiError entities.OpenWeatherMapAPIErrorResponse
if resp.StatusCode() == 401 {
if err := json.Unmarshal(body, &apiError); err != nil {
apiError = entities.OpenWeatherMapAPIErrorResponse{
Code: resp.StatusCode(),
Message: err.Error(),
}
}
} else {
apiError = entities.OpenWeatherMapAPIErrorResponse{
Code: resp.StatusCode(),
Message: string(body),
}
}
return nil, &apiError
}
if err := json.Unmarshal(body, &weatherResponse); err != nil {
apiError := entities.OpenWeatherMapAPIErrorResponse{
Code: resp.StatusCode(),
Message: "unknown error",
}
return nil, &apiError
}
return &weatherResponse, nil
}
func weatherHandler(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
city := params["city"]
log.Printf("Getting weather for %s", city)
apiKey := os.Getenv("OPENWEATHERMAP_API_KEY")
weatherResponse, err := getWeather(city, apiKey)
if err != nil {
log.Print(err)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
fmt.Fprintf(w, "The temperature in %s is %.1f°C and the weather is %s.", city, weatherResponse.Main.Temp, weatherResponse.Weather[0].Main)
}
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
services.GetWeatherService()
router := mux.NewRouter()
router.HandleFunc("/weather/{city}", weatherHandler).Methods("GET")
router.HandleFunc("/weather/{country}/{city}/forecast", controllers.CityWeatherForecastHandler).Methods("GET")
router.HandleFunc("/weather/{country}/{state}/{city}/forecast", controllers.CityWeatherForecastHandler).Methods("GET")
router.HandleFunc("/geo/{country}/{city}", controllers.GeoLocationHandler).Methods("GET")
router.HandleFunc("/geo/{country}/{state}/{city}", weatherHandler).Methods("GET")
router.HandleFunc("/icon/{os}/{icon}", controllers.IconHandler).Methods("GET")
port := os.Getenv("PORT")
if port == "" {
port = "5000"
}
log.Printf("Listening on port %s", port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), handlers.CORS()(router)))
}