diff --git a/handlers/getAdventurer.go b/handlers/getAdventurer.go index 9802503..b8a4dcb 100644 --- a/handlers/getAdventurer.go +++ b/handlers/getAdventurer.go @@ -6,6 +6,7 @@ import ( "bdo-rest-api/cache" "bdo-rest-api/scraper" + "bdo-rest-api/utils" "bdo-rest-api/validators" ) @@ -22,17 +23,20 @@ func getAdventurer(w http.ResponseWriter, r *http.Request) { return } - if data, status, date, expires, ok := cache.Profiles.GetRecord([]string{region, profileTarget}); ok { - w.Header().Set("Expires", expires) - w.Header().Set("Last-Modified", date) + bypassCache := validators.ValidateBypassCacheQueryParam(r.URL.Query()["bypassCache"]) + if !bypassCache || !utils.CheckAdminToken(r) { + if data, status, date, expires, ok := cache.Profiles.GetRecord([]string{region, profileTarget}); ok { + w.Header().Set("Expires", expires) + w.Header().Set("Last-Modified", date) - if status == http.StatusOK { - json.NewEncoder(w).Encode(data) - } else { - w.WriteHeader(status) - } + if status == http.StatusOK { + json.NewEncoder(w).Encode(data) + } else { + w.WriteHeader(status) + } - return + return + } } if ok := giveMaintenanceResponse(w, region); ok { diff --git a/handlers/getAdventurerSearch.go b/handlers/getAdventurerSearch.go index 29256be..fe28d93 100644 --- a/handlers/getAdventurerSearch.go +++ b/handlers/getAdventurerSearch.go @@ -6,6 +6,7 @@ import ( "bdo-rest-api/cache" "bdo-rest-api/scraper" + "bdo-rest-api/utils" "bdo-rest-api/validators" ) @@ -25,17 +26,20 @@ func getAdventurerSearch(w http.ResponseWriter, r *http.Request) { return } - if data, status, date, expires, ok := cache.ProfileSearch.GetRecord([]string{region, query, searchType}); ok { - w.Header().Set("Expires", expires) - w.Header().Set("Last-Modified", date) + bypassCache := validators.ValidateBypassCacheQueryParam(r.URL.Query()["bypassCache"]) + if !bypassCache || !utils.CheckAdminToken(r) { + if data, status, date, expires, ok := cache.ProfileSearch.GetRecord([]string{region, query, searchType}); !bypassCache && ok { + w.Header().Set("Expires", expires) + w.Header().Set("Last-Modified", date) - if status == http.StatusOK { - json.NewEncoder(w).Encode(data) - } else { - w.WriteHeader(status) - } + if status == http.StatusOK { + json.NewEncoder(w).Encode(data) + } else { + w.WriteHeader(status) + } - return + return + } } if ok := giveMaintenanceResponse(w, region); ok { diff --git a/handlers/getCache.go b/handlers/getCache.go index 230f25c..77606eb 100644 --- a/handlers/getCache.go +++ b/handlers/getCache.go @@ -1,21 +1,17 @@ package handlers import ( - "bdo-rest-api/cache" "encoding/json" "net/http" - "github.com/spf13/viper" + "bdo-rest-api/cache" + "bdo-rest-api/utils" ) func getCache(w http.ResponseWriter, r *http.Request) { - if token := viper.GetString("admintoken"); len(token) > 0 { - providedToken := r.Header.Get("Authorization") - - if providedToken != "Bearer "+token { - w.WriteHeader(http.StatusUnauthorized) - return - } + if !utils.CheckAdminToken(r) { + w.WriteHeader(http.StatusUnauthorized) + return } cacheType := r.PathValue("cacheType") diff --git a/handlers/getCacheSummary.go b/handlers/getCacheSummary.go index 7b8f84c..d1bc859 100644 --- a/handlers/getCacheSummary.go +++ b/handlers/getCacheSummary.go @@ -1,13 +1,14 @@ package handlers import ( - "bdo-rest-api/cache" "encoding/json" "net/http" "strings" + "bdo-rest-api/cache" + "bdo-rest-api/utils" + sf "github.com/sa-/slicefunk" - "github.com/spf13/viper" ) func getParseCacheKey(cacheType string) func(string) map[string]interface{} { @@ -44,13 +45,9 @@ func getParseCacheKey(cacheType string) func(string) map[string]interface{} { } func getCacheSummary(w http.ResponseWriter, r *http.Request) { - if token := viper.GetString("admintoken"); len(token) > 0 { - providedToken := r.Header.Get("Authorization") - - if providedToken != "Bearer "+token { - w.WriteHeader(http.StatusUnauthorized) - return - } + if !utils.CheckAdminToken(r) { + w.WriteHeader(http.StatusUnauthorized) + return } json.NewEncoder(w).Encode(map[string]interface{}{ diff --git a/handlers/getGuild.go b/handlers/getGuild.go index 3fcc71b..a84c984 100644 --- a/handlers/getGuild.go +++ b/handlers/getGuild.go @@ -6,6 +6,7 @@ import ( "bdo-rest-api/cache" "bdo-rest-api/scraper" + "bdo-rest-api/utils" "bdo-rest-api/validators" ) @@ -22,17 +23,20 @@ func getGuild(w http.ResponseWriter, r *http.Request) { return } - if data, status, date, expires, ok := cache.GuildProfiles.GetRecord([]string{region, name}); ok { - w.Header().Set("Expires", expires) - w.Header().Set("Last-Modified", date) + bypassCache := validators.ValidateBypassCacheQueryParam(r.URL.Query()["bypassCache"]) + if !bypassCache || !utils.CheckAdminToken(r) { + if data, status, date, expires, ok := cache.GuildProfiles.GetRecord([]string{region, name}); !bypassCache && ok { + w.Header().Set("Expires", expires) + w.Header().Set("Last-Modified", date) - if status == http.StatusOK { - json.NewEncoder(w).Encode(data) - } else { - w.WriteHeader(status) - } + if status == http.StatusOK { + json.NewEncoder(w).Encode(data) + } else { + w.WriteHeader(status) + } - return + return + } } if ok := giveMaintenanceResponse(w, region); ok { diff --git a/handlers/getGuildSearch.go b/handlers/getGuildSearch.go index 566e494..197e51f 100644 --- a/handlers/getGuildSearch.go +++ b/handlers/getGuildSearch.go @@ -6,6 +6,7 @@ import ( "bdo-rest-api/cache" "bdo-rest-api/scraper" + "bdo-rest-api/utils" "bdo-rest-api/validators" ) @@ -22,17 +23,20 @@ func getGuildSearch(w http.ResponseWriter, r *http.Request) { return } - if data, status, date, expires, ok := cache.GuildSearch.GetRecord([]string{region, name}); ok { - w.Header().Set("Expires", expires) - w.Header().Set("Last-Modified", date) + bypassCache := validators.ValidateBypassCacheQueryParam(r.URL.Query()["bypassCache"]) + if !bypassCache || !utils.CheckAdminToken(r) { + if data, status, date, expires, ok := cache.GuildSearch.GetRecord([]string{region, name}); !bypassCache && ok { + w.Header().Set("Expires", expires) + w.Header().Set("Last-Modified", date) - if status == http.StatusOK { - json.NewEncoder(w).Encode(data) - } else { - w.WriteHeader(status) - } + if status == http.StatusOK { + json.NewEncoder(w).Encode(data) + } else { + w.WriteHeader(status) + } - return + return + } } if ok := giveMaintenanceResponse(w, region); ok { diff --git a/handlers/getStatus.go b/handlers/getStatus.go index 0e916d2..4dcb5f9 100644 --- a/handlers/getStatus.go +++ b/handlers/getStatus.go @@ -12,7 +12,7 @@ import ( ) var initTime = time.Now() -var version = "1.16.0" +var version = "1.17.0" func getStatus(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(map[string]interface{}{ diff --git a/utils/CheckAdminToken.go b/utils/CheckAdminToken.go new file mode 100644 index 0000000..d5f4922 --- /dev/null +++ b/utils/CheckAdminToken.go @@ -0,0 +1,19 @@ +package utils + +import ( + "net/http" + + "github.com/spf13/viper" +) + +func CheckAdminToken(r *http.Request) bool { + if token := viper.GetString("admintoken"); len(token) > 0 { + providedToken := r.Header.Get("Authorization") + + if providedToken != "Bearer "+token { + return false + } + } + + return true +} diff --git a/validators/ValidateBypassCacheQueryParam.go b/validators/ValidateBypassCacheQueryParam.go new file mode 100644 index 0000000..57fdd30 --- /dev/null +++ b/validators/ValidateBypassCacheQueryParam.go @@ -0,0 +1,5 @@ +package validators + +func ValidateBypassCacheQueryParam(query []string) (bypassCache bool) { + return len(query) > 0 && query[0] == "1" +}