Update dependencies and add unique UUID route
This commit is contained in:
@@ -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"`
|
||||
|
||||
17
src/redis.go
17
src/redis.go
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user