aemet/cmd/serverFunc.go

33 lines
597 B
Go
Raw Normal View History

2024-05-08 09:08:36 +02:00
package cmd
import (
2024-05-08 09:29:58 +02:00
"encoding/json"
2024-05-08 09:08:36 +02:00
"fmt"
"github.com/gin-gonic/gin"
2024-05-08 09:29:58 +02:00
"log"
"net/http"
2024-05-08 09:08:36 +02:00
)
var (
listenPort string = "1302"
)
func server() {
2024-05-08 09:16:05 +02:00
router := gin.Default()
router.GET("/api/valencia", returnWeather)
fmt.Printf("Listening on port %v...\n", listenPort)
2024-05-08 10:47:41 +02:00
router.Run(":" + listenPort)
2024-05-08 09:16:05 +02:00
}
func returnWeather(c *gin.Context) {
jsonData := getJSON()
2024-05-08 09:29:58 +02:00
textBytes := []byte(jsonData)
aemetRequest := root{}
err := json.Unmarshal(textBytes, &aemetRequest)
if err != nil {
log.Fatalf("Error occurred unmarshalling data: %v\n", err)
}
2024-05-08 09:16:05 +02:00
2024-05-08 09:29:58 +02:00
c.IndentedJSON(http.StatusOK, aemetRequest)
2024-05-08 09:08:36 +02:00
}