-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
50 lines (39 loc) · 1.21 KB
/
router.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
package main
import (
"net/http"
"github.com/erickeniuk/geography"
"github.com/erickeniuk/weather"
"github.com/gin-gonic/gin"
)
func main() {
// run app
r := gin.Default()
r.LoadHTMLGlob("./templates/*.html")
r.StaticFile("/weather_icon.png", "./assets/weather_icon.png")
r.StaticFile("/weather.ico", "./assets/weather.ico")
r.Static("/bootstrap", "./templates/bootstrap")
r.Static("/css", "./templates/css")
r.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", gin.H{"content": "This is an index page..."})
})
r.GET("/weather", func(c *gin.Context) {
city := c.Query("city")
latLong, err := geography.GetLatLong(city)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
weatherResponse, err := weather.GetWeather(*latLong)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
weatherDisplay, err := weather.ExtractHourlyWeatherData(city, *weatherResponse)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.HTML(http.StatusOK, "weather.html", weatherDisplay)
})
r.Run(":8080")
}