add code third party code (#5)

Co-authored-by: daniel.w <daniel.w@intteam.net>
Reviewed-on: #5
This commit is contained in:
王性驊 2024-08-27 14:25:45 +00:00
parent 7567a234d5
commit b3cfd0afaa
9 changed files with 95 additions and 28 deletions

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 service -o notification
########## ##########
## 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/service"] CMD ["/app/notification"]

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.mailgun.org Host: smtp.mail.host
Port: 25 Port: 25
User: postmaster@abc123@30.com User: smtp@user.net
Password: gg88g88 Password: smtp_password
SMSSender: SMSSender:
User: daniel@30cm.net User: sms@user.net
Password : test123 Password : sms_password

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/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/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

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

@ -4,9 +4,7 @@ import (
"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"
"context" "context"
"fmt"
ers "code.30cm.net/digimon/library-go/errors"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
) )
@ -26,7 +24,6 @@ func NewSendMailByTemplateIdLogic(ctx context.Context, svcCtx *svc.ServiceContex
// SendMailByTemplateId 寄送模板信件 // SendMailByTemplateId 寄送模板信件
func (l *SendMailByTemplateIdLogic) SendMailByTemplateId(in *notification.SendByTemplateIDReq) (*notification.OKResp, error) { func (l *SendMailByTemplateIdLogic) SendMailByTemplateId(in *notification.SendByTemplateIDReq) (*notification.OKResp, error) {
fmt.Println(ers.ResourceNotFound("testing"))
return &notification.OKResp{}, nil return &notification.OKResp{}, nil
} }

View File

@ -1,10 +1,11 @@
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/errors" ers "code.30cm.net/digimon/library-go/errs"
"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"
@ -45,7 +46,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(err.Error()) return nil, ers.InvalidFormat("invalid format")
} }
// TODO 以後可以做換線 // TODO 以後可以做換線
@ -56,12 +57,15 @@ func (l *SendMailLogic) SendMail(in *notification.SendMailReq) (*notification.OK
Body: in.GetBody(), Body: in.GetBody(),
}) })
if err != nil { if err != nil {
logx.WithCallerSkip(1).WithFields( return nil, domain.SendMailErrorL(
logx.Field("func", "MailSender.SendMail"), logx.WithContext(l.ctx),
logx.Field("in", in), []logx.LogField{
logx.Field("err", err), logx.Field("func", "MailSender.SendMail"),
).Error(err.Error()) logx.Field("in", in),
return nil, ers.ArkInternal("MailSender.SendMail failed to send mail") logx.Field("err", err),
},
"MailSender.SendMail failed to send mail",
)
} }
return &notification.OKResp{}, nil 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) { 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

View File

@ -1,10 +1,11 @@
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/errors" "code.30cm.net/digimon/library-go/errs"
"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"
@ -42,7 +43,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, ers.InvalidFormat(err.Error()) return nil, errs.InvalidFormat(err.Error())
} }
// TODO 以後可以做換線 // TODO 以後可以做換線
@ -52,12 +53,15 @@ func (l *SendSmsLogic) SendSms(in *notification.SendSMSReq) (*notification.OKRes
Body: in.GetBody(), Body: in.GetBody(),
}) })
if err != nil { if err != nil {
logx.WithCallerSkip(1).WithFields( return nil, domain.SendSMSErrorL(
logx.Field("func", "SMSSender.SendSMS"), logx.WithContext(l.ctx),
logx.Field("in", in), []logx.LogField{
logx.Field("err", err), logx.Field("func", "SMSSender.SendSMS"),
).Error(err.Error()) logx.Field("in", in),
return nil, ers.ArkInternal("SMSSender.SendSMS failed to send sms") logx.Field("err", err),
},
"SMSSender.SendSMS failed to send sms",
)
} }
return &notification.OKResp{}, nil return &notification.OKResp{}, nil

View File

@ -5,6 +5,9 @@ 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"
) )
@ -17,6 +20,7 @@ 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}),