diff --git a/server/city.go b/server/city.go index 8c5f04c..4a8f20d 100644 --- a/server/city.go +++ b/server/city.go @@ -1,6 +1,7 @@ package server import ( + "github.com/LeRoid-hub/Mensa-API/cache" "github.com/LeRoid-hub/Mensa-API/fetch" "github.com/LeRoid-hub/Mensa-API/scrape" "github.com/gin-gonic/gin" @@ -15,6 +16,18 @@ func city(c *gin.Context) { return } + if cache.HasCacheData("city/" + city) { + cacheData, err := cache.GetCacheData("city/" + city) + if err != nil { + c.JSON(500, gin.H{ + "error": err.Error(), + }) + return + } + c.JSON(200, cacheData) + return + } + resp, err := fetch.Fetch(city) if err != nil { c.JSON(500, gin.H{ @@ -32,5 +45,7 @@ func city(c *gin.Context) { } scraped := scrape.ScrapeState(resp.Body) + + cache.SetCacheData("city/"+city, scraped) c.JSON(200, scraped) } diff --git a/server/state.go b/server/state.go index 3f8671c..4953bd8 100644 --- a/server/state.go +++ b/server/state.go @@ -1,6 +1,7 @@ package server import ( + "github.com/LeRoid-hub/Mensa-API/cache" "github.com/LeRoid-hub/Mensa-API/fetch" "github.com/LeRoid-hub/Mensa-API/scrape" "github.com/gin-gonic/gin" @@ -15,6 +16,18 @@ func state(c *gin.Context) { return } + if cache.HasCacheData("state/" + state) { + cacheData, err := cache.GetCacheData("state/" + state) + if err != nil { + c.JSON(500, gin.H{ + "error": err.Error(), + }) + return + } + c.JSON(200, cacheData) + return + } + resp, err := fetch.Fetch(state + ".html") if err != nil { c.JSON(500, gin.H{ @@ -32,6 +45,7 @@ func state(c *gin.Context) { } scraped := scrape.ScrapeState(resp.Body) + cache.SetCacheData("state/"+state, scraped) c.JSON(200, scraped) }