Update dependencies and refactor config variable
This commit is contained in:
parent
8b6d648cae
commit
630e69a03d
8
go.mod
8
go.mod
@ -1,10 +1,12 @@
|
|||||||
module main
|
module main
|
||||||
|
|
||||||
go 1.19
|
go 1.21
|
||||||
|
|
||||||
|
toolchain go1.21.0
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/go-redsync/redsync/v4 v4.8.1
|
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/mcstatus-io/mcutil/v2 v2.0.1
|
||||||
github.com/redis/go-redis/v9 v9.1.0
|
github.com/redis/go-redis/v9 v9.1.0
|
||||||
gopkg.in/yaml.v3 v3.0.1
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
@ -15,7 +17,7 @@ require (
|
|||||||
github.com/cespare/xxhash/v2 v2.2.0 // indirect
|
github.com/cespare/xxhash/v2 v2.2.0 // indirect
|
||||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||||
github.com/go-redis/redis/v8 v8.11.5 // 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/errwrap v1.1.0 // indirect
|
||||||
github.com/hashicorp/go-multierror v1.1.1 // indirect
|
github.com/hashicorp/go-multierror v1.1.1 // indirect
|
||||||
github.com/klauspost/compress v1.16.7 // indirect
|
github.com/klauspost/compress v1.16.7 // indirect
|
||||||
|
|||||||
4
go.sum
4
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/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 h1:cRVMCb9aUJDsyHxGFLwz/sGzDggdailZZyptU9F9cU0=
|
||||||
github.com/gofiber/fiber/v2 v2.48.0/go.mod h1:xqJgfqrc23FJuqGOW6DVgi3HyZEm2Mn9pRqUb2kHSX8=
|
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.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.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||||
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
|
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/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 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
|
||||||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
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.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 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
|
||||||
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
|
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
|
||||||
|
|||||||
@ -68,7 +68,6 @@ func (c *Config) WriteFile(file string) error {
|
|||||||
return os.WriteFile(file, data, 0777)
|
return os.WriteFile(file, data, 0777)
|
||||||
}
|
}
|
||||||
|
|
||||||
// overrideWithEnvVars overrides configuration values using environment variables.
|
|
||||||
func (c *Config) overrideWithEnvVars() error {
|
func (c *Config) overrideWithEnvVars() error {
|
||||||
if value := os.Getenv("ENVIRONMENT"); value != "" {
|
if value := os.Getenv("ENVIRONMENT"); value != "" {
|
||||||
c.Environment = value
|
c.Environment = value
|
||||||
|
|||||||
12
src/main.go
12
src/main.go
@ -26,18 +26,18 @@ var (
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
r *Redis = &Redis{}
|
r *Redis = &Redis{}
|
||||||
conf *Config = DefaultConfig
|
config *Config = DefaultConfig
|
||||||
instanceID uint16 = 0
|
instanceID uint16 = 0
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
var err error
|
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) {
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
log.Printf("config.yml does not exist, writing default config\n")
|
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)
|
log.Fatalf("Failed to write config file: %v", err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -51,7 +51,7 @@ func init() {
|
|||||||
|
|
||||||
log.Println("Successfully retrieved EULA blocked servers")
|
log.Println("Successfully retrieved EULA blocked servers")
|
||||||
|
|
||||||
if conf.Redis != nil {
|
if config.Redis != nil {
|
||||||
if err = r.Connect(); err != nil {
|
if err = r.Connect(); err != nil {
|
||||||
log.Fatalf("Failed to connect to Redis: %v", err)
|
log.Fatalf("Failed to connect to Redis: %v", err)
|
||||||
}
|
}
|
||||||
@ -64,7 +64,7 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
app.Hooks().OnListen(func(ld fiber.ListenData) error {
|
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
|
return nil
|
||||||
})
|
})
|
||||||
@ -73,7 +73,7 @@ func init() {
|
|||||||
func main() {
|
func main() {
|
||||||
defer r.Close()
|
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)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,7 +22,7 @@ type Redis struct {
|
|||||||
|
|
||||||
// Connect establishes a connection to the Redis server using the configuration.
|
// Connect establishes a connection to the Redis server using the configuration.
|
||||||
func (r *Redis) Connect() error {
|
func (r *Redis) Connect() error {
|
||||||
if conf.Redis == nil {
|
if config.Redis == nil {
|
||||||
return errors.New("missing Redis configuration")
|
return errors.New("missing Redis configuration")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -30,7 +30,7 @@ func (r *Redis) Connect() error {
|
|||||||
|
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
opts, err := redis.ParseURL(*conf.Redis)
|
opts, err := redis.ParseURL(*config.Redis)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@ -9,6 +9,7 @@ import (
|
|||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"github.com/gofiber/fiber/v2/middleware/cors"
|
"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/logger"
|
||||||
"github.com/gofiber/fiber/v2/middleware/recover"
|
"github.com/gofiber/fiber/v2/middleware/recover"
|
||||||
"github.com/mcstatus-io/mcutil/v2"
|
"github.com/mcstatus-io/mcutil/v2"
|
||||||
@ -20,7 +21,11 @@ func init() {
|
|||||||
EnableStackTrace: true,
|
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{
|
app.Use(cors.New(cors.Config{
|
||||||
AllowOrigins: "*",
|
AllowOrigins: "*",
|
||||||
AllowMethods: "HEAD,OPTIONS,GET",
|
AllowMethods: "HEAD,OPTIONS,GET",
|
||||||
@ -34,7 +39,6 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
app.Get("/ping", PingHandler)
|
app.Get("/ping", PingHandler)
|
||||||
app.Get("/favicon.ico", FaviconHandler)
|
|
||||||
app.Get("/status/java/:address", JavaStatusHandler)
|
app.Get("/status/java/:address", JavaStatusHandler)
|
||||||
app.Get("/status/bedrock/:address", BedrockStatusHandler)
|
app.Get("/status/bedrock/:address", BedrockStatusHandler)
|
||||||
app.Get("/icon", DefaultIconHandler)
|
app.Get("/icon", DefaultIconHandler)
|
||||||
@ -47,11 +51,6 @@ func PingHandler(ctx *fiber.Ctx) error {
|
|||||||
return ctx.SendStatus(http.StatusOK)
|
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.
|
// JavaStatusHandler returns the status of the Java edition Minecraft server specified in the address parameter.
|
||||||
func JavaStatusHandler(ctx *fiber.Ctx) error {
|
func JavaStatusHandler(ctx *fiber.Ctx) error {
|
||||||
opts, err := GetStatusOptions(ctx)
|
opts, err := GetStatusOptions(ctx)
|
||||||
|
|||||||
@ -127,7 +127,7 @@ func GetJavaStatus(host string, port uint16, opts *StatusOptions) (*JavaStatusRe
|
|||||||
cacheKey := GetCacheKey(host, port, opts)
|
cacheKey := GetCacheKey(host, port, opts)
|
||||||
|
|
||||||
// Wait for any other processes to finish fetching the status of this server
|
// 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 := r.NewMutex(fmt.Sprintf("java-lock:%s", cacheKey))
|
||||||
mutex.Lock()
|
mutex.Lock()
|
||||||
|
|
||||||
@ -161,7 +161,7 @@ func GetJavaStatus(host string, port uint16, opts *StatusOptions) (*JavaStatusRe
|
|||||||
return nil, 0, err
|
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
|
return nil, 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -174,7 +174,7 @@ func GetBedrockStatus(host string, port uint16, opts *StatusOptions) (*BedrockSt
|
|||||||
cacheKey := GetCacheKey(host, port, nil)
|
cacheKey := GetCacheKey(host, port, nil)
|
||||||
|
|
||||||
// Wait for any other processes to finish fetching the status of this server
|
// 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 := r.NewMutex(fmt.Sprintf("bedrock-lock:%s", cacheKey))
|
||||||
mutex.Lock()
|
mutex.Lock()
|
||||||
|
|
||||||
@ -208,7 +208,7 @@ func GetBedrockStatus(host string, port uint16, opts *StatusOptions) (*BedrockSt
|
|||||||
return nil, 0, err
|
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
|
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
|
// 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
|
return nil, 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -380,7 +380,7 @@ func BuildJavaResponse(host string, port uint16, status *response.JavaStatus, le
|
|||||||
Port: port,
|
Port: port,
|
||||||
EULABlocked: IsBlockedAddress(host),
|
EULABlocked: IsBlockedAddress(host),
|
||||||
RetrievedAt: time.Now().UnixMilli(),
|
RetrievedAt: time.Now().UnixMilli(),
|
||||||
ExpiresAt: time.Now().Add(conf.Cache.JavaStatusDuration).UnixMilli(),
|
ExpiresAt: time.Now().Add(config.Cache.JavaStatusDuration).UnixMilli(),
|
||||||
},
|
},
|
||||||
JavaStatus: nil,
|
JavaStatus: nil,
|
||||||
}
|
}
|
||||||
@ -572,7 +572,7 @@ func BuildBedrockResponse(host string, port uint16, status *response.BedrockStat
|
|||||||
Port: port,
|
Port: port,
|
||||||
EULABlocked: IsBlockedAddress(host),
|
EULABlocked: IsBlockedAddress(host),
|
||||||
RetrievedAt: time.Now().UnixMilli(),
|
RetrievedAt: time.Now().UnixMilli(),
|
||||||
ExpiresAt: time.Now().Add(conf.Cache.BedrockStatusDuration).UnixMilli(),
|
ExpiresAt: time.Now().Add(config.Cache.BedrockStatusDuration).UnixMilli(),
|
||||||
},
|
},
|
||||||
BedrockStatus: nil,
|
BedrockStatus: nil,
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user