add code third party code #5

Merged
daniel.w merged 1 commits from feature/errv2 into main 2024-08-27 14:25:47 +00:00
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 \
-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

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

2
go.mod
View File

@ -3,7 +3,7 @@ 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

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/internal/svc"
"context"
"fmt"
ers "code.30cm.net/digimon/library-go/errors"
"github.com/zeromicro/go-zero/core/logx"
)
@ -26,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,10 +1,11 @@
package senderservicelogic
import (
"app-cloudep-notification-service/internal/domain"
"app-cloudep-notification-service/internal/domain/usecase"
"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/internal/svc"
@ -45,7 +46,7 @@ func (l *SendMailLogic) SendMail(in *notification.SendMailReq) (*notification.OK
Body: in.GetBody(),
From: in.GetFrom(),
}); err != nil {
return nil, ers.InvalidFormat(err.Error())
return nil, ers.InvalidFormat("invalid format")
}
// TODO 以後可以做換線
@ -56,12 +57,15 @@ func (l *SendMailLogic) SendMail(in *notification.SendMailReq) (*notification.OK
Body: in.GetBody(),
})
if err != nil {
logx.WithCallerSkip(1).WithFields(
logx.Field("func", "MailSender.SendMail"),
logx.Field("in", in),
logx.Field("err", err),
).Error(err.Error())
return nil, ers.ArkInternal("MailSender.SendMail failed to send mail")
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,10 +1,11 @@
package senderservicelogic
import (
"app-cloudep-notification-service/internal/domain"
"app-cloudep-notification-service/internal/domain/usecase"
"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/internal/svc"
@ -42,7 +43,7 @@ func (l *SendSmsLogic) SendSms(in *notification.SendSMSReq) (*notification.OKRes
Body: in.GetBody(),
RecipientAddress: in.GetTo(),
}); err != nil {
return nil, ers.InvalidFormat(err.Error())
return nil, errs.InvalidFormat(err.Error())
}
// TODO 以後可以做換線
@ -52,12 +53,15 @@ func (l *SendSmsLogic) SendSms(in *notification.SendSMSReq) (*notification.OKRes
Body: in.GetBody(),
})
if err != nil {
logx.WithCallerSkip(1).WithFields(
logx.Field("func", "SMSSender.SendSMS"),
logx.Field("in", in),
logx.Field("err", err),
).Error(err.Error())
return nil, ers.ArkInternal("SMSSender.SendSMS failed to send sms")
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

@ -5,6 +5,9 @@ import (
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"
)
@ -17,6 +20,7 @@ type ServiceContext struct {
}
func NewServiceContext(c config.Config) *ServiceContext {
errs.Scope = code.CloudEPNotification
return &ServiceContext{
Config: c,
MailSender: usecase.MustMailgunUseCase(usecase.MailUseCaseParam{Conf: c}),