Compare commits

..

No commits in common. "b8b64302d67f8b740cfe8a5be8f3cdf3c85dd918" and "7567a234d5824374dc66f560d4edf9daae6b2e8c" have entirely different histories.

17 changed files with 88 additions and 163 deletions

View File

@ -117,14 +117,6 @@ issues:
- gocognit - gocognit
- contextcheck - contextcheck
exclude-dirs:
- internal/model
exclude-files:
- .*_test.go
linters-settings: linters-settings:
gci: gci:
sections: sections:

View File

@ -18,7 +18,6 @@ test: # 進行測試
fmt: # 格式優化 fmt: # 格式優化
$(GOFMT) -w $(GOFILES) $(GOFMT) -w $(GOFILES)
goimports -w ./ goimports -w ./
golangci-lint run
.PHONY: gen-rpc .PHONY: gen-rpc
gen-rpc: # 建立 rpc code gen-rpc: # 建立 rpc code

View File

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

View File

@ -24,10 +24,10 @@ type (
SendMail(ctx context.Context, in *SendMailReq, opts ...grpc.CallOption) (*OKResp, error) SendMail(ctx context.Context, in *SendMailReq, opts ...grpc.CallOption) (*OKResp, error)
// SendSms 寄簡訊 // SendSms 寄簡訊
SendSms(ctx context.Context, in *SendSMSReq, opts ...grpc.CallOption) (*OKResp, error) SendSms(ctx context.Context, in *SendSMSReq, opts ...grpc.CallOption) (*OKResp, error)
// SendMailByTemplateID 寄送模板信件 // SendMailByTemplateId 寄送模板信件
SendMailByTemplateID(ctx context.Context, in *SendByTemplateIDReq, opts ...grpc.CallOption) (*OKResp, error) SendMailByTemplateId(ctx context.Context, in *SendByTemplateIDReq, opts ...grpc.CallOption) (*OKResp, error)
// SendSmsByTemplateID 寄送模板簡訊 // SendSmsByTemplateId 寄送模板簡訊
SendSmsByTemplateID(ctx context.Context, in *SendByTemplateIDReq, opts ...grpc.CallOption) (*OKResp, error) SendSmsByTemplateId(ctx context.Context, in *SendByTemplateIDReq, opts ...grpc.CallOption) (*OKResp, error)
} }
defaultSenderService struct { defaultSenderService struct {
@ -53,14 +53,14 @@ func (m *defaultSenderService) SendSms(ctx context.Context, in *SendSMSReq, opts
return client.SendSms(ctx, in, opts...) return client.SendSms(ctx, in, opts...)
} }
// SendMailByTemplateID 寄送模板信件 // SendMailByTemplateId 寄送模板信件
func (m *defaultSenderService) SendMailByTemplateID(ctx context.Context, in *SendByTemplateIDReq, opts ...grpc.CallOption) (*OKResp, error) { func (m *defaultSenderService) SendMailByTemplateId(ctx context.Context, in *SendByTemplateIDReq, opts ...grpc.CallOption) (*OKResp, error) {
client := notification.NewSenderServiceClient(m.cli.Conn()) client := notification.NewSenderServiceClient(m.cli.Conn())
return client.SendMailByTemplateID(ctx, in, opts...) return client.SendMailByTemplateId(ctx, in, opts...)
} }
// SendSmsByTemplateID 寄送模板簡訊 // SendSmsByTemplateId 寄送模板簡訊
func (m *defaultSenderService) SendSmsByTemplateID(ctx context.Context, in *SendByTemplateIDReq, opts ...grpc.CallOption) (*OKResp, error) { func (m *defaultSenderService) SendSmsByTemplateId(ctx context.Context, in *SendByTemplateIDReq, opts ...grpc.CallOption) (*OKResp, error) {
client := notification.NewSenderServiceClient(m.cli.Conn()) client := notification.NewSenderServiceClient(m.cli.Conn())
return client.SendSmsByTemplateID(ctx, in, opts...) return client.SendSmsByTemplateId(ctx, in, opts...)
} }

View File

@ -5,12 +5,12 @@ Etcd:
- 127.0.0.1:2379 - 127.0.0.1:2379
Key: notification.rpc Key: notification.rpc
SMTP: SMTP:
Host: smtp.mail.host Host: smtp.mailgun.org
Port: 25 Port: 25
User: smtp@user.net User: postmaster@abc123@30.com
Password: smtp_password Password: gg88g88
SMSSender: SMSSender:
User: sms@user.net User: daniel@30cm.net
Password : sms_password Password : test123

View File

@ -35,10 +35,10 @@ service SenderService {
rpc SendMail(SendMailReq) returns(OKResp); rpc SendMail(SendMailReq) returns(OKResp);
// SendSms // SendSms
rpc SendSms(SendSMSReq) returns(OKResp); rpc SendSms(SendSMSReq) returns(OKResp);
// SendMailByTemplateID // SendMailByTemplateId
rpc SendMailByTemplateID(SendByTemplateIDReq) returns(OKResp); rpc SendMailByTemplateId(SendByTemplateIDReq) returns(OKResp);
// SendSmsByTemplateID // SendSmsByTemplateId
rpc SendSmsByTemplateID(SendByTemplateIDReq) returns(OKResp); rpc SendSmsByTemplateId(SendByTemplateIDReq) returns(OKResp);
} }

