Add ability to bypass cache with auth header

This commit is contained in:
Jacob Gunther 2024-10-26 22:48:00 +00:00
parent a9febd2540
commit 9c90d370ae
No known key found for this signature in database
GPG Key ID: CC91E427B9B5D07D
4 changed files with 14 additions and 9 deletions

View File

@ -8,7 +8,4 @@ cache:
java_status_duration: 1m
bedrock_status_duration: 1m
icon_duration: 24h
access_control:
enable: true
allowed_origins:
- '*'
bypass_tokens:

View File

@ -22,6 +22,7 @@ var (
JavaStatusDuration: time.Minute,
BedrockStatusDuration: time.Minute,
IconDuration: time.Minute * 15,
BypassTokens: []string{},
},
}
)
@ -42,6 +43,7 @@ type ConfigCache struct {
JavaStatusDuration time.Duration `yaml:"java_status_duration"`
BedrockStatusDuration time.Duration `yaml:"bedrock_status_duration"`
IconDuration time.Duration `yaml:"icon_duration"`
BypassTokens []string `yaml:"bypass_tokens"`
}
// ReadFile reads the configuration from the given file and overrides values using environment variables.

View File

@ -138,7 +138,7 @@ func GetJavaStatus(hostname string, port uint16, opts *StatusOptions) (*JavaStat
}
// Fetch the cached status if it exists
{
if !opts.BypassCache {
cache, ttl, err := r.Get(fmt.Sprintf("java:%s", cacheKey))
if err != nil {
@ -189,7 +189,7 @@ func GetBedrockStatus(hostname string, port uint16, opts *StatusOptions) (*Bedro
}
// Fetch the cached status if it exists
{
if !opts.BypassCache {
cache, ttl, err := r.Get(fmt.Sprintf("bedrock:%s", cacheKey))
if err != nil {
@ -232,7 +232,7 @@ func GetServerIcon(hostname string, port uint16, opts *StatusOptions) ([]byte, t
cacheKey := GetCacheKey(hostname, port, nil)
// Fetch the cached icon if it exists
{
if !opts.BypassCache {
cache, ttl, err := r.Get(fmt.Sprintf("icon:%s", cacheKey))
if err != nil {

View File

@ -45,8 +45,9 @@ type VoteOptions struct {
// StatusOptions is the options provided as query parameters to the status route.
type StatusOptions struct {
Query bool
Timeout time.Duration
Query bool
Timeout time.Duration
BypassCache bool
}
// MutexArray is a thread-safe array for storing and retrieving values.
@ -251,6 +252,11 @@ func GetStatusOptions(ctx *fiber.Ctx) (*StatusOptions, error) {
result.Timeout = time.Duration(math.Max(float64(time.Second)*ctx.QueryFloat("timeout", 5.0), float64(time.Millisecond*500)))
}
// Bypass Cache
{
result.BypassCache = Contains(config.Cache.BypassTokens, ctx.Get("Authorization"))
}
return result, nil
}