Compare commits

..

9 Commits

Author SHA1 Message Date
王性驊 b3cfd0afaa add code third party code (#5)
Co-authored-by: daniel.w <daniel.w@intteam.net>
Reviewed-on: #5
2024-08-27 14:25:45 +00:00
daniel.w 7567a234d5 merge mail 2024-08-27 15:45:34 +08:00
daniel.w 5b9355c4e9 add checkout verify code 2024-08-26 16:34:16 +08:00
王性驊 10e9caaaa6 feature/mail (#3)
Co-authored-by: daniel.w <daniel.w@intteam.net>
Reviewed-on: #3
2024-08-20 15:56:21 +00:00
daniel.w 44e736daab feat: add worker_pool 2024-08-20 23:55:09 +08:00
daniel.w cef8a172e6 feat: add worker_pool 2024-08-20 23:53:20 +08:00
daniel.w 73ed862867 feat: add main sender and sms base sender 2024-08-20 21:08:00 +08:00
daniel.w 8226e179ab feat: init make file and docker file 2024-08-20 18:17:34 +08:00
王性驊 d72993368e feat: make file (#1)
Co-authored-by: daniel.w <daniel.w@intteam.net>
Reviewed-on: #1
2024-08-20 06:35:46 +00:00
22 changed files with 497 additions and 40 deletions

5
.gitignore vendored
View File

@ -2,5 +2,6 @@
go.sum
account/
gen_result/
etc/app-cloudep-notification-service.yaml
./client
etc/notification.yaml
client/
.DS_Store

140
.golangci.yaml Normal file
View File

@ -0,0 +1,140 @@
run:
timeout: 3m
# Exit code when at least one issue was found.
# Default: 1
issues-exit-code: 2
# Include test files or not.
# Default: true
tests: false
# Reference URL: https://golangci-lint.run/usage/linters/
linters:
# Disable everything by default so upgrades to not include new - default
# enabled- linters.
disable-all: true
# Specifically enable linters we want to use.
enable:
# - depguard
- errcheck
# - godot
- gofmt
- goimports
- gosimple
- govet
- ineffassign
- misspell
- revive
# - staticcheck
- typecheck
- unused
# - wsl
- asasalint
- asciicheck
- bidichk
- bodyclose
# - containedctx
- contextcheck
# - cyclop
# - varnamelen
# - gci
- wastedassign
- whitespace
# - wrapcheck
- thelper
- tparallel
- unconvert
- unparam
- usestdlibvars
- tenv
- testableexamples
- stylecheck
- sqlclosecheck
- nosprintfhostport
- paralleltest
- prealloc
- predeclared
- promlinter
- reassign
- rowserrcheck
- nakedret
- nestif
- nilerr
- nilnil
- nlreturn
- noctx
- nolintlint
- nonamedreturns
- decorder
- dogsled
# - dupl
- dupword
- durationcheck
- errchkjson
- errname
- errorlint
# - execinquery
- exhaustive
- exportloopref
- forbidigo
- forcetypeassert
# - gochecknoglobals
- gochecknoinits
- gocognit
- goconst
- gocritic
- gocyclo
# - godox
# - goerr113
# - gofumpt
- goheader
- gomoddirectives
# - gomodguard always failed
- goprintffuncname
- gosec
- grouper
- importas
- interfacebloat
# - ireturn
- lll
- loggercheck
- maintidx
- makezero
issues:
exclude-rules:
- path: _test\.go
linters:
- funlen
- goconst
- interfacer
- dupl
- lll
- goerr113
- errcheck
- gocritic
- cyclop
- wrapcheck
- gocognit
- contextcheck
linters-settings:
gci:
sections:
- standard # Standard section: captures all standard packages.
- default # Default section: contains all imports that could not be matched to another section type.
gocognit:
# Minimal code complexity to report.
# Default: 30 (but we recommend 10-20)
min-complexity: 40
nestif:
# Minimal complexity of if statements to report.
# Default: 5
min-complexity: 10
lll:
# Max line length, lines longer will be reported.
# '\t' is counted as 1 character by default, and can be changed with the tab-width option.
# Default: 120.
line-length: 200
# Tab width in spaces.
# Default: 1
tab-width: 1

View File

@ -1,4 +1,5 @@
GO_CTL_NAME=goctl
# go-zero 生成風格
GO_ZERO_STYLE=go_zero
@ -9,7 +10,6 @@ LDFLAGS := -s -w
VERSION="v1.0.3"
DOCKER_REPO="igs170911/notification"
.PHONY: test
test: # 進行測試
go test -v --cover ./...
@ -21,7 +21,7 @@ fmt: # 格式優化
.PHONY: gen-rpc
gen-rpc: # 建立 rpc code
$(GO_CTL_NAME) rpc protoc ./generate/protobuf/service.proto -m --style=$(GO_ZERO_STYLE) --go_out=./gen_result/pb --go-grpc_out=./gen_result/pb --zrpc_out=.
$(GO_CTL_NAME) rpc protoc ./generate/protobuf/notification.proto -m --style=$(GO_ZERO_STYLE) --go_out=./gen_result/pb --go-grpc_out=./gen_result/pb --zrpc_out=.
copy ./etc/service.yaml ./etc/service.example.yaml
go mod tidy
@echo "Generate core-api files successfully"
@ -46,3 +46,4 @@ build-docker:
cp ./build/Dockerfile Dockerfile
docker buildx build -t $(DOCKER_REPO):$(VERSION) --build-arg SSH_PRIVATE_KEY="$(cat ~/.ssh/ed_25519)" .
rm -rf Dockerfile
@echo "Generate core-api files successfully"

View File

@ -32,7 +32,7 @@ RUN --mount=type=ssh go mod download
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
-ldflags "$FLAG" \
-o service
-o notification
##########
## FINAL #
@ -44,4 +44,4 @@ WORKDIR /app
COPY --from=builder /app/service /app/service
COPY --from=builder /app/etc/service.yaml /app/etc/service.yaml
EXPOSE 8080
CMD ["/app/service"]
CMD ["/app/notification"]

View File

@ -1,5 +1,5 @@
// Code generated by goctl. DO NOT EDIT.
// Source: service.proto
// Source: notification.proto
package senderservice
@ -22,11 +22,11 @@ type (
SenderService interface {
// SendMail 寄信
SendMail(ctx context.Context, in *SendMailReq, opts ...grpc.CallOption) (*OKResp, error)
// SendSMS 寄簡訊
// SendSms 寄簡訊
SendSms(ctx context.Context, in *SendSMSReq, opts ...grpc.CallOption) (*OKResp, error)
// SendMailByTemplateID 寄送模板信件
// SendMailByTemplateId 寄送模板信件
SendMailByTemplateId(ctx context.Context, in *SendByTemplateIDReq, opts ...grpc.CallOption) (*OKResp, error)
// SendSMSByTemplateID 寄送模板簡訊
// SendSmsByTemplateId 寄送模板簡訊
SendSmsByTemplateId(ctx context.Context, in *SendByTemplateIDReq, opts ...grpc.CallOption) (*OKResp, error)
}
@ -47,19 +47,19 @@ func (m *defaultSenderService) SendMail(ctx context.Context, in *SendMailReq, op
return client.SendMail(ctx, in, opts...)
}
// SendSMS 寄簡訊
// SendSms 寄簡訊
func (m *defaultSenderService) SendSms(ctx context.Context, in *SendSMSReq, opts ...grpc.CallOption) (*OKResp, error) {
client := notification.NewSenderServiceClient(m.cli.Conn())
return client.SendSms(ctx, in, opts...)
}
// SendMailByTemplateID 寄送模板信件
// SendMailByTemplateId 寄送模板信件
func (m *defaultSenderService) SendMailByTemplateId(ctx context.Context, in *SendByTemplateIDReq, opts ...grpc.CallOption) (*OKResp, error) {
client := notification.NewSenderServiceClient(m.cli.Conn())
return client.SendMailByTemplateId(ctx, in, opts...)
}
// SendSMSByTemplateID 寄送模板簡訊
// SendSmsByTemplateId 寄送模板簡訊
func (m *defaultSenderService) SendSmsByTemplateId(ctx context.Context, in *SendByTemplateIDReq, opts ...grpc.CallOption) (*OKResp, error) {
client := notification.NewSenderServiceClient(m.cli.Conn())
return client.SendSmsByTemplateId(ctx, in, opts...)

16
etc/service.example.yaml Normal file
View File

@ -0,0 +1,16 @@
Name: notification.rpc
ListenOn: 0.0.0.0:8003
Etcd:
Hosts:
- 127.0.0.1:2379
Key: notification.rpc
SMTP:
Host: smtp.mail.host
Port: 25
User: smtp@user.net
Password: smtp_password
SMSSender:
User: sms@user.net
Password : sms_password

View File

@ -1,6 +0,0 @@
Name: service.rpc
ListenOn: 0.0.0.0:8080
Etcd:
Hosts:
- docker-etcd.digimon.orb.local:2379
Key: service.rpc

View File

@ -13,11 +13,13 @@ message SendMailReq {
string to = 1;
string subject = 2;
string body = 3;
string from =4;
}
message SendSMSReq {
string to = 1;
string body = 2;
string recipient_name=3;
}
message SendByTemplateIDReq {
@ -31,11 +33,11 @@ message SendByTemplateIDReq {
service SenderService {
// SendMail
rpc SendMail(SendMailReq) returns(OKResp);
// SendSMS
// SendSms
rpc SendSms(SendSMSReq) returns(OKResp);
// SendMailByTemplateID
// SendMailByTemplateId
rpc SendMailByTemplateId(SendByTemplateIDReq) returns(OKResp);
// SendSMSByTemplateID
// SendSmsByTemplateId
rpc SendSmsByTemplateId(SendByTemplateIDReq) returns(OKResp);
}

15
go.mod
View File

@ -3,10 +3,14 @@ module app-cloudep-notification-service
go 1.22.3
require (
code.30cm.net/digimon/library-go/errors v1.0.0
code.30cm.net/digimon/library-go/errs v1.2.3
code.30cm.net/digimon/library-go/validator v1.0.0
code.30cm.net/digimon/library-go/worker_pool v0.0.0-20240820153352-f9c90a90f5e2
github.com/minchao/go-mitake v1.0.0
github.com/zeromicro/go-zero v1.7.0
google.golang.org/grpc v1.65.0
google.golang.org/protobuf v1.34.2
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
)
require (
@ -19,11 +23,15 @@ require (
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/fatih/color v1.17.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.22.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/mock v1.6.0 // indirect
github.com/golang/protobuf v1.5.4 // indirect
@ -34,6 +42,7 @@ require (
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
@ -41,6 +50,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/openzipkin/zipkin-go v0.4.3 // indirect
github.com/panjf2000/ants/v2 v2.10.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/prometheus/client_golang v1.19.1 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
@ -66,14 +76,17 @@ require (
go.uber.org/automaxprocs v1.5.3 // indirect
go.uber.org/multierr v1.9.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/oauth2 v0.20.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/term v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240711142825-46eb208f015d // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect

View File

@ -4,4 +4,16 @@ import "github.com/zeromicro/go-zero/zrpc"
type Config struct {
zrpc.RpcServerConf
SMTP struct {
Host string
Port int
User string
Password string
}
SMSSender struct {
User string
Password string
}
}

58
internal/domain/errors.go Normal file
View File

@ -0,0 +1,58 @@
package domain
import (
"fmt"
"strings"
"code.30cm.net/digimon/library-go/errs"
"code.30cm.net/digimon/library-go/errs/code"
"github.com/zeromicro/go-zero/core/logx"
)
type ErrorCode uint32
func (e ErrorCode) ToUint32() uint32 {
return uint32(e)
}
const (
_ = iota
SendMailErrorCode ErrorCode = iota
SendSMSErrorCode
)
// SendMailError ...
func SendMailError(s ...string) *errs.LibError {
return errs.NewError(code.CloudEPNotification, code.ThirdParty,
SendMailErrorCode.ToUint32(),
fmt.Sprintf("%s", strings.Join(s, " ")))
}
// SendMailErrorL logs error message and returns Err
func SendMailErrorL(l logx.Logger, filed []logx.LogField, s ...string) *errs.LibError {
e := SendMailError(s...)
if filed != nil || len(filed) >= 0 {
l.WithCallerSkip(1).WithFields(filed...).Error(e.Error())
}
l.WithCallerSkip(1).Error(e.Error())
return e
}
// SendSMSError ...
func SendSMSError(s ...string) *errs.LibError {
return errs.NewError(code.CloudEPNotification, code.ThirdParty,
SendSMSErrorCode.ToUint32(),
fmt.Sprintf("%s", strings.Join(s, " ")))
}
// SendSMSErrorL logs error message and returns Err
func SendSMSErrorL(l logx.Logger, filed []logx.LogField, s ...string) *errs.LibError {
e := SendSMSError(s...)
if filed != nil || len(filed) >= 0 {
l.WithCallerSkip(1).WithFields(filed...).Error(e.Error())
}
l.WithCallerSkip(1).Error(e.Error())
return e
}

View File

@ -0,0 +1,18 @@
package usecase
import "context"
type MailClientUseCase interface {
SendMail(ctx context.Context, req MailReq) error
}
type MailReq struct {
To string
From string
Subject string
Body string
}
type MailUseCase interface {
GetMailTemplateByID(ctx context.Context, tid int64) ([]rune, error)
}

View File

@ -0,0 +1,18 @@
package usecase
import (
"context"
)
type SMSClientUseCase interface {
SendSMS(ctx context.Context, req SMSReq) error
}
type SMSReq struct {
// RecipientAddress 接收者號碼
RecipientAddress string
// RecipientName 接收者姓名
RecipientName string
// Body 要傳送的訊息
Body string
}

View File

@ -1,13 +1,10 @@
package senderservicelogic
import (
"context"
"fmt"
"app-cloudep-notification-service/gen_result/pb/notification"
"app-cloudep-notification-service/internal/svc"
"context"
ers "code.30cm.net/digimon/library-go/errors"
"github.com/zeromicro/go-zero/core/logx"
)
@ -27,7 +24,6 @@ func NewSendMailByTemplateIdLogic(ctx context.Context, svcCtx *svc.ServiceContex
// SendMailByTemplateId 寄送模板信件
func (l *SendMailByTemplateIdLogic) SendMailByTemplateId(in *notification.SendByTemplateIDReq) (*notification.OKResp, error) {
fmt.Println(ers.ResourceNotFound("testing"))
return &notification.OKResp{}, nil
}

View File

@ -1,8 +1,12 @@
package senderservicelogic
import (
"app-cloudep-notification-service/internal/domain"
"app-cloudep-notification-service/internal/domain/usecase"
"context"
ers "code.30cm.net/digimon/library-go/errs"
"app-cloudep-notification-service/gen_result/pb/notification"
"app-cloudep-notification-service/internal/svc"
@ -23,9 +27,46 @@ func NewSendMailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendMail
}
}
type sendMailReq struct {
// TO 收件者
To string `validate:"required,email"`
// Subject 信件主旨
Subject string `validate:"required,max=128"`
// Body 內容
Body string `validate:"required"`
// From 寄件者
From string `validate:"required"`
}
// SendMail 寄信
func (l *SendMailLogic) SendMail(in *notification.SendMailReq) (*notification.OKResp, error) {
// todo: add your logic here and delete this line
if err := l.svcCtx.Validate.ValidateAll(&sendMailReq{
To: in.GetTo(),
Subject: in.GetSubject(),
Body: in.GetBody(),
From: in.GetFrom(),
}); err != nil {
return nil, ers.InvalidFormat("invalid format")
}
// TODO 以後可以做換線
err := l.svcCtx.MailSender.SendMail(l.ctx, usecase.MailReq{
To: in.GetTo(),
Subject: in.GetSubject(),
From: in.GetFrom(),
Body: in.GetBody(),
})
if err != nil {
return nil, domain.SendMailErrorL(
logx.WithContext(l.ctx),
[]logx.LogField{
logx.Field("func", "MailSender.SendMail"),
logx.Field("in", in),
logx.Field("err", err),
},
"MailSender.SendMail failed to send mail",
)
}
return &notification.OKResp{}, nil
}

View File

@ -23,7 +23,7 @@ func NewSendSmsByTemplateIdLogic(ctx context.Context, svcCtx *svc.ServiceContext
}
}
// SendSMSByTemplateID 寄送模板簡訊
// SendSmsByTemplateId 寄送模板簡訊
func (l *SendSmsByTemplateIdLogic) SendSmsByTemplateId(in *notification.SendByTemplateIDReq) (*notification.OKResp, error) {
// todo: add your logic here and delete this line

View File

@ -1,8 +1,12 @@
package senderservicelogic
import (
"app-cloudep-notification-service/internal/domain"
"app-cloudep-notification-service/internal/domain/usecase"
"context"
"code.30cm.net/digimon/library-go/errs"
"app-cloudep-notification-service/gen_result/pb/notification"
"app-cloudep-notification-service/internal/svc"
@ -23,9 +27,42 @@ func NewSendSmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendSmsLo
}
}
// SendSMS 寄簡訊
type sendSMSReq struct {
// RecipientAddress 收件者
RecipientAddress string `validate:"required,phone"`
// Body 內容
Body string `validate:"required"`
// RecipientName 收件者信名
RecipientName string `validate:"required"`
}
// SendSms 寄簡訊
func (l *SendSmsLogic) SendSms(in *notification.SendSMSReq) (*notification.OKResp, error) {
// todo: add your logic here and delete this line
if err := l.svcCtx.Validate.ValidateAll(&sendSMSReq{
RecipientName: in.GetTo(),
Body: in.GetBody(),
RecipientAddress: in.GetTo(),
}); err != nil {
return nil, errs.InvalidFormat(err.Error())
}
// TODO 以後可以做換線
err := l.svcCtx.SMSSender.SendSMS(l.ctx, usecase.SMSReq{
RecipientAddress: in.GetTo(),
RecipientName: in.GetRecipientName(),
Body: in.GetBody(),
})
if err != nil {
return nil, domain.SendSMSErrorL(
logx.WithContext(l.ctx),
[]logx.LogField{
logx.Field("func", "SMSSender.SendSMS"),
logx.Field("in", in),
logx.Field("err", err),
},
"SMSSender.SendSMS failed to send sms",
)
}
return &notification.OKResp{}, nil
}

View File

@ -1,5 +1,5 @@
// Code generated by goctl. DO NOT EDIT.
// Source: service.proto
// Source: notification.proto
package server
@ -8,6 +8,7 @@ import (
"app-cloudep-notification-service/gen_result/pb/notification"
senderservicelogic "app-cloudep-notification-service/internal/logic/senderservice"
"app-cloudep-notification-service/internal/svc"
)
@ -28,19 +29,19 @@ func (s *SenderServiceServer) SendMail(ctx context.Context, in *notification.Sen
return l.SendMail(in)
}
// SendSMS 寄簡訊
// SendSms 寄簡訊
func (s *SenderServiceServer) SendSms(ctx context.Context, in *notification.SendSMSReq) (*notification.OKResp, error) {
l := senderservicelogic.NewSendSmsLogic(ctx, s.svcCtx)
return l.SendSms(in)
}
// SendMailByTemplateID 寄送模板信件
// SendMailByTemplateId 寄送模板信件
func (s *SenderServiceServer) SendMailByTemplateId(ctx context.Context, in *notification.SendByTemplateIDReq) (*notification.OKResp, error) {
l := senderservicelogic.NewSendMailByTemplateIdLogic(ctx, s.svcCtx)
return l.SendMailByTemplateId(in)
}
// SendSMSByTemplateID 寄送模板簡訊
// SendSmsByTemplateId 寄送模板簡訊
func (s *SenderServiceServer) SendSmsByTemplateId(ctx context.Context, in *notification.SendByTemplateIDReq) (*notification.OKResp, error) {
l := senderservicelogic.NewSendSmsByTemplateIdLogic(ctx, s.svcCtx)
return l.SendSmsByTemplateId(in)

View File

@ -1,13 +1,30 @@
package svc
import "app-cloudep-notification-service/internal/config"
import (
"app-cloudep-notification-service/internal/config"
domainUC "app-cloudep-notification-service/internal/domain/usecase"
"app-cloudep-notification-service/internal/usecase"
"code.30cm.net/digimon/library-go/errs"
"code.30cm.net/digimon/library-go/errs/code"
v "code.30cm.net/digimon/library-go/validator"
)
type ServiceContext struct {
Config config.Config
Validate v.Validate
MailSender domainUC.MailClientUseCase
SMSSender domainUC.SMSClientUseCase
}
func NewServiceContext(c config.Config) *ServiceContext {
errs.Scope = code.CloudEPNotification
return &ServiceContext{
Config: c,
Config: c,
MailSender: usecase.MustMailgunUseCase(usecase.MailUseCaseParam{Conf: c}),
SMSSender: usecase.MustMitakeUseCase(usecase.SMSUseCaseParam{Conf: c}),
Validate: v.MustValidator(),
}
}

View File

@ -0,0 +1,36 @@
package usecase
import (
"app-cloudep-notification-service/internal/config"
"app-cloudep-notification-service/internal/domain/usecase"
"context"
"github.com/minchao/go-mitake"
)
type SMSUseCaseParam struct {
Conf config.Config
}
type SMSUseCase struct {
Client *mitake.Client
}
func (s *SMSUseCase) SendSMS(_ context.Context, req usecase.SMSReq) error {
message := mitake.Message{
Dstaddr: req.RecipientAddress,
Destname: req.RecipientName,
Smbody: req.Body,
}
_, err := s.Client.Send(message)
if err != nil {
return err
}
return nil
}
func MustMitakeUseCase(param SMSUseCaseParam) usecase.SMSClientUseCase {
return &SMSUseCase{
Client: mitake.NewClient(param.Conf.SMSSender.User, param.Conf.SMSSender.Password, nil),
}
}

56
internal/usecase/smtp.go Normal file
View File

@ -0,0 +1,56 @@
package usecase
import (
"app-cloudep-notification-service/internal/config"
"app-cloudep-notification-service/internal/domain/usecase"
pool "code.30cm.net/digimon/library-go/worker_pool"
"github.com/zeromicro/go-zero/core/logx"
"context"
"gopkg.in/gomail.v2"
)
type MailUseCaseParam struct {
Conf config.Config
}
type MailUseCase struct {
Host string
Port int
User string
Password string
Pool pool.WorkerPool
}
func (mu *MailUseCase) SendMail(_ context.Context, req usecase.MailReq) error {
// 用 goroutine pool 送,否則會超時
err := mu.Pool.Submit(func() {
m := gomail.NewMessage()
m.SetHeader("From", req.From)
m.SetHeader("To", req.To)
m.SetHeader("Subject", req.Subject)
m.SetBody("text/html", req.Body)
d := gomail.NewDialer(mu.Host, mu.Port, mu.User, mu.Password)
if err := d.DialAndSend(m); err != nil {
logx.WithCallerSkip(1).WithFields(
logx.Field("func", "MailUseCase.SendMail"),
logx.Field("req", req),
logx.Field("err", err),
).Error("failed to send mail by mailgun")
}
})
return err
}
func MustMailgunUseCase(param MailUseCaseParam) usecase.MailClientUseCase {
return &MailUseCase{
Host: param.Conf.SMTP.Host,
Port: param.Conf.SMTP.Port,
User: param.Conf.SMTP.User,
Password: param.Conf.SMTP.Password,
Pool: pool.NewWorkerPool(2000),
}
}

View File

@ -16,7 +16,7 @@ import (
"google.golang.org/grpc/reflection"
)
var configFile = flag.String("f", "etc/service.yaml", "the config file")
var configFile = flag.String("f", "etc/notification.yaml", "the config file")
func main() {
flag.Parse()