Major code cleanup

This commit is contained in:
Jacob Gunther
2023-02-24 09:48:40 -06:00
parent 94f42a4af5
commit 4d926e5976
3 changed files with 169 additions and 268 deletions

View File

@@ -52,7 +52,7 @@ func (r *Redis) GetString(key string) (string, bool, error) {
return result.Val(), true, result.Err() 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) ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel() defer cancel()
@@ -60,32 +60,32 @@ func (r *Redis) GetBytes(key string) ([]byte, bool, error) {
existsResult := r.conn.Exists(ctx, key) existsResult := r.conn.Exists(ctx, key)
if err := existsResult.Err(); err != nil { if err := existsResult.Err(); err != nil {
return nil, false, err return nil, err
} }
if existsResult.Val() == 0 { if existsResult.Val() == 0 {
return nil, false, nil return nil, nil
} }
result := r.conn.Get(ctx, key) result := r.conn.Get(ctx, key)
if err := result.Err(); err != nil { if err := result.Err(); err != nil {
return nil, true, err return nil, err
} }
data, err := result.Bytes() data, err := result.Bytes()
return data, true, err return data, err
} }
func (r *Redis) GetNRGBA(key string) (*image.NRGBA, bool, error) { func (r *Redis) GetNRGBA(key string) (*image.NRGBA, bool, error) {
value, ok, err := r.GetBytes(key) value, err := r.GetBytes(key)
if err != nil { if err != nil {
return nil, false, err return nil, false, err
} }
if !ok { if value == nil {
return nil, false, nil return nil, false, nil
} }

View File

@@ -3,6 +3,7 @@ package main
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"strconv"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/mineatar-io/skin-render" "github.com/mineatar-io/skin-render"
@@ -23,15 +24,13 @@ func init() {
} }
func PingHandler(ctx *fiber.Ctx) error { func PingHandler(ctx *fiber.Ctx) error {
return ctx.SendString("Pong!") return ctx.SendStatus(http.StatusOK)
} }
func FullBodyHandler(ctx *fiber.Ctx) error { func FullBodyHandler(ctx *fiber.Ctx) error {
user := ctx.Params("user")
opts := ParseQueryParams(ctx, config.Routes.FullBody) opts := ParseQueryParams(ctx, config.Routes.FullBody)
uuid, ok, err := LookupUUID(user) uuid, ok, err := LookupUUID(ParseUserParam(ctx))
if err != nil { if err != nil {
return err 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) cacheKey := fmt.Sprintf("result:fullbody-%d-%t-%s", opts.Scale, opts.Overlay, uuid)
{ data, err := r.GetBytes(cacheKey)
cache, ok, err := r.GetBytes(cacheKey)
if err != nil { if err != nil {
return err return err
} }
if ok { ctx.Set("X-Cache-Hit", strconv.FormatBool(data != nil))
if opts.Download {
ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user))
}
ctx.Set("X-Cache-Hit", "TRUE")
return ctx.Type("png").Send(cache)
}
}
if data == nil {
rawSkin, slim, err := GetPlayerSkin(uuid) rawSkin, slim, err := GetPlayerSkin(uuid)
if err != nil { if err != nil {
return err return err
} }
render := skin.RenderBody(rawSkin, skin.Options{ if data, err = EncodePNG(skin.RenderBody(rawSkin, skin.Options{
Overlay: opts.Overlay, Overlay: opts.Overlay,
Slim: slim, Slim: slim,
Scale: opts.Scale, Scale: opts.Scale,
}) })); err != nil {
data, err := EncodePNG(render)
if err != nil {
return err return err
} }
if err = r.Set(cacheKey, data, config.Cache.RenderCacheDuration); err != nil { if err = r.Set(cacheKey, data, config.Cache.RenderCacheDuration); err != nil {
return err return err
} }
if opts.Download {
ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user))
} }
ctx.Set("X-Cache-Hit", "FALSE") if opts.Download {
ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, uuid))
}
return ctx.Type("png").Send(data) return ctx.Type("png").Send(data)
} }
func FrontBodyHandler(ctx *fiber.Ctx) error { func FrontBodyHandler(ctx *fiber.Ctx) error {
user := ctx.Params("user")
opts := ParseQueryParams(ctx, config.Routes.FrontBody) opts := ParseQueryParams(ctx, config.Routes.FrontBody)
uuid, ok, err := LookupUUID(user) uuid, ok, err := LookupUUID(ParseUserParam(ctx))
if err != nil { if err != nil {
return err return err
@@ -109,37 +92,26 @@ func FrontBodyHandler(ctx *fiber.Ctx) error {
cacheKey := fmt.Sprintf("result:frontbody-%d-%t-%s", opts.Scale, opts.Overlay, uuid) cacheKey := fmt.Sprintf("result:frontbody-%d-%t-%s", opts.Scale, opts.Overlay, uuid)
{ data, err := r.GetBytes(cacheKey)
cache, ok, err := r.GetBytes(cacheKey)
if err != nil { if err != nil {
return err return err
} }
if ok { ctx.Set("X-Cache-Hit", strconv.FormatBool(data != nil))
if opts.Download {
ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user))
}
ctx.Set("X-Cache-Hit", "TRUE")
return ctx.Type("png").Send(cache)
}
}
if data == nil {
rawSkin, slim, err := GetPlayerSkin(uuid) rawSkin, slim, err := GetPlayerSkin(uuid)
if err != nil { if err != nil {
return err return err
} }
render := skin.RenderFrontBody(rawSkin, skin.Options{ data, err = EncodePNG(skin.RenderFrontBody(rawSkin, skin.Options{
Overlay: opts.Overlay, Overlay: opts.Overlay,
Slim: slim, Slim: slim,
Scale: opts.Scale, Scale: opts.Scale,
}) }))
data, err := EncodePNG(render)
if err != nil { if err != nil {
return err return err
@@ -148,22 +120,19 @@ func FrontBodyHandler(ctx *fiber.Ctx) error {
if err = r.Set(cacheKey, data, config.Cache.RenderCacheDuration); err != nil { if err = r.Set(cacheKey, data, config.Cache.RenderCacheDuration); err != nil {
return err return err
} }
if opts.Download {
ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user))
} }
ctx.Set("X-Cache-Hit", "FALSE") if opts.Download {
ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, uuid))
}
return ctx.Type("png").Send(data) return ctx.Type("png").Send(data)
} }
func BackBodyHandler(ctx *fiber.Ctx) error { func BackBodyHandler(ctx *fiber.Ctx) error {
user := ctx.Params("user")
opts := ParseQueryParams(ctx, config.Routes.BackBody) opts := ParseQueryParams(ctx, config.Routes.BackBody)
uuid, ok, err := LookupUUID(user) uuid, ok, err := LookupUUID(ParseUserParam(ctx))
if err != nil { if err != nil {
return err return err
@@ -175,37 +144,26 @@ func BackBodyHandler(ctx *fiber.Ctx) error {
cacheKey := fmt.Sprintf("result:backbody-%d-%t-%s", opts.Scale, opts.Overlay, uuid) cacheKey := fmt.Sprintf("result:backbody-%d-%t-%s", opts.Scale, opts.Overlay, uuid)
{ data, err := r.GetBytes(cacheKey)
cache, ok, err := r.GetBytes(cacheKey)
if err != nil { if err != nil {
return err return err
} }
if ok { ctx.Set("X-Cache-Hit", strconv.FormatBool(data != nil))
if opts.Download {
ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user))
}
ctx.Set("X-Cache-Hit", "TRUE")
return ctx.Type("png").Send(cache)
}
}
if data == nil {
rawSkin, slim, err := GetPlayerSkin(uuid) rawSkin, slim, err := GetPlayerSkin(uuid)
if err != nil { if err != nil {
return err return err
} }
render := skin.RenderBackBody(rawSkin, skin.Options{ data, err = EncodePNG(skin.RenderBackBody(rawSkin, skin.Options{
Overlay: opts.Overlay, Overlay: opts.Overlay,
Slim: slim, Slim: slim,
Scale: opts.Scale, Scale: opts.Scale,
}) }))
data, err := EncodePNG(render)
if err != nil { if err != nil {
return err return err
@@ -214,22 +172,19 @@ func BackBodyHandler(ctx *fiber.Ctx) error {
if err = r.Set(cacheKey, data, config.Cache.RenderCacheDuration); err != nil { if err = r.Set(cacheKey, data, config.Cache.RenderCacheDuration); err != nil {
return err return err
} }
if opts.Download {
ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user))
} }
ctx.Set("X-Cache-Hit", "FALSE") if opts.Download {
ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, uuid))
}
return ctx.Type("png").Send(data) return ctx.Type("png").Send(data)
} }
func LeftBodyHandler(ctx *fiber.Ctx) error { func LeftBodyHandler(ctx *fiber.Ctx) error {
user := ctx.Params("user")
opts := ParseQueryParams(ctx, config.Routes.LeftBody) opts := ParseQueryParams(ctx, config.Routes.LeftBody)
uuid, ok, err := LookupUUID(user) uuid, ok, err := LookupUUID(ParseUserParam(ctx))
if err != nil { if err != nil {
return err return err
@@ -241,37 +196,26 @@ func LeftBodyHandler(ctx *fiber.Ctx) error {
cacheKey := fmt.Sprintf("result:leftbody-%d-%t-%s", opts.Scale, opts.Overlay, uuid) cacheKey := fmt.Sprintf("result:leftbody-%d-%t-%s", opts.Scale, opts.Overlay, uuid)
{ data, err := r.GetBytes(cacheKey)
cache, ok, err := r.GetBytes(cacheKey)
if err != nil { if err != nil {
return err return err
} }
if ok { ctx.Set("X-Cache-Hit", strconv.FormatBool(data != nil))
if opts.Download {
ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user))
}
ctx.Set("X-Cache-Hit", "TRUE")
return ctx.Type("png").Send(cache)
}
}
if data == nil {
rawSkin, slim, err := GetPlayerSkin(uuid) rawSkin, slim, err := GetPlayerSkin(uuid)
if err != nil { if err != nil {
return err return err
} }
render := skin.RenderLeftBody(rawSkin, skin.Options{ data, err = EncodePNG(skin.RenderLeftBody(rawSkin, skin.Options{
Overlay: opts.Overlay, Overlay: opts.Overlay,
Slim: slim, Slim: slim,
Scale: opts.Scale, Scale: opts.Scale,
}) }))
data, err := EncodePNG(render)
if err != nil { if err != nil {
return err return err
@@ -280,22 +224,19 @@ func LeftBodyHandler(ctx *fiber.Ctx) error {
if err = r.Set(cacheKey, data, config.Cache.RenderCacheDuration); err != nil { if err = r.Set(cacheKey, data, config.Cache.RenderCacheDuration); err != nil {
return err return err
} }
if opts.Download {
ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user))
} }
ctx.Set("X-Cache-Hit", "FALSE") if opts.Download {
ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, uuid))
}
return ctx.Type("png").Send(data) return ctx.Type("png").Send(data)
} }
func RightBodyHandler(ctx *fiber.Ctx) error { func RightBodyHandler(ctx *fiber.Ctx) error {
user := ctx.Params("user")
opts := ParseQueryParams(ctx, config.Routes.RightBody) opts := ParseQueryParams(ctx, config.Routes.RightBody)
uuid, ok, err := LookupUUID(user) uuid, ok, err := LookupUUID(ParseUserParam(ctx))
if err != nil { if err != nil {
return err return err
@@ -307,37 +248,26 @@ func RightBodyHandler(ctx *fiber.Ctx) error {
cacheKey := fmt.Sprintf("result:rightbody-%d-%t-%s", opts.Scale, opts.Overlay, uuid) cacheKey := fmt.Sprintf("result:rightbody-%d-%t-%s", opts.Scale, opts.Overlay, uuid)
{ data, err := r.GetBytes(cacheKey)
cache, ok, err := r.GetBytes(cacheKey)
if err != nil { if err != nil {
return err return err
} }
if ok { ctx.Set("X-Cache-Hit", strconv.FormatBool(data != nil))
if opts.Download {
ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user))
}
ctx.Set("X-Cache-Hit", "TRUE")
return ctx.Type("png").Send(cache)
}
}
if data == nil {
rawSkin, slim, err := GetPlayerSkin(uuid) rawSkin, slim, err := GetPlayerSkin(uuid)
if err != nil { if err != nil {
return err return err
} }
render := skin.RenderRightBody(rawSkin, skin.Options{ data, err = EncodePNG(skin.RenderRightBody(rawSkin, skin.Options{
Overlay: opts.Overlay, Overlay: opts.Overlay,
Slim: slim, Slim: slim,
Scale: opts.Scale, Scale: opts.Scale,
}) }))
data, err := EncodePNG(render)
if err != nil { if err != nil {
return err return err
@@ -346,22 +276,19 @@ func RightBodyHandler(ctx *fiber.Ctx) error {
if err = r.Set(cacheKey, data, config.Cache.RenderCacheDuration); err != nil { if err = r.Set(cacheKey, data, config.Cache.RenderCacheDuration); err != nil {
return err return err
} }
if opts.Download {
ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user))
} }
ctx.Set("X-Cache-Hit", "FALSE") if opts.Download {
ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, uuid))
}
return ctx.Type("png").Send(data) return ctx.Type("png").Send(data)
} }
func FaceHandler(ctx *fiber.Ctx) error { func FaceHandler(ctx *fiber.Ctx) error {
user := ctx.Params("user")
opts := ParseQueryParams(ctx, config.Routes.Face) opts := ParseQueryParams(ctx, config.Routes.Face)
uuid, ok, err := LookupUUID(user) uuid, ok, err := LookupUUID(ParseUserParam(ctx))
if err != nil { if err != nil {
return err return err
@@ -373,37 +300,26 @@ func FaceHandler(ctx *fiber.Ctx) error {
cacheKey := fmt.Sprintf("result:face-%d-%t-%s", opts.Scale, opts.Overlay, uuid) cacheKey := fmt.Sprintf("result:face-%d-%t-%s", opts.Scale, opts.Overlay, uuid)
{ data, err := r.GetBytes(cacheKey)
cache, ok, err := r.GetBytes(cacheKey)
if err != nil { if err != nil {
return err return err
} }
if ok { ctx.Set("X-Cache-Hit", strconv.FormatBool(data != nil))
if opts.Download {
ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user))
}
ctx.Set("X-Cache-Hit", "TRUE")
return ctx.Type("png").Send(cache)
}
}
if data == nil {
rawSkin, slim, err := GetPlayerSkin(uuid) rawSkin, slim, err := GetPlayerSkin(uuid)
if err != nil { if err != nil {
return err return err
} }
render := skin.RenderFace(rawSkin, skin.Options{ data, err = EncodePNG(skin.RenderFace(rawSkin, skin.Options{
Overlay: opts.Overlay, Overlay: opts.Overlay,
Slim: slim, Slim: slim,
Scale: opts.Scale, Scale: opts.Scale,
}) }))
data, err := EncodePNG(render)
if err != nil { if err != nil {
return err return err
@@ -412,22 +328,19 @@ func FaceHandler(ctx *fiber.Ctx) error {
if err = r.Set(cacheKey, data, config.Cache.RenderCacheDuration); err != nil { if err = r.Set(cacheKey, data, config.Cache.RenderCacheDuration); err != nil {
return err return err
} }
if opts.Download {
ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user))
} }
ctx.Set("X-Cache-Hit", "FALSE") if opts.Download {
ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, uuid))
}
return ctx.Type("png").Send(data) return ctx.Type("png").Send(data)
} }
func HeadHandler(ctx *fiber.Ctx) error { func HeadHandler(ctx *fiber.Ctx) error {
user := ctx.Params("user")
opts := ParseQueryParams(ctx, config.Routes.Head) opts := ParseQueryParams(ctx, config.Routes.Head)
uuid, ok, err := LookupUUID(user) uuid, ok, err := LookupUUID(ParseUserParam(ctx))
if err != nil { if err != nil {
return err return err
@@ -439,37 +352,26 @@ func HeadHandler(ctx *fiber.Ctx) error {
cacheKey := fmt.Sprintf("result:head-%d-%t-%s", opts.Scale, opts.Overlay, uuid) cacheKey := fmt.Sprintf("result:head-%d-%t-%s", opts.Scale, opts.Overlay, uuid)
{ data, err := r.GetBytes(cacheKey)
cache, ok, err := r.GetBytes(cacheKey)
if err != nil { if err != nil {
return err return err
} }
if ok { ctx.Set("X-Cache-Hit", strconv.FormatBool(data != nil))
if opts.Download {
ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user))
}
ctx.Set("X-Cache-Hit", "TRUE")
return ctx.Type("png").Send(cache)
}
}
if data == nil {
rawSkin, slim, err := GetPlayerSkin(uuid) rawSkin, slim, err := GetPlayerSkin(uuid)
if err != nil { if err != nil {
return err return err
} }
render := skin.RenderHead(rawSkin, skin.Options{ data, err = EncodePNG(skin.RenderHead(rawSkin, skin.Options{
Overlay: opts.Overlay, Overlay: opts.Overlay,
Slim: slim, Slim: slim,
Scale: opts.Scale, Scale: opts.Scale,
}) }))
data, err := EncodePNG(render)
if err != nil { if err != nil {
return err return err
@@ -478,22 +380,19 @@ func HeadHandler(ctx *fiber.Ctx) error {
if err = r.Set(cacheKey, data, config.Cache.RenderCacheDuration); err != nil { if err = r.Set(cacheKey, data, config.Cache.RenderCacheDuration); err != nil {
return err return err
} }
if opts.Download {
ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, user))
} }
ctx.Set("X-Cache-Hit", "FALSE") if opts.Download {
ctx.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.png"`, uuid))
}
return ctx.Type("png").Send(data) return ctx.Type("png").Send(data)
} }
func SkinHandler(ctx *fiber.Ctx) error { func SkinHandler(ctx *fiber.Ctx) error {
user := ctx.Params("user")
opts := ParseQueryParams(ctx, config.Routes.RawSkin) opts := ParseQueryParams(ctx, config.Routes.RawSkin)
uuid, ok, err := LookupUUID(user) uuid, ok, err := LookupUUID(ParseUserParam(ctx))
if err != nil { if err != nil {
return err return err
@@ -516,16 +415,14 @@ func SkinHandler(ctx *fiber.Ctx) error {
} }
if opts.Download { 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) return ctx.Type("png").Send(data)
} }
func UUIDHandler(ctx *fiber.Ctx) error { func UUIDHandler(ctx *fiber.Ctx) error {
user := ctx.Params("user") uuid, ok, err := LookupUUID(ctx.Params("user"))
uuid, ok, err := LookupUUID(user)
if err != nil { if err != nil {
return err return err

View File

@@ -252,3 +252,7 @@ func GetInstanceID() (uint16, error) {
return 0, nil return 0, nil
} }
func ParseUserParam(ctx *fiber.Ctx) string {
return strings.Split(ctx.Params("user"), ".")[0]
}