From 4d926e59760675afc061375420cd7d9866c063ea Mon Sep 17 00:00:00 2001 From: Jacob Gunther Date: Fri, 24 Feb 2023 09:48:40 -0600 Subject: [PATCH] Major code cleanup --- src/redis.go | 14 +- src/routes.go | 419 +++++++++++++++++++------------------------------- src/util.go | 4 + 3 files changed, 169 insertions(+), 268 deletions(-) diff --git a/src/redis.go b/src/redis.go index b47cebb..1d54b7b 100644 --- a/src/redis.go +++ b/src/redis.go @@ -52,7 +52,7 @@ func (r *Redis) GetString(key string) (string, bool, error) { return result.Val(), true, result.Err() } -func (r *Redis) GetBytes(key string) ([]byte, bool, error) { +func (r *Redis) GetBytes(key string) ([]byte, error) { ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) defer cancel() @@ -60,32 +60,32 @@ func (r *Redis) GetBytes(key string) ([]byte, bool, error) { existsResult := r.conn.Exists(ctx, key) if err := existsResult.Err(); err != nil { - return nil, false, err + return nil, err } if existsResult.Val() == 0 { - return nil, false, nil + return nil, nil } result := r.conn.Get(ctx, key) if err := result.Err(); err != nil { - return nil, true, err + return nil, err } data, err := result.Bytes() - return data, true, err + return data, err } func (r *Redis) GetNRGBA(key string) (*image.NRGBA, bool, error) { - value, ok, err := r.GetBytes(key) + value, err := r.GetBytes(key) if err != nil { return nil, false, err } - if !ok { + if value == nil { return nil, false, nil } diff --git a/src/routes.go b/src/routes.go index 5f9c3bd..b6dce3c 100644 --- a/src/routes.go +++ b/src/routes.go @@ -3,6 +3,7 @@ package main import ( "fmt" "net/http" + "strconv" "github.com/gofiber/fiber/v2" "github.com/mineatar-io/skin-render" @@ -23,15 +24,13 @@ func init() { } func PingHandler(ctx *fiber.Ctx) error { - return ctx.SendString("Pong!") + return ctx.SendStatus(http.StatusOK) } func FullBodyHandler(ctx *fiber.Ctx) error { - user := ctx.Params("user") - opts := ParseQueryParams(ctx, config.Routes.FullBody) - uuid, ok, err := LookupUUID(user) + uuid, ok, err := LookupUUID(ParseUserParam(ctx)) if err != nil { return err @@ -43,61 +42,45 @@ func FullBodyHandler(ctx *fiber.Ctx) error { cacheKey := fmt.Sprintf("result:fullbody-%d-%t-%s", opts.Scale, opts.Overlay, uuid) - { - cache, ok, err := r.GetBytes(cacheKey) + data, err := r.GetBytes(cacheKey) + + if err != nil { + return err + } + + ctx.Set("X-Cache-Hit", strconv.FormatBool(data != nil)) + + if data == nil { + rawSkin, slim, err := GetPlayerSkin(uuid) if err != nil { return err } - if ok { - if opts.Download { - ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user)) - } + if data, err = EncodePNG(skin.RenderBody(rawSkin, skin.Options{ + Overlay: opts.Overlay, + Slim: slim, + Scale: opts.Scale, + })); err != nil { + return err + } - ctx.Set("X-Cache-Hit", "TRUE") - - return ctx.Type("png").Send(cache) + if err = r.Set(cacheKey, data, config.Cache.RenderCacheDuration); err != nil { + return err } } - rawSkin, slim, err := GetPlayerSkin(uuid) - - if err != nil { - return err - } - - render := skin.RenderBody(rawSkin, skin.Options{ - Overlay: opts.Overlay, - Slim: slim, - Scale: opts.Scale, - }) - - data, err := EncodePNG(render) - - if err != nil { - return err - } - - if err = r.Set(cacheKey, data, config.Cache.RenderCacheDuration); err != nil { - return err - } - if opts.Download { - ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user)) + ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, uuid)) } - ctx.Set("X-Cache-Hit", "FALSE") - return ctx.Type("png").Send(data) } func FrontBodyHandler(ctx *fiber.Ctx) error { - user := ctx.Params("user") - opts := ParseQueryParams(ctx, config.Routes.FrontBody) - uuid, ok, err := LookupUUID(user) + uuid, ok, err := LookupUUID(ParseUserParam(ctx)) if err != nil { return err @@ -109,61 +92,47 @@ func FrontBodyHandler(ctx *fiber.Ctx) error { cacheKey := fmt.Sprintf("result:frontbody-%d-%t-%s", opts.Scale, opts.Overlay, uuid) - { - cache, ok, err := r.GetBytes(cacheKey) + data, err := r.GetBytes(cacheKey) + + if err != nil { + return err + } + + ctx.Set("X-Cache-Hit", strconv.FormatBool(data != nil)) + + if data == nil { + rawSkin, slim, err := GetPlayerSkin(uuid) if err != nil { return err } - if ok { - if opts.Download { - ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user)) - } + data, err = EncodePNG(skin.RenderFrontBody(rawSkin, skin.Options{ + Overlay: opts.Overlay, + Slim: slim, + Scale: opts.Scale, + })) - ctx.Set("X-Cache-Hit", "TRUE") + if err != nil { + return err + } - return ctx.Type("png").Send(cache) + if err = r.Set(cacheKey, data, config.Cache.RenderCacheDuration); err != nil { + return err } } - rawSkin, slim, err := GetPlayerSkin(uuid) - - if err != nil { - return err - } - - render := skin.RenderFrontBody(rawSkin, skin.Options{ - Overlay: opts.Overlay, - Slim: slim, - Scale: opts.Scale, - }) - - data, err := EncodePNG(render) - - if err != nil { - return err - } - - if err = r.Set(cacheKey, data, config.Cache.RenderCacheDuration); err != nil { - return err - } - if opts.Download { - ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user)) + ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, uuid)) } - ctx.Set("X-Cache-Hit", "FALSE") - return ctx.Type("png").Send(data) } func BackBodyHandler(ctx *fiber.Ctx) error { - user := ctx.Params("user") - opts := ParseQueryParams(ctx, config.Routes.BackBody) - uuid, ok, err := LookupUUID(user) + uuid, ok, err := LookupUUID(ParseUserParam(ctx)) if err != nil { return err @@ -175,61 +144,47 @@ func BackBodyHandler(ctx *fiber.Ctx) error { cacheKey := fmt.Sprintf("result:backbody-%d-%t-%s", opts.Scale, opts.Overlay, uuid) - { - cache, ok, err := r.GetBytes(cacheKey) + data, err := r.GetBytes(cacheKey) + + if err != nil { + return err + } + + ctx.Set("X-Cache-Hit", strconv.FormatBool(data != nil)) + + if data == nil { + rawSkin, slim, err := GetPlayerSkin(uuid) if err != nil { return err } - if ok { - if opts.Download { - ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user)) - } + data, err = EncodePNG(skin.RenderBackBody(rawSkin, skin.Options{ + Overlay: opts.Overlay, + Slim: slim, + Scale: opts.Scale, + })) - ctx.Set("X-Cache-Hit", "TRUE") + if err != nil { + return err + } - return ctx.Type("png").Send(cache) + if err = r.Set(cacheKey, data, config.Cache.RenderCacheDuration); err != nil { + return err } } - rawSkin, slim, err := GetPlayerSkin(uuid) - - if err != nil { - return err - } - - render := skin.RenderBackBody(rawSkin, skin.Options{ - Overlay: opts.Overlay, - Slim: slim, - Scale: opts.Scale, - }) - - data, err := EncodePNG(render) - - if err != nil { - return err - } - - if err = r.Set(cacheKey, data, config.Cache.RenderCacheDuration); err != nil { - return err - } - if opts.Download { - ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user)) + ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, uuid)) } - ctx.Set("X-Cache-Hit", "FALSE") - return ctx.Type("png").Send(data) } func LeftBodyHandler(ctx *fiber.Ctx) error { - user := ctx.Params("user") - opts := ParseQueryParams(ctx, config.Routes.LeftBody) - uuid, ok, err := LookupUUID(user) + uuid, ok, err := LookupUUID(ParseUserParam(ctx)) if err != nil { return err @@ -241,61 +196,47 @@ func LeftBodyHandler(ctx *fiber.Ctx) error { cacheKey := fmt.Sprintf("result:leftbody-%d-%t-%s", opts.Scale, opts.Overlay, uuid) - { - cache, ok, err := r.GetBytes(cacheKey) + data, err := r.GetBytes(cacheKey) + + if err != nil { + return err + } + + ctx.Set("X-Cache-Hit", strconv.FormatBool(data != nil)) + + if data == nil { + rawSkin, slim, err := GetPlayerSkin(uuid) if err != nil { return err } - if ok { - if opts.Download { - ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user)) - } + data, err = EncodePNG(skin.RenderLeftBody(rawSkin, skin.Options{ + Overlay: opts.Overlay, + Slim: slim, + Scale: opts.Scale, + })) - ctx.Set("X-Cache-Hit", "TRUE") + if err != nil { + return err + } - return ctx.Type("png").Send(cache) + if err = r.Set(cacheKey, data, config.Cache.RenderCacheDuration); err != nil { + return err } } - rawSkin, slim, err := GetPlayerSkin(uuid) - - if err != nil { - return err - } - - render := skin.RenderLeftBody(rawSkin, skin.Options{ - Overlay: opts.Overlay, - Slim: slim, - Scale: opts.Scale, - }) - - data, err := EncodePNG(render) - - if err != nil { - return err - } - - if err = r.Set(cacheKey, data, config.Cache.RenderCacheDuration); err != nil { - return err - } - if opts.Download { - ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user)) + ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, uuid)) } - ctx.Set("X-Cache-Hit", "FALSE") - return ctx.Type("png").Send(data) } func RightBodyHandler(ctx *fiber.Ctx) error { - user := ctx.Params("user") - opts := ParseQueryParams(ctx, config.Routes.RightBody) - uuid, ok, err := LookupUUID(user) + uuid, ok, err := LookupUUID(ParseUserParam(ctx)) if err != nil { return err @@ -307,61 +248,47 @@ func RightBodyHandler(ctx *fiber.Ctx) error { cacheKey := fmt.Sprintf("result:rightbody-%d-%t-%s", opts.Scale, opts.Overlay, uuid) - { - cache, ok, err := r.GetBytes(cacheKey) + data, err := r.GetBytes(cacheKey) + + if err != nil { + return err + } + + ctx.Set("X-Cache-Hit", strconv.FormatBool(data != nil)) + + if data == nil { + rawSkin, slim, err := GetPlayerSkin(uuid) if err != nil { return err } - if ok { - if opts.Download { - ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user)) - } + data, err = EncodePNG(skin.RenderRightBody(rawSkin, skin.Options{ + Overlay: opts.Overlay, + Slim: slim, + Scale: opts.Scale, + })) - ctx.Set("X-Cache-Hit", "TRUE") + if err != nil { + return err + } - return ctx.Type("png").Send(cache) + if err = r.Set(cacheKey, data, config.Cache.RenderCacheDuration); err != nil { + return err } } - rawSkin, slim, err := GetPlayerSkin(uuid) - - if err != nil { - return err - } - - render := skin.RenderRightBody(rawSkin, skin.Options{ - Overlay: opts.Overlay, - Slim: slim, - Scale: opts.Scale, - }) - - data, err := EncodePNG(render) - - if err != nil { - return err - } - - if err = r.Set(cacheKey, data, config.Cache.RenderCacheDuration); err != nil { - return err - } - if opts.Download { - ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user)) + ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, uuid)) } - ctx.Set("X-Cache-Hit", "FALSE") - return ctx.Type("png").Send(data) } func FaceHandler(ctx *fiber.Ctx) error { - user := ctx.Params("user") - opts := ParseQueryParams(ctx, config.Routes.Face) - uuid, ok, err := LookupUUID(user) + uuid, ok, err := LookupUUID(ParseUserParam(ctx)) if err != nil { return err @@ -373,61 +300,47 @@ func FaceHandler(ctx *fiber.Ctx) error { cacheKey := fmt.Sprintf("result:face-%d-%t-%s", opts.Scale, opts.Overlay, uuid) - { - cache, ok, err := r.GetBytes(cacheKey) + data, err := r.GetBytes(cacheKey) + + if err != nil { + return err + } + + ctx.Set("X-Cache-Hit", strconv.FormatBool(data != nil)) + + if data == nil { + rawSkin, slim, err := GetPlayerSkin(uuid) if err != nil { return err } - if ok { - if opts.Download { - ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user)) - } + data, err = EncodePNG(skin.RenderFace(rawSkin, skin.Options{ + Overlay: opts.Overlay, + Slim: slim, + Scale: opts.Scale, + })) - ctx.Set("X-Cache-Hit", "TRUE") + if err != nil { + return err + } - return ctx.Type("png").Send(cache) + if err = r.Set(cacheKey, data, config.Cache.RenderCacheDuration); err != nil { + return err } } - rawSkin, slim, err := GetPlayerSkin(uuid) - - if err != nil { - return err - } - - render := skin.RenderFace(rawSkin, skin.Options{ - Overlay: opts.Overlay, - Slim: slim, - Scale: opts.Scale, - }) - - data, err := EncodePNG(render) - - if err != nil { - return err - } - - if err = r.Set(cacheKey, data, config.Cache.RenderCacheDuration); err != nil { - return err - } - if opts.Download { - ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user)) + ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, uuid)) } - ctx.Set("X-Cache-Hit", "FALSE") - return ctx.Type("png").Send(data) } func HeadHandler(ctx *fiber.Ctx) error { - user := ctx.Params("user") - opts := ParseQueryParams(ctx, config.Routes.Head) - uuid, ok, err := LookupUUID(user) + uuid, ok, err := LookupUUID(ParseUserParam(ctx)) if err != nil { return err @@ -439,61 +352,47 @@ func HeadHandler(ctx *fiber.Ctx) error { cacheKey := fmt.Sprintf("result:head-%d-%t-%s", opts.Scale, opts.Overlay, uuid) - { - cache, ok, err := r.GetBytes(cacheKey) + data, err := r.GetBytes(cacheKey) + + if err != nil { + return err + } + + ctx.Set("X-Cache-Hit", strconv.FormatBool(data != nil)) + + if data == nil { + rawSkin, slim, err := GetPlayerSkin(uuid) if err != nil { return err } - if ok { - if opts.Download { - ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user)) - } + data, err = EncodePNG(skin.RenderHead(rawSkin, skin.Options{ + Overlay: opts.Overlay, + Slim: slim, + Scale: opts.Scale, + })) - ctx.Set("X-Cache-Hit", "TRUE") + if err != nil { + return err + } - return ctx.Type("png").Send(cache) + if err = r.Set(cacheKey, data, config.Cache.RenderCacheDuration); err != nil { + return err } } - rawSkin, slim, err := GetPlayerSkin(uuid) - - if err != nil { - return err - } - - render := skin.RenderHead(rawSkin, skin.Options{ - Overlay: opts.Overlay, - Slim: slim, - Scale: opts.Scale, - }) - - data, err := EncodePNG(render) - - if err != nil { - return err - } - - if err = r.Set(cacheKey, data, config.Cache.RenderCacheDuration); err != nil { - return err - } - if opts.Download { - ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user)) + ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, uuid)) } - ctx.Set("X-Cache-Hit", "FALSE") - return ctx.Type("png").Send(data) } func SkinHandler(ctx *fiber.Ctx) error { - user := ctx.Params("user") - opts := ParseQueryParams(ctx, config.Routes.RawSkin) - uuid, ok, err := LookupUUID(user) + uuid, ok, err := LookupUUID(ParseUserParam(ctx)) if err != nil { return err @@ -516,16 +415,14 @@ func SkinHandler(ctx *fiber.Ctx) error { } if opts.Download { - ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user)) + ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, uuid)) } return ctx.Type("png").Send(data) } func UUIDHandler(ctx *fiber.Ctx) error { - user := ctx.Params("user") - - uuid, ok, err := LookupUUID(user) + uuid, ok, err := LookupUUID(ctx.Params("user")) if err != nil { return err diff --git a/src/util.go b/src/util.go index c1e36f7..f9e0ab7 100644 --- a/src/util.go +++ b/src/util.go @@ -252,3 +252,7 @@ func GetInstanceID() (uint16, error) { return 0, nil } + +func ParseUserParam(ctx *fiber.Ctx) string { + return strings.Split(ctx.Params("user"), ".")[0] +}