smpp is added

This commit is contained in:
kemvl 2024-03-28 16:00:22 +05:00
parent ecba78e8cc
commit 10703b0762
8 changed files with 122 additions and 16 deletions

View File

@ -7,6 +7,7 @@ import (
"os/signal" "os/signal"
"smpp-otp/internal/config" "smpp-otp/internal/config"
"smpp-otp/internal/delivery/routers" "smpp-otp/internal/delivery/routers"
smpp "smpp-otp/internal/infrastructure"
"smpp-otp/internal/repository" "smpp-otp/internal/repository"
"smpp-otp/internal/service" "smpp-otp/internal/service"
db "smpp-otp/pkg/database" db "smpp-otp/pkg/database"
@ -31,8 +32,15 @@ func main() {
logger.ErrorLogger.Error("failed to initialize database: %v", err) logger.ErrorLogger.Error("failed to initialize database: %v", err)
os.Exit(1) os.Exit(1)
} }
smppClient, err := smpp.NewSMPPClient(cfg)
if err != nil {
logger.ErrorLogger.Error("failed to initialize SMPP client: %v", err)
os.Exit(1)
}
repo := repository.NewOTPRepository(database.GetClient(), logger) repo := repository.NewOTPRepository(database.GetClient(), logger)
otpService := service.NewOTPService(repo, logger) otpService := service.NewOTPService(repo, logger, cfg, smppClient)
r := routers.SetupOTPRoutes(otpService, logger) r := routers.SetupOTPRoutes(otpService, logger)

13
go.mod
View File

@ -3,12 +3,17 @@ module smpp-otp
go 1.21.5 go 1.21.5
require ( require (
github.com/BurntSushi/toml v1.2.1 // indirect github.com/fiorix/go-smpp v0.0.0-20210403173735-2894b96e70ba
github.com/go-chi/chi/v5 v5.0.12
github.com/go-redis/redis v6.15.9+incompatible
github.com/ilyakaznacheev/cleanenv v1.5.0
)
require (
github.com/BurntSushi/toml v1.3.2 // indirect
github.com/go-chi/chi v1.5.5 // indirect github.com/go-chi/chi v1.5.5 // indirect
github.com/go-chi/chi/v5 v5.0.12 // indirect
github.com/go-redis/redis v6.15.9+incompatible // indirect
github.com/ilyakaznacheev/cleanenv v1.5.0 // indirect
github.com/joho/godotenv v1.5.1 // indirect github.com/joho/godotenv v1.5.1 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect
olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 // indirect olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 // indirect
) )

17
go.sum
View File

@ -1,5 +1,11 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak= github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/fiorix/go-smpp v0.0.0-20210403173735-2894b96e70ba h1:vBqABUa2HUSc6tj22Tw+ZMVGHuBzKtljM38kbRanmrM=
github.com/fiorix/go-smpp v0.0.0-20210403173735-2894b96e70ba/go.mod h1:VfKFK7fGeCP81xEhbrOqUEh45n73Yy6jaPWwTVbxprI=
github.com/go-chi/chi v1.5.5 h1:vOB/HbEMt9QqBqErz07QehcOKHaWFtuj87tTDVz2qXE= github.com/go-chi/chi v1.5.5 h1:vOB/HbEMt9QqBqErz07QehcOKHaWFtuj87tTDVz2qXE=
github.com/go-chi/chi v1.5.5/go.mod h1:C9JqLr3tIYjDOZpzn+BCuxY8z8vmca43EeMgyZt7irw= github.com/go-chi/chi v1.5.5/go.mod h1:C9JqLr3tIYjDOZpzn+BCuxY8z8vmca43EeMgyZt7irw=
github.com/go-chi/chi/v5 v5.0.12 h1:9euLV5sTrTNTRUU9POmDUvfxyj6LAABLUcEWO+JJb4s= github.com/go-chi/chi/v5 v5.0.12 h1:9euLV5sTrTNTRUU9POmDUvfxyj6LAABLUcEWO+JJb4s=
@ -10,7 +16,18 @@ github.com/ilyakaznacheev/cleanenv v1.5.0 h1:0VNZXggJE2OYdXE87bfSSwGxeiGt9moSR2l
github.com/ilyakaznacheev/cleanenv v1.5.0/go.mod h1:a5aDzaJrLCQZsazHol1w8InnDcOX0OColm64SlIi6gk= github.com/ilyakaznacheev/cleanenv v1.5.0/go.mod h1:a5aDzaJrLCQZsazHol1w8InnDcOX0OColm64SlIi6gk=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/urfave/cli v1.22.5/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 h1:slmdOY3vp8a7KQbHkL+FLbvbkgMqmXojpFUO/jENuqQ= olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 h1:slmdOY3vp8a7KQbHkL+FLbvbkgMqmXojpFUO/jENuqQ=

View File

@ -0,0 +1,7 @@
package smpp
import "smpp-otp/internal/config"
type SMPPClient interface {
SendSMS(cfg *config.Config, dest, text string) error
}

View File

@ -0,0 +1,53 @@
package smpp
import (
"log"
"smpp-otp/internal/config"
"github.com/fiorix/go-smpp/smpp"
"github.com/fiorix/go-smpp/smpp/pdu"
"github.com/fiorix/go-smpp/smpp/pdu/pdutext"
)
type SMPPClient struct {
Transceiver *smpp.Transceiver
}
func NewSMPPClient(cfg *config.Config) (*SMPPClient, error) {
smppCfg := cfg.SMPP
tx := &smpp.Transceiver{
Addr: smppCfg.Addr,
User: smppCfg.User,
Passwd: smppCfg.Pass,
Handler: func(p pdu.Body) {},
}
connStatus := tx.Bind()
go func() {
for c := range connStatus {
if c.Status() != smpp.Connected {
log.Println("Failed to connect:", c.Error())
}
}
}()
return &SMPPClient{Transceiver: tx}, nil
}
func (c *SMPPClient) SendSMS(cfg *config.Config, dest, text string) error {
src := cfg.SMPP.Src_Phone_Number
params := &smpp.ShortMessage{
Src: src,
Dst: dest,
Text: pdutext.Raw(text),
Register: 0, // No delivery receipt
}
resp, err := c.Transceiver.Submit(params)
if err != nil {
log.Println("Failed to send SMS:", err)
return err
}
log.Println("SMS sent successfully, response:", resp)
return nil
}

