Compare commits
No commits in common. "main" and "feature/create_service" have entirely different histories.
main
...
feature/cr
|
@ -2,6 +2,5 @@
|
|||
go.sum
|
||||
account/
|
||||
gen_result/
|
||||
etc/notification.yaml
|
||||
client/
|
||||
.DS_Store
|
||||
etc/app-cloudep-notification-service.yaml
|
||||
./client
|
140
.golangci.yaml
140
.golangci.yaml
|
@ -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
|
7
Makefile
7
Makefile
|
@ -1,5 +1,4 @@
|
|||
GO_CTL_NAME=goctl
|
||||
|
||||
# go-zero 生成風格
|
||||
GO_ZERO_STYLE=go_zero
|
||||
|
||||
|
@ -10,6 +9,7 @@ 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/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"
|
||||
|
@ -45,5 +45,4 @@ run-docker: # 建立 rpc code
|
|||
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"
|
||||
rm -rf Dockerfile
|
90
README.md
90
README.md
|
@ -1,90 +0,0 @@
|
|||
# 專案說明
|
||||
|
||||
通知服務
|
||||
|
||||
# 專案架構
|
||||
|
||||
```
|
||||
$tree -L 3 --gitignore
|
||||
|
||||
.
|
||||
├── Makefile
|
||||
├── build
|
||||
│ └── Dockerfile
|
||||
├── chart
|
||||
│ └── readme.md
|
||||
├── etc
|
||||
│ └── service.example.yaml # 設定檔範例
|
||||
├── generate # 這個資料夾會用來自動生成一些程式
|
||||
│ ├── database ## 資料庫生成檔案或紀錄遷移版本
|
||||
│ │ ├── mongodb
|
||||
│ │ ├── mysql
|
||||
│ │ ├── readme.md
|
||||
│ │ └── seeder
|
||||
│ └── protobuf ## gRPC 的定義,用來生成 gRPC 檔案
|
||||
│ └── notification.proto
|
||||
├── go.mod
|
||||
├── internal
|
||||
│ ├── config # 設定的結構
|
||||
│ │ └── config.go
|
||||
│ ├── domain # 此服務下的一些常數定義
|
||||
│ │ ├── errors.go ## 錯誤定義
|
||||
│ │ └── usecase ## usecase 內的常數定義
|
||||
│ │ ├── mail.go
|
||||
│ │ └── sms.go
|
||||
│ ├── logic # 主要的程式邏輯都在這,每個功能用一個資料夾開發
|
||||
│ │ └── senderservice
|
||||
│ ├── server
|
||||
│ │ └── senderservice # gRPC 自動產生的檔案
|
||||
│ ├── svc # 此服務下會共用的功能,例如 API context, db 連線等
|
||||
│ │ └── service_context.go ## API context
|
||||
│ └── usecase
|
||||
│ ├── mitake.go
|
||||
│ └── smtp.go
|
||||
└── notification.go # 服務主入口
|
||||
```
|
||||
|
||||
# 啟動說明
|
||||
|
||||
此文件會介紹以下兩種啟動方式:
|
||||
|
||||
1. Docker 啟動 (Production 時使用)
|
||||
2. 本地端 go compiler 啟動 (開發、測試時使用)
|
||||
|
||||
## Docker 啟動 (Production)
|
||||
|
||||
```bash
|
||||
make build-docker
|
||||
make run-docker
|
||||
```
|
||||
|
||||
## 本地端啟動(測試用)
|
||||
|
||||
```bash
|
||||
go mod tidy
|
||||
# 進入 etc/notification.yml 設定各依賴的 config
|
||||
go run notification.go
|
||||
```
|
||||
|
||||
### 本地啟動需要依賴套件或工具
|
||||
|
||||
以下已 macOS 為例,若已安裝或有其他可用服務則可跳過
|
||||
|
||||
- go-zero goctl (https://go-zero.dev/docs/tasks/installation/goctl)
|
||||
```bash
|
||||
$ go install github.com/zeromicro/go-zero/tools/goctl@latest
|
||||
```
|
||||
- protobuf (https://protobuf.dev/)
|
||||
```bash
|
||||
brew install protobuf
|
||||
```
|
||||
- etcd (https://etcd.io/)
|
||||
```bash
|
||||
brew install etcd
|
||||
brew services start etcd
|
||||
```
|
||||
- SMTP 服務(以 mailhog 為例) (https://github.com/mailhog/MailHog)
|
||||
```bash
|
||||
brew install mailhog
|
||||
brew services start mailhog
|
||||
```
|
|
@ -32,7 +32,7 @@ RUN --mount=type=ssh go mod download
|
|||
|
||||
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
|
||||
-ldflags "$FLAG" \
|
||||
-o notification
|
||||
-o service
|
||||
|
||||
##########
|
||||
## 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/notification"]
|
||||
CMD ["/app/service"]
|
|
@ -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...)
|
||||
|
|
|
@ -1,16 +0,0 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
Name: service.rpc
|
||||
ListenOn: 0.0.0.0:8080
|
||||
Etcd:
|
||||
Hosts:
|
||||
- docker-etcd.digimon.orb.local:2379
|
||||
Key: service.rpc
|
|
@ -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);
|
||||
}
|
||||
|
15
go.mod
15
go.mod
|
@ -3,14 +3,10 @@ module app-cloudep-notification-service
|
|||
go 1.22.3
|
||||
|
||||
require (
|
||||
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
|
||||
code.30cm.net/digimon/library-go/errors 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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,58 +0,0 @@
|
|||
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
|
||||
}
|
|
@ -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)
|
||||
}
|
|
@ -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
|
||||
}
|
|
@ -1,10 +1,13 @@
|
|||
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"
|
||||
)
|
||||
|
||||
|
@ -24,6 +27,7 @@ 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 ¬ification.OKResp{}, nil
|
||||
}
|
||||
|
|
|
@ -1,12 +1,8 @@
|
|||
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"
|
||||
|
||||
|
@ -27,46 +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("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",
|
||||
)
|
||||
}
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return ¬ification.OKResp{}, nil
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -1,12 +1,8 @@
|
|||
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"
|
||||
|
||||
|
@ -27,42 +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, 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",
|
||||
)
|
||||
}
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return ¬ification.OKResp{}, nil
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
// Code generated by goctl. DO NOT EDIT.
|
||||
// Source: notification.proto
|
||||
// Source: service.proto
|
||||
|
||||
package server
|
||||
|
||||
|
@ -8,7 +8,6 @@ import (
|
|||
|
||||
"app-cloudep-notification-service/gen_result/pb/notification"
|
||||
senderservicelogic "app-cloudep-notification-service/internal/logic/senderservice"
|
||||
|
||||
"app-cloudep-notification-service/internal/svc"
|
||||
)
|
||||
|
||||
|
@ -29,19 +28,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)
|
||||
|
|
|
@ -1,30 +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"
|
||||
|
||||
"code.30cm.net/digimon/library-go/errs"
|
||||
"code.30cm.net/digimon/library-go/errs/code"
|
||||
|
||||
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 {
|
||||
errs.Scope = code.CloudEPNotification
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
MailSender: usecase.MustMailgunUseCase(usecase.MailUseCaseParam{Conf: c}),
|
||||
SMSSender: usecase.MustMitakeUseCase(usecase.SMSUseCaseParam{Conf: c}),
|
||||
Validate: v.MustValidator(),
|
||||
Config: c,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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),
|
||||
}
|
||||
}
|
|
@ -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),
|
||||
}
|
||||
}
|
|
@ -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()
|
Loading…
Reference in New Issue