From 0863a258c6fd4733197ef2dd18698aed17e8daa0 Mon Sep 17 00:00:00 2001 From: raul Date: Mon, 27 May 2024 08:44:59 +0200 Subject: [PATCH] Clean up codebase --- cmd/serverFunc.go | 65 +++++++++++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 22 deletions(-) diff --git a/cmd/serverFunc.go b/cmd/serverFunc.go index 6777b93..46f09a7 100644 --- a/cmd/serverFunc.go +++ b/cmd/serverFunc.go @@ -28,26 +28,41 @@ func server() { LoadHTMLFromEmbedFS(r, templatesFolder, "templates/*.html") r.StaticFileFS("/style.css", "./templates/style.css", http.FS(templatesFolder)) - r.GET("/", returnIndex) - r.GET("/api/:localidad", returnWeather) - r.GET("/api/:localidad/:dia", returnWeather) + 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 returnIndex(c *gin.Context) { - c.HTML(http.StatusOK, "templates/index.html", gin.H{ - "title": "Hello world!", - }) +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 returnWeather(c *gin.Context) { - c.Writer.Header().Set("Federal-Agents", "Outside my home") - var n int - var err error - codPostal := c.Param("localidad") - numDias := c.Param("dia") +func returnProvince(c *gin.Context) { + // TODO: Return prettified HTML representation of the weather based on url parameter +} +func pullWeatherInfo(codPostal string) (root, error) { + jsonData, err := getJSON(localidades[codPostal]) + if err != nil { + return root{}, err + } + textBytes := []byte(jsonData) + aemetRequest := root{} + err = json.Unmarshal(textBytes, &aemetRequest) + if err != nil { + return root{}, err + } + return aemetRequest, nil +} + +func localityIsAvailable(codPostal string) bool { isAvailable := false for k := range localidades { if codPostal == k { @@ -55,23 +70,29 @@ func returnWeather(c *gin.Context) { } } 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 + codPostal := c.Param("localidad") + numDias := c.Param("dia") + + if isAv := localityIsAvailable(codPostal); isAv != true { c.String(http.StatusNotFound, "The locality doesn't exist or is currently not supported, sorry\n") return } - jsonData, err := getJSON(localidades[codPostal]) + + aemetRequest, err := pullWeatherInfo(codPostal) if err != nil { e := fmt.Sprint(err) c.String(http.StatusInternalServerError, e) return } - textBytes := []byte(jsonData) - aemetRequest := root{} - err = json.Unmarshal(textBytes, &aemetRequest) - if err != nil { - e := fmt.Sprintf("Error occurred unmarshalling data: %v\n", err) - c.String(http.StatusInternalServerError, e) - return - } if numDias != "" { n, err = strconv.Atoi(numDias)