diff --git a/cmd/clientFunc.go b/cmd/clientFunc.go index a6fef06..24f231a 100644 --- a/cmd/clientFunc.go +++ b/cmd/clientFunc.go @@ -58,9 +58,9 @@ type root struct { // fmt.Printf("Temperatura mínima: %v°C\n", aemetRequest.Base.Prediccion.Dia[0].Temperatura.Minima) // } -func getJSON() (s string, err error) { +func getJSON(codPostal string) (s string, err error) { client := &http.Client{} - req, err := http.NewRequest("GET", "https://www.aemet.es/xml/municipios/localidad_46250.xml", nil) + req, err := http.NewRequest("GET", "https://www.aemet.es/xml/municipios/localidad_"+codPostal+".xml", nil) if err != nil { e := fmt.Errorf("Error occurred pulling data: %v\n", err) return "", e diff --git a/cmd/serverFunc.go b/cmd/serverFunc.go index 4026fe9..27d96dc 100644 --- a/cmd/serverFunc.go +++ b/cmd/serverFunc.go @@ -8,18 +8,33 @@ import ( ) var ( - listenPort string = "1302" + listenPort string = "1302" + localidades = map[string]string{ + "valencia": "46250", + "madrid": "28079", + } ) func server() { router := gin.Default() - router.GET("/api/valencia", returnWeather) + router.GET("/api/:codigopostal", returnWeather) fmt.Printf("Listening on port %v...\n", listenPort) router.Run(":" + listenPort) } func returnWeather(c *gin.Context) { - jsonData, err := getJSON() + codPostal := c.Param("codigopostal") + isAvailable := false + for k := range localidades { + if codPostal == k { + isAvailable = true + } + } + if isAvailable == false { + c.String(http.StatusNotFound, "The locality doesn't exist or is currently not supported\n") + return + } + jsonData, err := getJSON(localidades[codPostal]) if err != nil { e := fmt.Sprint(err) c.String(http.StatusInternalServerError, e) @@ -34,4 +49,5 @@ func returnWeather(c *gin.Context) { } c.IndentedJSON(http.StatusOK, aemetRequest) + //c.String(http.StatusOK, jsonData) }