proper loggers are added

This commit is contained in:
kemvl 2024-03-29 12:23:30 +05:00
parent c74d5584b0
commit 28b34868cb
4 changed files with 32 additions and 14 deletions

View File

@ -31,7 +31,7 @@ func (h *OTPHandler) GenerateAndSaveOTPHandler(w http.ResponseWriter, r *http.Re
return return
} }
err = h.Service.GenerateAndSaveOTP(request.PhoneNumber) err = h.Service.SaveAndSendOTP(request.PhoneNumber)
if err != nil { if err != nil {
utils.RespondWithErrorJSON(w, http.StatusInternalServerError, err.Error()) utils.RespondWithErrorJSON(w, http.StatusInternalServerError, err.Error())
return return

View File

@ -2,7 +2,11 @@ package smpp
import ( import (
"log" "log"
"log/slog"
"os"
"smpp-otp/internal/config" "smpp-otp/internal/config"
"smpp-otp/pkg/lib/logger"
"time"
"github.com/fiorix/go-smpp/smpp" "github.com/fiorix/go-smpp/smpp"
"github.com/fiorix/go-smpp/smpp/pdu" "github.com/fiorix/go-smpp/smpp/pdu"
@ -14,19 +18,33 @@ type SMPPClient struct {
} }
func NewSMPPClient(cfg *config.Config) (*SMPPClient, error) { func NewSMPPClient(cfg *config.Config) (*SMPPClient, error) {
smppCfg := cfg.SMPP logger, err := logger.SetupLogger(cfg.Env)
tx := &smpp.Transceiver{ if err != nil {
Addr: smppCfg.Addr, slog.Error("failed to set up logger: %v", err)
User: smppCfg.User, os.Exit(1)
Passwd: smppCfg.Pass,
Handler: func(p pdu.Body) {},
} }
connStatus := tx.Bind() smppCfg := cfg.SMPP
status := <-connStatus var tx *smpp.Transceiver
if status.Status() != smpp.Connected {
log.Println("Failed to connect:", status.Error()) for {
return nil, status.Error() tx = &smpp.Transceiver{
Addr: smppCfg.Addr,
User: smppCfg.User,
Passwd: smppCfg.Pass,
Handler: func(p pdu.Body) {},
}
connStatus := tx.Bind()
status := <-connStatus
if status.Status() == smpp.Connected {
logger.InfoLogger.Info("Connected to SMPP server.")
break
} else {
logger.ErrorLogger.Error("Failed to connect:", status.Error())
logger.InfoLogger.Info("Retrying in 5 seconds...")
time.Sleep(5 * time.Second) // Wait for 5 seconds before retrying
}
} }
return &SMPPClient{Transceiver: tx}, nil return &SMPPClient{Transceiver: tx}, nil

View File

@ -5,6 +5,6 @@ import (
) )
type OTPService interface { type OTPService interface {
GenerateAndSendOTP(cfg config.Config, phoneNumber string) error SaveAndSendOTP(cfg config.Config, phoneNumber string) error
ValidateOTP(phoneNumber string, otp string) error ValidateOTP(phoneNumber string, otp string) error
} }

View File

@ -27,7 +27,7 @@ func GenerateOTP() string {
return otp return otp
} }
func (s *OTPService) GenerateAndSaveOTP(phoneNumber string) error { func (s *OTPService) SaveAndSendOTP(phoneNumber string) error {
otp := GenerateOTP() otp := GenerateOTP()
err := s.repository.SaveOTP(phoneNumber, otp) err := s.repository.SaveOTP(phoneNumber, otp)
if err != nil { if err != nil {