diff --git a/src/main.go b/src/main.go index a2e4780..778b3b2 100644 --- a/src/main.go +++ b/src/main.go @@ -6,7 +6,6 @@ import ( "log" "net/http" "os" - "strconv" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/cors" @@ -80,18 +79,6 @@ func init() { } func main() { - if v := os.Getenv("PROFILE"); len(v) > 0 { - port, err := strconv.ParseUint(v, 10, 16) - - if err != nil { - panic(err) - } - - go Profile(uint16(port)) - - log.Printf("Profiler is listening on :%d\n", port) - } - defer r.Close() log.Printf("Listening on %s:%d\n", conf.Host, conf.Port+instanceID) diff --git a/src/profiler.go b/src/profiler.go deleted file mode 100644 index 9d50258..0000000 --- a/src/profiler.go +++ /dev/null @@ -1,15 +0,0 @@ -package main - -import ( - "fmt" - "log" - "net/http" - "net/http/pprof" -) - -func Profile(port uint16) { - mux := http.NewServeMux() - mux.HandleFunc("/", pprof.Profile) - - log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), mux)) -} diff --git a/src/routes.go b/src/routes.go index 3a19eec..e2cd434 100644 --- a/src/routes.go +++ b/src/routes.go @@ -1,7 +1,6 @@ package main import ( - "encoding/json" "fmt" "main/src/assets" "net/http" @@ -21,11 +20,6 @@ func init() { app.Get("/icon", DefaultIconHandler) app.Get("/icon/:address", IconHandler) app.Post("/vote", SendVoteHandler) - app.Get("/debug/java/:address", DebugJavaStatusHandler) - app.Get("/debug/legacy/:address", DebugLegacyStatusHandler) - app.Get("/debug/bedrock/:address", DebugBedrockStatusHandler) - app.Get("/debug/query/basic/:address", DebugBasicQueryHandler) - app.Get("/debug/query/full/:address", DebugFullQueryHandler) app.Use(NotFoundHandler) } @@ -150,141 +144,6 @@ func DefaultIconHandler(ctx *fiber.Ctx) error { return ctx.Type("png").Send(assets.DefaultIcon) } -// DebugJavaStatusHandler returns the status of the Java edition Minecraft server specified in the address parameter. -func DebugJavaStatusHandler(ctx *fiber.Ctx) error { - host, port, err := ParseAddress(ctx.Params("address"), 25565) - - if err != nil { - return ctx.Status(http.StatusBadRequest).SendString("Invalid address value") - } - - if err = r.Increment(fmt.Sprintf("java-hits:%s-%d", host, port)); err != nil { - return err - } - - result, err := mcutil.StatusRaw(host, port) - - if err != nil { - return ctx.Status(http.StatusBadRequest).SendString(err.Error()) - } - - data, err := json.MarshalIndent(result, "", " ") - - if err != nil { - return err - } - - return ctx.Type("json").Send(data) -} - -// DebugJavaStatusHandler returns the legacy status of the Java edition Minecraft server specified in the address parameter. -func DebugLegacyStatusHandler(ctx *fiber.Ctx) error { - host, port, err := ParseAddress(ctx.Params("address"), 25565) - - if err != nil { - return ctx.Status(http.StatusBadRequest).SendString("Invalid address value") - } - - if err = r.Increment(fmt.Sprintf("java-hits:%s-%d", host, port)); err != nil { - return err - } - - result, err := mcutil.StatusLegacy(host, port) - - if err != nil { - return ctx.Status(http.StatusBadRequest).SendString(err.Error()) - } - - data, err := json.MarshalIndent(result, "", " ") - - if err != nil { - return err - } - - return ctx.Type("json").Send(data) -} - -// DebugBedrockStatusHandler returns the status of the Bedrock edition Minecraft server specified in the address parameter. -func DebugBedrockStatusHandler(ctx *fiber.Ctx) error { - host, port, err := ParseAddress(ctx.Params("address"), 19132) - - if err != nil { - return ctx.Status(http.StatusBadRequest).SendString("Invalid address value") - } - - if err = r.Increment(fmt.Sprintf("bedrock-hits:%s-%d", host, port)); err != nil { - return err - } - - result, err := mcutil.StatusBedrock(host, port) - - if err != nil { - return ctx.Status(http.StatusBadRequest).SendString(err.Error()) - } - - data, err := json.MarshalIndent(result, "", " ") - - if err != nil { - return err - } - - return ctx.Type("json").Send(data) -} - -// DebugBasicQueryHandler returns the basic query information of the Java edition Minecraft server specified in the address parameter. -func DebugBasicQueryHandler(ctx *fiber.Ctx) error { - host, port, err := ParseAddress(ctx.Params("address"), 25565) - - if err != nil { - return ctx.Status(http.StatusBadRequest).SendString("Invalid address value") - } - - if err = r.Increment(fmt.Sprintf("bedrock-hits:%s-%d", host, port)); err != nil { - return err - } - - result, err := mcutil.BasicQuery(host, port) - - if err != nil { - return ctx.Status(http.StatusBadRequest).SendString(err.Error()) - } - - data, err := json.MarshalIndent(result, "", " ") - - if err != nil { - return err - } - - return ctx.Type("json").Send(data) -} - -// DebugFullQueryHandler returns the full query information of the Java edition Minecraft server specified in the address parameter. -func DebugFullQueryHandler(ctx *fiber.Ctx) error { - host, port, err := ParseAddress(ctx.Params("address"), 25565) - - if err != nil { - return ctx.Status(http.StatusBadRequest).SendString("Invalid address value") - } - - if err = r.Increment(fmt.Sprintf("bedrock-hits:%s-%d", host, port)); err != nil { - return err - } - - result, err := mcutil.FullQuery(host, port) - - if err != nil { - return ctx.Status(http.StatusBadRequest).SendString(err.Error()) - } - - data, err := json.MarshalIndent(result, "", " ") - - if err != nil { - return err - } - - return ctx.Type("json").Send(data) -} - // NotFoundHandler handles requests to routes that do not exist and returns a 404 Not Found status. func NotFoundHandler(ctx *fiber.Ctx) error { return ctx.SendStatus(http.StatusNotFound) diff --git a/src/status.go b/src/status.go index 5a7ebb0..ff6777d 100644 --- a/src/status.go +++ b/src/status.go @@ -16,8 +16,8 @@ import ( "github.com/mcstatus-io/mcutil/response" ) -// StatusResponse is the root response for returning any status response from the API. -type StatusResponse struct { +// BaseStatus is the base response properties for returning any status response from the API. +type BaseStatus struct { Online bool `json:"online"` Host string `json:"host"` Port uint16 `json:"port"` @@ -28,7 +28,7 @@ type StatusResponse struct { // JavaStatusResponse is the combined response of the root response and the Java Edition status response. type JavaStatusResponse struct { - StatusResponse + BaseStatus *JavaStatus } @@ -45,7 +45,7 @@ type JavaStatus struct { // BedrockStatusResponse is the combined response of the root response and the Bedrock Edition status response. type BedrockStatusResponse struct { - StatusResponse + BaseStatus *BedrockStatus } @@ -308,7 +308,7 @@ func FetchBedrockStatus(host string, port uint16) *BedrockStatusResponse { if err != nil { return &BedrockStatusResponse{ - StatusResponse: StatusResponse{ + BaseStatus: BaseStatus{ Online: false, Host: host, Port: port, @@ -320,7 +320,7 @@ func FetchBedrockStatus(host string, port uint16) *BedrockStatusResponse { } response := &BedrockStatusResponse{ - StatusResponse: StatusResponse{ + BaseStatus: BaseStatus{ Online: true, Host: host, Port: port, @@ -396,7 +396,7 @@ func FetchBedrockStatus(host string, port uint16) *BedrockStatusResponse { // BuildJavaResponse builds the response data from the status and query information. func BuildJavaResponse(host string, port uint16, status interface{}, query *response.FullQuery) (result JavaStatusResponse) { result = JavaStatusResponse{ - StatusResponse: StatusResponse{ + BaseStatus: BaseStatus{ Online: status != nil || query != nil, Host: host, Port: port,