aemet/cmd/serverFunc.go

131 lines
3.0 KiB
Go

package cmd
import (
"embed"
"encoding/json"
"fmt"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
)
var (
listenPort string = "1302"
localidades = map[string]string{
"valencia": "46250",
"madrid": "28079",
"barcelona": "08019",
}
)
//go:embed templates/*
var templatesFolder embed.FS
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("/", autoDetectProvince)
r.GET("/:local", returnProvince)
r.GET("/api/:localidad", returnAPIWeather)
r.GET("/api/:localidad/:dia", returnAPIWeather)
fmt.Printf("Listening on port %v...\n", listenPort)
r.Run(":" + listenPort)
}
func autoDetectProvince(c *gin.Context) {
// c.HTML(http.StatusOK, "templates/index.html", gin.H{
// "title": "Hello world!",
// })
// TODO: Try to autodetect the province of the IP and show the correct corresponding weather
}
func returnProvince(c *gin.Context) {
// TODO: Return prettified HTML representation of the weather based on url parameter
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)
//c.String(500, jsonData)
return
}
c.HTML(http.StatusOK, "templates/index.html", gin.H{
"Base": aemetRequest.Base,
})
}
func pullWeatherInfo(localidad string) (root, string, error) {
jsonData, err := getJSON(localidades[localidad])
if err != nil {
return root{}, jsonData, err
}
textBytes := []byte(jsonData)
aemetRequest := root{}
err = json.Unmarshal(textBytes, &aemetRequest)
if err != nil {
return root{}, jsonData, err
}
return aemetRequest, jsonData, nil
}
func localityIsAvailable(localidad string) bool {
isAvailable := false
for k := range localidades {
if localidad == k {
isAvailable = true
}
}
if isAvailable == false {
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")
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
}
aemetRequest, _, err := pullWeatherInfo(localidad)
if err != nil {
e := fmt.Sprint(err)
c.String(http.StatusInternalServerError, e)
//c.String(500, jsonData)
return
}
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)
}