Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions handlers/getAdventurer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"bdo-rest-api/cache"
"bdo-rest-api/scraper"
"bdo-rest-api/utils"
"bdo-rest-api/validators"
)

Expand All @@ -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 {
Expand Down
22 changes: 13 additions & 9 deletions handlers/getAdventurerSearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"bdo-rest-api/cache"
"bdo-rest-api/scraper"
"bdo-rest-api/utils"
"bdo-rest-api/validators"
)

Expand All @@ -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 {
Expand Down
14 changes: 5 additions & 9 deletions handlers/getCache.go
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
15 changes: 6 additions & 9 deletions handlers/getCacheSummary.go
Original file line number Diff line number Diff line change
@@ -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{} {
Expand Down Expand Up @@ -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{}{
Expand Down
22 changes: 13 additions & 9 deletions handlers/getGuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"bdo-rest-api/cache"
"bdo-rest-api/scraper"
"bdo-rest-api/utils"
"bdo-rest-api/validators"
)

Expand All @@ -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 {
Expand Down
22 changes: 13 additions & 9 deletions handlers/getGuildSearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"bdo-rest-api/cache"
"bdo-rest-api/scraper"
"bdo-rest-api/utils"
"bdo-rest-api/validators"
)

Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion handlers/getStatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{
Expand Down
19 changes: 19 additions & 0 deletions utils/CheckAdminToken.go
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 5 additions & 0 deletions validators/ValidateBypassCacheQueryParam.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package validators

func ValidateBypassCacheQueryParam(query []string) (bypassCache bool) {
return len(query) > 0 && query[0] == "1"
}