diff --git a/handler/response.go b/handler/response.go index cc26a13..8d44546 100644 --- a/handler/response.go +++ b/handler/response.go @@ -115,6 +115,41 @@ func Prober(c *fiber.Ctx) error { }) } +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 := repo.NewMoneroRepo(database.GetDB()) + 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 := repo.NewMoneroRepo(database.GetDB()) query := repo.MoneroQueryParams{ diff --git a/handler/routes.go b/handler/routes.go index 64a5058..5113db3 100644 --- a/handler/routes.go +++ b/handler/routes.go @@ -16,6 +16,7 @@ func V1Api(app *fiber.App) { v1.Post("/prober", Prober) v1.Get("/nodes", MoneroNodes) v1.Post("/nodes", AddNode) + v1.Get("/nodes/id/:id", MoneroNode) v1.Get("/nodes/logs", ProbeLogs) v1.Get("/fees", NetFee) v1.Get("/countries", Countries) diff --git a/internal/repo/monero.go b/internal/repo/monero.go index 8e454e3..45db173 100644 --- a/internal/repo/monero.go +++ b/internal/repo/monero.go @@ -1,6 +1,7 @@ package repo import ( + "database/sql" "encoding/json" "errors" "fmt" @@ -16,6 +17,7 @@ import ( ) type MoneroRepository interface { + Node(id int) (MoneroNode, error) Add(protocol string, host string, port uint) error Nodes(q MoneroQueryParams) (MoneroNodes, error) GiveJob(acceptTor int) (MoneroNode, error) @@ -64,6 +66,19 @@ type MoneroNode struct { CorsCapable bool `json:"cors" db:"cors_capable"` } +func (repo *MoneroRepo) Node(id int) (MoneroNode, error) { + node := MoneroNode{} + err := repo.db.Get(&node, `SELECT * FROM tbl_node WHERE id = ?`, id) + if err != nil && err != sql.ErrNoRows { + fmt.Println("WARN:", err) + return node, errors.New("Can't get node information") + } + if err == sql.ErrNoRows { + return node, errors.New("Node not found") + } + return node, err +} + type MoneroNodes struct { TotalRows int `json:"total_rows"` RowsPerPage int `json:"rows_per_page"`