Update dependencies and add unique UUID route

This commit is contained in:
Jacob Gunther
2023-05-02 19:21:07 -05:00
parent d4f88af1f8
commit 953eb04476
7 changed files with 64 additions and 1 deletions

View File

@@ -1,6 +1,7 @@
environment: development
host: 127.0.0.1
port: 3000
auth_key: ""
redis:
host: 127.0.0.1
port: 6379

2
go.mod
View File

@@ -5,7 +5,7 @@ go 1.18
require (
github.com/go-redis/redis/v8 v8.11.5
github.com/gofiber/fiber/v2 v2.44.0
github.com/mineatar-io/skin-render v1.0.6
github.com/mineatar-io/skin-render v1.0.8
gopkg.in/yaml.v3 v3.0.1
)

4
go.sum
View File

@@ -30,6 +30,10 @@ github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWV
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mineatar-io/skin-render v1.0.6 h1:tvuS/L1w4Awx4qxZDbrnvmHG8C4UGUFPaw9IDD/r1jg=
github.com/mineatar-io/skin-render v1.0.6/go.mod h1:KkgHwrhTIqD73dkmeQwh5k2aHuWS8cDahVmd64gM128=
github.com/mineatar-io/skin-render v1.0.7 h1:mJ7KIcekZFFKFffjF6swYTchTl2tjD86/4OJdprdTso=
github.com/mineatar-io/skin-render v1.0.7/go.mod h1:KkgHwrhTIqD73dkmeQwh5k2aHuWS8cDahVmd64gM128=
github.com/mineatar-io/skin-render v1.0.8 h1:5HJGqYJ2t2UxQtfN00v1+VWelG32d0TQZip7Htz4cR4=
github.com/mineatar-io/skin-render v1.0.8/go.mod h1:KkgHwrhTIqD73dkmeQwh5k2aHuWS8cDahVmd64gM128=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=

View File

@@ -13,6 +13,7 @@ var (
Environment: "development",
Host: "127.0.0.1",
Port: 3001,
AuthKey: "",
Redis: RedisConfig{
Host: "127.0.0.1",
Port: 6379,
@@ -95,6 +96,7 @@ type Config struct {
Environment string `yaml:"environment"`
Host string `yaml:"host"`
Port uint16 `yaml:"port"`
AuthKey string `yaml:"auth_key"`
Redis RedisConfig `yaml:"redis"`
Routes RoutesConfig `yaml:"routes"`
Cache CacheConfig `yaml:"cache"`

View File

@@ -34,6 +34,23 @@ func (r *Redis) Connect(conf RedisConfig) error {
return c.Ping(ctx).Err()
}
// Scan returns all keys by the pattern in the Redis database.
func (r *Redis) Scan(cursor uint64, pattern string) ([]string, uint64, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
res := r.conn.Scan(ctx, cursor, pattern, 25)
if err := res.Err(); err != nil {
return nil, 0, err
}
keys, newCursor := res.Val()
return keys, newCursor, nil
}
// GetString gets the value from Redis by the key and returns the value as a string.
func (r *Redis) GetString(key string) (string, bool, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)

View File

@@ -4,6 +4,7 @@ import (
"fmt"
"net/http"
"strconv"
"strings"
"github.com/gofiber/fiber/v2"
"github.com/mineatar-io/skin-render"
@@ -11,6 +12,7 @@ import (
func init() {
app.Get("/ping", PingHandler)
app.Get("/list", ListHandler)
app.Get("/skin/:uuid", SkinHandler)
app.Get("/face/:uuid", FaceHandler)
app.Get("/head/:uuid", HeadHandler)
@@ -27,6 +29,39 @@ func PingHandler(ctx *fiber.Ctx) error {
return ctx.SendStatus(http.StatusOK)
}
// ListHandler is the API handler used for the `/list` route.
func ListHandler(ctx *fiber.Ctx) error {
authKey := ctx.Get("Authorization")
if len(authKey) < 1 {
return ctx.SendStatus(http.StatusUnauthorized)
}
if authKey != conf.AuthKey {
return ctx.SendStatus(http.StatusForbidden)
}
result := make([]string, 0)
for {
keys, cursor, err := r.Scan(0, "unique:*")
if err != nil {
return err
}
for _, uuid := range keys {
result = append(result, strings.TrimPrefix(uuid, "unique:"))
}
if cursor == 0 || len(keys) < 1 {
break
}
}
return ctx.JSON(result)
}
// FullBodyHandler is the API handler used for the `/body/full/:uuid` route.
func FullBodyHandler(ctx *fiber.Ctx) error {
opts := ParseQueryParams(ctx, conf.Routes.FullBody)

View File

@@ -96,6 +96,10 @@ func GetPlayerSkin(uuid string) (*image.NRGBA, bool, error) {
return skin.GetDefaultSkin(slim), slim, nil
}
if err = r.Set(fmt.Sprintf("unique:%s", textures.UUID), "0", 0); err != nil {
return nil, false, err
}
value := ""
for _, property := range textures.Properties {