More cleanup, fix default icon dimensions
This commit is contained in:
BIN
src/icon.png
BIN
src/icon.png
Binary file not shown.
|
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 3.1 KiB |
44
src/redis.go
44
src/redis.go
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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{} {
|
||||
|
||||
Reference in New Issue
Block a user