Skip to content

Commit d02d4d9

Browse files
committed
feat(constants): add new constants for error, success, and info messages to improve code readability and maintainability
fix(main.go): replace hardcoded error messages with constants for better consistency and easier updates fix(handlers.go): update API version response to a fixed version string for clarity and consistency
1 parent ed8107c commit d02d4d9

File tree

3 files changed

+60
-26
lines changed

3 files changed

+60
-26
lines changed

cmd/api/main.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77

88
"github.com/anqorithm/naqa-api/internal/config"
9+
"github.com/anqorithm/naqa-api/internal/constants"
910
"github.com/anqorithm/naqa-api/internal/middleware"
1011
"github.com/anqorithm/naqa-api/internal/routes"
1112
"github.com/anqorithm/naqa-api/internal/seeders"
@@ -23,20 +24,20 @@ func main() {
2324
mongoConfig := config.NewMongoConfig()
2425
db, err := config.ConnectDB(mongoConfig)
2526
if err != nil {
26-
log.Fatal("Database Connection Error: ", err)
27+
log.Fatal(constants.ErrDatabaseConnection, err)
2728
}
2829

2930
// ###############################################################################
3031
// Data Seeding
3132
// ###############################################################################
3233
shouldSeed := strings.ToLower(os.Getenv("SEED_DATA")) == "true"
3334
if shouldSeed {
34-
log.Println("Starting data seeding process...")
35+
log.Println(constants.InfoStartingSeeding)
3536
if err := seeders.LoadDataSources(db); err != nil {
36-
log.Printf("Warning: Error seeding data: %v", err)
37+
log.Printf("Warning: %s: %v", constants.ErrDataSeeding, err)
3738
}
3839
} else {
39-
log.Println("Skipping data seeding (SEED_DATA is not set to 'true')")
40+
log.Println(constants.InfoSkippingSeeding)
4041
}
4142

4243
// ###############################################################################
@@ -68,6 +69,6 @@ func main() {
6869
port = "3000"
6970
}
7071

71-
log.Printf("Server starting on port %s", port)
72+
log.Printf(constants.InfoServerStarting, port)
7273
log.Fatal(app.Listen(":" + port))
7374
}

internal/constants/constants.go

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,60 @@ const DateFormat = "2006-01-02"
99

1010
// Error codes
1111
const (
12-
ErrCodeInvalidRequest = "INVALID_REQUEST"
13-
ErrCodeValidationFailed = "VALIDATION_FAILED"
14-
ErrCodeDatabaseError = "DATABASE_ERROR"
15-
ErrCodeNotFound = "NOT_FOUND"
16-
ErrCodeInvalidData = "INVALID_DATA"
12+
ErrCodeInvalidRequest = "INVALID_REQUEST"
13+
ErrCodeValidationFailed = "VALIDATION_FAILED"
14+
ErrCodeDatabaseError = "DATABASE_ERROR"
15+
ErrCodeNotFound = "NOT_FOUND"
16+
ErrCodeInvalidData = "INVALID_DATA"
1717
)
1818

1919
// Error messages
2020
const (
21-
MsgInvalidRequestBody = "Invalid request body"
22-
MsgValidationFailed = "Validation failed"
23-
MsgInvalidStartDateFormat = "Invalid start date format. Use YYYY-MM-DD."
24-
MsgInvalidEndDateFormat = "Invalid end date format. Use YYYY-MM-DD."
25-
MsgEndDateAfterStartDate = "End date must be greater than start date."
26-
MsgStockNotFound = "Stock not found"
27-
MsgInvalidPurificationRate = "Invalid purification rate in database"
21+
MsgInvalidRequestBody = "Invalid request body"
22+
MsgValidationFailed = "Validation failed"
23+
MsgInvalidStartDateFormat = "Invalid start date format. Use YYYY-MM-DD."
24+
MsgInvalidEndDateFormat = "Invalid end date format. Use YYYY-MM-DD."
25+
MsgEndDateAfterStartDate = "End date must be greater than start date."
26+
MsgStockNotFound = "Stock not found"
27+
MsgInvalidPurificationRate = "Invalid purification rate in database"
2828
)
2929

3030
// Available years for stock data
3131
var AvailableYears = []string{"2015", "2016", "2017", "2018", "2019", "2020", "2021", "2022", "2023"}
3232

3333
// Monitor titles
3434
const (
35-
MonitorTitleAr = "لوحة مراقبة نقاء"
36-
MonitorTitleEn = "NAQA Monitoring Dashboard"
35+
MonitorTitleAr = "لوحة مراقبة نقاء"
36+
MonitorTitleEn = "NAQA Monitoring Dashboard"
37+
)
38+
39+
// Error Messages
40+
const (
41+
ErrDatabaseConnection = "Database Connection Error"
42+
ErrDataSeeding = "Error seeding data"
43+
ErrInternalServer = "Internal Server Error"
44+
ErrInvalidInput = "Invalid input data"
45+
ErrNotFound = "Resource not found"
46+
ErrUnauthorized = "Unauthorized access"
47+
)
48+
49+
// Success Messages
50+
const (
51+
SuccessDataSeeded = "Data seeding completed successfully"
52+
SuccessServerStarted = "Server started successfully"
53+
SuccessHealthCheck = "Health check passed"
54+
)
55+
56+
// Info Messages
57+
const (
58+
InfoSkippingSeeding = "Skipping data seeding (SEED_DATA is not set to 'true')"
59+
InfoStartingSeeding = "Starting data seeding process..."
60+
InfoServerStarting = "Server starting on port %s"
61+
)
62+
63+
// Validation Messages
64+
const (
65+
ValidateRequiredField = "This field is required"
66+
ValidateInvalidEmail = "Invalid email format"
67+
ValidateInvalidInput = "Invalid input format"
3768
)

internal/handlers/handlers.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package handlers
22

3-
// ###############################################################################
4-
// Handler Functions
5-
// ###############################################################################
6-
73
import (
84
"fmt"
95
"os"
@@ -25,6 +21,10 @@ func NewHandler(db *mongo.Database) *Handler {
2521
}
2622
}
2723

24+
// ###############################################################################
25+
// Handler Functions
26+
// ###############################################################################
27+
2828
// RootHandler handles the "/" endpoint
2929
func (h *Handler) RootHandler(c *fiber.Ctx) error {
3030
return c.JSON(fiber.Map{
@@ -42,7 +42,7 @@ func (h *Handler) ApiV1Handler(c *fiber.Ctx) error {
4242
return c.JSON(fiber.Map{
4343
"status": "active",
4444
"message": "Welcome to NAQA API v1",
45-
"version": os.Getenv("API_VERSION"),
45+
"version": "1.0.0",
4646
"env": os.Getenv("ENVIRONMENT"),
4747
"server_time": time.Now().Format(time.RFC3339),
4848
"request_id": c.Get("X-Request-ID", uuid.New().String()),
@@ -57,8 +57,10 @@ func (h *Handler) ApiV1Handler(c *fiber.Ctx) error {
5757
// HealthCheckHandler handles the health check endpoint
5858
func (h *Handler) HealthCheckHandler(c *fiber.Ctx) error {
5959
return c.JSON(fiber.Map{
60-
"status": "OK",
61-
"timestamp": time.Now().Format(time.RFC3339),
60+
"status": "OK",
61+
"timestamp": time.Now().Format(time.RFC3339),
62+
"message": constants.SuccessHealthCheck,
63+
"request_id": c.Get("X-Request-ID", uuid.New().String()),
6264
})
6365
}
6466

0 commit comments

Comments
 (0)