99 lines
2.1 KiB
Go
99 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
func init() {
|
|
app.Get("/ping", PingHandler)
|
|
app.Get("/status/java/:address", JavaStatusHandler)
|
|
app.Get("/status/bedrock/:address", BedrockStatusHandler)
|
|
app.Get("/icon", DefaultIconHandler)
|
|
app.Get("/icon/:address", IconHandler)
|
|
app.Use(NotFoundHandler)
|
|
}
|
|
|
|
func PingHandler(ctx *fiber.Ctx) error {
|
|
return ctx.SendStatus(http.StatusOK)
|
|
}
|
|
|
|
func JavaStatusHandler(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
|
|
}
|
|
|
|
response, expiresAt, err := GetJavaStatus(host, port)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if expiresAt != nil {
|
|
ctx.Set("X-Cache-Time-Remaining", strconv.Itoa(int(expiresAt.Seconds())))
|
|
}
|
|
|
|
return ctx.JSON(response)
|
|
}
|
|
|
|
func BedrockStatusHandler(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
|
|
}
|
|
|
|
response, expiresAt, err := GetBedrockStatus(host, port)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if expiresAt != nil {
|
|
ctx.Set("X-Cache-Time-Remaining", strconv.Itoa(int(expiresAt.Seconds())))
|
|
}
|
|
|
|
return ctx.JSON(response)
|
|
}
|
|
|
|
func IconHandler(ctx *fiber.Ctx) error {
|
|
host, port, err := ParseAddress(ctx.Params("address"), 25565)
|
|
|
|
if err != nil {
|
|
return ctx.Status(http.StatusBadRequest).SendString("Invalid address value")
|
|
}
|
|
|
|
icon, expiresAt, err := GetServerIcon(host, port)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if expiresAt != nil {
|
|
ctx.Set("X-Cache-Time-Remaining", strconv.Itoa(int(expiresAt.Seconds())))
|
|
}
|
|
|
|
return ctx.Type("png").Send(icon)
|
|
}
|
|
|
|
func DefaultIconHandler(ctx *fiber.Ctx) error {
|
|
return ctx.Type("png").Send(defaultIcon)
|
|
}
|
|
|
|
func NotFoundHandler(ctx *fiber.Ctx) error {
|
|
return ctx.SendStatus(http.StatusNotFound)
|
|
}
|