2
go.mod
View File

@ -3,7 +3,7 @@ module app-cloudep-notification-service
go 1.22.3 go 1.22.3
require ( require (
code.30cm.net/digimon/library-go/errs v1.2.3 code.30cm.net/digimon/library-go/errors v1.0.0
code.30cm.net/digimon/library-go/validator v1.0.0 code.30cm.net/digimon/library-go/validator v1.0.0
code.30cm.net/digimon/library-go/worker_pool v0.0.0-20240820153352-f9c90a90f5e2 code.30cm.net/digimon/library-go/worker_pool v0.0.0-20240820153352-f9c90a90f5e2
github.com/minchao/go-mitake v1.0.0 github.com/minchao/go-mitake v1.0.0

View File

@ -1,55 +0,0 @@
package domain
import (
"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(), 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(), 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

@ -1,31 +0,0 @@
package senderservicelogic
import (
"context"
"app-cloudep-notification-service/gen_result/pb/notification"
"app-cloudep-notification-service/internal/svc"
"github.com/zeromicro/go-zero/core/logx"
)
type SendMailByTemplateIDLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewSendMailByTemplateIDLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendMailByTemplateIDLogic {
return &SendMailByTemplateIDLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
// SendMailByTemplateID 寄送模板信件
func (l *SendMailByTemplateIDLogic) SendMailByTemplateID(_ *notification.SendByTemplateIDReq) (*notification.OKResp, error) {
// todo: add your logic here and delete this line
return &notification.OKResp{}, nil
}

View File

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

View File

@ -1,11 +1,10 @@
package senderservicelogic package senderservicelogic
import ( import (
"app-cloudep-notification-service/internal/domain"
"app-cloudep-notification-service/internal/domain/usecase" "app-cloudep-notification-service/internal/domain/usecase"
"context" "context"
ers "code.30cm.net/digimon/library-go/errs" ers "code.30cm.net/digimon/library-go/errors"
"app-cloudep-notification-service/gen_result/pb/notification" "app-cloudep-notification-service/gen_result/pb/notification"
"app-cloudep-notification-service/internal/svc" "app-cloudep-notification-service/internal/svc"
@ -46,7 +45,7 @@ func (l *SendMailLogic) SendMail(in *notification.SendMailReq) (*notification.OK
Body: in.GetBody(), Body: in.GetBody(),
From: in.GetFrom(), From: in.GetFrom(),
}); err != nil { }); err != nil {
return nil, ers.InvalidFormat("invalid format") return nil, ers.InvalidFormat(err.Error())
} }
// TODO 以後可以做換線 // TODO 以後可以做換線
@ -57,15 +56,12 @@ func (l *SendMailLogic) SendMail(in *notification.SendMailReq) (*notification.OK
Body: in.GetBody(), Body: in.GetBody(),
}) })
if err != nil { if err != nil {
return nil, domain.SendMailErrorL( logx.WithCallerSkip(1).WithFields(
logx.WithContext(l.ctx),
[]logx.LogField{
logx.Field("func", "MailSender.SendMail"), logx.Field("func", "MailSender.SendMail"),
logx.Field("in", in), logx.Field("in", in),
logx.Field("err", err), logx.Field("err", err),
}, ).Error(err.Error())
"MailSender.SendMail failed to send mail", return nil, ers.ArkInternal("MailSender.SendMail failed to send mail")
)
} }
return &notification.OKResp{}, nil return &notification.OKResp{}, nil

