Add votifier testing route
This commit is contained in:
parent
6d996138a3
commit
e5ef98aa59
@ -4,18 +4,34 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"github.com/mcstatus-io/mcutil"
|
||||||
|
"github.com/mcstatus-io/mcutil/options"
|
||||||
"github.com/mcstatus-io/shared/status"
|
"github.com/mcstatus-io/shared/status"
|
||||||
"github.com/mcstatus-io/shared/util"
|
"github.com/mcstatus-io/shared/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type SendVoteBody struct {
|
||||||
|
Host string `json:"host"`
|
||||||
|
Port uint16 `json:"port"`
|
||||||
|
Username string `json:"username"`
|
||||||
|
Token string `json:"token"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type SendVoteResponse struct {
|
||||||
|
Success bool `json:"success"`
|
||||||
|
Error string `json:"error,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
app.Get("/ping", PingHandler)
|
app.Get("/ping", PingHandler)
|
||||||
app.Get("/status/java/:address", JavaStatusHandler)
|
app.Get("/status/java/:address", JavaStatusHandler)
|
||||||
app.Get("/status/bedrock/:address", BedrockStatusHandler)
|
app.Get("/status/bedrock/:address", BedrockStatusHandler)
|
||||||
app.Get("/icon/default", DefaultIconHandler)
|
app.Get("/icon/default", DefaultIconHandler)
|
||||||
app.Get("/icon/:address", IconHandler)
|
app.Get("/icon/:address", IconHandler)
|
||||||
|
app.Post("/vote", SendVoteHandler)
|
||||||
app.Use(NotFoundHandler)
|
app.Use(NotFoundHandler)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,6 +111,32 @@ func DefaultIconHandler(ctx *fiber.Ctx) error {
|
|||||||
return ctx.Type("png").Send(util.DefaultIconBytes)
|
return ctx.Type("png").Send(util.DefaultIconBytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SendVoteHandler(ctx *fiber.Ctx) error {
|
||||||
|
var body SendVoteBody
|
||||||
|
|
||||||
|
if err := ctx.BodyParser(&body); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := mcutil.SendVote(body.Host, body.Port, options.Vote{
|
||||||
|
ServiceName: "mcstatus.io Vote Tester",
|
||||||
|
Username: body.Username,
|
||||||
|
Token: body.Token,
|
||||||
|
UUID: "",
|
||||||
|
Timestamp: time.Now(),
|
||||||
|
Timeout: time.Second * 5,
|
||||||
|
}); err != nil {
|
||||||
|
return ctx.JSON(SendVoteResponse{
|
||||||
|
Success: false,
|
||||||
|
Error: err.Error(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return ctx.JSON(SendVoteResponse{
|
||||||
|
Success: true,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func NotFoundHandler(ctx *fiber.Ctx) error {
|
func NotFoundHandler(ctx *fiber.Ctx) error {
|
||||||
return ctx.SendStatus(http.StatusNotFound)
|
return ctx.SendStatus(http.StatusNotFound)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user