Update dependencies and refactor config variable

This commit is contained in:
Jacob Gunther
2023-08-27 21:51:33 -05:00
parent 8b6d648cae
commit 630e69a03d
7 changed files with 30 additions and 26 deletions

View File

@@ -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

View File

@@ -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)
}
}

View File

@@ -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

View File

@@ -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)

View File

@@ -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,
}