View File

@ -9,22 +9,22 @@ import (
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
) )
type SendSmsByTemplateIDLogic struct { type SendSmsByTemplateIdLogic struct {
ctx context.Context ctx context.Context
svcCtx *svc.ServiceContext svcCtx *svc.ServiceContext
logx.Logger logx.Logger
} }
func NewSendSmsByTemplateIDLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendSmsByTemplateIDLogic { func NewSendSmsByTemplateIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendSmsByTemplateIdLogic {
return &SendSmsByTemplateIDLogic{ return &SendSmsByTemplateIdLogic{
ctx: ctx, ctx: ctx,
svcCtx: svcCtx, svcCtx: svcCtx,
Logger: logx.WithContext(ctx), Logger: logx.WithContext(ctx),
} }
} }
// SendSmsByTemplateID 寄送模板簡訊 // SendSMSByTemplateID 寄送模板簡訊
func (l *SendSmsByTemplateIDLogic) SendSmsByTemplateID(_ *notification.SendByTemplateIDReq) (*notification.OKResp, error) { func (l *SendSmsByTemplateIdLogic) SendSmsByTemplateId(in *notification.SendByTemplateIDReq) (*notification.OKResp, error) {
// todo: add your logic here and delete this line // todo: add your logic here and delete this line
return &notification.OKResp{}, nil return &notification.OKResp{}, nil

View File

@ -1,11 +1,10 @@
package senderservicelogic package senderservicelogic
import ( import (
"app-cloudep-notification-service/internal/domain"
"app-cloudep-notification-service/internal/domain/usecase" "app-cloudep-notification-service/internal/domain/usecase"
"context" "context"
"code.30cm.net/digimon/library-go/errs" ers "code.30cm.net/digimon/library-go/errors"
"app-cloudep-notification-service/gen_result/pb/notification" "app-cloudep-notification-service/gen_result/pb/notification"
"app-cloudep-notification-service/internal/svc" "app-cloudep-notification-service/internal/svc"
@ -43,7 +42,7 @@ func (l *SendSmsLogic) SendSms(in *notification.SendSMSReq) (*notification.OKRes
Body: in.GetBody(), Body: in.GetBody(),
RecipientAddress: in.GetTo(), RecipientAddress: in.GetTo(),
}); err != nil { }); err != nil {
return nil, errs.InvalidFormat(err.Error()) return nil, ers.InvalidFormat(err.Error())
} }
// TODO 以後可以做換線 // TODO 以後可以做換線
@ -53,15 +52,12 @@ func (l *SendSmsLogic) SendSms(in *notification.SendSMSReq) (*notification.OKRes
Body: in.GetBody(), Body: in.GetBody(),
}) })
if err != nil { if err != nil {
return nil, domain.SendSMSErrorL( logx.WithCallerSkip(1).WithFields(
logx.WithContext(l.ctx),
[]logx.LogField{
logx.Field("func", "SMSSender.SendSMS"), logx.Field("func", "SMSSender.SendSMS"),
logx.Field("in", in), logx.Field("in", in),
logx.Field("err", err), logx.Field("err", err),
}, ).Error(err.Error())
"SMSSender.SendSMS failed to send sms", return nil, ers.ArkInternal("SMSSender.SendSMS failed to send sms")
)
} }
return &notification.OKResp{}, nil return &notification.OKResp{}, nil

View File

