Skip to content

Commit

Permalink
refactor(infrastructure): use a struct for auth values in smtp
Browse files Browse the repository at this point in the history
  • Loading branch information
bastean committed Aug 10, 2024
1 parent 8781bd1 commit e0a703f
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 56 deletions.
71 changes: 40 additions & 31 deletions internal/pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ import (
"github.com/bastean/codexgo/v4/internal/pkg/service/user"
)

var (
Service = &struct {
SMTP, RabbitMQ, MongoDB string
}{
SMTP: log.Service("SMTP"),
RabbitMQ: log.Service("RabbitMQ"),
MongoDB: log.Service("MongoDB"),
}
Module = &struct {
User string
}{
User: log.Module("User"),
}
)
var Service = &struct {
SMTP, RabbitMQ, MongoDB, Terminal string
}{
SMTP: log.Service("SMTP"),
RabbitMQ: log.Service("RabbitMQ"),
MongoDB: log.Service("MongoDB"),
Terminal: log.Service("Terminal"),
}

var Module = &struct {
User string
}{
User: log.Module("User"),
}

var (
err error
Expand All @@ -35,28 +35,37 @@ var (
)

func Up() error {
log.EstablishingConnectionWith(Service.SMTP)

user.InitCreated(&user.TerminalConfirmation{
Logger: log.Log,
ServerURL: env.ServerGinURL,
},
user.QueueSendConfirmation,
)
switch {
case env.SMTPHost != "":
log.EstablishingConnectionWith(Service.SMTP)

if env.SMTPHost != "" {
SMTP = smtp.Open(
env.SMTPHost,
env.SMTPPort,
env.SMTPUsername,
env.SMTPPassword,
env.ServerGinURL,
&smtp.Auth{
Host: env.SMTPHost,
Port: env.SMTPPort,
Username: env.SMTPUsername,
Password: env.SMTPPassword,
},
)

user.InitCreated(&user.MailConfirmation{SMTP: SMTP}, user.QueueSendConfirmation)
}
user.InitCreated(&user.MailConfirmation{
SMTP: SMTP,
AppServerURL: env.ServerGinURL,
}, user.QueueSendConfirmation)

log.ConnectionEstablishedWith(Service.SMTP)
default:
log.Starting(Service.Terminal)

user.InitCreated(&user.TerminalConfirmation{
Logger: log.Log,
AppServerURL: env.ServerGinURL,
},
user.QueueSendConfirmation,
)

log.ConnectionEstablishedWith(Service.SMTP)
log.Started(Service.Terminal)
}

log.EstablishingConnectionWith(Service.RabbitMQ)

Expand Down
1 change: 1 addition & 0 deletions internal/pkg/service/transport/smtp/smtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

type (
SMTP = smtp.SMTP
Auth = smtp.Auth
)

var (
Expand Down
24 changes: 16 additions & 8 deletions pkg/context/shared/infrastructure/transports/smtp/smtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ import (
"net/smtp"
)

type Auth struct {
Host, Port, Username, Password string
}

type SMTP struct {
smtp.Auth
SMTPServerURL, Username, Password, ServerURL string
ServerURL, Username string
}

func (*SMTP) SetHeader(key, value string) string {
Expand All @@ -31,15 +35,19 @@ func (client *SMTP) Headers(to, subject string) string {
}

func (client *SMTP) SendMail(to []string, message []byte) error {
return smtp.SendMail(client.SMTPServerURL, client.Auth, client.Username, to, message)
return smtp.SendMail(
client.ServerURL,
client.Auth,
client.Username,
to,
message,
)
}

func Open(host, port, username, password, serverURL string) *SMTP {
func Open(auth *Auth) *SMTP {
return &SMTP{
Auth: smtp.PlainAuth("", username, password, host),
SMTPServerURL: fmt.Sprintf("%s:%s", host, port),
Username: username,
Password: password,
ServerURL: serverURL,
Auth: smtp.PlainAuth("", auth.Username, auth.Password, auth.Host),
ServerURL: fmt.Sprintf("%s:%s", auth.Host, auth.Port),
Username: auth.Username,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

type Confirmation struct {
*smtp.SMTP
AppServerURL string
}

func (client *Confirmation) Submit(data any) error {
Expand Down Expand Up @@ -46,7 +47,7 @@ func (client *Confirmation) Submit(data any) error {
})
}

link := fmt.Sprintf("%s/v4/account/verify/%s", client.ServerURL, attributes.Id)
link := fmt.Sprintf("%s/v4/account/verify/%s", client.AppServerURL, attributes.Id)

ConfirmationTemplate(attributes.Username, link).Render(context.Background(), &message)

Expand All @@ -58,7 +59,7 @@ func (client *Confirmation) Submit(data any) error {
What: "Failure to send an account confirmation mail",
Why: errors.Meta{
"User Id": attributes.Id,
"SMTP Server URL": client.SMTPServerURL,
"SMTP Server URL": client.ServerURL,
},
Who: err,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ type ConfirmationTestSuite struct {

func (suite *ConfirmationTestSuite) SetupTest() {
suite.smtp = smtp.Open(
os.Getenv("CODEXGO_SMTP_HOST"),
os.Getenv("CODEXGO_SMTP_PORT"),
os.Getenv("CODEXGO_SMTP_USERNAME"),
os.Getenv("CODEXGO_SMTP_PASSWORD"),
os.Getenv("CODEXGO_SERVER_GIN_URL"),
&smtp.Auth{
Host: os.Getenv("CODEXGO_SMTP_HOST"),
Port: os.Getenv("CODEXGO_SMTP_PORT"),
Username: os.Getenv("CODEXGO_SMTP_USERNAME"),
Password: os.Getenv("CODEXGO_SMTP_PASSWORD"),
},
)

suite.sut = &mail.Confirmation{
SMTP: suite.smtp,
SMTP: suite.smtp,
AppServerURL: os.Getenv("CODEXGO_SERVER_GIN_URL"),
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

type Confirmation struct {
loggers.Logger
ServerURL string
AppServerURL string
}

func (client *Confirmation) Submit(data any) error {
Expand All @@ -27,7 +27,7 @@ func (client *Confirmation) Submit(data any) error {
})
}

link := fmt.Sprintf("Hi %s, please confirm your account through this link: %s/v4/account/verify/%s", attributes.Username, client.ServerURL, attributes.Id)
link := fmt.Sprintf("Hi %s, please confirm your account through this link: %s/v4/account/verify/%s", attributes.Username, client.AppServerURL, attributes.Id)

client.Logger.Info(link)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ import (

type ConfirmationTestSuite struct {
suite.Suite
sut transfers.Transfer
logger *records.LoggerMock
serverURL string
sut transfers.Transfer
logger *records.LoggerMock
appServerURL string
}

func (suite *ConfirmationTestSuite) SetupTest() {
suite.logger = new(records.LoggerMock)

suite.serverURL = os.Getenv("CODEXGO_SERVER_GIN_URL")
suite.appServerURL = os.Getenv("CODEXGO_SERVER_GIN_URL")

suite.sut = &terminal.Confirmation{
Logger: suite.logger,
ServerURL: suite.serverURL,
Logger: suite.logger,
AppServerURL: suite.appServerURL,
}
}

Expand All @@ -40,7 +40,7 @@ func (suite *ConfirmationTestSuite) TestSubmit() {

suite.NoError(json.Unmarshal(message.Attributes, event.Attributes))

link := fmt.Sprintf("Hi %s, please confirm your account through this link: %s/v4/account/verify/%s", event.Attributes.Username, suite.serverURL, event.Attributes.Id)
link := fmt.Sprintf("Hi %s, please confirm your account through this link: %s/v4/account/verify/%s", event.Attributes.Username, suite.appServerURL, event.Attributes.Id)

suite.logger.Mock.On("Info", link)

Expand Down

0 comments on commit e0a703f

Please sign in to comment.