xmr-remote-nodes/handler/response.go

207 lines
4.6 KiB
Go
Raw Normal View History

package handler
import (
2024-05-04 10:24:47 +00:00
"strconv"
"xmr-remote-nodes/internal/database"
"xmr-remote-nodes/internal/monero"
"github.com/gofiber/fiber/v2"
)
2024-05-06 18:08:01 +00:00
func MoneroNode(c *fiber.Ctx) error {
nodeId, err := c.ParamsInt("id", 0)
if err != nil {
return c.Status(fiber.StatusUnprocessableEntity).JSON(fiber.Map{
"status": "error",
"message": err.Error(),
"data": nil,
})
}
if nodeId == 0 {
return c.Status(fiber.StatusUnprocessableEntity).JSON(fiber.Map{
"status": "error",
"message": "Invalid node id",
"data": nil,
})
}
moneroRepo := monero.NewMoneroRepo(database.GetDB())
2024-05-06 18:08:01 +00:00
node, err := moneroRepo.Node(nodeId)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"status": "error",
"message": err.Error(),
"data": nil,
})
}
return c.JSON(fiber.Map{
"status": "ok",
"message": "Success",
"data": node,
})
}
func MoneroNodes(c *fiber.Ctx) error {
moneroRepo := monero.NewMoneroRepo(database.GetDB())
query := monero.MoneroQueryParams{
RowsPerPage: c.QueryInt("limit", 10),
Page: c.QueryInt("page", 1),
SortBy: c.Query("sort_by", "id"),
SortDirection: c.Query("sort_direction", "desc"),
Host: c.Query("host"),
Nettype: c.Query("nettype", "any"),
Protocol: c.Query("protocol", "any"),
CC: c.Query("cc", "any"),
Status: c.QueryInt("status", -1),
CORS: c.QueryInt("cors", -1),
}
nodes, err := moneroRepo.Nodes(query)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"status": "error",
"message": err.Error(),
"data": nil,
})
}
return c.JSON(fiber.Map{
"status": "ok",
"message": "Success",
"data": nodes,
})
}
2024-05-06 10:19:17 +00:00
func ProbeLogs(c *fiber.Ctx) error {
moneroRepo := monero.NewMoneroRepo(database.GetDB())
query := monero.MoneroLogQueryParams{
2024-05-06 10:19:17 +00:00
RowsPerPage: c.QueryInt("limit", 10),
Page: c.QueryInt("page", 1),
SortBy: c.Query("sort_by", "id"),
SortDirection: c.Query("sort_direction", "desc"),
NodeID: c.QueryInt("node_id", 0),
Status: c.QueryInt("status", -1),
FailedReason: c.Query("failed_reason"),
2024-05-06 10:19:17 +00:00
}
logs, err := moneroRepo.Logs(query)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"status": "error",
"message": err.Error(),
"data": nil,
})
}
return c.JSON(fiber.Map{
"status": "ok",
"message": "Success",
"data": logs,
})
}
2024-05-04 10:24:47 +00:00
func AddNode(c *fiber.Ctx) error {
formPort := c.FormValue("port")
port, err := strconv.Atoi(formPort)
if err != nil {
return c.JSON(fiber.Map{
"status": "error",
"message": "Invalid port number",
"data": nil,
})
}
protocol := c.FormValue("protocol")
hostname := c.FormValue("hostname")
moneroRepo := monero.NewMoneroRepo(database.GetDB())
2024-05-04 10:24:47 +00:00
if err := moneroRepo.Add(protocol, hostname, uint(port)); err != nil {
return c.JSON(fiber.Map{
"status": "error",
"message": err.Error(),
"data": nil,
})
}
return c.JSON(fiber.Map{
"status": "ok",
"message": "Query Ok",
"data": nil,
})
}
func NetFee(c *fiber.Ctx) error {
moneroRepo := monero.NewMoneroRepo(database.GetDB())
return c.JSON(fiber.Map{
"status": "ok",
"message": "Success",
"data": moneroRepo.NetFee(),
})
}
func Countries(c *fiber.Ctx) error {
moneroRepo := monero.NewMoneroRepo(database.GetDB())
countries, err := moneroRepo.Countries()
if err != nil {
return c.JSON(fiber.Map{
"status": "error",
"message": err.Error(),
"data": nil,
})
}
return c.JSON(fiber.Map{
"status": "ok",
"message": "Success",
"data": countries,
})
}
2024-05-04 12:27:21 +00:00
func GiveJob(c *fiber.Ctx) error {
acceptTor := c.QueryInt("accept_tor", 0)
moneroRepo := monero.NewMoneroRepo(database.GetDB())
2024-05-04 12:27:21 +00:00
node, err := moneroRepo.GiveJob(acceptTor)
if err != nil {
return c.JSON(fiber.Map{
"status": "error",
"message": err.Error(),
"data": nil,
})
}
return c.JSON(fiber.Map{
"status": "ok",
"message": "Success",
"data": node,
})
}
2024-05-04 18:42:47 +00:00
func ProcessJob(c *fiber.Ctx) error {
moneroRepo := monero.NewMoneroRepo(database.GetDB())
report := monero.ProbeReport{}
2024-05-04 18:42:47 +00:00
if err := c.BodyParser(&report); err != nil {
return c.Status(fiber.StatusUnprocessableEntity).JSON(fiber.Map{
"status": "error",
"message": err.Error(),
"data": nil,
})
}
if err := moneroRepo.ProcessJob(report, c.Locals("prober_id").(int64)); err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"status": "error",
"message": err.Error(),
"data": nil,
})
}
return c.JSON(fiber.Map{
"status": "ok",
"message": "Success",
"data": nil,
})
}