Compare commits

..

No commits in common. "5b9355c4e9154f085dbafcd7ffe493a4b14f432b" and "8226e179ab133da96a5327360c28af15afe28b5b" have entirely different histories.

19 changed files with 25 additions and 430 deletions

5
.gitignore vendored
View File

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

View File

@ -1,140 +0,0 @@
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

@ -21,7 +21,7 @@ fmt: # 格式優化
.PHONY: gen-rpc
gen-rpc: # 建立 rpc code
$(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=.
$(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=.
copy ./etc/service.yaml ./etc/service.example.yaml
go mod tidy
@echo "Generate core-api files successfully"

View File

@ -1,5 +1,5 @@
// Code generated by goctl. DO NOT EDIT.
// Source: notification.proto
// Source: service.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...)

View File

@ -1,16 +0,0 @@
Name: service.rpc
ListenOn: 0.0.0.0:8080
Etcd:
Hosts:
- 127.0.0.1:2379
Key: service.rpc
SMTP:
Host: smtp.mailgun.org
Port: 25
User: xxx
Password: 000
SMSSender:
User: daniel@30cm.net
Password : test123

View File

@ -4,13 +4,3 @@ Etcd:
Hosts:
- 127.0.0.1:2379
Key: service.rpc
SMTP:
Host: smtp.mailgun.org
Port: 25
User: postmaster@code.30cm.net
Password: 9015592e10f385e4f16705e00a5e0d3d-2b91eb47-31205b4e
SMSSender:
User: daniel@30cm.net
Password : test123

View File

@ -13,13 +13,11 @@ 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 {
@ -33,11 +31,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);
}

13
go.mod
View File

@ -4,13 +4,9 @@ go 1.22.3
require (
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/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 (
@ -23,15 +19,11 @@ 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
@ -42,7 +34,6 @@ 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
@ -50,7 +41,6 @@ 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
@ -76,17 +66,14 @@ 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,16 +4,4 @@ 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
}
}

View File

@ -1,18 +0,0 @@
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

@ -1,18 +0,0 @@
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

@ -3,10 +3,9 @@ package senderservicelogic
import (
"app-cloudep-notification-service/gen_result/pb/notification"
"app-cloudep-notification-service/internal/svc"
ers "code.30cm.net/digimon/library-go/errors"
"context"
"fmt"
ers "code.30cm.net/digimon/library-go/errors"
"github.com/zeromicro/go-zero/core/logx"
)

View File

@ -1,11 +1,8 @@
package senderservicelogic
import (
"app-cloudep-notification-service/internal/domain/usecase"
"context"
ers "code.30cm.net/digimon/library-go/errors"
"app-cloudep-notification-service/gen_result/pb/notification"
"app-cloudep-notification-service/internal/svc"
@ -26,43 +23,9 @@ 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) {
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(err.Error())
}
// 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 {
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")
}
// todo: add your logic here and delete this line
return &notification.OKResp{}, nil
}

View File

@ -1,11 +1,8 @@
package senderservicelogic
import (
"app-cloudep-notification-service/internal/domain/usecase"
"context"
ers "code.30cm.net/digimon/library-go/errors"
"app-cloudep-notification-service/gen_result/pb/notification"
"app-cloudep-notification-service/internal/svc"
@ -26,39 +23,9 @@ func NewSendSmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendSmsLo
}
}
type sendSMSReq struct {
// RecipientAddress 收件者
RecipientAddress string `validate:"required,phone"`
// Body 內容
Body string `validate:"required"`
// RecipientName 收件者信名
RecipientName string `validate:"required"`
}
// SendSms 寄簡訊
// SendSMS 寄簡訊
func (l *SendSmsLogic) SendSms(in *notification.SendSMSReq) (*notification.OKResp, error) {
if err := l.svcCtx.Validate.ValidateAll(&sendSMSReq{
RecipientName: in.GetTo(),
Body: in.GetBody(),
RecipientAddress: in.GetTo(),
}); err != nil {
return nil, ers.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 {
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")
}
// todo: add your logic here and delete this line
return &notification.OKResp{}, nil
}

View File

@ -1,5 +1,5 @@
// Code generated by goctl. DO NOT EDIT.
// Source: notification.proto
// Source: service.proto
package server
@ -7,6 +7,7 @@ import (
"context"
"app-cloudep-notification-service/gen_result/pb/notification"
senderservicelogic "app-cloudep-notification-service/internal/logic/senderservice"
"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,26 +1,13 @@
package svc
import (
"app-cloudep-notification-service/internal/config"
domainUC "app-cloudep-notification-service/internal/domain/usecase"
"app-cloudep-notification-service/internal/usecase"
v "code.30cm.net/digimon/library-go/validator"
)
import "app-cloudep-notification-service/internal/config"
type ServiceContext struct {
Config config.Config
Validate v.Validate
MailSender domainUC.MailClientUseCase
SMSSender domainUC.SMSClientUseCase
}
func NewServiceContext(c config.Config) *ServiceContext {
return &ServiceContext{
Config: c,
MailSender: usecase.MustMailgunUseCase(usecase.MailUseCaseParam{Conf: c}),
SMSSender: usecase.MustMitakeUseCase(usecase.SMSUseCaseParam{Conf: c}),
Validate: v.MustValidator(),
Config: c,
}
}

View File

@ -1,36 +0,0 @@
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),
}
}

View File

@ -1,56 +0,0 @@
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/notification.yaml", "the config file")
var configFile = flag.String("f", "etc/service.yaml", "the config file")
func main() {
flag.Parse()