38 lines
744 B
Go
38 lines
744 B
Go
package cmd
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/gin-gonic/gin"
|
|
"net/http"
|
|
)
|
|
|
|
var (
|
|
listenPort string = "1302"
|
|
)
|
|
|
|
func server() {
|
|
router := gin.Default()
|
|
router.GET("/api/valencia", returnWeather)
|
|
fmt.Printf("Listening on port %v...\n", listenPort)
|
|
router.Run(":" + listenPort)
|
|
}
|
|
|
|
func returnWeather(c *gin.Context) {
|
|
jsonData, err := getJSON()
|
|
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)
|
|
}
|
|
|
|
c.IndentedJSON(http.StatusOK, aemetRequest)
|
|
}
|