diff --git a/src/main.go b/src/main.go index 13c7bbc..26db9da 100644 --- a/src/main.go +++ b/src/main.go @@ -3,16 +3,18 @@ package main import ( "fmt" "log" + "net/http" "os" "strconv" "sync" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/cors" + "github.com/gofiber/fiber/v2/middleware/recover" ) var ( - app *fiber.App = fiber.New(fiber.Config{DisableStartupMessage: true}) + app *fiber.App = nil r *Redis = &Redis{} config *Config = &Config{} blockedServers []string = nil @@ -20,6 +22,15 @@ var ( ) func init() { + app = fiber.New(fiber.Config{ + DisableStartupMessage: true, + ErrorHandler: func(ctx *fiber.Ctx, err error) error { + log.Println(err) + + return ctx.SendStatus(http.StatusInternalServerError) + }, + }) + if err := config.ReadFile("config.yml"); err != nil { log.Fatal(err) } @@ -55,6 +66,8 @@ func init() { AllowMethods: "HEAD,OPTIONS,GET", ExposeHeaders: "Content-Type,X-Cache-Time-Remaining", })) + + app.Use(recover.New()) } func main() { diff --git a/src/redis.go b/src/redis.go index 970b4a0..1aae9b2 100644 --- a/src/redis.go +++ b/src/redis.go @@ -75,9 +75,9 @@ func (r *Redis) TTL(key string) (time.Duration, error) { return res.Result() } -func (r *Redis) GetJSON(key string, value interface{}) error { +func (r *Redis) GetString(key string) (string, error) { if !r.Enabled { - return nil + return "", nil } ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) @@ -87,16 +87,10 @@ func (r *Redis) GetJSON(key string, value interface{}) error { res := r.Client.Get(ctx, key) if err := res.Err(); err != nil { - return err + return "", nil } - data, err := res.Bytes() - - if err != nil { - return err - } - - return json.Unmarshal(data, value) + return res.Result() } func (r *Redis) GetBytes(key string) ([]byte, error) { diff --git a/src/routes.go b/src/routes.go index 829b4a9..a8390ae 100644 --- a/src/routes.go +++ b/src/routes.go @@ -1,7 +1,6 @@ package main import ( - "log" "net/http" "strconv" @@ -23,8 +22,6 @@ func JavaStatusHandler(ctx *fiber.Ctx) error { host, port, err := ParseAddress(ctx.Params("address"), 25565) if err != nil { - log.Println(err) - return ctx.Status(http.StatusBadRequest).SendString("Invalid address value") } @@ -38,7 +35,7 @@ func JavaStatusHandler(ctx *fiber.Ctx) error { ctx.Set("X-Cache-Time-Remaining", strconv.Itoa(int(expiresAt.Seconds()))) } - return ctx.JSON(response) + return ctx.SendString(response) } func BedrockStatusHandler(ctx *fiber.Ctx) error { @@ -58,7 +55,7 @@ func BedrockStatusHandler(ctx *fiber.Ctx) error { ctx.Set("X-Cache-Time-Remaining", strconv.Itoa(int(expiresAt.Seconds()))) } - return ctx.JSON(response) + return ctx.SendString(response) } func IconHandler(ctx *fiber.Ctx) error { diff --git a/src/status.go b/src/status.go index 732bdbd..ed77c71 100644 --- a/src/status.go +++ b/src/status.go @@ -2,6 +2,7 @@ package main import ( "encoding/base64" + "encoding/json" "fmt" "strings" "time" @@ -76,20 +77,20 @@ type Mod struct { Version string `json:"version"` } -func GetJavaStatus(host string, port uint16) (interface{}, *time.Duration, error) { +func GetJavaStatus(host string, port uint16) (string, *time.Duration, error) { cacheKey := fmt.Sprintf("java:%s-%d", host, port) cacheExists, err := r.Exists(cacheKey) if err != nil { - return nil, nil, err + return "", nil, err } if cacheExists { - var response JavaStatusResponse + response, err := r.GetString(cacheKey) - if err = r.GetJSON(cacheKey, &response); err != nil { - return nil, nil, err + if err != nil { + return "", nil, err } expiresAt, err := r.TTL(cacheKey) @@ -97,29 +98,33 @@ func GetJavaStatus(host string, port uint16) (interface{}, *time.Duration, error return response, &expiresAt, err } - response := FetchJavaStatus(host, port) + response, err := json.Marshal(FetchJavaStatus(host, port)) - if err := r.SetJSON(cacheKey, response, config.Cache.JavaCacheDuration); err != nil { - return nil, nil, err + if err != nil { + return "", nil, err } - return response, nil, nil + if err := r.Set(cacheKey, response, config.Cache.JavaCacheDuration); err != nil { + return "", nil, err + } + + return string(response), nil, nil } -func GetBedrockStatus(host string, port uint16) (interface{}, *time.Duration, error) { +func GetBedrockStatus(host string, port uint16) (string, *time.Duration, error) { cacheKey := fmt.Sprintf("bedrock:%s-%d", host, port) cacheExists, err := r.Exists(cacheKey) if err != nil { - return nil, nil, err + return "", nil, err } if cacheExists { - var response BedrockStatusResponse + response, err := r.GetString(cacheKey) - if err = r.GetJSON(cacheKey, &response); err != nil { - return nil, nil, err + if err != nil { + return "", nil, err } expiresAt, err := r.TTL(cacheKey) @@ -127,13 +132,17 @@ func GetBedrockStatus(host string, port uint16) (interface{}, *time.Duration, er return response, &expiresAt, err } - response := FetchBedrockStatus(host, port) + response, err := json.Marshal(FetchBedrockStatus(host, port)) - if err := r.SetJSON(cacheKey, response, config.Cache.BedrockCacheDuration); err != nil { - return nil, nil, err + if err != nil { + return "", nil, err } - return response, nil, nil + if err := r.Set(cacheKey, response, config.Cache.BedrockCacheDuration); err != nil { + return "", nil, err + } + + return string(response), nil, nil } func GetServerIcon(host string, port uint16) ([]byte, error) {