Clean up codebase

This commit is contained in:
raul 2024-05-27 08:44:59 +02:00
parent 7d5c8a26aa
commit 0863a258c6
1 changed files with 43 additions and 22 deletions

View File

@ -28,26 +28,41 @@ func server() {
LoadHTMLFromEmbedFS(r, templatesFolder, "templates/*.html") LoadHTMLFromEmbedFS(r, templatesFolder, "templates/*.html")
r.StaticFileFS("/style.css", "./templates/style.css", http.FS(templatesFolder)) r.StaticFileFS("/style.css", "./templates/style.css", http.FS(templatesFolder))
r.GET("/", returnIndex) r.GET("/", autoDetectProvince)
r.GET("/api/:localidad", returnWeather) r.GET("/:local", returnProvince)
r.GET("/api/:localidad/:dia", returnWeather) r.GET("/api/:localidad", returnAPIWeather)
r.GET("/api/:localidad/:dia", returnAPIWeather)
fmt.Printf("Listening on port %v...\n", listenPort) fmt.Printf("Listening on port %v...\n", listenPort)
r.Run(":" + listenPort) r.Run(":" + listenPort)
} }
func returnIndex(c *gin.Context) { func autoDetectProvince(c *gin.Context) {
c.HTML(http.StatusOK, "templates/index.html", gin.H{ // c.HTML(http.StatusOK, "templates/index.html", gin.H{
"title": "Hello world!", // "title": "Hello world!",
}) // })
// TODO: Try to autodetect the province of the IP and show the correct corresponding weather
} }
func returnWeather(c *gin.Context) { func returnProvince(c *gin.Context) {
c.Writer.Header().Set("Federal-Agents", "Outside my home") // TODO: Return prettified HTML representation of the weather based on url parameter
var n int }
var err error
codPostal := c.Param("localidad")
numDias := c.Param("dia")
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 isAvailable := false
for k := range localidades { for k := range localidades {
if codPostal == k { if codPostal == k {
@ -55,23 +70,29 @@ func returnWeather(c *gin.Context) {
} }
} }
if isAvailable == false { 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") c.String(http.StatusNotFound, "The locality doesn't exist or is currently not supported, sorry\n")
return return
} }
jsonData, err := getJSON(localidades[codPostal])
aemetRequest, err := pullWeatherInfo(codPostal)
if err != nil { if err != nil {
e := fmt.Sprint(err) e := fmt.Sprint(err)
c.String(http.StatusInternalServerError, e) c.String(http.StatusInternalServerError, e)
return 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 != "" { if numDias != "" {
n, err = strconv.Atoi(numDias) n, err = strconv.Atoi(numDias)