aemet/cmd/serverFunc.go

128 lines
2.9 KiB
Go
Raw Normal View History

2024-05-08 09:08:36 +02:00
package cmd
import (
"embed"
2024-05-08 09:29:58 +02:00
"encoding/json"
2024-05-08 09:08:36 +02:00
"fmt"
2024-05-08 09:29:58 +02:00
"net/http"
"strconv"
"github.com/gin-gonic/gin"
2024-05-08 09:08:36 +02:00
)
var (
listenPort string = "1302"
localidades = map[string]string{
"valencia": "46250",
"madrid": "28079",
"barcelona": "08019",
}
2024-05-08 09:08:36 +02:00
)
//go:embed templates/*
var templatesFolder embed.FS
2024-05-08 09:08:36 +02:00
func server() {
//gin.SetMode(gin.ReleaseMode)
r := gin.Default()
LoadHTMLFromEmbedFS(r, templatesFolder, "templates/*.html")
r.StaticFileFS("/style.css", "./templates/style.css", http.FS(templatesFolder))
r.GET("/", chooseProvince)
2024-05-27 08:44:59 +02:00
r.GET("/:local", returnProvince)
r.GET("/api/:localidad", returnAPIWeather)
r.GET("/api/:localidad/:dia", returnAPIWeather)
2024-05-08 09:16:05 +02:00
fmt.Printf("Listening on port %v...\n", listenPort)
r.Run(":" + listenPort)
}
func chooseProvince(c *gin.Context) {
c.HTML(http.StatusOK, "templates/redirect.html", gin.H{
"Localidades": localidades,
})
2024-05-08 09:16:05 +02:00
}
2024-05-27 08:44:59 +02:00
func returnProvince(c *gin.Context) {
localidad := c.Param("local")
if isAv := localityIsAvailable(localidad); isAv != true {
c.String(http.StatusNotFound, "The locality doesn't exist or is currently not supported, sorry\n")
return
}
aemetRequest, _, err := pullWeatherInfo(localidad)
if err != nil {
e := fmt.Sprint(err)
c.String(http.StatusInternalServerError, e)
return
}
c.HTML(http.StatusOK, "templates/index.html", gin.H{
"Base": aemetRequest.Base,
"Localidades": localidades,
})
2024-05-27 08:44:59 +02:00
}
func pullWeatherInfo(localidad string) (root, string, error) {
jsonData, err := getJSON(localidades[localidad])
2024-05-27 08:44:59 +02:00
if err != nil {
2024-05-27 09:03:42 +02:00
return root{}, jsonData, err
2024-05-27 08:44:59 +02:00
}
textBytes := []byte(jsonData)
aemetRequest := root{}
err = json.Unmarshal(textBytes, &aemetRequest)
if err != nil {
2024-05-27 09:03:42 +02:00
return root{}, jsonData, err
2024-05-27 08:44:59 +02:00
}
2024-05-27 09:03:42 +02:00
return aemetRequest, jsonData, nil
2024-05-27 08:44:59 +02:00
}
func localityIsAvailable(localidad string) bool {
isAvailable := false
for k := range localidades {
if localidad == k {
isAvailable = true
}
}
if isAvailable == false {
2024-05-27 08:44:59 +02:00
return false
}
return true
}
func returnAPIWeather(c *gin.Context) {
c.Writer.Header().Set("Federal-Agents", "Outside my home")
var n int
var err error
localidad := c.Param("localidad")
2024-05-27 08:44:59 +02:00
numDias := c.Param("dia")
if isAv := localityIsAvailable(localidad); isAv != true {
c.String(http.StatusNotFound, "The locality doesn't exist or is currently not supported, sorry\n")
return
}
2024-05-27 08:44:59 +02:00
aemetRequest, _, err := pullWeatherInfo(localidad)
2024-05-08 11:14:30 +02:00
if err != nil {
e := fmt.Sprint(err)
c.String(http.StatusInternalServerError, e)
2024-05-27 09:03:42 +02:00
//c.String(500, jsonData)
2024-05-08 11:14:30 +02:00
return
}
2024-05-08 09:16:05 +02:00
2024-05-21 10:38:28 +02:00
if numDias != "" {
n, err = strconv.Atoi(numDias)
if err != nil || n > len(aemetRequest.Base.Prediccion.Dia)-1 || n < 0 {
c.String(http.StatusNotAcceptable, "Invalid day identifier")
return
}
}
if numDias != "" {
c.IndentedJSON(http.StatusOK, aemetRequest.Base.Prediccion.Dia[n])
} else {
c.IndentedJSON(http.StatusOK, aemetRequest)
}
//c.JSON(http.StatusOK, aemetRequest)
2024-05-08 09:08:36 +02:00
}