Add symbol definitions, code cleanup
This commit is contained in:
132
src/config.go
132
src/config.go
@@ -7,6 +7,110 @@ import (
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
var (
|
||||
// DefaultConfig is the default configuration values used by the application.
|
||||
DefaultConfig *Config = &Config{
|
||||
Host: "127.0.0.1",
|
||||
Port: 3001,
|
||||
Redis: RedisConfig{
|
||||
Host: "127.0.0.1",
|
||||
Port: 6379,
|
||||
User: "",
|
||||
Password: "",
|
||||
Database: 0,
|
||||
},
|
||||
Routes: RoutesConfig{
|
||||
Face: RouteConfig{
|
||||
DefaultOverlay: true,
|
||||
DefaultDownload: false,
|
||||
DefaultFallback: true,
|
||||
DefaultScale: 4,
|
||||
MinScale: 1,
|
||||
MaxScale: 64,
|
||||
},
|
||||
Head: RouteConfig{
|
||||
DefaultOverlay: true,
|
||||
DefaultDownload: false,
|
||||
DefaultFallback: true,
|
||||
DefaultScale: 4,
|
||||
MinScale: 1,
|
||||
MaxScale: 64,
|
||||
},
|
||||
FullBody: RouteConfig{
|
||||
DefaultOverlay: true,
|
||||
DefaultDownload: false,
|
||||
DefaultFallback: true,
|
||||
DefaultScale: 4,
|
||||
MinScale: 1,
|
||||
MaxScale: 64,
|
||||
},
|
||||
FrontBody: RouteConfig{
|
||||
DefaultOverlay: true,
|
||||
DefaultDownload: false,
|
||||
DefaultFallback: true,
|
||||
DefaultScale: 4,
|
||||
MinScale: 1,
|
||||
MaxScale: 64,
|
||||
},
|
||||
BackBody: RouteConfig{
|
||||
DefaultOverlay: true,
|
||||
DefaultDownload: false,
|
||||
DefaultFallback: true,
|
||||
DefaultScale: 4,
|
||||
MinScale: 1,
|
||||
MaxScale: 64,
|
||||
},
|
||||
LeftBody: RouteConfig{
|
||||
DefaultOverlay: true,
|
||||
DefaultDownload: false,
|
||||
DefaultFallback: true,
|
||||
DefaultScale: 4,
|
||||
MinScale: 1,
|
||||
MaxScale: 64,
|
||||
},
|
||||
RightBody: RouteConfig{
|
||||
DefaultOverlay: true,
|
||||
DefaultDownload: false,
|
||||
DefaultFallback: true,
|
||||
DefaultScale: 4,
|
||||
MinScale: 1,
|
||||
MaxScale: 64,
|
||||
},
|
||||
RawSkin: RouteConfig{
|
||||
DefaultDownload: false,
|
||||
DefaultFallback: true,
|
||||
},
|
||||
},
|
||||
Cache: CacheConfig{
|
||||
UUIDCacheDuration: time.Hour * 168,
|
||||
SkinCacheDuration: time.Hour * 12,
|
||||
RenderCacheDuration: time.Hour * 12,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
// Config is the root configuration object for the application.
|
||||
type Config struct {
|
||||
Host string `yaml:"host"`
|
||||
Port uint16 `yaml:"port"`
|
||||
Redis RedisConfig `yaml:"redis"`
|
||||
Routes RoutesConfig `yaml:"routes"`
|
||||
Cache CacheConfig `yaml:"cache"`
|
||||
}
|
||||
|
||||
// RoutesConfig is the configuration data of all API routes.
|
||||
type RoutesConfig struct {
|
||||
Face RouteConfig `yaml:"face"`
|
||||
Head RouteConfig `yaml:"head"`
|
||||
FullBody RouteConfig `yaml:"full_body"`
|
||||
FrontBody RouteConfig `yaml:"front_body"`
|
||||
BackBody RouteConfig `yaml:"back_body"`
|
||||
LeftBody RouteConfig `yaml:"left_body"`
|
||||
RightBody RouteConfig `yaml:"right_body"`
|
||||
RawSkin RouteConfig `yaml:"raw_skin"`
|
||||
}
|
||||
|
||||
// RouteConfig is the configuration data used by a single API route.
|
||||
type RouteConfig struct {
|
||||
DefaultScale int `yaml:"default_scale"`
|
||||
DefaultOverlay bool `yaml:"default_overlay"`
|
||||
@@ -16,6 +120,7 @@ type RouteConfig struct {
|
||||
MaxScale int `yaml:"max_scale"`
|
||||
}
|
||||
|
||||
// RedisConfig is the configuration data used to connect to Redis.
|
||||
type RedisConfig struct {
|
||||
Host string `yaml:"host"`
|
||||
Port uint16 `yaml:"port"`
|
||||
@@ -24,28 +129,15 @@ type RedisConfig struct {
|
||||
Database int `yaml:"database"`
|
||||
}
|
||||
|
||||
type Configuration struct {
|
||||
Host string `yaml:"host"`
|
||||
Port uint16 `yaml:"port"`
|
||||
Redis RedisConfig `yaml:"redis"`
|
||||
Routes struct {
|
||||
Face RouteConfig `yaml:"face"`
|
||||
Head RouteConfig `yaml:"head"`
|
||||
FullBody RouteConfig `yaml:"full_body"`
|
||||
FrontBody RouteConfig `yaml:"front_body"`
|
||||
BackBody RouteConfig `yaml:"back_body"`
|
||||
LeftBody RouteConfig `yaml:"left_body"`
|
||||
RightBody RouteConfig `yaml:"right_body"`
|
||||
RawSkin RouteConfig `yaml:"raw_skin"`
|
||||
} `yaml:"routes"`
|
||||
Cache struct {
|
||||
UUIDCacheDuration time.Duration `yaml:"uuid_cache_duration"`
|
||||
SkinCacheDuration time.Duration `yaml:"skin_cache_duration"`
|
||||
RenderCacheDuration time.Duration `yaml:"render_cache_duration"`
|
||||
} `yaml:"cache"`
|
||||
// CacheConfig is the configuration data used to set TTL values for Redis keys.
|
||||
type CacheConfig struct {
|
||||
UUIDCacheDuration time.Duration `yaml:"uuid_cache_duration"`
|
||||
SkinCacheDuration time.Duration `yaml:"skin_cache_duration"`
|
||||
RenderCacheDuration time.Duration `yaml:"render_cache_duration"`
|
||||
}
|
||||
|
||||
func (c *Configuration) ReadFile(file string) error {
|
||||
// ReadFile reads the configuration from the file and parses it as YAML.
|
||||
func (c *Config) ReadFile(file string) error {
|
||||
data, err := os.ReadFile(file)
|
||||
|
||||
if err != nil {
|
||||
|
||||
12
src/main.go
12
src/main.go
@@ -20,8 +20,8 @@ var (
|
||||
return ctx.SendStatus(http.StatusInternalServerError)
|
||||
},
|
||||
})
|
||||
r *Redis = &Redis{}
|
||||
config *Configuration = &Configuration{}
|
||||
r *Redis = &Redis{}
|
||||
conf *Config = &Config{}
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -29,11 +29,11 @@ func init() {
|
||||
|
||||
godotenv.Load()
|
||||
|
||||
if err = config.ReadFile("config.yml"); err != nil {
|
||||
if err = conf.ReadFile("config.yml"); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if err = r.Connect(config.Redis); err != nil {
|
||||
if err = r.Connect(conf.Redis); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -56,6 +56,6 @@ func main() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
log.Printf("Listening on %s:%d\n", config.Host, config.Port+instanceID)
|
||||
log.Fatal(app.Listen(fmt.Sprintf("%s:%d", config.Host, config.Port+instanceID)))
|
||||
log.Printf("Listening on %s:%d\n", conf.Host, conf.Port+instanceID)
|
||||
log.Fatal(app.Listen(fmt.Sprintf("%s:%d", conf.Host, conf.Port+instanceID)))
|
||||
}
|
||||
|
||||
@@ -8,11 +8,13 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// MinecraftProfile is Minecraft profile information returned from the Mojang API.
|
||||
type MinecraftProfile struct {
|
||||
Username string `json:"name"`
|
||||
UUID string `json:"id"`
|
||||
}
|
||||
|
||||
// MinecraftProfileTextures is texture information about a Minecraft profile returned from the Mojang API.
|
||||
type MinecraftProfileTextures struct {
|
||||
UUID string `json:"id"`
|
||||
Username string `json:"name"`
|
||||
@@ -24,6 +26,7 @@ type MinecraftProfileTextures struct {
|
||||
} `json:"properties"`
|
||||
}
|
||||
|
||||
// MinecraftDecodedTextures is the decoded object of the Base64-encoded values property in a MinecraftProfileTextures texture value.
|
||||
type MinecraftDecodedTextures struct {
|
||||
Timestamp int64 `json:"timestamp"`
|
||||
UUID string `json:"uuid"`
|
||||
@@ -42,6 +45,7 @@ type MinecraftDecodedTextures struct {
|
||||
} `json:"textures"`
|
||||
}
|
||||
|
||||
// UsernameToUUID converts a Minecraft username into a UUID using Mojang.
|
||||
func UsernameToUUID(username string) (*MinecraftProfile, error) {
|
||||
req, err := http.NewRequest("GET", fmt.Sprintf("https://api.mojang.com/users/profiles/minecraft/%s", username), nil)
|
||||
|
||||
@@ -62,7 +66,7 @@ func UsernameToUUID(username string) (*MinecraftProfile, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("yggdrasil: unexpected response: %s", resp.Status)
|
||||
return nil, fmt.Errorf("mojang: unexpected response: %s", resp.Status)
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
@@ -82,6 +86,7 @@ func UsernameToUUID(username string) (*MinecraftProfile, error) {
|
||||
return response, nil
|
||||
}
|
||||
|
||||
// GetProfileTextures returns the textures of a Minecraft player from Mojang.
|
||||
func GetProfileTextures(uuid string) (*MinecraftProfileTextures, error) {
|
||||
req, err := http.NewRequest("GET", fmt.Sprintf("https://sessionserver.mojang.com/session/minecraft/profile/%s", uuid), nil)
|
||||
|
||||
@@ -97,12 +102,12 @@ func GetProfileTextures(uuid string) (*MinecraftProfileTextures, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
if resp.StatusCode == 204 {
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
if resp.StatusCode == http.StatusNoContent {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("yggdrasil: unexpected response: %s", resp.Status)
|
||||
return nil, fmt.Errorf("mojang: unexpected response: %s", resp.Status)
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
@@ -122,6 +127,7 @@ func GetProfileTextures(uuid string) (*MinecraftProfileTextures, error) {
|
||||
return response, nil
|
||||
}
|
||||
|
||||
// GetDecodedTexturesValue decodes the values from a MinecraftProfileTextures texture value.
|
||||
func GetDecodedTexturesValue(value string) (*MinecraftDecodedTextures, error) {
|
||||
rawResult, err := base64.StdEncoding.DecodeString(value)
|
||||
|
||||
@@ -129,11 +135,11 @@ func GetDecodedTexturesValue(value string) (*MinecraftDecodedTextures, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := &MinecraftDecodedTextures{}
|
||||
result := MinecraftDecodedTextures{}
|
||||
|
||||
if err = json.Unmarshal(rawResult, result); err != nil {
|
||||
if err = json.Unmarshal(rawResult, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
return &result, nil
|
||||
}
|
||||
@@ -11,10 +11,12 @@ import (
|
||||
"github.com/go-redis/redis/v8"
|
||||
)
|
||||
|
||||
// Redis is a utility client for reading and writing values to the Redis server.
|
||||
type Redis struct {
|
||||
conn *redis.Client
|
||||
}
|
||||
|
||||
// Connect connects to the Redis server using the configuration values provided.
|
||||
func (r *Redis) Connect(conf RedisConfig) error {
|
||||
c := redis.NewClient(&redis.Options{
|
||||
Addr: fmt.Sprintf("%s:%d", conf.Host, conf.Port),
|
||||
@@ -32,6 +34,7 @@ func (r *Redis) Connect(conf RedisConfig) error {
|
||||
return c.Ping(ctx).Err()
|
||||
}
|
||||
|
||||
// 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)
|
||||
|
||||
@@ -52,6 +55,7 @@ func (r *Redis) GetString(key string) (string, bool, error) {
|
||||
return result.Val(), true, result.Err()
|
||||
}
|
||||
|
||||
// GetBytes gets the value from Redis by the key and returns the value as a byte array.
|
||||
func (r *Redis) GetBytes(key string) ([]byte, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
|
||||
@@ -78,6 +82,7 @@ func (r *Redis) GetBytes(key string) ([]byte, error) {
|
||||
return data, err
|
||||
}
|
||||
|
||||
// GetNRGBA gets the value from Redis by the key and returns the value as Go image.NRGBA image type.
|
||||
func (r *Redis) GetNRGBA(key string) (*image.NRGBA, bool, error) {
|
||||
value, err := r.GetBytes(key)
|
||||
|
||||
@@ -106,6 +111,7 @@ func (r *Redis) GetNRGBA(key string) (*image.NRGBA, bool, error) {
|
||||
return img.(*image.NRGBA), true, nil
|
||||
}
|
||||
|
||||
// Exists returns whether or not the key exists in the database.
|
||||
func (r *Redis) Exists(key string) (bool, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
|
||||
@@ -116,6 +122,7 @@ func (r *Redis) Exists(key string) (bool, error) {
|
||||
return result.Val() == 1, result.Err()
|
||||
}
|
||||
|
||||
// Delete deletes the key from the database if it exists.
|
||||
func (r *Redis) Delete(key string) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
|
||||
@@ -124,6 +131,7 @@ func (r *Redis) Delete(key string) error {
|
||||
return r.conn.Del(ctx, key).Err()
|
||||
}
|
||||
|
||||
// Set puts the key-value into the database with an optional TTL value.
|
||||
func (r *Redis) Set(key string, value interface{}, ttl time.Duration) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
|
||||
@@ -132,6 +140,7 @@ func (r *Redis) Set(key string, value interface{}, ttl time.Duration) error {
|
||||
return r.conn.Set(ctx, key, value, ttl).Err()
|
||||
}
|
||||
|
||||
// Close closes the connection to the database.
|
||||
func (r *Redis) Close() error {
|
||||
return r.conn.Close()
|
||||
}
|
||||
|
||||
@@ -23,12 +23,14 @@ func init() {
|
||||
app.Use(NotFoundHandler)
|
||||
}
|
||||
|
||||
// PingHandler is the API handler used for the `/ping` route.
|
||||
func PingHandler(ctx *fiber.Ctx) error {
|
||||
return ctx.SendStatus(http.StatusOK)
|
||||
}
|
||||
|
||||
// FullBodyHandler is the API handler used for the `/body/full/:user` route.
|
||||
func FullBodyHandler(ctx *fiber.Ctx) error {
|
||||
opts := ParseQueryParams(ctx, config.Routes.FullBody)
|
||||
opts := ParseQueryParams(ctx, conf.Routes.FullBody)
|
||||
|
||||
uuid, ok, err := LookupUUID(ParseUserParam(ctx))
|
||||
|
||||
@@ -65,7 +67,7 @@ func FullBodyHandler(ctx *fiber.Ctx) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = r.Set(cacheKey, data, config.Cache.RenderCacheDuration); err != nil {
|
||||
if err = r.Set(cacheKey, data, conf.Cache.RenderCacheDuration); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -77,8 +79,9 @@ func FullBodyHandler(ctx *fiber.Ctx) error {
|
||||
return ctx.Type("png").Send(data)
|
||||
}
|
||||
|
||||
// FrontBodyHandler is the API handler used for the `/body/front/:user` route.
|
||||
func FrontBodyHandler(ctx *fiber.Ctx) error {
|
||||
opts := ParseQueryParams(ctx, config.Routes.FrontBody)
|
||||
opts := ParseQueryParams(ctx, conf.Routes.FrontBody)
|
||||
|
||||
uuid, ok, err := LookupUUID(ParseUserParam(ctx))
|
||||
|
||||
@@ -117,7 +120,7 @@ func FrontBodyHandler(ctx *fiber.Ctx) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = r.Set(cacheKey, data, config.Cache.RenderCacheDuration); err != nil {
|
||||
if err = r.Set(cacheKey, data, conf.Cache.RenderCacheDuration); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -129,8 +132,9 @@ func FrontBodyHandler(ctx *fiber.Ctx) error {
|
||||
return ctx.Type("png").Send(data)
|
||||
}
|
||||
|
||||
// BackBodyHandler is the API handler used for the `/body/back/:user` route.
|
||||
func BackBodyHandler(ctx *fiber.Ctx) error {
|
||||
opts := ParseQueryParams(ctx, config.Routes.BackBody)
|
||||
opts := ParseQueryParams(ctx, conf.Routes.BackBody)
|
||||
|
||||
uuid, ok, err := LookupUUID(ParseUserParam(ctx))
|
||||
|
||||
@@ -169,7 +173,7 @@ func BackBodyHandler(ctx *fiber.Ctx) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = r.Set(cacheKey, data, config.Cache.RenderCacheDuration); err != nil {
|
||||
if err = r.Set(cacheKey, data, conf.Cache.RenderCacheDuration); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -181,8 +185,9 @@ func BackBodyHandler(ctx *fiber.Ctx) error {
|
||||
return ctx.Type("png").Send(data)
|
||||
}
|
||||
|
||||
// LeftBodyHandler is the API handler used for the `/body/left/:user` route.
|
||||
func LeftBodyHandler(ctx *fiber.Ctx) error {
|
||||
opts := ParseQueryParams(ctx, config.Routes.LeftBody)
|
||||
opts := ParseQueryParams(ctx, conf.Routes.LeftBody)
|
||||
|
||||
uuid, ok, err := LookupUUID(ParseUserParam(ctx))
|
||||
|
||||
@@ -221,7 +226,7 @@ func LeftBodyHandler(ctx *fiber.Ctx) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = r.Set(cacheKey, data, config.Cache.RenderCacheDuration); err != nil {
|
||||
if err = r.Set(cacheKey, data, conf.Cache.RenderCacheDuration); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -233,8 +238,9 @@ func LeftBodyHandler(ctx *fiber.Ctx) error {
|
||||
return ctx.Type("png").Send(data)
|
||||
}
|
||||
|
||||
// RightBodyHandler is the API handler used for the `/body/right/:user` route.
|
||||
func RightBodyHandler(ctx *fiber.Ctx) error {
|
||||
opts := ParseQueryParams(ctx, config.Routes.RightBody)
|
||||
opts := ParseQueryParams(ctx, conf.Routes.RightBody)
|
||||
|
||||
uuid, ok, err := LookupUUID(ParseUserParam(ctx))
|
||||
|
||||
@@ -273,7 +279,7 @@ func RightBodyHandler(ctx *fiber.Ctx) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = r.Set(cacheKey, data, config.Cache.RenderCacheDuration); err != nil {
|
||||
if err = r.Set(cacheKey, data, conf.Cache.RenderCacheDuration); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -285,8 +291,9 @@ func RightBodyHandler(ctx *fiber.Ctx) error {
|
||||
return ctx.Type("png").Send(data)
|
||||
}
|
||||
|
||||
// FaceHandler is the API handler used for the `/face/:user` route.
|
||||
func FaceHandler(ctx *fiber.Ctx) error {
|
||||
opts := ParseQueryParams(ctx, config.Routes.Face)
|
||||
opts := ParseQueryParams(ctx, conf.Routes.Face)
|
||||
|
||||
uuid, ok, err := LookupUUID(ParseUserParam(ctx))
|
||||
|
||||
@@ -325,7 +332,7 @@ func FaceHandler(ctx *fiber.Ctx) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = r.Set(cacheKey, data, config.Cache.RenderCacheDuration); err != nil {
|
||||
if err = r.Set(cacheKey, data, conf.Cache.RenderCacheDuration); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -337,8 +344,9 @@ func FaceHandler(ctx *fiber.Ctx) error {
|
||||
return ctx.Type("png").Send(data)
|
||||
}
|
||||
|
||||
// HeadHandler is the API handler used for the `/head/:user` route.
|
||||
func HeadHandler(ctx *fiber.Ctx) error {
|
||||
opts := ParseQueryParams(ctx, config.Routes.Head)
|
||||
opts := ParseQueryParams(ctx, conf.Routes.Head)
|
||||
|
||||
uuid, ok, err := LookupUUID(ParseUserParam(ctx))
|
||||
|
||||
@@ -377,7 +385,7 @@ func HeadHandler(ctx *fiber.Ctx) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = r.Set(cacheKey, data, config.Cache.RenderCacheDuration); err != nil {
|
||||
if err = r.Set(cacheKey, data, conf.Cache.RenderCacheDuration); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -389,8 +397,9 @@ func HeadHandler(ctx *fiber.Ctx) error {
|
||||
return ctx.Type("png").Send(data)
|
||||
}
|
||||
|
||||
// SkinHandler is the API handler used for the `/skin/:user` route.
|
||||
func SkinHandler(ctx *fiber.Ctx) error {
|
||||
opts := ParseQueryParams(ctx, config.Routes.RawSkin)
|
||||
opts := ParseQueryParams(ctx, conf.Routes.RawSkin)
|
||||
|
||||
uuid, ok, err := LookupUUID(ParseUserParam(ctx))
|
||||
|
||||
@@ -421,6 +430,7 @@ func SkinHandler(ctx *fiber.Ctx) error {
|
||||
return ctx.Type("png").Send(data)
|
||||
}
|
||||
|
||||
// UUIDHandler is the API handler used for the `/uuid/:user` route.
|
||||
func UUIDHandler(ctx *fiber.Ctx) error {
|
||||
uuid, ok, err := LookupUUID(ctx.Params("user"))
|
||||
|
||||
@@ -435,6 +445,7 @@ func UUIDHandler(ctx *fiber.Ctx) error {
|
||||
return ctx.SendString(uuid)
|
||||
}
|
||||
|
||||
// NotFoundHandler is the API handler used for any requests that do not match an existing route.
|
||||
func NotFoundHandler(ctx *fiber.Ctx) error {
|
||||
return ctx.SendStatus(http.StatusNotFound)
|
||||
}
|
||||
|
||||
19
src/util.go
19
src/util.go
@@ -16,6 +16,7 @@ import (
|
||||
"github.com/mineatar-io/skin-render"
|
||||
)
|
||||
|
||||
// QueryParams is used by most all API routes as options for how the image should be rendered, or how errors should be handled.
|
||||
type QueryParams struct {
|
||||
Scale int `query:"scale"`
|
||||
Download bool `query:"download"`
|
||||
@@ -23,10 +24,12 @@ type QueryParams struct {
|
||||
Fallback bool `query:"fallback"`
|
||||
}
|
||||
|
||||
// FormatUUID returns the UUID string without any dashes.
|
||||
func FormatUUID(uuid string) string {
|
||||
return strings.ToLower(strings.ReplaceAll(uuid, "-", ""))
|
||||
}
|
||||
|
||||
// LookupUUID returns the UUID of a player either by username or UUID, while attempting to use any cached values in the database.
|
||||
func LookupUUID(value string) (string, bool, error) {
|
||||
value = FormatUUID(value)
|
||||
|
||||
@@ -56,13 +59,14 @@ func LookupUUID(value string) (string, bool, error) {
|
||||
return "", false, nil
|
||||
}
|
||||
|
||||
if err = r.Set(cacheKey, profile.UUID, config.Cache.UUIDCacheDuration); err != nil {
|
||||
if err = r.Set(cacheKey, profile.UUID, conf.Cache.UUIDCacheDuration); err != nil {
|
||||
return "", true, err
|
||||
}
|
||||
|
||||
return profile.UUID, true, nil
|
||||
}
|
||||
|
||||
// FetchImage fetches the image by the URL and returns it as a parsed image.
|
||||
func FetchImage(url string) (*image.NRGBA, error) {
|
||||
resp, err := http.Get(url)
|
||||
|
||||
@@ -89,6 +93,7 @@ func FetchImage(url string) (*image.NRGBA, error) {
|
||||
return img.(*image.NRGBA), nil
|
||||
}
|
||||
|
||||
// GetPlayerSkin fetches the skin of the Minecraft player by the UUID.
|
||||
func GetPlayerSkin(uuid string) (*image.NRGBA, bool, error) {
|
||||
uuid = FormatUUID(uuid)
|
||||
|
||||
@@ -166,12 +171,12 @@ func GetPlayerSkin(uuid string) (*image.NRGBA, bool, error) {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
if err = r.Set(fmt.Sprintf("skin:%s", uuid), encodedSkin, config.Cache.SkinCacheDuration); err != nil {
|
||||
if err = r.Set(fmt.Sprintf("skin:%s", uuid), encodedSkin, conf.Cache.SkinCacheDuration); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
if slim {
|
||||
if err = r.Set(fmt.Sprintf("slim:%s", uuid), "true", config.Cache.SkinCacheDuration); err != nil {
|
||||
if err = r.Set(fmt.Sprintf("slim:%s", uuid), "true", conf.Cache.SkinCacheDuration); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
} else {
|
||||
@@ -183,8 +188,8 @@ func GetPlayerSkin(uuid string) (*image.NRGBA, bool, error) {
|
||||
return skin, slim, nil
|
||||
}
|
||||
|
||||
// This is used instead of `math.Min/Max` because of the
|
||||
// unnecessary coercion from/to float64.
|
||||
// Clamp clamps the input value between the minimum and maximum values.
|
||||
// This method is preferred over `math.Min()` and `math.Max()` to prevent any type coercion between floats and integers.
|
||||
func Clamp(value, min, max int) int {
|
||||
if value > max {
|
||||
return max
|
||||
@@ -197,6 +202,7 @@ func Clamp(value, min, max int) int {
|
||||
return value
|
||||
}
|
||||
|
||||
// EncodePNG encodes the image into PNG format and returns the data as a byte array.
|
||||
func EncodePNG(img image.Image) ([]byte, error) {
|
||||
buf := &bytes.Buffer{}
|
||||
|
||||
@@ -207,6 +213,7 @@ func EncodePNG(img image.Image) ([]byte, error) {
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
// ParseQueryParams parses the query parameters from the request and returns a QueryParams struct, using default values from the provided configuration.
|
||||
func ParseQueryParams(ctx *fiber.Ctx, route RouteConfig) *QueryParams {
|
||||
args := ctx.Context().QueryArgs()
|
||||
|
||||
@@ -238,6 +245,7 @@ func ParseQueryParams(ctx *fiber.Ctx, route RouteConfig) *QueryParams {
|
||||
return response
|
||||
}
|
||||
|
||||
// GetInstanceID returns the INSTANCE_ID environment variable parsed as an unsigned 16-bit integer.
|
||||
func GetInstanceID() (uint16, error) {
|
||||
if instanceID := os.Getenv("INSTANCE_ID"); len(instanceID) > 0 {
|
||||
value, err := strconv.ParseUint(instanceID, 10, 16)
|
||||
@@ -252,6 +260,7 @@ func GetInstanceID() (uint16, error) {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// ParseUserParam returns the user name from the route param, allowing values such as "PassTheMayo.png" to be returned as "PassTheMayo".
|
||||
func ParseUserParam(ctx *fiber.Ctx) string {
|
||||
return strings.Split(ctx.Params("user"), ".")[0]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user