Main project #1

Merged
raul merged 33 commits from testing into main 2024-06-14 07:49:34 +02:00
1 changed files with 43 additions and 22 deletions
Showing only changes of commit 0863a258c6 - Show all commits

View File

@ -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)