More cleanup, fix default icon dimensions

This commit is contained in:
Jacob Gunther
2022-08-25 16:39:28 -05:00
parent 738fb77832
commit f7ae8a539b
6 changed files with 68 additions and 44 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 3.1 KiB

View File

@@ -31,6 +31,50 @@ func (r *Redis) Connect(uri string) error {
return r.Client.Ping(ctx).Err()
}
func (r *Redis) GetCacheString(key string) (bool, string, time.Duration, error) {
exists, err := r.Exists(key)
if err != nil {
return false, "", 0, err
}
if !exists {
return false, "", 0, nil
}
value, err := r.GetString(key)
if err != nil {
return true, "", 0, err
}
ttl, err := r.TTL(key)
return true, value, ttl, err
}
func (r *Redis) GetCacheBytes(key string) (bool, []byte, time.Duration, error) {
exists, err := r.Exists(key)
if err != nil {
return false, nil, 0, err
}
if !exists {
return false, nil, 0, nil
}
value, err := r.GetBytes(key)
if err != nil {
return true, nil, 0, err
}
ttl, err := r.TTL(key)
return true, value, ttl, err
}
func (r *Redis) Exists(key string) (bool, error) {
if !config.Cache.Enable {
return false, nil

View File

@@ -66,12 +66,16 @@ func IconHandler(ctx *fiber.Ctx) error {
return ctx.Status(http.StatusBadRequest).SendString("Invalid address value")
}
icon, err := GetServerIcon(host, port)
icon, expiresAt, err := GetServerIcon(host, port)
if err != nil {
return err
}
if expiresAt != nil {
ctx.Set("X-Cache-Time-Remaining", strconv.Itoa(int(expiresAt.Seconds())))
}
return ctx.Type("png").Send(icon)
}

View File

@@ -80,22 +80,10 @@ type Mod struct {
func GetJavaStatus(host string, port uint16) (string, *time.Duration, error) {
cacheKey := fmt.Sprintf("java:%s-%d", host, port)
cacheExists, err := r.Exists(cacheKey)
exists, value, ttl, err := r.GetCacheString(cacheKey)
if err != nil {
return "", nil, err
}
if cacheExists {
response, err := r.GetString(cacheKey)
if err != nil {
return "", nil, err
}
expiresAt, err := r.TTL(cacheKey)
return response, &expiresAt, err
if exists {
return value, &ttl, err
}
response, err := json.Marshal(FetchJavaStatus(host, port))
@@ -114,22 +102,10 @@ func GetJavaStatus(host string, port uint16) (string, *time.Duration, error) {
func GetBedrockStatus(host string, port uint16) (string, *time.Duration, error) {
cacheKey := fmt.Sprintf("bedrock:%s-%d", host, port)
cacheExists, err := r.Exists(cacheKey)
exists, value, ttl, err := r.GetCacheString(cacheKey)
if err != nil {
return "", nil, err
}
if cacheExists {
response, err := r.GetString(cacheKey)
if err != nil {
return "", nil, err
}
expiresAt, err := r.TTL(cacheKey)
return response, &expiresAt, err
if exists {
return value, &ttl, err
}
response, err := json.Marshal(FetchBedrockStatus(host, port))
@@ -145,17 +121,13 @@ func GetBedrockStatus(host string, port uint16) (string, *time.Duration, error)
return string(response), nil, nil
}
func GetServerIcon(host string, port uint16) ([]byte, error) {
func GetServerIcon(host string, port uint16) ([]byte, *time.Duration, error) {
cacheKey := fmt.Sprintf("icon:%s-%d", host, port)
cacheExists, err := r.Exists(cacheKey)
exists, value, ttl, err := r.GetCacheBytes(cacheKey)
if err != nil {
return nil, err
}
if cacheExists {
return r.GetBytes(cacheKey)
if exists {
return value, &ttl, err
}
icon := defaultIconBytes
@@ -166,17 +138,17 @@ func GetServerIcon(host string, port uint16) ([]byte, error) {
data, err := base64.StdEncoding.DecodeString(strings.TrimPrefix(*status.Favicon, "data:image/png;base64,"))
if err != nil {
return nil, err
return nil, nil, err
}
icon = data
}
if err := r.Set(cacheKey, icon, config.Cache.IconCacheDuration); err != nil {
return nil, err
return nil, nil, err
}
return icon, nil
return icon, nil, nil
}
func FetchJavaStatus(host string, port uint16) interface{} {