Add ability to bypass cache with auth header
This commit is contained in:
@@ -8,7 +8,4 @@ cache:
|
|||||||
java_status_duration: 1m
|
java_status_duration: 1m
|
||||||
bedrock_status_duration: 1m
|
bedrock_status_duration: 1m
|
||||||
icon_duration: 24h
|
icon_duration: 24h
|
||||||
access_control:
|
bypass_tokens:
|
||||||
enable: true
|
|
||||||
allowed_origins:
|
|
||||||
- '*'
|
|
||||||
@@ -22,6 +22,7 @@ var (
|
|||||||
JavaStatusDuration: time.Minute,
|
JavaStatusDuration: time.Minute,
|
||||||
BedrockStatusDuration: time.Minute,
|
BedrockStatusDuration: time.Minute,
|
||||||
IconDuration: time.Minute * 15,
|
IconDuration: time.Minute * 15,
|
||||||
|
BypassTokens: []string{},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@@ -42,6 +43,7 @@ type ConfigCache struct {
|
|||||||
JavaStatusDuration time.Duration `yaml:"java_status_duration"`
|
JavaStatusDuration time.Duration `yaml:"java_status_duration"`
|
||||||
BedrockStatusDuration time.Duration `yaml:"bedrock_status_duration"`
|
BedrockStatusDuration time.Duration `yaml:"bedrock_status_duration"`
|
||||||
IconDuration time.Duration `yaml:"icon_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.
|
// ReadFile reads the configuration from the given file and overrides values using environment variables.
|
||||||
|
|||||||
@@ -138,7 +138,7 @@ func GetJavaStatus(hostname string, port uint16, opts *StatusOptions) (*JavaStat
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Fetch the cached status if it exists
|
// Fetch the cached status if it exists
|
||||||
{
|
if !opts.BypassCache {
|
||||||
cache, ttl, err := r.Get(fmt.Sprintf("java:%s", cacheKey))
|
cache, ttl, err := r.Get(fmt.Sprintf("java:%s", cacheKey))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -189,7 +189,7 @@ func GetBedrockStatus(hostname string, port uint16, opts *StatusOptions) (*Bedro
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Fetch the cached status if it exists
|
// Fetch the cached status if it exists
|
||||||
{
|
if !opts.BypassCache {
|
||||||
cache, ttl, err := r.Get(fmt.Sprintf("bedrock:%s", cacheKey))
|
cache, ttl, err := r.Get(fmt.Sprintf("bedrock:%s", cacheKey))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -232,7 +232,7 @@ func GetServerIcon(hostname string, port uint16, opts *StatusOptions) ([]byte, t
|
|||||||
cacheKey := GetCacheKey(hostname, port, nil)
|
cacheKey := GetCacheKey(hostname, port, nil)
|
||||||
|
|
||||||
// Fetch the cached icon if it exists
|
// Fetch the cached icon if it exists
|
||||||
{
|
if !opts.BypassCache {
|
||||||
cache, ttl, err := r.Get(fmt.Sprintf("icon:%s", cacheKey))
|
cache, ttl, err := r.Get(fmt.Sprintf("icon:%s", cacheKey))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
10
src/util.go
10
src/util.go
@@ -45,8 +45,9 @@ type VoteOptions struct {
|
|||||||
|
|
||||||
// StatusOptions is the options provided as query parameters to the status route.
|
// StatusOptions is the options provided as query parameters to the status route.
|
||||||
type StatusOptions struct {
|
type StatusOptions struct {
|
||||||
Query bool
|
Query bool
|
||||||
Timeout time.Duration
|
Timeout time.Duration
|
||||||
|
BypassCache bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// MutexArray is a thread-safe array for storing and retrieving values.
|
// 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)))
|
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
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user