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
No known key found for this signature in database
GPG Key ID: FD93EF55818E3D85
6 changed files with 68 additions and 44 deletions

4
go.mod
View File

@ -5,7 +5,7 @@ go 1.18
require ( require (
github.com/go-redis/redis/v8 v8.11.5 github.com/go-redis/redis/v8 v8.11.5
github.com/gofiber/fiber/v2 v2.36.0 github.com/gofiber/fiber/v2 v2.36.0
github.com/mcstatus-io/mcutil v0.0.0-20220819164348-a0bb9bd8df56 github.com/mcstatus-io/mcutil v0.0.0-20220825201801-d3b6a8b30b56
gopkg.in/yaml.v3 v3.0.1 gopkg.in/yaml.v3 v3.0.1
) )
@ -17,5 +17,5 @@ require (
github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.39.0 // indirect github.com/valyala/fasthttp v1.39.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect github.com/valyala/tcplisten v1.0.0 // indirect
golang.org/x/sys v0.0.0-20220818161305-2296e01440c6 // indirect golang.org/x/sys v0.0.0-20220823224334-20c2bfdbfe24 // indirect
) )

4
go.sum
View File

@ -14,6 +14,8 @@ github.com/klauspost/compress v1.15.9 h1:wKRjX6JRtDdrE9qwa4b/Cip7ACOshUI4smpCQan
github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU= github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
github.com/mcstatus-io/mcutil v0.0.0-20220819164348-a0bb9bd8df56 h1:FZg8KfK/9b/Jp3BtZ3KgcS0iXkqPWn80cZrc6IUe1ps= github.com/mcstatus-io/mcutil v0.0.0-20220819164348-a0bb9bd8df56 h1:FZg8KfK/9b/Jp3BtZ3KgcS0iXkqPWn80cZrc6IUe1ps=
github.com/mcstatus-io/mcutil v0.0.0-20220819164348-a0bb9bd8df56/go.mod h1:VUB87/x9EYITmQVXZO4eS+egaZOdvxId4IdpU4L5LoA= github.com/mcstatus-io/mcutil v0.0.0-20220819164348-a0bb9bd8df56/go.mod h1:VUB87/x9EYITmQVXZO4eS+egaZOdvxId4IdpU4L5LoA=
github.com/mcstatus-io/mcutil v0.0.0-20220825201801-d3b6a8b30b56 h1:XtoLxtSRzXYWnLxYfkq+oJTUg0i43dltU4LQxXtr7rk=
github.com/mcstatus-io/mcutil v0.0.0-20220825201801-d3b6a8b30b56/go.mod h1:VUB87/x9EYITmQVXZO4eS+egaZOdvxId4IdpU4L5LoA=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE= github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
@ -35,6 +37,8 @@ golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220818161305-2296e01440c6 h1:Sx/u41w+OwrInGdEckYmEuU5gHoGSL4QbDz3S9s6j4U= golang.org/x/sys v0.0.0-20220818161305-2296e01440c6 h1:Sx/u41w+OwrInGdEckYmEuU5gHoGSL4QbDz3S9s6j4U=
golang.org/x/sys v0.0.0-20220818161305-2296e01440c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220818161305-2296e01440c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220823224334-20c2bfdbfe24 h1:TyKJRhyo17yWxOMCTHKWrc5rddHORMlnZ/j57umaUd8=
golang.org/x/sys v0.0.0-20220823224334-20c2bfdbfe24/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=

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() 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) { func (r *Redis) Exists(key string) (bool, error) {
if !config.Cache.Enable { if !config.Cache.Enable {
return false, nil return false, nil

View File

@ -66,12 +66,16 @@ func IconHandler(ctx *fiber.Ctx) error {
return ctx.Status(http.StatusBadRequest).SendString("Invalid address value") return ctx.Status(http.StatusBadRequest).SendString("Invalid address value")
} }
icon, err := GetServerIcon(host, port) icon, expiresAt, err := GetServerIcon(host, port)
if err != nil { if err != nil {
return err return err
} }
if expiresAt != nil {
ctx.Set("X-Cache-Time-Remaining", strconv.Itoa(int(expiresAt.Seconds())))
}
return ctx.Type("png").Send(icon) 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) { func GetJavaStatus(host string, port uint16) (string, *time.Duration, error) {
cacheKey := fmt.Sprintf("java:%s-%d", host, port) cacheKey := fmt.Sprintf("java:%s-%d", host, port)
cacheExists, err := r.Exists(cacheKey) exists, value, ttl, err := r.GetCacheString(cacheKey)
if err != nil { if exists {
return "", nil, err return value, &ttl, err
}
if cacheExists {
response, err := r.GetString(cacheKey)
if err != nil {
return "", nil, err
}
expiresAt, err := r.TTL(cacheKey)
return response, &expiresAt, err
} }
response, err := json.Marshal(FetchJavaStatus(host, port)) 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) { func GetBedrockStatus(host string, port uint16) (string, *time.Duration, error) {
cacheKey := fmt.Sprintf("bedrock:%s-%d", host, port) cacheKey := fmt.Sprintf("bedrock:%s-%d", host, port)
cacheExists, err := r.Exists(cacheKey) exists, value, ttl, err := r.GetCacheString(cacheKey)
if err != nil { if exists {
return "", nil, err return value, &ttl, err
}
if cacheExists {
response, err := r.GetString(cacheKey)
if err != nil {
return "", nil, err
}
expiresAt, err := r.TTL(cacheKey)
return response, &expiresAt, err
} }
response, err := json.Marshal(FetchBedrockStatus(host, port)) 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 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) cacheKey := fmt.Sprintf("icon:%s-%d", host, port)
cacheExists, err := r.Exists(cacheKey) exists, value, ttl, err := r.GetCacheBytes(cacheKey)
if err != nil { if exists {
return nil, err return value, &ttl, err
}
if cacheExists {
return r.GetBytes(cacheKey)
} }
icon := defaultIconBytes 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,")) data, err := base64.StdEncoding.DecodeString(strings.TrimPrefix(*status.Favicon, "data:image/png;base64,"))
if err != nil { if err != nil {
return nil, err return nil, nil, err
} }
icon = data icon = data
} }
if err := r.Set(cacheKey, icon, config.Cache.IconCacheDuration); err != nil { 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{} { func FetchJavaStatus(host string, port uint16) interface{} {