Skip to content

Commit 8251d53

Browse files
authored
Implement query parameter for bypassing cache (#55)
1 parent 0b64165 commit 8251d53

File tree

9 files changed

+88
-55
lines changed

9 files changed

+88
-55
lines changed

handlers/getAdventurer.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"bdo-rest-api/cache"
88
"bdo-rest-api/scraper"
9+
"bdo-rest-api/utils"
910
"bdo-rest-api/validators"
1011
)
1112

@@ -22,17 +23,20 @@ func getAdventurer(w http.ResponseWriter, r *http.Request) {
2223
return
2324
}
2425

25-
if data, status, date, expires, ok := cache.Profiles.GetRecord([]string{region, profileTarget}); ok {
26-
w.Header().Set("Expires", expires)
27-
w.Header().Set("Last-Modified", date)
26+
bypassCache := validators.ValidateBypassCacheQueryParam(r.URL.Query()["bypassCache"])
27+
if !bypassCache || !utils.CheckAdminToken(r) {
28+
if data, status, date, expires, ok := cache.Profiles.GetRecord([]string{region, profileTarget}); ok {
29+
w.Header().Set("Expires", expires)
30+
w.Header().Set("Last-Modified", date)
2831

29-
if status == http.StatusOK {
30-
json.NewEncoder(w).Encode(data)
31-
} else {
32-
w.WriteHeader(status)
33-
}
32+
if status == http.StatusOK {
33+
json.NewEncoder(w).Encode(data)
34+
} else {
35+
w.WriteHeader(status)
36+
}
3437

35-
return
38+
return
39+
}
3640
}
3741

3842
if ok := giveMaintenanceResponse(w, region); ok {

handlers/getAdventurerSearch.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"bdo-rest-api/cache"
88
"bdo-rest-api/scraper"
9+
"bdo-rest-api/utils"
910
"bdo-rest-api/validators"
1011
)
1112

@@ -25,17 +26,20 @@ func getAdventurerSearch(w http.ResponseWriter, r *http.Request) {
2526
return
2627
}
2728

28-
if data, status, date, expires, ok := cache.ProfileSearch.GetRecord([]string{region, query, searchType}); ok {
29-
w.Header().Set("Expires", expires)
30-
w.Header().Set("Last-Modified", date)
29+
bypassCache := validators.ValidateBypassCacheQueryParam(r.URL.Query()["bypassCache"])
30+
if !bypassCache || !utils.CheckAdminToken(r) {
31+
if data, status, date, expires, ok := cache.ProfileSearch.GetRecord([]string{region, query, searchType}); !bypassCache && ok {
32+
w.Header().Set("Expires", expires)
33+
w.Header().Set("Last-Modified", date)
3134

32-
if status == http.StatusOK {
33-
json.NewEncoder(w).Encode(data)
34-
} else {
35-
w.WriteHeader(status)
36-
}
35+
if status == http.StatusOK {
36+
json.NewEncoder(w).Encode(data)
37+
} else {
38+
w.WriteHeader(status)
39+
}
3740

38-
return
41+
return
42+
}
3943
}
4044

4145
if ok := giveMaintenanceResponse(w, region); ok {

handlers/getCache.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
package handlers
22

33
import (
4-
"bdo-rest-api/cache"
54
"encoding/json"
65
"net/http"
76

8-
"github.com/spf13/viper"
7+
"bdo-rest-api/cache"
8+
"bdo-rest-api/utils"
99
)
1010

1111
func getCache(w http.ResponseWriter, r *http.Request) {
12-
if token := viper.GetString("admintoken"); len(token) > 0 {
13-
providedToken := r.Header.Get("Authorization")
14-
15-
if providedToken != "Bearer "+token {
16-
w.WriteHeader(http.StatusUnauthorized)
17-
return
18-
}
12+
if !utils.CheckAdminToken(r) {
13+
w.WriteHeader(http.StatusUnauthorized)
14+
return
1915
}
2016

2117
cacheType := r.PathValue("cacheType")

handlers/getCacheSummary.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package handlers
22

33
import (
4-
"bdo-rest-api/cache"
54
"encoding/json"
65
"net/http"
76
"strings"
87

8+
"bdo-rest-api/cache"
9+
"bdo-rest-api/utils"
10+
911
sf "github.com/sa-/slicefunk"
10-
"github.com/spf13/viper"
1112
)
1213

1314
func getParseCacheKey(cacheType string) func(string) map[string]interface{} {
@@ -44,13 +45,9 @@ func getParseCacheKey(cacheType string) func(string) map[string]interface{} {
4445
}
4546

4647
func getCacheSummary(w http.ResponseWriter, r *http.Request) {
47-
if token := viper.GetString("admintoken"); len(token) > 0 {
48-
providedToken := r.Header.Get("Authorization")
49-
50-
if providedToken != "Bearer "+token {
51-
w.WriteHeader(http.StatusUnauthorized)
52-
return
53-
}
48+
if !utils.CheckAdminToken(r) {
49+
w.WriteHeader(http.StatusUnauthorized)
50+
return
5451
}
5552

5653
json.NewEncoder(w).Encode(map[string]interface{}{

handlers/getGuild.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"bdo-rest-api/cache"
88
"bdo-rest-api/scraper"
9+
"bdo-rest-api/utils"
910
"bdo-rest-api/validators"
1011
)
1112

@@ -22,17 +23,20 @@ func getGuild(w http.ResponseWriter, r *http.Request) {
2223
return
2324
}
2425

25-
if data, status, date, expires, ok := cache.GuildProfiles.GetRecord([]string{region, name}); ok {
26-
w.Header().Set("Expires", expires)
27-
w.Header().Set("Last-Modified", date)
26+
bypassCache := validators.ValidateBypassCacheQueryParam(r.URL.Query()["bypassCache"])
27+
if !bypassCache || !utils.CheckAdminToken(r) {
28+
if data, status, date, expires, ok := cache.GuildProfiles.GetRecord([]string{region, name}); !bypassCache && ok {
29+
w.Header().Set("Expires", expires)
30+
w.Header().Set("Last-Modified", date)
2831

29-
if status == http.StatusOK {
30-
json.NewEncoder(w).Encode(data)
31-
} else {
32-
w.WriteHeader(status)
33-
}
32+
if status == http.StatusOK {
33+
json.NewEncoder(w).Encode(data)
34+
} else {
35+
w.WriteHeader(status)
36+
}
3437

35-
return
38+
return
39+
}
3640
}
3741

3842
if ok := giveMaintenanceResponse(w, region); ok {

handlers/getGuildSearch.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"bdo-rest-api/cache"
88
"bdo-rest-api/scraper"
9+
"bdo-rest-api/utils"
910
"bdo-rest-api/validators"
1011
)
1112

@@ -22,17 +23,20 @@ func getGuildSearch(w http.ResponseWriter, r *http.Request) {
2223
return
2324
}
2425

25-
if data, status, date, expires, ok := cache.GuildSearch.GetRecord([]string{region, name}); ok {
26-
w.Header().Set("Expires", expires)
27-
w.Header().Set("Last-Modified", date)
26+
bypassCache := validators.ValidateBypassCacheQueryParam(r.URL.Query()["bypassCache"])
27+
if !bypassCache || !utils.CheckAdminToken(r) {
28+
if data, status, date, expires, ok := cache.GuildSearch.GetRecord([]string{region, name}); !bypassCache && ok {
29+
w.Header().Set("Expires", expires)
30+
w.Header().Set("Last-Modified", date)
2831

29-
if status == http.StatusOK {
30-
json.NewEncoder(w).Encode(data)
31-
} else {
32-
w.WriteHeader(status)
33-
}
32+
if status == http.StatusOK {
33+
json.NewEncoder(w).Encode(data)
34+
} else {
35+
w.WriteHeader(status)
36+
}
3437

35-
return
38+
return
39+
}
3640
}
3741

3842
if ok := giveMaintenanceResponse(w, region); ok {

handlers/getStatus.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
var initTime = time.Now()
15-
var version = "1.16.0"
15+
var version = "1.17.0"
1616

1717
func getStatus(w http.ResponseWriter, r *http.Request) {
1818
json.NewEncoder(w).Encode(map[string]interface{}{

utils/CheckAdminToken.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package utils
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/spf13/viper"
7+
)
8+
9+
func CheckAdminToken(r *http.Request) bool {
10+
if token := viper.GetString("admintoken"); len(token) > 0 {
11+
providedToken := r.Header.Get("Authorization")
12+
13+
if providedToken != "Bearer "+token {
14+
return false
15+
}
16+
}
17+
18+
return true
19+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package validators
2+
3+
func ValidateBypassCacheQueryParam(query []string) (bypassCache bool) {
4+
return len(query) > 0 && query[0] == "1"
5+
}

0 commit comments

Comments
 (0)