Skip to content

Commit

Permalink
fixed.
Browse files Browse the repository at this point in the history
  • Loading branch information
y-miyazaki committed Feb 7, 2023
1 parent cd41d35 commit 224995e
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 139 deletions.
4 changes: 2 additions & 2 deletions example/gin1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func main() {
sess := infrastructure.NewS3Session(&session.Options{
SharedConfigState: session.SharedConfigEnable,
})
awsS3Repository := repository.NewAWSS3Repository(log, sess, s3Config)
awsS3Repository := repository.NewAWSS3Repository(sess, s3Config)

// --------------------------------------------------------------
// Redis
Expand All @@ -140,7 +140,7 @@ func main() {
Password: redisPassword,
}
r := infrastructure.NewRedis(o)
redisRepository := repository.NewRedisRepository(log, r)
redisRepository := repository.NewRedisRepository(r)
defer closeRedis(log, r)

// --------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion example/s3/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func main() {
// --------------------------------------------------------------
// example: S3
// --------------------------------------------------------------
awsS3Repository := repository.NewAWSS3Repository(l, sess, s3Config)
awsS3Repository := repository.NewAWSS3Repository(sess, s3Config)
text := "aaaaaaaab"
bucket := "test"

Expand Down
42 changes: 41 additions & 1 deletion pkg/middleware/gin_http_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/sirupsen/logrus"
"github.com/y-miyazaki/go-common/pkg/context"
"github.com/y-miyazaki/go-common/pkg/logger"
"go.uber.org/zap"
)

// GinHTTPLogger retrieves the request/response logs.
Expand All @@ -34,7 +35,6 @@ func GinHTTPLogger(
fields[traceIDHeader] = c.Request.Header.Get(traceIDHeader)
}
// get error
l := l
if err, err2 := context.GetGinContextError(c); err2 == nil {
l = l.WithError(err)
}
Expand All @@ -50,6 +50,46 @@ func GinHTTPLogger(
}
}

// GinHTTPZapLogger retrieves the request/response logs.
func GinHTTPZapLogger(
l *logger.ZapLogger,
traceIDHeader string,
clientIPHeader string,
) gin.HandlerFunc {
return func(c *gin.Context) {
start := time.Now()
c.Next()
duration := time.Since(start)
l = l.With(
zap.String("host", c.Request.Host),
zap.String("duration", duration.String()),
zap.String("clientIP", clientIP(c, clientIPHeader)),
zap.String("method", c.Request.Method),
zap.String("url", c.Request.RequestURI),
zap.Int("status", c.Writer.Status()),
zap.String("referer", c.Request.Referer()),
zap.String("userAgent", c.Request.UserAgent()),
)

if traceIDHeader != "" {
l = l.With(zap.String(traceIDHeader, c.Request.Header.Get(traceIDHeader)))
}
// get error
if err, err2 := context.GetGinContextError(c); err2 == nil {
l = l.WithError(err)
}
// get error message
if messages, err := context.GetGinContextErrorMessage(c); err == nil {
l = l.With(zap.String("messages", messages))
}
if c.Writer.Status() >= http.StatusInternalServerError {
l.Error("")
} else {
l.Info("")
}
}
}

