Browse Source

Adding caching to the Dark Sky API call

master
Levi Olson 5 years ago
parent
commit
5ab4a83098
2 changed files with 13 additions and 7 deletions
  1. +13
    -3
      main.go
  2. +0
    -4
      rest.go

+ 13
- 3
main.go View File

@ -5,7 +5,10 @@ import (
"net/http"
"os"
"strconv"
"time"
"github.com/gin-contrib/cache"
"github.com/gin-contrib/cache/persistence"
"github.com/gin-gonic/autotls"
"github.com/gin-gonic/gin"
)
@ -29,7 +32,10 @@ func main() {
c.String(http.StatusOK, "pong")
})
router.GET("/current_weather/:lat/:long", func(c *gin.Context) {
cacheDuration := time.Minute * 15
cacheStore := persistence.NewInMemoryStore(cacheDuration)
router.GET("/current_weather/:lat/:long", cache.CachePage(cacheStore, cacheDuration, func(c *gin.Context) {
lat, err := strconv.ParseFloat(c.Params.ByName("lat"), 64)
long, err := strconv.ParseFloat(c.Params.ByName("long"), 64)
if err != nil {
@ -44,9 +50,13 @@ func main() {
}
c.Header("Access-Control-Allow-Origin", "*")
c.JSON(http.StatusOK, gin.H{"weather": response})
})
}))
log.Fatal(autotls.Run(router, "weather.l3vi.co"))
if os.Getenv("GIN_MODE") == "release" {
log.Fatal(autotls.Run(router, "weather.l3vi.co"))
} else {
log.Fatal(router.Run(":80"))
}
}
func currentWeather(lat, long float64, apikey string) (ForecastResponse, error) {

+ 0
- 4
rest.go View File

@ -11,7 +11,6 @@ import (
func get(client *http.Client, url string, output interface{}) error {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
@ -20,7 +19,6 @@ func get(client *http.Client, url string, output interface{}) error {
req.Header.Add("Accept-Encoding", "gzip")
response, err := client.Do(req)
if err != nil {
return err
}
@ -28,13 +26,11 @@ func get(client *http.Client, url string, output interface{}) error {
defer response.Body.Close()
err = checkErrors(response)
if err != nil {
return err
}
body, err := decompress(response)
if err != nil {
return err
}

Loading…
Cancel
Save