Skip to content

Commit

Permalink
wip refacto 7 (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
Medzoner authored Oct 28, 2024
1 parent 013eaf7 commit 75e12a9
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 19 deletions.
20 changes: 6 additions & 14 deletions pkg/infra/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,15 @@ type DatabaseConfig struct {

// NewConfig is a constructor for Config
func NewConfig() (Config, error) {
conf, err := parseEnv()
if err != nil {
return conf, err
}
if conf.RootPath == "" {
cfg := Config{}
err := env.Parse(&cfg)
if err == nil && cfg.RootPath == "" {
pwd, err := os.Getwd()
if err != nil {
return conf, err
return cfg, err
}
conf.RootPath = RootPath(pwd + "/")
cfg.RootPath = RootPath(pwd + "/")
}

return conf, nil
}

// parseEnv parseEnv
func parseEnv() (Config, error) {
cfg := &Config{}
return *cfg, env.Parse(cfg)
return cfg, err
}
8 changes: 3 additions & 5 deletions pkg/infra/mailersmtp/mailer_smtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (m *MailerSMTP) Send(ctx context.Context, view entity.Contact) (bool, error

auth := smtp.PlainAuth(m.User, m.User, m.Password, m.Host)
req := NewRequest([]string{m.User}, "Message [medzoner.com]", "Hello, World!")
if err := req.ParseTemplate(m.RootPath+"/tmpl/contact/contactEmail.html", view); err != nil {
if err := req.parseTemplate(m.RootPath+"/tmpl/contact/contactEmail.html", view); err != nil {
iSpan.RecordError(err)
return false, fmt.Errorf("parse template failed: %w", err)
}
Expand All @@ -82,15 +82,13 @@ func (m *MailerSMTP) Send(ctx context.Context, view entity.Contact) (bool, error
servername := fmt.Sprintf("%s:%s", m.Host, m.Port)

if err := smtp.SendMail(servername, auth, m.User, req.to, msg); err != nil {
iSpan.RecordError(err)
return false, fmt.Errorf("sendmail failed: %w", err)
return false, m.Tracer.Error(iSpan, fmt.Errorf("send mail failed: %w", err))
}

return true, nil
}

// ParseTemplate ParseTemplate
func (r *Request) ParseTemplate(templateFileName string, data interface{}) error {
func (r *Request) parseTemplate(templateFileName string, data interface{}) error {
t, err := template.ParseFiles(templateFileName)
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions pkg/infra/mailersmtp/mailer_smtp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func TestSmtp(t *testing.T) {
t.Run("Unit: test Smtp success", func(t *testing.T) {
httpTracerMock := tracerMock.NewMockTracer(gomock.NewController(t))
httpTracerMock.EXPECT().Start(gomock.Any(), gomock.Any(), gomock.Any()).Return(context.Background(), noop.Span{}).Times(1)
httpTracerMock.EXPECT().Error(gomock.Any(), gomock.Any()).Return(nil).Times(1)
mailer := mailersmtp.MailerSMTP{RootPath: "./../../..", Tracer: httpTracerMock}
ctx := context.WithValue(context.Background(), middleware.CorrelationContextKey{}, uuid.New().String())
_, _ = mailer.Send(ctx, entity.Contact{})
Expand Down
1 change: 1 addition & 0 deletions pkg/infra/tracer/http_tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func (t HttpTracer) StartRoot(ctx context.Context, request *http.Request, spanNa

func (t HttpTracer) Error(span otelTrace.Span, err error) error {
span.RecordError(err)
t.Logger.Error(err.Error())
return fmt.Errorf("error during handle event: %w", err)
}

Expand Down
14 changes: 14 additions & 0 deletions pkg/infra/tracer/http_tracer_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tracer_test

import (
"context"
"github.com/Medzoner/medzoner-go/pkg/infra/config"
"github.com/Medzoner/medzoner-go/pkg/infra/tracer"
"gotest.tools/assert"
Expand All @@ -15,4 +16,17 @@ func TestTracer(t *testing.T) {
assert.NilError(t, err)
assert.Assert(t, tr != nil)
})
t.Run("Unit: test Tracer success", func(t *testing.T) {
tr, err := tracer.NewHttpTracer(config.Config{
TracerFile: "trace.out",
})
err = tr.ShutdownLogger(context.Background())
err = tr.ShutdownMeter(context.Background())
err = tr.ShutdownTracer(context.Background())
if err != nil {
t.Error(err)
}
assert.NilError(t, err)
assert.Assert(t, tr != nil)
})
}

0 comments on commit 75e12a9

Please sign in to comment.