// clientIP gets ip address of client.
func clientIP(c *gin.Context, clientIPHeader string) string {
if ip := c.Request.Header.Get(clientIPHeader); ip != "" {
Expand Down
20 changes: 4 additions & 16 deletions pkg/repository/aws_s3_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/y-miyazaki/go-common/pkg/logger"
)

const (
Expand All @@ -37,15 +36,13 @@ type AWSS3RepositoryInterface interface {

// AWSS3Repository struct.
type AWSS3Repository struct {
logger *logger.Logger
s3 *s3.S3
session *session.Session
}

// NewAWSS3Repository returns AWSS3Repository instance.
func NewAWSS3Repository(l *logger.Logger, s *session.Session, config *aws.Config) *AWSS3Repository {
func NewAWSS3Repository(s *session.Session, config *aws.Config) *AWSS3Repository {
return &AWSS3Repository{
logger: l,
s3: s3.New(s, config),
session: s,
}
Expand All @@ -69,10 +66,7 @@ func (r *AWSS3Repository) PutObjectFile(bucket, key, filePath string) (*s3.PutOb
return nil, err
}
defer func() {
err = file.Close()
if err != nil {
r.logger.WithError(err).Errorf("can't close file(%s)", filePath)
}
_ = file.Close()
}()
// Get content-type
buf := make([]byte, maxBufferSize)
Expand Down Expand Up @@ -205,10 +199,7 @@ func (r *AWSS3Repository) Upload(bucket, key, filePath string) (*s3manager.Uploa
return nil, err
}
defer func() {
err = file.Close()
if err != nil {
r.logger.WithError(err).Errorf("can't close file(%s)", filePath)
}
_ = file.Close()
}()

// Get content-type
Expand Down Expand Up @@ -236,10 +227,7 @@ func (r *AWSS3Repository) Download(bucket, key, filePath string) error {
return err
}
defer func() {
err = file.Close()
if err != nil {
r.logger.WithError(err).Errorf("can't close file(%s)", filePath)
}
_ = file.Close()
}()

downloader := s3manager.NewDownloader(r.session)
Expand Down
59 changes: 9 additions & 50 deletions pkg/repository/aws_ses_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package repository
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ses"
"github.com/y-miyazaki/go-common/pkg/logger"
)

// AWSSESRepositoryInterface interface.
Expand All @@ -16,24 +15,22 @@ type AWSSESRepositoryInterface interface {

// AWSSESRepository struct.
type AWSSESRepository struct {
logger *logger.Logger
s *ses.SES
configurationSetName *string
isOutputLogPersonalInformation bool
}

// NewAWSSESRepository returns AWSSESRepository instance.
func NewAWSSESRepository(l *logger.Logger, s *ses.SES, configurationSetName *string, isOutputLogPersonalInformation bool) *AWSSESRepository {
func NewAWSSESRepository(s *ses.SES, configurationSetName *string, isOutputLogPersonalInformation bool) *AWSSESRepository {
return &AWSSESRepository{
logger: l,
s: s,
configurationSetName: configurationSetName,
isOutputLogPersonalInformation: isOutputLogPersonalInformation,
}
}

// SendTextEmail sends text email.
func (r *AWSSESRepository) SendTextEmail(from, to, subject, content string) error {
func (r *AWSSESRepository) SendTextEmail(from, to, subject, content string) (*ses.SendEmailOutput, error) {
response, err := r.s.SendEmail(&ses.SendEmailInput{
ConfigurationSetName: r.configurationSetName,
Destination: &ses.Destination{
Expand All @@ -56,13 +53,11 @@ func (r *AWSSESRepository) SendTextEmail(from, to, subject, content string) erro
Source: aws.String(from),
})

r.log(to, subject, response, err)

return err
return response, err
}

// SendHTMLEmail sends HTML email.
func (r *AWSSESRepository) SendHTMLEmail(from, to, subject, content string) error {
func (r *AWSSESRepository) SendHTMLEmail(from, to, subject, content string) (*ses.SendEmailOutput, error) {
response, err := r.s.SendEmail(&ses.SendEmailInput{
ConfigurationSetName: r.configurationSetName,
Destination: &ses.Destination{
Expand All @@ -85,13 +80,11 @@ func (r *AWSSESRepository) SendHTMLEmail(from, to, subject, content string) erro
Source: aws.String(from),
})

r.log(to, subject, response, err)

return err
return response, err
}

// SendEmail sends email.
func (r *AWSSESRepository) SendEmail(from, to, subject, contentText, contentHTML string) error {
func (r *AWSSESRepository) SendEmail(from, to, subject, contentText, contentHTML string) (*ses.SendEmailOutput, error) {
response, err := r.s.SendEmail(&ses.SendEmailInput{
ConfigurationSetName: r.configurationSetName,
Destination: &ses.Destination{
Expand All @@ -117,52 +110,18 @@ func (r *AWSSESRepository) SendEmail(from, to, subject, contentText, contentHTML
},
Source: aws.String(from),
})
r.log(to, subject, response, err)
return err
return response, err
}

// SendBulkTemplatedEmail sends bulk emails.
// Note: One or more Destination objects. All of the recipients in a Destination receive the same version of the email.
// You can specify up to 50 Destination objects within a Destinations array.
func (r *AWSSESRepository) SendBulkTemplatedEmail(from, template, defaultTemplateData string, destinations []*ses.BulkEmailDestination) error {
func (r *AWSSESRepository) SendBulkTemplatedEmail(from, template, defaultTemplateData string, destinations []*ses.BulkEmailDestination) (*ses.SendBulkTemplatedEmailOutput, error) {
response, err := r.s.SendBulkTemplatedEmail(&ses.SendBulkTemplatedEmailInput{
DefaultTemplateData: aws.String(defaultTemplateData),
Destinations: destinations,
Source: aws.String(from),
Template: aws.String(template),
})
r.logBulkTemplated(template, defaultTemplateData, response, err)
return err
}

func (r *AWSSESRepository) log(to, subject string, responseObject *ses.SendEmailOutput, responseError error) {
log := r.logger

// Check output personal information flag.
if r.isOutputLogPersonalInformation {
log = r.logger.
WithField("to", to).
WithField("subject", subject)
}
if responseObject.MessageId != nil {
log = log.WithField("messageId", *responseObject.MessageId)
}
if responseError == nil {
log.Debug("Successfully sent an SES email")
} else {
log.WithError(responseError).Error("Error while sending an SES email")
}
}
func (r *AWSSESRepository) logBulkTemplated(template, defaultTemplateData string, responseObject *ses.SendBulkTemplatedEmailOutput, responseError error) {
log := r.logger

log = log.WithField("template", template).WithField("defaultTemplateData", defaultTemplateData)
if responseObject.Status != nil {
log = log.WithField("status", responseObject.Status)
}
if responseError == nil {
log.Debug("Successfully sent an SES email")
} else {
log.WithError(responseError).Error("Error while sending an SES email")
}
return response, err
}
7 changes: 2 additions & 5 deletions pkg/repository/base_repository.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package repository

import "github.com/y-miyazaki/go-common/pkg/logger"

// BaseRepository struct.
type BaseRepository struct {
Logger *logger.Logger
}

// NewBaseRepository returns BaseRepository instance.
func NewBaseRepository(l *logger.Logger) *BaseRepository {
return &BaseRepository{Logger: l}
func NewBaseRepository() *BaseRepository {
return &BaseRepository{}
}
49 changes: 0 additions & 49 deletions pkg/repository/mock/aws_s3_repository.go

This file was deleted.

9 changes: 3 additions & 6 deletions pkg/repository/redis_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"time"

redis "github.com/go-redis/redis/v8"
"github.com/y-miyazaki/go-common/pkg/logger"
)

// RedisRepositoryInterface interface.
Expand All @@ -14,15 +13,13 @@ type RedisRepositoryInterface interface {

// RedisRepository struct.
type RedisRepository struct {
logger *logger.Logger
redis *redis.Client
redis *redis.Client
}

// NewRedisRepository returns RedisRepository instance.
func NewRedisRepository(l *logger.Logger, r *redis.Client) *RedisRepository {
func NewRedisRepository(r *redis.Client) *RedisRepository {
return &RedisRepository{
logger: l,
redis: r,
redis: r,
}
}

Expand Down
5 changes: 1 addition & 4 deletions pkg/repository/slack_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package repository

import (
"github.com/slack-go/slack"
"github.com/y-miyazaki/go-common/pkg/logger"
)

// SlackRepositoryInterface interface
Expand All @@ -13,15 +12,13 @@ type SlackRepositoryInterface interface {

// SlackRepository struct.
type SlackRepository struct {
logger *logger.Logger
client *slack.Client
channelID string
}

// NewSlackRepository returns SlackRepository instance.
func NewSlackRepository(l *logger.Logger, client *slack.Client, channelID string) *SlackRepository {
func NewSlackRepository(client *slack.Client, channelID string) *SlackRepository {
return &SlackRepository{
logger: l,
client: client,
channelID: channelID,
}
Expand Down
Loading

0 comments on commit 224995e

Please sign in to comment.