-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
39 lines (33 loc) · 815 Bytes
/
main.go
File metadata and controls
39 lines (33 loc) · 815 Bytes
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
package main
import (
"net/http"
"os"
"github.com/gin-gonic/gin"
)
func main() {
apikey := os.Getenv("OWM_API_KEY")
router := gin.Default()
router.LoadHTMLGlob("templates/*.html")
router.Static("/static", "./static")
router.POST("/", func(c *gin.Context) {
location := c.PostForm("location")
if location == "" {
c.HTML(http.StatusBadRequest, "error.html", gin.H{
"message": "error",
})
return
}
output, err := getWeather(apikey, location)
if err != nil {
c.HTML(http.StatusInternalServerError, "error.html", gin.H{
"message": "failed to get weather data",
})
return
}
c.HTML(http.StatusOK, "interface.html", getTemplate(location, output))
})
router.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "interface.html", gin.H{})
})
router.Run(":8080")
}