From 3e33e2fb0b14c222b2e89847cda47d1b7fe8be2d Mon Sep 17 00:00:00 2001 From: Abdullah Alqahtani <0xqahta0@gmail.com> Date: Sat, 28 Dec 2024 21:42:46 +0300 Subject: [PATCH] feat(api): update metrics endpoint to /api/v1/metrics for better versioning feat(constants): add monitor titles in Arabic and English for localization refactor(middleware): enhance Monitor middleware to use localized titles docs(seeders): add comments for stock data seeder for better clarity docs(utils): add comments for utility functions for better understanding --- cmd/api/main.go | 2 +- internal/constants/constants.go | 8 +++++++- internal/handlers/handlers.go | 1 + internal/middleware/middleware.go | 17 ++++++++++------- internal/seeders/seeders.go | 4 ++++ internal/utils/utils.go | 6 ++++++ 6 files changed, 29 insertions(+), 9 deletions(-) diff --git a/cmd/api/main.go b/cmd/api/main.go index d7eeba4..dc31d6d 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -55,7 +55,7 @@ func main() { app.Use(middleware.Compress()) app.Use(middleware.CORS()) app.Use(middleware.RateLimit()) - app.Use("/metrics", middleware.Monitor()) + app.Use("/api/v1/metrics", middleware.Monitor()) // ############################################################################### // Router Setup and Server Startup diff --git a/internal/constants/constants.go b/internal/constants/constants.go index 54f42ab..d3290cc 100644 --- a/internal/constants/constants.go +++ b/internal/constants/constants.go @@ -28,4 +28,10 @@ const ( ) // Available years for stock data -var AvailableYears = []string{"2015", "2016", "2017", "2018", "2019", "2020", "2021", "2022", "2023"} \ No newline at end of file +var AvailableYears = []string{"2015", "2016", "2017", "2018", "2019", "2020", "2021", "2022", "2023"} + +// Monitor titles +const ( + MonitorTitleAr = "لوحة مراقبة نقاء" + MonitorTitleEn = "NAQA Monitoring Dashboard" +) \ No newline at end of file diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index 4084083..2f844f8 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -49,6 +49,7 @@ func (h *Handler) ApiV1Handler(c *fiber.Ctx) error { "endpoints": []string{ "/api/v1/stocks", "/api/v1/health", + "/api/v1/metrics", }, }) } diff --git a/internal/middleware/middleware.go b/internal/middleware/middleware.go index 373e8cf..8eca0a6 100644 --- a/internal/middleware/middleware.go +++ b/internal/middleware/middleware.go @@ -1,8 +1,10 @@ package middleware import ( + "fmt" "time" + "github.com/anqorithm/naqa-api/internal/constants" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/compress" "github.com/gofiber/fiber/v2/middleware/cors" @@ -54,11 +56,12 @@ func RateLimit() fiber.Handler { }) } -// Monitor returns an enhanced dashboard monitoring middleware +// Monitor returns a monitor middleware func Monitor() fiber.Handler { - return monitor.New(monitor.Config{ - Title: "Naqa API - System Metrics", - Refresh: 3 * time.Second, // Refresh metrics every 3 seconds - APIOnly: true, // Monitor only API routes - }) -} + return monitor.New(monitor.Config{ + Title: fmt.Sprintf("%s | %s", constants.MonitorTitleAr, constants.MonitorTitleEn), + Refresh: 3 * time.Second, + Next: nil, + APIOnly: false, + }) +} \ No newline at end of file diff --git a/internal/seeders/seeders.go b/internal/seeders/seeders.go index d086d4b..b964cf0 100644 --- a/internal/seeders/seeders.go +++ b/internal/seeders/seeders.go @@ -1,5 +1,9 @@ package seeders +// ############################################################################### +// Stock Data Seeder +// ############################################################################### + import ( "context" "encoding/json" diff --git a/internal/utils/utils.go b/internal/utils/utils.go index fd9cbb7..28e112e 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -1,11 +1,16 @@ package utils +// ############################################################################### +// Utility Functions +// ############################################################################### + import ( "fmt" "github.com/go-playground/validator/v10" ) +// SafeString converts an interface to a string func SafeString(v interface{}) string { switch val := v.(type) { case string: @@ -19,6 +24,7 @@ func SafeString(v interface{}) string { } } +// ValidateRequest validates the request using the validator package var validate *validator.Validate func init() {