Skip to content

Commit b4c4271

Browse files
author
hirsch
committed
♻️ Refacor providers
Move core implementation to the providers
1 parent 38525be commit b4c4271

File tree

12 files changed

+98
-76
lines changed

12 files changed

+98
-76
lines changed

app/http/middlewares/global.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
"github.com/thinkerou/favicon"
1010
)
1111

12-
func GlobalMiddlewares(engine *gin.Engine) {
13-
engine.Use(
12+
func GlobalMiddlewares(server *gin.Engine) {
13+
server.Use(
1414
cors.Default(),
1515
gzip.Gzip(gzip.DefaultCompression),
1616
helmet.NoSniff(),

app/providers/database.provider.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ import (
55
"github.com/hirsch88/go-micro-framework/config"
66
"github.com/jinzhu/gorm"
77
_ "github.com/jinzhu/gorm/dialects/mysql"
8+
"github.com/wantedly/gorm-zap"
9+
"go.uber.org/zap"
810
)
911

1012
type databaseProvider struct {
1113
config *config.DatabaseConfig
14+
log *zap.SugaredLogger
1215
}
1316

1417
func (p *databaseProvider) Connect() *gorm.DB {
@@ -24,6 +27,7 @@ func (p *databaseProvider) Connect() *gorm.DB {
2427
}
2528

2629
db.LogMode(p.config.LogMode)
30+
db.SetLogger(gormzap.New(p.log.Desugar()))
2731
db.DB().SetMaxIdleConns(p.config.IdleConnections)
2832
db.DB().SetMaxOpenConns(p.config.OpenConnections)
2933

@@ -43,6 +47,6 @@ type DatabaseProvider interface {
4347
Migrate()
4448
}
4549

46-
func NewDatabaseProvider(config *config.DatabaseConfig) DatabaseProvider {
47-
return &databaseProvider{config}
50+
func NewDatabaseProvider(config *config.DatabaseConfig, log *zap.SugaredLogger) DatabaseProvider {
51+
return &databaseProvider{config, log}
4852
}

core/logger.go renamed to app/providers/logger.provider.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
package core
1+
package providers
22

33
import (
44
"github.com/hirsch88/go-micro-framework/config"
55
"go.uber.org/zap"
66
"go.uber.org/zap/zapcore"
77
)
88

9-
func NewLogger(config *config.AppConfig) *zap.SugaredLogger {
9+
func NewLoggerProvider(config *config.AppConfig) *zap.SugaredLogger {
1010
var log *zap.Logger
1111
if config.Env != "production" {
1212
logConfig := zap.NewDevelopmentConfig()

app/providers/mail.provider.go

Lines changed: 22 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,49 @@
11
package providers
22

33
import (
4-
"bytes"
54
"github.com/hirsch88/go-micro-framework/app/mail"
65
"github.com/hirsch88/go-micro-framework/config"
7-
"github.com/hirsch88/go-micro-framework/core"
86
"go.uber.org/zap"
9-
"html/template"
107
"log"
118
)
129

1310
type mailProvider struct {
14-
config *config.MailConfig
15-
log *zap.SugaredLogger
11+
config *config.MailConfig
12+
log *zap.SugaredLogger
13+
template TemplateProvider
14+
smtpMail SMTPMailProvider
1615
}
1716

18-
func (p *mailProvider) Send(mail mail.Mailable, to string) {
17+
func (p *mailProvider) Send(mail mail.Mailable, to string) bool {
1918
mailTemplate := mail.Build()
20-
message, err := p.parseTemplate(mailTemplate)
19+
message, err := p.template.Parse(mailTemplate.TemplatePath, mailTemplate.Context)
2120
if err != nil {
2221
log.Fatal(err)
2322
}
24-
if ok := p.sendMail(to, mailTemplate.Subject, message); ok {
25-
p.log.Infof("Email has been sent to %s", to)
26-
} else {
27-
p.log.Infof("Failed to send the email to %s", to)
28-
}
29-
}
30-
31-
func (p *mailProvider) parseTemplate(mailTemplate *mail.Template) (string, error) {
32-
t, err := template.ParseFiles(mailTemplate.TemplatePath)
33-
if err != nil {
34-
return "", err
35-
}
36-
buffer := new(bytes.Buffer)
37-
if err = t.Execute(buffer, mailTemplate.Context); err != nil {
38-
return "", err
39-
}
40-
return buffer.String(), nil
41-
}
42-
43-
func (p *mailProvider) sendMail(to string, subject string, message string) bool {
44-
smtpServer := core.SmtpServer{
45-
Host: p.config.Host,
46-
Port: p.config.Port,
47-
}
48-
49-
mail := core.Mail{
50-
From: p.config.From,
51-
To: to,
52-
Subject: subject,
53-
Body: message,
54-
}
5523

56-
if err := core.SendMail(smtpServer, mail, p.config.Password); err != nil {
24+
if err := p.smtpMail.Send(
25+
SmtpServer{
26+
Host: p.config.Host,
27+
Port: p.config.Port,
28+
},
29+
Mail{
30+
From: p.config.From,
31+
To: to,
32+
Subject: mailTemplate.Subject,
33+
Body: message,
34+
},
35+
p.config.Password,
36+
); err != nil {
5737
return false
5838
}
5939

6040
return true
6141
}
6242

6343
type MailProvider interface {
64-
Send(mail mail.Mailable, to string)
44+
Send(mail mail.Mailable, to string) bool
6545
}
6646

67-
func NewMailProvider(config *config.MailConfig, log *zap.SugaredLogger) MailProvider {
68-
return &mailProvider{config, log}
47+
func NewMailProvider(config *config.MailConfig, log *zap.SugaredLogger, template TemplateProvider, smtpMail SMTPMailProvider) MailProvider {
48+
return &mailProvider{config, log, template, smtpMail}
6949
}

core/server.go renamed to app/providers/server.provider.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package core
1+
package providers
22

33
import (
44
"context"
@@ -11,12 +11,12 @@ import (
1111
"time"
1212
)
1313

14-
func NewGinEngine(lifecycle fx.Lifecycle, appConfig *config.AppConfig, log *zap.SugaredLogger) *gin.Engine {
14+
func NewServerProvider(lifecycle fx.Lifecycle, appConfig *config.AppConfig, log *zap.SugaredLogger) *gin.Engine {
1515
gin.SetMode(gin.ReleaseMode)
1616

17-
engine := gin.New()
18-
engine.Use(ginzap.Ginzap(log.Desugar(), time.RFC3339, true))
19-
engine.Use(ginzap.RecoveryWithZap(log.Desugar(), true))
17+
server := gin.New()
18+
server.Use(ginzap.Ginzap(log.Desugar(), time.RFC3339, true))
19+
server.Use(ginzap.RecoveryWithZap(log.Desugar(), true))
2020

2121
lifecycle.Append(fx.Hook{
2222
OnStart: func(context.Context) error {
@@ -28,9 +28,9 @@ func NewGinEngine(lifecycle fx.Lifecycle, appConfig *config.AppConfig, log *zap.
2828
fmt.Println("")
2929
}
3030

31-
return engine.Run(":" + appConfig.Port)
31+
return server.Run(":" + appConfig.Port)
3232
},
3333
})
3434

35-
return engine
35+
return server
3636
}

core/mail.go renamed to app/providers/smtpMail.provider.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
1-
package core
1+
package providers
22

33
import (
44
"crypto/tls"
55
"fmt"
6+
"go.uber.org/zap"
67
"net/smtp"
78
)
89

9-
/*
10-
|--------------------------------------------------------------------------
11-
| GMail lib
12-
|--------------------------------------------------------------------------
13-
|
14-
| This is solution to send emails with the gmail server.
15-
| https://hackernoon.com/golang-sendmail-sending-mail-through-net-smtp-package-5cadbe2670e0
16-
|
17-
*/
18-
1910
const (
2011
MIME = "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n"
2112
)
2213

14+
type smtpMailProvider struct {
15+
log *zap.SugaredLogger
16+
}
17+
2318
type Mail struct {
2419
From string
2520
To string
@@ -46,7 +41,7 @@ func (mail *Mail) BuildMessage() string {
4641
return message
4742
}
4843

49-
func SendMail(smtpServer SmtpServer, mail Mail, password string) error {
44+
func (p *smtpMailProvider) Send(smtpServer SmtpServer, mail Mail, password string) error {
5045
messageBody := mail.BuildMessage()
5146
auth := smtp.PlainAuth("", mail.From, password, smtpServer.Host)
5247

@@ -98,5 +93,12 @@ func SendMail(smtpServer SmtpServer, mail Mail, password string) error {
9893

9994
client.Quit()
10095
return nil
96+
}
97+
98+
type SMTPMailProvider interface {
99+
Send(smtpServer SmtpServer, mail Mail, password string) error
100+
}
101101

102+
func NewSMTPMailProvider(log *zap.SugaredLogger) SMTPMailProvider {
103+
return &smtpMailProvider{log}
102104
}

app/providers/template.provider.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package providers
2+
3+
import (
4+
"bytes"
5+
"go.uber.org/zap"
6+
"html/template"
7+
)
8+
9+
type templateProvider struct {
10+
log *zap.SugaredLogger
11+
}
12+
13+
func (p *templateProvider) Parse(templatePath string, context interface{}) (string, error) {
14+
p.log.Infof("Parsing template %s", templatePath)
15+
t, err := template.ParseFiles(templatePath)
16+
if err != nil {
17+
return "", err
18+
}
19+
buffer := new(bytes.Buffer)
20+
if err = t.Execute(buffer, context); err != nil {
21+
return "", err
22+
}
23+
return buffer.String(), nil
24+
}
25+
26+
type TemplateProvider interface {
27+
Parse(templatePath string, context interface{}) (string, error)
28+
}
29+
30+
func NewTemplateProvider(log *zap.SugaredLogger) TemplateProvider {
31+
return &templateProvider{log}
32+
}

app/services/user.service_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func testSetup(t *testing.T) (UserService, models.User) {
1818
var userRepositoryMock = new(mocks.UserRepository)
1919

2020
userRepositoryMock.On("Create", mock.Anything).Return(user)
21-
mailProviderMock.On("Send", mock.Anything, mock.Anything)
21+
mailProviderMock.On("Send", mock.Anything, mock.Anything).Return(true)
2222

2323
var service = NewUserService(userRepositoryMock, mailProviderMock)
2424
return service, user

bootstrap/app.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/hirsch88/go-micro-framework/app/repositories"
88
"github.com/hirsch88/go-micro-framework/app/services"
99
"github.com/hirsch88/go-micro-framework/config"
10-
"github.com/hirsch88/go-micro-framework/core"
1110
"github.com/hirsch88/go-micro-framework/routes"
1211
"go.uber.org/fx"
1312
)
@@ -40,13 +39,13 @@ func App() *fx.App {
4039
fx.Provide(config.NewDatabaseConfig),
4140
fx.Provide(config.NewMailConfig),
4241

43-
// Core (./core)
44-
fx.Provide(core.NewLogger),
45-
fx.Provide(core.NewGinEngine),
46-
4742
// Providers (./app/providers)
43+
fx.Provide(providers.NewLoggerProvider),
44+
fx.Provide(providers.NewTemplateProvider),
4845
fx.Provide(providers.NewDatabaseProvider),
46+
fx.Provide(providers.NewSMTPMailProvider),
4947
fx.Provide(providers.NewMailProvider),
48+
fx.Provide(providers.NewServerProvider),
5049

5150
// Repositores (./app/repositores)
5251
fx.Provide(repositories.NewUserRepository),

config/app.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ func NewAppConfig() *AppConfig {
77
| Environment Name
88
|--------------------------------------------------------------------------
99
|
10-
| TODO
10+
| With the help of the environment name we can implement different behaviour for
11+
| our environments. For example for local development we like to have another
12+
| logger encoding with colors, but in production we have the JSON encoding.
1113
|
1214
*/
1315

0 commit comments

Comments
 (0)