View File

@ -3,6 +3,8 @@ package service
import ( import (
"fmt" "fmt"
"math/rand" "math/rand"
"smpp-otp/internal/config"
smpp "smpp-otp/internal/infrastructure/interfaces"
repository "smpp-otp/internal/repository/interfaces" repository "smpp-otp/internal/repository/interfaces"
"smpp-otp/pkg/lib/logger" "smpp-otp/pkg/lib/logger"
"time" "time"
@ -11,10 +13,12 @@ import (
type OTPService struct { type OTPService struct {
repository repository.OTPRepository repository repository.OTPRepository
logger *logger.Loggers logger *logger.Loggers
cfg *config.Config
smppClient smpp.SMPPClient
} }
func NewOTPService(repo repository.OTPRepository, logger *logger.Loggers) OTPService { func NewOTPService(repo repository.OTPRepository, logger *logger.Loggers, cfg *config.Config, smppClient smpp.SMPPClient) OTPService {
return OTPService{repository: repo, logger: logger} return OTPService{repository: repo, logger: logger, cfg: cfg, smppClient: smppClient}
} }
func GenerateOTP() string { func GenerateOTP() string {
@ -30,6 +34,13 @@ func (s *OTPService) GenerateAndSaveOTP(phoneNumber string) error {
s.logger.ErrorLogger.Error("Error saving OTP to repository: %v", err) s.logger.ErrorLogger.Error("Error saving OTP to repository: %v", err)
return err return err
} }
err = s.smppClient.SendSMS(s.cfg, phoneNumber, otp)
if err != nil {
s.logger.ErrorLogger.Error("Error sending OTP via SMS: %v", err)
return err
}
return nil return nil
} }

View File

@ -1,3 +1,4 @@
time=2024-03-28T00:07:01.216+05:00 level=ERROR msg="Error retrieving OTP timestamp from repository: %v" !BADKEY="redis: nil" time=2024-03-28T00:07:01.216+05:00 level=ERROR msg="Error retrieving OTP timestamp from repository: %v" !BADKEY="redis: nil"
time=2024-03-28T00:09:57.336+05:00 level=ERROR msg="Error retrieving OTP timestamp from repository: %v" !BADKEY="redis: nil" time=2024-03-28T00:09:57.336+05:00 level=ERROR msg="Error retrieving OTP timestamp from repository: %v" !BADKEY="redis: nil"
time=2024-03-28T00:19:15.421+05:00 level=ERROR msg="Error retrieving OTP timestamp from repository: %v" !BADKEY="redis: nil" time=2024-03-28T00:19:15.421+05:00 level=ERROR msg="Error retrieving OTP timestamp from repository: %v" !BADKEY="redis: nil"
time=2024-03-28T15:52:19.482+05:00 level=ERROR msg="Server failed to start:" !BADKEY="listen tcp 127.0.0.1:8080: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted."

View File

@ -1,9 +1,13 @@
time=2024-03-27T23:24:15.715+05:00 level=INFO msg="Shutting down the server gracefully..." time=2024-03-28T15:52:19.463+05:00 level=INFO msg="Server is up and running"
time=2024-03-27T23:31:35.765+05:00 level=INFO msg="Shutting down the server gracefully..." time=2024-03-28T15:53:03.368+05:00 level=INFO msg="Server is up and running"
time=2024-03-27T23:39:10.332+05:00 level=INFO msg="Server is up and running" time=2024-03-28T15:53:14.102+05:00 level=INFO msg="Shutting down the server gracefully..."
time=2024-03-27T23:39:22.414+05:00 level=INFO msg="Shutting down the server gracefully..." time=2024-03-28T15:53:31.254+05:00 level=INFO msg="Server is up and running"
time=2024-03-28T00:06:29.253+05:00 level=INFO msg="Server is up and running" time=2024-03-28T15:53:43.763+05:00 level=INFO msg="Shutting down the server gracefully..."
time=2024-03-28T00:18:28.728+05:00 level=INFO msg="Shutting down the server gracefully..." time=2024-03-28T15:54:00.477+05:00 level=INFO msg="Server is up and running"
time=2024-03-28T00:18:31.350+05:00 level=INFO msg="Server is up and running" time=2024-03-28T15:54:14.785+05:00 level=INFO msg="Shutting down the server gracefully..."
time=2024-03-28T00:20:32.122+05:00 level=INFO msg="Shutting down the server gracefully..." time=2024-03-28T15:54:42.921+05:00 level=INFO msg="Server is up and running"
time=2024-03-28T00:20:34.708+05:00 level=INFO msg="Server is up and running" time=2024-03-28T15:55:09.091+05:00 level=INFO msg="Shutting down the server gracefully..."
time=2024-03-28T15:57:27.775+05:00 level=INFO msg="Server is up and running"
time=2024-03-28T15:57:35.522+05:00 level=INFO msg="Shutting down the server gracefully..."
time=2024-03-28T15:58:06.282+05:00 level=INFO msg="Server is up and running"
time=2024-03-28T15:58:12.643+05:00 level=INFO msg="Shutting down the server gracefully..."