More optimizations and error handling
This commit is contained in:
15
src/main.go
15
src/main.go
@@ -3,16 +3,18 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"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/recover"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
app *fiber.App = fiber.New(fiber.Config{DisableStartupMessage: true})
|
app *fiber.App = nil
|
||||||
r *Redis = &Redis{}
|
r *Redis = &Redis{}
|
||||||
config *Config = &Config{}
|
config *Config = &Config{}
|
||||||
blockedServers []string = nil
|
blockedServers []string = nil
|
||||||
@@ -20,6 +22,15 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
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 {
|
if err := config.ReadFile("config.yml"); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -55,6 +66,8 @@ func init() {
|
|||||||
AllowMethods: "HEAD,OPTIONS,GET",
|
AllowMethods: "HEAD,OPTIONS,GET",
|
||||||
ExposeHeaders: "Content-Type,X-Cache-Time-Remaining",
|
ExposeHeaders: "Content-Type,X-Cache-Time-Remaining",
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
app.Use(recover.New())
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|||||||
14
src/redis.go
14
src/redis.go
@@ -75,9 +75,9 @@ func (r *Redis) TTL(key string) (time.Duration, error) {
|
|||||||
return res.Result()
|
return res.Result()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Redis) GetJSON(key string, value interface{}) error {
|
func (r *Redis) GetString(key string) (string, error) {
|
||||||
if !r.Enabled {
|
if !r.Enabled {
|
||||||
return nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
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)
|
res := r.Client.Get(ctx, key)
|
||||||
|
|
||||||
if err := res.Err(); err != nil {
|
if err := res.Err(); err != nil {
|
||||||
return err
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err := res.Bytes()
|
return res.Result()
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return json.Unmarshal(data, value)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Redis) GetBytes(key string) ([]byte, error) {
|
func (r *Redis) GetBytes(key string) ([]byte, error) {
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
@@ -23,8 +22,6 @@ func JavaStatusHandler(ctx *fiber.Ctx) error {
|
|||||||
host, port, err := ParseAddress(ctx.Params("address"), 25565)
|
host, port, err := ParseAddress(ctx.Params("address"), 25565)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
|
||||||
|
|
||||||
return ctx.Status(http.StatusBadRequest).SendString("Invalid address value")
|
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())))
|
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 {
|
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())))
|
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 {
|
func IconHandler(ctx *fiber.Ctx) error {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -76,20 +77,20 @@ type Mod struct {
|
|||||||
Version string `json:"version"`
|
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)
|
cacheKey := fmt.Sprintf("java:%s-%d", host, port)
|
||||||
|
|
||||||
cacheExists, err := r.Exists(cacheKey)
|
cacheExists, err := r.Exists(cacheKey)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if cacheExists {
|
if cacheExists {
|
||||||
var response JavaStatusResponse
|
response, err := r.GetString(cacheKey)
|
||||||
|
|
||||||
if err = r.GetJSON(cacheKey, &response); err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
expiresAt, err := r.TTL(cacheKey)
|
expiresAt, err := r.TTL(cacheKey)
|
||||||
@@ -97,29 +98,33 @@ func GetJavaStatus(host string, port uint16) (interface{}, *time.Duration, error
|
|||||||
return response, &expiresAt, err
|
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 {
|
if err != nil {
|
||||||
return nil, nil, err
|
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)
|
cacheKey := fmt.Sprintf("bedrock:%s-%d", host, port)
|
||||||
|
|
||||||
cacheExists, err := r.Exists(cacheKey)
|
cacheExists, err := r.Exists(cacheKey)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if cacheExists {
|
if cacheExists {
|
||||||
var response BedrockStatusResponse
|
response, err := r.GetString(cacheKey)
|
||||||
|
|
||||||
if err = r.GetJSON(cacheKey, &response); err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
expiresAt, err := r.TTL(cacheKey)
|
expiresAt, err := r.TTL(cacheKey)
|
||||||
@@ -127,13 +132,17 @@ func GetBedrockStatus(host string, port uint16) (interface{}, *time.Duration, er
|
|||||||
return response, &expiresAt, err
|
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 {
|
if err != nil {
|
||||||
return nil, nil, err
|
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) {
|
func GetServerIcon(host string, port uint16) ([]byte, error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user