From b68f626ce275d9b072c4e083284746c107bcdcc8 Mon Sep 17 00:00:00 2001 From: Christian Ditaputratama Date: Wed, 6 Nov 2024 22:15:53 +0700 Subject: [PATCH] refactor!: Use function method for routes Will be useful for future development using standard `net/http`. --- internal/handler/middlewares.go | 3 ++- internal/handler/response.go | 34 ++++++++++++++++----------------- internal/handler/routes.go | 16 ++++++++-------- 3 files changed, 26 insertions(+), 27 deletions(-) diff --git a/internal/handler/middlewares.go b/internal/handler/middlewares.go index ec55dce..b8599e1 100644 --- a/internal/handler/middlewares.go +++ b/internal/handler/middlewares.go @@ -6,7 +6,8 @@ import ( "github.com/gofiber/fiber/v2" ) -func CheckProber(c *fiber.Ctx) error { +// checkProberMW is a middleware to check prober API key +func (s *fiberServer) checkProberMW(c *fiber.Ctx) error { key := c.Get(monero.ProberAPIKey) if key == "" { return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ diff --git a/internal/handler/response.go b/internal/handler/response.go index 4f31f9b..89e28af 100644 --- a/internal/handler/response.go +++ b/internal/handler/response.go @@ -85,8 +85,8 @@ func (s *fiberServer) addNodeHandler(c *fiber.Ctx) error { return handler(c) } -// Returns a single node information based on `id` query param -func Node(c *fiber.Ctx) error { +// Returns a single node information based on `id` query param (API endpoint, JSON data) +func (s *fiberServer) nodeAPI(c *fiber.Ctx) error { nodeId, err := c.ParamsInt("id", 0) if err != nil { return c.Status(fiber.StatusUnprocessableEntity).JSON(fiber.Map{ @@ -264,8 +264,8 @@ func (s *fiberServer) nodeHandler(c *fiber.Ctx) error { return handler(c) } -// Returns a list of nodes (API) -func Nodes(c *fiber.Ctx) error { +// Returns list of nodes (API endpoint, JSON data) +func (s *fiberServer) nodesAPI(c *fiber.Ctx) error { moneroRepo := monero.New() query := monero.QueryNodes{ Paging: paging.Paging{ @@ -299,10 +299,8 @@ func Nodes(c *fiber.Ctx) error { }) } -// Returns probe logs reported by nodes -// -// The embadded web UI use `node_id` query param to filter logs -func ProbeLogs(c *fiber.Ctx) error { +// Returns probe logs reported by nodes (API endpoint, JSON data) +func (s *fiberServer) probeLogsAPI(c *fiber.Ctx) error { moneroRepo := monero.New() query := monero.QueryLogs{ Paging: paging.Paging{ @@ -336,7 +334,7 @@ func ProbeLogs(c *fiber.Ctx) error { // Handles `POST /nodes` request to add a new node // // Deprecated: AddNode is deprecated, use s.addNodeHandler with put method instead -func AddNode(c *fiber.Ctx) error { +func (s *fiberServer) addNodeAPI(c *fiber.Ctx) error { formPort := c.FormValue("port") port, err := strconv.Atoi(formPort) if err != nil { @@ -366,8 +364,8 @@ func AddNode(c *fiber.Ctx) error { }) } -// Returns majority network fees -func NetFees(c *fiber.Ctx) error { +// Returns majority network fees (API endpoint, JSON data) +func (s *fiberServer) netFeesAPI(c *fiber.Ctx) error { moneroRepo := monero.New() return c.JSON(fiber.Map{ "status": "ok", @@ -376,8 +374,8 @@ func NetFees(c *fiber.Ctx) error { }) } -// Returns list of countries (count by nodes) -func Countries(c *fiber.Ctx) error { +// Returns list of countries, count by nodes (API endpoint, JSON data) +func (s *fiberServer) countriesAPI(c *fiber.Ctx) error { moneroRepo := monero.New() countries, err := moneroRepo.Countries() if err != nil { @@ -394,10 +392,10 @@ func Countries(c *fiber.Ctx) error { }) } -// Returns node to be probed by the client (prober) +// Returns node to be probed by the prober (API endpoint, JSON data) // -// This handler should protected by `CheckProber` middleware. -func GiveJob(c *fiber.Ctx) error { +// This handler should protected by `s.checkProberMW` middleware. +func (s *fiberServer) giveJobAPI(c *fiber.Ctx) error { acceptTor := c.QueryInt("accept_tor", 0) acceptIPv6 := c.QueryInt("accept_ipv6", 0) @@ -418,10 +416,10 @@ func GiveJob(c *fiber.Ctx) error { }) } -// Handles probe report submission by the prober +// Handles probe report submission by the prober (API endpoint, JSON data) // // This handler should protected by `CheckProber` middleware. -func ProcessJob(c *fiber.Ctx) error { +func (s *fiberServer) processJobAPI(c *fiber.Ctx) error { var report monero.ProbeReport if err := c.BodyParser(&report); err != nil { diff --git a/internal/handler/routes.go b/internal/handler/routes.go index 12cf8db..0030cd7 100644 --- a/internal/handler/routes.go +++ b/internal/handler/routes.go @@ -15,14 +15,14 @@ func (s *fiberServer) Routes() { v1 := s.App.Group("/api/v1") // these routes are public, they don't require a prober api key - v1.Get("/nodes", Nodes) - v1.Post("/nodes", AddNode) // old add node form action endpoint. Deprecated: Use PUT /add-node instead - v1.Get("/nodes/id/:id", Node) - v1.Get("/nodes/logs", ProbeLogs) - v1.Get("/fees", NetFees) - v1.Get("/countries", Countries) + v1.Get("/nodes", s.nodesAPI) + v1.Post("/nodes", s.addNodeAPI) // old add node form action endpoint. Deprecated: Use PUT /add-node instead + v1.Get("/nodes/id/:id", s.nodeAPI) + v1.Get("/nodes/logs", s.probeLogsAPI) + v1.Get("/fees", s.netFeesAPI) + v1.Get("/countries", s.countriesAPI) // these routes are for prober, they require a prober api key - v1.Get("/job", CheckProber, GiveJob) - v1.Post("/job", CheckProber, ProcessJob) + v1.Get("/job", s.checkProberMW, s.giveJobAPI) + v1.Post("/job", s.checkProberMW, s.processJobAPI) }