From d2acd8c31ff14c619ee91ec3d003cab210cb5ee0 Mon Sep 17 00:00:00 2001 From: its-ernest Date: Tue, 14 Apr 2026 13:01:42 +0000 Subject: [PATCH] style: update formatting to pass gofmt and improve Go Report Card --- .../internal/auth/handler.go | 114 +++++++++--------- .../internal/middleware/otpcache.go | 10 +- .../internal/service/auth_service.go | 10 +- .../internal/utils/generator.go | 2 +- 4 files changed, 68 insertions(+), 68 deletions(-) diff --git a/_examples/cache/otp_code_verification/internal/auth/handler.go b/_examples/cache/otp_code_verification/internal/auth/handler.go index 247bb44..4000e12 100644 --- a/_examples/cache/otp_code_verification/internal/auth/handler.go +++ b/_examples/cache/otp_code_verification/internal/auth/handler.go @@ -1,37 +1,37 @@ package auth import ( - "net/http" - "time" + "net/http" + "time" - "otp-backend/internal/middleware" - "otp-backend/internal/service" - "otp-backend/internal/utils" + "otp-backend/internal/middleware" + "otp-backend/internal/service" + "otp-backend/internal/utils" - "github.com/its-ernest/echox/store" - "github.com/labstack/echo/v5" - "github.com/golang-jwt/jwt/v5" + "github.com/golang-jwt/jwt/v5" + "github.com/its-ernest/echox/store" + "github.com/labstack/echo/v5" ) type Handler struct { - service *service.AuthService - jwtSecret []byte + service *service.AuthService + jwtSecret []byte } func NewHandler(s *service.AuthService, secret []byte) *Handler { - return &Handler{ - service: s, - jwtSecret: secret, - } + return &Handler{ + service: s, + jwtSecret: secret, + } } type requestOTP struct { - Phone string `json:"phone"` + Phone string `json:"phone"` } type verifyOTP struct { - Phone string `json:"phone"` - Code string `json:"code"` + Phone string `json:"phone"` + Code string `json:"code"` } func (h *Handler) Register(g *echo.Group, store store.Store) { @@ -40,55 +40,55 @@ func (h *Handler) Register(g *echo.Group, store store.Store) { } func (h *Handler) requestOTP(c *echo.Context) error { - req := new(requestOTP) - if err := c.Bind(req); err != nil { - return echo.NewHTTPError(http.StatusBadRequest, "Invalid JSON") - } - - // number validation - if req.Phone == "" { - return echo.NewHTTPError(http.StatusBadRequest, "Phone number is required") - } + req := new(requestOTP) + if err := c.Bind(req); err != nil { + return echo.NewHTTPError(http.StatusBadRequest, "Invalid JSON") + } + + // number validation + if req.Phone == "" { + return echo.NewHTTPError(http.StatusBadRequest, "Phone number is required") + } // format for es64 format - phone := utils.FormatPhone(req.Phone) + phone := utils.FormatPhone(req.Phone) // utilize auth_service.go's RequestOTP function - if err := h.service.RequestOTP(c.Request().Context(), phone); err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "OTP failed") - } + if err := h.service.RequestOTP(c.Request().Context(), phone); err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, "OTP failed") + } - return c.JSON(http.StatusOK, map[string]string{"message": "OTP sent"}) + return c.JSON(http.StatusOK, map[string]string{"message": "OTP sent"}) } func (h *Handler) verifyOTP(c *echo.Context) error { // validate JSON request - req := new(verifyOTP) - if err := c.Bind(req); err != nil { - return echo.NewHTTPError(http.StatusBadRequest, "Invalid JSON") - } - + req := new(verifyOTP) + if err := c.Bind(req); err != nil { + return echo.NewHTTPError(http.StatusBadRequest, "Invalid JSON") + } + // verify OTP stored in echox cache - phone := utils.FormatPhone(req.Phone) - if err := h.service.VerifyOTP(c.Request().Context(), phone, req.Code); err != nil { - return echo.NewHTTPError(http.StatusUnauthorized, err.Error()) - } - - // create and hand over a jwt afterwards - claims := &jwt.RegisteredClaims{ - Subject: phone, - ExpiresAt: jwt.NewNumericDate(time.Now().Add(24 * time.Hour)), - IssuedAt: jwt.NewNumericDate(time.Now()), - } + phone := utils.FormatPhone(req.Phone) + if err := h.service.VerifyOTP(c.Request().Context(), phone, req.Code); err != nil { + return echo.NewHTTPError(http.StatusUnauthorized, err.Error()) + } + + // create and hand over a jwt afterwards + claims := &jwt.RegisteredClaims{ + Subject: phone, + ExpiresAt: jwt.NewNumericDate(time.Now().Add(24 * time.Hour)), + IssuedAt: jwt.NewNumericDate(time.Now()), + } // sign jwt - token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) - t, err := token.SignedString(h.jwtSecret) - if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "Could not generate token") - } + token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) + t, err := token.SignedString(h.jwtSecret) + if err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, "Could not generate token") + } // deliver jwt - return c.JSON(http.StatusOK, map[string]string{ - "message": "Verified", - "token": t, - }) -} \ No newline at end of file + return c.JSON(http.StatusOK, map[string]string{ + "message": "Verified", + "token": t, + }) +} diff --git a/_examples/cache/otp_code_verification/internal/middleware/otpcache.go b/_examples/cache/otp_code_verification/internal/middleware/otpcache.go index 5bc09c9..068b3a1 100644 --- a/_examples/cache/otp_code_verification/internal/middleware/otpcache.go +++ b/_examples/cache/otp_code_verification/internal/middleware/otpcache.go @@ -11,11 +11,11 @@ import ( // OTPCache uses my echox MemoryStore to prevent OTP spamming func OTPCache(store store.Store) echo.MiddlewareFunc { return cache.New(cache.Config{ - Store: store, - TTL: 5 * time.Minute, // expire after 5 mins - MaxBodySize: 1024 * 10, // 10KB safety limit + Store: store, + TTL: 5 * time.Minute, // expire after 5 mins + MaxBodySize: 1024 * 10, // 10KB safety limit // key-generator for cache values - // this one uses a generator specific to a phone number. + // this one uses a generator specific to a phone number. //for e.g, 'otp_lock:+23324512XXXX', 'otp_lock:+23350348XXXX' // each of the keys above is assigned a distinct generated values from other parts of the code KeyGenerator: func(c *echo.Context) string { @@ -24,4 +24,4 @@ func OTPCache(store store.Store) echo.MiddlewareFunc { return "otp_lock:" + phone }, }) -} \ No newline at end of file +} diff --git a/_examples/cache/otp_code_verification/internal/service/auth_service.go b/_examples/cache/otp_code_verification/internal/service/auth_service.go index 1c87cf1..4405abc 100644 --- a/_examples/cache/otp_code_verification/internal/service/auth_service.go +++ b/_examples/cache/otp_code_verification/internal/service/auth_service.go @@ -3,12 +3,12 @@ package service import ( "context" "errors" - "time" - "log" "fmt" + "log" + "time" - "otp-backend/internal/utils" "github.com/its-ernest/echox/store" + "otp-backend/internal/utils" ) type AuthService struct { @@ -38,7 +38,7 @@ func (s *AuthService) VerifyOTP(ctx context.Context, phone, code string) error { } if string(entry.Body) != code { - return errors.New("invalid otp: original: "+string(entry.Body)) + return errors.New("invalid otp: original: " + string(entry.Body)) } // delete otp after verification to free memory @@ -46,4 +46,4 @@ func (s *AuthService) VerifyOTP(ctx context.Context, phone, code string) error { // simulate registration return nil -} \ No newline at end of file +} diff --git a/_examples/cache/otp_code_verification/internal/utils/generator.go b/_examples/cache/otp_code_verification/internal/utils/generator.go index 6b8d4da..aafbe45 100644 --- a/_examples/cache/otp_code_verification/internal/utils/generator.go +++ b/_examples/cache/otp_code_verification/internal/utils/generator.go @@ -18,4 +18,4 @@ func FormatPhone(phone string) string { return "+" + phone } return phone -} \ No newline at end of file +}