From 630e69a03dfcbb3314eee1b01d4999c0a06f2c6e Mon Sep 17 00:00:00 2001 From: Jacob Gunther Date: Sun, 27 Aug 2023 21:51:33 -0500 Subject: [PATCH] Update dependencies and refactor config variable --- go.mod | 8 +++++--- go.sum | 4 ++++ src/config.go | 1 - src/main.go | 12 ++++++------ src/redis.go | 4 ++-- src/routes.go | 13 ++++++------- src/status.go | 14 +++++++------- 7 files changed, 30 insertions(+), 26 deletions(-) diff --git a/go.mod b/go.mod index 1f59138..db15bee 100644 --- a/go.mod +++ b/go.mod @@ -1,10 +1,12 @@ module main -go 1.19 +go 1.21 + +toolchain go1.21.0 require ( github.com/go-redsync/redsync/v4 v4.8.1 - github.com/gofiber/fiber/v2 v2.48.0 + github.com/gofiber/fiber/v2 v2.49.0 github.com/mcstatus-io/mcutil/v2 v2.0.1 github.com/redis/go-redis/v9 v9.1.0 gopkg.in/yaml.v3 v3.0.1 @@ -15,7 +17,7 @@ require ( github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/go-redis/redis/v8 v8.11.5 // indirect - github.com/google/uuid v1.3.0 // indirect + github.com/google/uuid v1.3.1 // indirect github.com/hashicorp/errwrap v1.1.0 // indirect github.com/hashicorp/go-multierror v1.1.1 // indirect github.com/klauspost/compress v1.16.7 // indirect diff --git a/go.sum b/go.sum index 0cab53b..8eafdf6 100644 --- a/go.sum +++ b/go.sum @@ -25,6 +25,8 @@ github.com/go-redsync/redsync/v4 v4.8.1/go.mod h1:LmUAsQuQxhzZAoGY7JS6+dNhNmZyon github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= github.com/gofiber/fiber/v2 v2.48.0 h1:cRVMCb9aUJDsyHxGFLwz/sGzDggdailZZyptU9F9cU0= github.com/gofiber/fiber/v2 v2.48.0/go.mod h1:xqJgfqrc23FJuqGOW6DVgi3HyZEm2Mn9pRqUb2kHSX8= +github.com/gofiber/fiber/v2 v2.49.0 h1:xBVG2c66GDcWfww56xHvMn52Q0XX7UrSvjj6MD8/5EE= +github.com/gofiber/fiber/v2 v2.49.0/go.mod h1:oxpt7wQaEYgdDmq7nMxCGhilYicBLFnZ+jQSJcQDlSE= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= @@ -44,6 +46,8 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4= +github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I= github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= diff --git a/src/config.go b/src/config.go index ea270bc..5a32340 100644 --- a/src/config.go +++ b/src/config.go @@ -68,7 +68,6 @@ func (c *Config) WriteFile(file string) error { return os.WriteFile(file, data, 0777) } -// overrideWithEnvVars overrides configuration values using environment variables. func (c *Config) overrideWithEnvVars() error { if value := os.Getenv("ENVIRONMENT"); value != "" { c.Environment = value diff --git a/src/main.go b/src/main.go index def2768..b3b055d 100644 --- a/src/main.go +++ b/src/main.go @@ -26,18 +26,18 @@ var ( }, }) r *Redis = &Redis{} - conf *Config = DefaultConfig + config *Config = DefaultConfig instanceID uint16 = 0 ) func init() { var err error - if err = conf.ReadFile("config.yml"); err != nil { + if err = config.ReadFile("config.yml"); err != nil { if errors.Is(err, os.ErrNotExist) { log.Printf("config.yml does not exist, writing default config\n") - if err = conf.WriteFile("config.yml"); err != nil { + if err = config.WriteFile("config.yml"); err != nil { log.Fatalf("Failed to write config file: %v", err) } } else { @@ -51,7 +51,7 @@ func init() { log.Println("Successfully retrieved EULA blocked servers") - if conf.Redis != nil { + if config.Redis != nil { if err = r.Connect(); err != nil { log.Fatalf("Failed to connect to Redis: %v", err) } @@ -64,7 +64,7 @@ func init() { } app.Hooks().OnListen(func(ld fiber.ListenData) error { - log.Printf("Listening on %s:%d\n", conf.Host, conf.Port+instanceID) + log.Printf("Listening on %s:%d\n", config.Host, config.Port+instanceID) return nil }) @@ -73,7 +73,7 @@ func init() { func main() { defer r.Close() - if err := app.Listen(fmt.Sprintf("%s:%d", conf.Host, conf.Port+instanceID)); err != nil { + if err := app.Listen(fmt.Sprintf("%s:%d", config.Host, config.Port+instanceID)); err != nil { panic(err) } } diff --git a/src/redis.go b/src/redis.go index 3e18bdc..f1dc904 100644 --- a/src/redis.go +++ b/src/redis.go @@ -22,7 +22,7 @@ type Redis struct { // Connect establishes a connection to the Redis server using the configuration. func (r *Redis) Connect() error { - if conf.Redis == nil { + if config.Redis == nil { return errors.New("missing Redis configuration") } @@ -30,7 +30,7 @@ func (r *Redis) Connect() error { defer cancel() - opts, err := redis.ParseURL(*conf.Redis) + opts, err := redis.ParseURL(*config.Redis) if err != nil { return err diff --git a/src/routes.go b/src/routes.go index dfffbf5..b46b30e 100644 --- a/src/routes.go +++ b/src/routes.go @@ -9,6 +9,7 @@ import ( "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/cors" + "github.com/gofiber/fiber/v2/middleware/favicon" "github.com/gofiber/fiber/v2/middleware/logger" "github.com/gofiber/fiber/v2/middleware/recover" "github.com/mcstatus-io/mcutil/v2" @@ -20,7 +21,11 @@ func init() { EnableStackTrace: true, })) - if conf.Environment == "development" { + app.Use(favicon.New(favicon.Config{ + Data: assets.Favicon, + })) + + if config.Environment == "development" { app.Use(cors.New(cors.Config{ AllowOrigins: "*", AllowMethods: "HEAD,OPTIONS,GET", @@ -34,7 +39,6 @@ func init() { } app.Get("/ping", PingHandler) - app.Get("/favicon.ico", FaviconHandler) app.Get("/status/java/:address", JavaStatusHandler) app.Get("/status/bedrock/:address", BedrockStatusHandler) app.Get("/icon", DefaultIconHandler) @@ -47,11 +51,6 @@ func PingHandler(ctx *fiber.Ctx) error { return ctx.SendStatus(http.StatusOK) } -// FaviconHandler serves the favicon.ico file to any users that visit the API using a browser. -func FaviconHandler(ctx *fiber.Ctx) error { - return ctx.Type("ico").Send(assets.Favicon) -} - // JavaStatusHandler returns the status of the Java edition Minecraft server specified in the address parameter. func JavaStatusHandler(ctx *fiber.Ctx) error { opts, err := GetStatusOptions(ctx) diff --git a/src/status.go b/src/status.go index a23d33c..ce9630a 100644 --- a/src/status.go +++ b/src/status.go @@ -127,7 +127,7 @@ func GetJavaStatus(host string, port uint16, opts *StatusOptions) (*JavaStatusRe cacheKey := GetCacheKey(host, port, opts) // Wait for any other processes to finish fetching the status of this server - if conf.Cache.EnableLocks { + if config.Cache.EnableLocks { mutex := r.NewMutex(fmt.Sprintf("java-lock:%s", cacheKey)) mutex.Lock() @@ -161,7 +161,7 @@ func GetJavaStatus(host string, port uint16, opts *StatusOptions) (*JavaStatusRe return nil, 0, err } - if err := r.Set(fmt.Sprintf("java:%s", cacheKey), data, conf.Cache.JavaStatusDuration); err != nil { + if err := r.Set(fmt.Sprintf("java:%s", cacheKey), data, config.Cache.JavaStatusDuration); err != nil { return nil, 0, err } @@ -174,7 +174,7 @@ func GetBedrockStatus(host string, port uint16, opts *StatusOptions) (*BedrockSt cacheKey := GetCacheKey(host, port, nil) // Wait for any other processes to finish fetching the status of this server - if conf.Cache.EnableLocks { + if config.Cache.EnableLocks { mutex := r.NewMutex(fmt.Sprintf("bedrock-lock:%s", cacheKey)) mutex.Lock() @@ -208,7 +208,7 @@ func GetBedrockStatus(host string, port uint16, opts *StatusOptions) (*BedrockSt return nil, 0, err } - if err = r.Set(fmt.Sprintf("bedrock:%s", cacheKey), data, conf.Cache.BedrockStatusDuration); err != nil { + if err = r.Set(fmt.Sprintf("bedrock:%s", cacheKey), data, config.Cache.BedrockStatusDuration); err != nil { return nil, 0, err } @@ -259,7 +259,7 @@ func GetServerIcon(host string, port uint16, opts *StatusOptions) ([]byte, time. } // Put the icon into the cache for future requests - if err := r.Set(fmt.Sprintf("icon:%s", cacheKey), icon, conf.Cache.IconDuration); err != nil { + if err := r.Set(fmt.Sprintf("icon:%s", cacheKey), icon, config.Cache.IconDuration); err != nil { return nil, 0, err } @@ -380,7 +380,7 @@ func BuildJavaResponse(host string, port uint16, status *response.JavaStatus, le Port: port, EULABlocked: IsBlockedAddress(host), RetrievedAt: time.Now().UnixMilli(), - ExpiresAt: time.Now().Add(conf.Cache.JavaStatusDuration).UnixMilli(), + ExpiresAt: time.Now().Add(config.Cache.JavaStatusDuration).UnixMilli(), }, JavaStatus: nil, } @@ -572,7 +572,7 @@ func BuildBedrockResponse(host string, port uint16, status *response.BedrockStat Port: port, EULABlocked: IsBlockedAddress(host), RetrievedAt: time.Now().UnixMilli(), - ExpiresAt: time.Now().Add(conf.Cache.BedrockStatusDuration).UnixMilli(), + ExpiresAt: time.Now().Add(config.Cache.BedrockStatusDuration).UnixMilli(), }, BedrockStatus: nil, }