-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
108 lines (94 loc) · 2.89 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
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"log"
"net/http"
"github.com/gin-gonic/gin"
)
const weatherAPIURL = "http://api.weatherapi.com/v1/current.json"
const apiKey = "deleted but is in Binary file"
type WeatherResponse struct {
Location struct {
Name string `json:"name"`
Region string `json:"region"`
Country string `json:"country"`
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
TzID string `json:"tz_id"`
LocaltimeEpoch int `json:"localtime_epoch"`
Localtime string `json:"localtime"`
} `json:"location"`
Current struct {
LastUpdatedEpoch int `json:"last_updated_epoch"`
LastUpdated string `json:"last_updated"`
TempC float64 `json:"temp_c"`
TempF float64 `json:"temp_f"`
IsDay int `json:"is_day"`
Condition struct {
Text string `json:"text"`
Icon string `json:"icon"`
Code int `json:"code"`
} `json:"condition"`
WindMph float64 `json:"wind_mph"`
WindKph float64 `json:"wind_kph"`
WindDegree int `json:"wind_degree"`
WindDir string `json:"wind_dir"`
PressureMb float64 `json:"pressure_mb"`
PressureIn float64 `json:"pressure_in"`
PrecipMm float64 `json:"precip_mm"`
PrecipIn float64 `json:"precip_in"`
Humidity int `json:"humidity"`
Cloud int `json:"cloud"`
FeelslikeC float64 `json:"feelslike_c"`
FeelslikeF float64 `json:"feelslike_f"`
WindchillC float64 `json:"windchill_c"`
WindchillF float64 `json:"windchill_f"`
HeatindexC float64 `json:"heatindex_c"`
HeatindexF float64 `json:"heatindex_f"`
DewpointC float64 `json:"dewpoint_c"`
DewpointF float64 `json:"dewpoint_f"`
VisKm float64 `json:"vis_km"`
VisMiles float64 `json:"vis_miles"`
UV float64 `json:"uv"`
GustMph float64 `json:"gust_mph"`
GustKph float64 `json:"gust_kph"`
} `json:"current"`
}
func fetchWeather(city string) (*WeatherResponse, error) {
url := weatherAPIURL + "?key=" + apiKey + "&q=" + city + "&aqi=no"
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var weatherResponse WeatherResponse
err = json.Unmarshal(body, &weatherResponse)
if err != nil {
return nil, err
}
return &weatherResponse, nil
}
func main() {
// Define a command-line flag for the port number
port := flag.String("port", "8080", "port number to run the server on")
flag.Parse()
router := gin.Default()
router.GET("/weather/:city", func(c *gin.Context) {
city := c.Param("city")
weather, err := fetchWeather(city)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, weather)
})
// Start the server with the specified or default port number
log.Fatal(router.Run(fmt.Sprintf(":%s", *port)))
}