@ -8,6 +8,7 @@ import (
"app-cloudep-notification-service/gen_result/pb/notification" "app-cloudep-notification-service/gen_result/pb/notification"
senderservicelogic "app-cloudep-notification-service/internal/logic/senderservice" senderservicelogic "app-cloudep-notification-service/internal/logic/senderservice"
"app-cloudep-notification-service/internal/svc" "app-cloudep-notification-service/internal/svc"
) )
@ -34,14 +35,14 @@ func (s *SenderServiceServer) SendSms(ctx context.Context, in *notification.Send
return l.SendSms(in) return l.SendSms(in)
} }
// SendMailByTemplateID 寄送模板信件 // SendMailByTemplateId 寄送模板信件
func (s *SenderServiceServer) SendMailByTemplateID(ctx context.Context, in *notification.SendByTemplateIDReq) (*notification.OKResp, error) { func (s *SenderServiceServer) SendMailByTemplateId(ctx context.Context, in *notification.SendByTemplateIDReq) (*notification.OKResp, error) {
l := senderservicelogic.NewSendMailByTemplateIDLogic(ctx, s.svcCtx) l := senderservicelogic.NewSendMailByTemplateIdLogic(ctx, s.svcCtx)
return l.SendMailByTemplateID(in) return l.SendMailByTemplateId(in)
} }
// SendSmsByTemplateID 寄送模板簡訊 // SendSmsByTemplateId 寄送模板簡訊
func (s *SenderServiceServer) SendSmsByTemplateID(ctx context.Context, in *notification.SendByTemplateIDReq) (*notification.OKResp, error) { func (s *SenderServiceServer) SendSmsByTemplateId(ctx context.Context, in *notification.SendByTemplateIDReq) (*notification.OKResp, error) {
l := senderservicelogic.NewSendSmsByTemplateIDLogic(ctx, s.svcCtx) l := senderservicelogic.NewSendSmsByTemplateIdLogic(ctx, s.svcCtx)
return l.SendSmsByTemplateID(in) return l.SendSmsByTemplateId(in)
} }

View File

@ -5,9 +5,6 @@ import (
domainUC "app-cloudep-notification-service/internal/domain/usecase" domainUC "app-cloudep-notification-service/internal/domain/usecase"
"app-cloudep-notification-service/internal/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" v "code.30cm.net/digimon/library-go/validator"
) )
@ -20,8 +17,6 @@ type ServiceContext struct {
} }
func NewServiceContext(c config.Config) *ServiceContext { func NewServiceContext(c config.Config) *ServiceContext {
errs.Scope = code.CloudEPNotification
return &ServiceContext{ return &ServiceContext{
Config: c, Config: c,
MailSender: usecase.MustMailgunUseCase(usecase.MailUseCaseParam{Conf: c}), MailSender: usecase.MustMailgunUseCase(usecase.MailUseCaseParam{Conf: c}),

View File

@ -26,7 +26,6 @@ func (s *SMSUseCase) SendSMS(_ context.Context, req usecase.SMSReq) error {
if err != nil { if err != nil {
return err return err
} }
return nil return nil
} }

View File

@ -1,12 +1,13 @@
package main package main
import ( import (
"flag"
"fmt"
"app-cloudep-notification-service/gen_result/pb/notification" "app-cloudep-notification-service/gen_result/pb/notification"
"app-cloudep-notification-service/internal/config" "app-cloudep-notification-service/internal/config"
senderserviceServer "app-cloudep-notification-service/internal/server/senderservice" senderserviceServer "app-cloudep-notification-service/internal/server/senderservice"
"app-cloudep-notification-service/internal/svc" "app-cloudep-notification-service/internal/svc"
"flag"
"log"
"github.com/zeromicro/go-zero/core/conf" "github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/core/service" "github.com/zeromicro/go-zero/core/service"
@ -33,6 +34,6 @@ func main() {
}) })
defer s.Stop() defer s.Stop()
log.Printf("Starting rpc server at %s...\n", c.ListenOn) fmt.Printf("Starting rpc server at %s...\n", c.ListenOn)
s.Start() s.Start()
} }