Compare commits
No commits in common. "main" and "feature/create_service" have entirely different histories.
main
...
feature/cr
|
@ -2,6 +2,5 @@
|
||||||
go.sum
|
go.sum
|
||||||
account/
|
account/
|
||||||
gen_result/
|
gen_result/
|
||||||
etc/notification.yaml
|
etc/app-cloudep-notification-service.yaml
|
||||||
client/
|
./client
|
||||||
.DS_Store
|
|
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
|
|
13
Makefile
13
Makefile
|
@ -1,5 +1,4 @@
|
||||||
GO_CTL_NAME=goctl
|
GO_CTL_NAME=goctl
|
||||||
|
|
||||||
# go-zero 生成風格
|
# go-zero 生成風格
|
||||||
GO_ZERO_STYLE=go_zero
|
GO_ZERO_STYLE=go_zero
|
||||||
|
|
||||||
|
@ -7,8 +6,9 @@ GO ?= go
|
||||||
GOFMT ?= gofmt "-s"
|
GOFMT ?= gofmt "-s"
|
||||||
GOFILES := $(shell find . -name "*.go")
|
GOFILES := $(shell find . -name "*.go")
|
||||||
LDFLAGS := -s -w
|
LDFLAGS := -s -w
|
||||||
VERSION="v1.0.4"
|
VERSION="v1.0.3"
|
||||||
DOCKER_REPO="reg.wang/app-cloudep-notification-service"
|
DOCKER_REPO="igs170911/notification"
|
||||||
|
|
||||||
|
|
||||||
.PHONY: test
|
.PHONY: test
|
||||||
test: # 進行測試
|
test: # 進行測試
|
||||||
|
@ -18,11 +18,11 @@ 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
|
||||||
$(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
|
go mod tidy
|
||||||
@echo "Generate core-api files successfully"
|
@echo "Generate core-api files successfully"
|
||||||
|
|
||||||
|
@ -44,6 +44,5 @@ run-docker: # 建立 rpc code
|
||||||
.PHONY: build-docker
|
.PHONY: build-docker
|
||||||
build-docker:
|
build-docker:
|
||||||
cp ./build/Dockerfile Dockerfile
|
cp ./build/Dockerfile Dockerfile
|
||||||
docker buildx build -t $(DOCKER_REPO):$(VERSION) --build-arg SSH_PRIVATE_KEY="$(cat ~/.ssh/id_ed25519)" .
|
docker buildx build -t $(DOCKER_REPO):$(VERSION) --build-arg SSH_PRIVATE_KEY="$(cat ~/.ssh/ed_25519)" .
|
||||||
rm -rf Dockerfile
|
rm -rf Dockerfile
|
||||||
@echo "Generate core-api files successfully"
|
|
||||||
|
|
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
|
|
||||||
```
|
|
|
@ -2,7 +2,7 @@
|
||||||
# BUILDER #
|
# BUILDER #
|
||||||
###########
|
###########
|
||||||
|
|
||||||
FROM golang:1.24.0 as builder
|
FROM golang:1.22.3 as builder
|
||||||
|
|
||||||
ARG VERSION
|
ARG VERSION
|
||||||
ARG BUILT
|
ARG BUILT
|
||||||
|
@ -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 #
|
||||||
|
@ -41,7 +41,7 @@ RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
|
||||||
FROM gcr.io/distroless/static-debian11
|
FROM gcr.io/distroless/static-debian11
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
COPY --from=builder /app /app
|
COPY --from=builder /app/service /app/service
|
||||||
COPY --from=builder /app/etc/notification.yaml /app/etc/notification.yaml
|
COPY --from=builder /app/etc/service.yaml /app/etc/service.yaml
|
||||||
EXPOSE 8080
|
EXPOSE 8080
|
||||||
CMD ["/app/notification"]
|
CMD ["/app/service"]
|
|
@ -1,6 +1,5 @@
|
||||||
// Code generated by goctl. DO NOT EDIT.
|
// Code generated by goctl. DO NOT EDIT.
|
||||||
// goctl 1.7.3
|
// Source: service.proto
|
||||||
// Source: notification.proto
|
|
||||||
|
|
||||||
package senderservice
|
package senderservice
|
||||||
|
|
||||||
|
@ -14,20 +13,21 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
NoneReq = notification.NoneReq
|
NoneReq = notification.NoneReq
|
||||||
OKResp = notification.OKResp
|
OKResp = notification.OKResp
|
||||||
SendMailReq = notification.SendMailReq
|
SendByTemplateIDReq = notification.SendByTemplateIDReq
|
||||||
SendSMSReq = notification.SendSMSReq
|
SendMailReq = notification.SendMailReq
|
||||||
TemplateReq = notification.TemplateReq
|
SendSMSReq = notification.SendSMSReq
|
||||||
TemplateResp = notification.TemplateResp
|
|
||||||
|
|
||||||
SenderService interface {
|
SenderService interface {
|
||||||
// SendMail 寄信
|
// SendMail 寄信
|
||||||
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)
|
||||||
// 取得 Template
|
// SendMailByTemplateID 寄送模板信件
|
||||||
GetStaticTemplate(ctx context.Context, in *TemplateReq, opts ...grpc.CallOption) (*TemplateResp, error)
|
SendMailByTemplateId(ctx context.Context, in *SendByTemplateIDReq, opts ...grpc.CallOption) (*OKResp, error)
|
||||||
|
// SendSMSByTemplateID 寄送模板簡訊
|
||||||
|
SendSmsByTemplateId(ctx context.Context, in *SendByTemplateIDReq, opts ...grpc.CallOption) (*OKResp, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
defaultSenderService struct {
|
defaultSenderService struct {
|
||||||
|
@ -47,14 +47,20 @@ func (m *defaultSenderService) SendMail(ctx context.Context, in *SendMailReq, op
|
||||||
return client.SendMail(ctx, in, opts...)
|
return client.SendMail(ctx, in, opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendSms 寄簡訊
|
// SendSMS 寄簡訊
|
||||||
func (m *defaultSenderService) SendSms(ctx context.Context, in *SendSMSReq, opts ...grpc.CallOption) (*OKResp, error) {
|
func (m *defaultSenderService) SendSms(ctx context.Context, in *SendSMSReq, opts ...grpc.CallOption) (*OKResp, error) {
|
||||||
client := notification.NewSenderServiceClient(m.cli.Conn())
|
client := notification.NewSenderServiceClient(m.cli.Conn())
|
||||||
return client.SendSms(ctx, in, opts...)
|
return client.SendSms(ctx, in, opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 取得 Template
|
// SendMailByTemplateID 寄送模板信件
|
||||||
func (m *defaultSenderService) GetStaticTemplate(ctx context.Context, in *TemplateReq, opts ...grpc.CallOption) (*TemplateResp, 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.GetStaticTemplate(ctx, in, opts...)
|
return client.SendMailByTemplateId(ctx, in, opts...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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,44 +0,0 @@
|
||||||
Name: notification.rpc
|
|
||||||
ListenOn: 0.0.0.0:8003
|
|
||||||
Etcd:
|
|
||||||
Hosts:
|
|
||||||
- 127.0.0.1:2379
|
|
||||||
Key: notification.rpc
|
|
||||||
|
|
||||||
SMTPConfig:
|
|
||||||
Enable: false
|
|
||||||
Sort: 1
|
|
||||||
GoroutinePoolNum: 10
|
|
||||||
Host: xxxxxx
|
|
||||||
Port: xxxxxx
|
|
||||||
Username: xxxxxx
|
|
||||||
Password: xxxxxx
|
|
||||||
|
|
||||||
AmazonSesSettings:
|
|
||||||
Enable: false
|
|
||||||
Sort: 2
|
|
||||||
PoolSize: 2000
|
|
||||||
Region : ap-northeast-3
|
|
||||||
Sender : xxxxxx
|
|
||||||
Charset : xxxxxx
|
|
||||||
AccessKey : xxxxxx
|
|
||||||
SecretKey : xxxxxx
|
|
||||||
Token : xxxxxx
|
|
||||||
|
|
||||||
MitakeSMSSender:
|
|
||||||
Enable: false
|
|
||||||
Sort: 1
|
|
||||||
PoolSize: 10
|
|
||||||
User: xxxxxx
|
|
||||||
Password : xxxxxx
|
|
||||||
|
|
||||||
DevServer:
|
|
||||||
Enabled: false
|
|
||||||
Port: 6470
|
|
||||||
MetricsPath: /metrics
|
|
||||||
EnableMetrics: true
|
|
||||||
|
|
||||||
PyroScope:
|
|
||||||
Enable: false
|
|
||||||
URL: http://10.0.0.13:4040
|
|
||||||
Token: "xxx"
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
Name: service.rpc
|
||||||
|
ListenOn: 0.0.0.0:8080
|
||||||
|
Etcd:
|
||||||
|
Hosts:
|
||||||
|
- docker-etcd.digimon.orb.local:2379
|
||||||
|
Key: service.rpc
|
|
@ -0,0 +1 @@
|
||||||
|
如果有需要可以把 mongo 放這邊
|
|
@ -0,0 +1 @@
|
||||||
|
DROP TABLE IF EXISTS `permission`;
|
|
@ -0,0 +1,15 @@
|
||||||
|
-- 通常會把整個表都放到記憶體當中,不常搜尋,不需要加其他搜尋的 index
|
||||||
|
CREATE TABLE `permission`
|
||||||
|
(
|
||||||
|
`id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT 'PK',
|
||||||
|
`parent` bigint unsigned DEFAULT NULL,
|
||||||
|
`name` varchar(255) NOT NULL,
|
||||||
|
`http_method` varchar(255) NOT NULL,
|
||||||
|
`http_path` text NOT NULL,
|
||||||
|
`status` tinyint NOT NULL DEFAULT '1' COMMENT '狀態 1: 啟用, 2: 關閉',
|
||||||
|
`type` tinyint NOT NULL DEFAULT '1' COMMENT '狀態 1: 後台, 2: 前台',
|
||||||
|
`create_time` bigint DEFAULT 0 NOT NULL COMMENT '創建時間',
|
||||||
|
`update_time` bigint DEFAULT 0 NOT NULL COMMENT '更新時間',
|
||||||
|
PRIMARY KEY (`id`),
|
||||||
|
UNIQUE KEY `name_unique_key` (`name`)
|
||||||
|
) ENGINE = InnoDB COMMENT ='權限表';
|
|
@ -0,0 +1 @@
|
||||||
|
DROP DATABASE IF EXISTS `example`;
|
|
@ -0,0 +1 @@
|
||||||
|
CREATE DATABASE IF NOT EXISTS `example`;
|
|
@ -0,0 +1,39 @@
|
||||||
|
# migrate
|
||||||
|
數據庫遷移工具
|
||||||
|
|
||||||
|
[golang-migrate](https://github.com/golang-migrate/migrate)
|
||||||
|
|
||||||
|
## 安裝 make
|
||||||
|
```shell
|
||||||
|
brew install Makefile
|
||||||
|
```
|
||||||
|
|
||||||
|
## 安裝 golang-migrate
|
||||||
|
```shell
|
||||||
|
brew install golang-migrate
|
||||||
|
```
|
||||||
|
|
||||||
|
## 執行刪除 mysql schema
|
||||||
|
|
||||||
|
```shell
|
||||||
|
migrate -source file://database/migrations/mysql -database 'mysql://account:password@tcp(127.0.0.1:3306)/esc_c2c' down
|
||||||
|
```
|
||||||
|
|
||||||
|
## 執行安裝 mysql schema
|
||||||
|
|
||||||
|
```shell
|
||||||
|
migrate -source file://database/migrations/mysql -database 'mysql://account:password@tcp(127.0.0.1:3306)/esc_c2c' up
|
||||||
|
```
|
||||||
|
|
||||||
|
## 執行刪除 mongo schema
|
||||||
|
|
||||||
|
```shell
|
||||||
|
migrate -source file://database/migrations/mongodb -database 'mongodb://127.0.0.1:27017/esc_c2c' down
|
||||||
|
```
|
||||||
|
|
||||||
|
## 執行安裝 mongo schema
|
||||||
|
|
||||||
|
```shell
|
||||||
|
migrate -source file://database/migrations/mongodb -database 'mongodb://127.0.0.1:27017/esc_c2c' up
|
||||||
|
```
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
DELETE FROM `role` WHERE (`role_id` = 'AM000000');
|
|
@ -0,0 +1,3 @@
|
||||||
|
INSERT INTO `role` (`role_id`, `display_name`, `status`, `create_time`, `update_time`)
|
||||||
|
VALUES ('AM000000', 'admin', 1, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
|
||||||
|
('AM000001', 'visitor', 1, UNIX_TIMESTAMP(), UNIX_TIMESTAMP());
|
|
@ -13,33 +13,30 @@ message SendMailReq {
|
||||||
string to = 1;
|
string to = 1;
|
||||||
string subject = 2;
|
string subject = 2;
|
||||||
string body = 3;
|
string body = 3;
|
||||||
string from =4;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
message SendSMSReq {
|
message SendSMSReq {
|
||||||
string to = 1;
|
string to = 1;
|
||||||
string body = 2;
|
string body = 2;
|
||||||
string recipient_name=3;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
message TemplateReq {
|
message SendByTemplateIDReq {
|
||||||
string language = 1;
|
string to = 1;
|
||||||
string template_id = 2;
|
string template_id =2;
|
||||||
}
|
string lang=3;
|
||||||
|
map<string, string> content_data = 4;
|
||||||
message TemplateResp {
|
|
||||||
string title = 1;
|
|
||||||
string body = 2;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
service SenderService {
|
service SenderService {
|
||||||
// SendMail 寄信
|
// SendMail 寄信
|
||||||
rpc SendMail(SendMailReq) returns(OKResp);
|
rpc SendMail(SendMailReq) returns(OKResp);
|
||||||
// SendSms 寄簡訊
|
// SendSMS 寄簡訊
|
||||||
rpc SendSms(SendSMSReq) returns(OKResp);
|
rpc SendSms(SendSMSReq) returns(OKResp);
|
||||||
// 取得 Template
|
// SendMailByTemplateID 寄送模板信件
|
||||||
rpc GetStaticTemplate(TemplateReq) returns(TemplateResp);
|
rpc SendMailByTemplateId(SendByTemplateIDReq) returns(OKResp);
|
||||||
|
// SendSMSByTemplateID 寄送模板簡訊
|
||||||
|
rpc SendSmsByTemplateId(SendByTemplateIDReq) returns(OKResp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
40
go.mod
40
go.mod
|
@ -1,31 +1,15 @@
|
||||||
module code.30cm.net/digimon/app-cloudep-notification-service
|
module app-cloudep-notification-service
|
||||||
|
|
||||||
go 1.24.0
|
go 1.22.3
|
||||||
|
|
||||||
require (
|
require (
|
||||||
code.30cm.net/digimon/library-go/errs v1.2.14
|
code.30cm.net/digimon/library-go/errors v1.0.0
|
||||||
code.30cm.net/digimon/library-go/worker_pool v0.0.0-20240820153352-f9c90a90f5e2
|
|
||||||
github.com/aws/aws-sdk-go-v2 v1.36.3
|
|
||||||
github.com/aws/aws-sdk-go-v2/credentials v1.17.61
|
|
||||||
github.com/aws/aws-sdk-go-v2/service/ses v1.30.0
|
|
||||||
github.com/grafana/pyroscope-go v1.2.0
|
|
||||||
github.com/matcornic/hermes/v2 v2.1.0
|
|
||||||
github.com/minchao/go-mitake v1.0.0
|
|
||||||
github.com/zeromicro/go-zero v1.7.0
|
github.com/zeromicro/go-zero v1.7.0
|
||||||
google.golang.org/grpc v1.65.0
|
google.golang.org/grpc v1.65.0
|
||||||
google.golang.org/protobuf v1.34.2
|
google.golang.org/protobuf v1.34.2
|
||||||
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
|
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/Masterminds/semver v1.4.2 // indirect
|
|
||||||
github.com/Masterminds/sprig v2.16.0+incompatible // indirect
|
|
||||||
github.com/PuerkitoBio/goquery v1.5.0 // indirect
|
|
||||||
github.com/andybalholm/cascadia v1.0.0 // indirect
|
|
||||||
github.com/aokoli/goutils v1.0.1 // indirect
|
|
||||||
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.34 // indirect
|
|
||||||
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.34 // indirect
|
|
||||||
github.com/aws/smithy-go v1.22.2 // indirect
|
|
||||||
github.com/beorn7/perks v1.0.1 // indirect
|
github.com/beorn7/perks v1.0.1 // indirect
|
||||||
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
|
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
|
||||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||||
|
@ -47,38 +31,23 @@ require (
|
||||||
github.com/google/go-cmp v0.6.0 // indirect
|
github.com/google/go-cmp v0.6.0 // indirect
|
||||||
github.com/google/gofuzz v1.2.0 // indirect
|
github.com/google/gofuzz v1.2.0 // indirect
|
||||||
github.com/google/uuid v1.6.0 // indirect
|
github.com/google/uuid v1.6.0 // indirect
|
||||||
github.com/gorilla/css v1.0.0 // indirect
|
|
||||||
github.com/grafana/pyroscope-go/godeltaprof v0.1.8 // indirect
|
|
||||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect
|
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect
|
||||||
github.com/huandu/xstrings v1.2.0 // indirect
|
|
||||||
github.com/imdario/mergo v0.3.6 // indirect
|
|
||||||
github.com/jaytaylor/html2text v0.0.0-20180606194806-57d518f124b0 // indirect
|
|
||||||
github.com/josharian/intern v1.0.0 // indirect
|
github.com/josharian/intern v1.0.0 // indirect
|
||||||
github.com/json-iterator/go v1.1.12 // indirect
|
github.com/json-iterator/go v1.1.12 // indirect
|
||||||
github.com/klauspost/compress v1.17.8 // indirect
|
|
||||||
github.com/mailru/easyjson v0.7.7 // indirect
|
github.com/mailru/easyjson v0.7.7 // indirect
|
||||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
github.com/mattn/go-runewidth v0.0.15 // indirect
|
|
||||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
|
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
|
||||||
github.com/olekukonko/tablewriter v0.0.5 // indirect
|
|
||||||
github.com/openzipkin/zipkin-go v0.4.3 // 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/pelletier/go-toml/v2 v2.2.2 // indirect
|
||||||
github.com/prometheus/client_golang v1.19.1 // indirect
|
github.com/prometheus/client_golang v1.19.1 // indirect
|
||||||
github.com/prometheus/client_model v0.5.0 // indirect
|
github.com/prometheus/client_model v0.5.0 // indirect
|
||||||
github.com/prometheus/common v0.48.0 // indirect
|
github.com/prometheus/common v0.48.0 // indirect
|
||||||
github.com/prometheus/procfs v0.12.0 // indirect
|
github.com/prometheus/procfs v0.12.0 // indirect
|
||||||
github.com/redis/go-redis/v9 v9.6.1 // indirect
|
github.com/redis/go-redis/v9 v9.6.1 // indirect
|
||||||
github.com/rivo/uniseg v0.2.0 // indirect
|
|
||||||
github.com/russross/blackfriday/v2 v2.0.1 // indirect
|
|
||||||
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
|
|
||||||
github.com/spaolacci/murmur3 v1.1.0 // indirect
|
github.com/spaolacci/murmur3 v1.1.0 // indirect
|
||||||
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf // indirect
|
|
||||||
github.com/vanng822/css v0.0.0-20190504095207-a21e860bcd04 // indirect
|
|
||||||
github.com/vanng822/go-premailer v0.0.0-20191214114701-be27abe028fe // indirect
|
|
||||||
go.etcd.io/etcd/api/v3 v3.5.15 // indirect
|
go.etcd.io/etcd/api/v3 v3.5.15 // indirect
|
||||||
go.etcd.io/etcd/client/pkg/v3 v3.5.15 // indirect
|
go.etcd.io/etcd/client/pkg/v3 v3.5.15 // indirect
|
||||||
go.etcd.io/etcd/client/v3 v3.5.15 // indirect
|
go.etcd.io/etcd/client/v3 v3.5.15 // indirect
|
||||||
|
@ -97,17 +66,14 @@ require (
|
||||||
go.uber.org/automaxprocs v1.5.3 // indirect
|
go.uber.org/automaxprocs v1.5.3 // indirect
|
||||||
go.uber.org/multierr v1.9.0 // indirect
|
go.uber.org/multierr v1.9.0 // indirect
|
||||||
go.uber.org/zap v1.24.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/net v0.27.0 // indirect
|
||||||
golang.org/x/oauth2 v0.20.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/sys v0.22.0 // indirect
|
||||||
golang.org/x/term v0.22.0 // indirect
|
golang.org/x/term v0.22.0 // indirect
|
||||||
golang.org/x/text v0.16.0 // indirect
|
golang.org/x/text v0.16.0 // indirect
|
||||||
golang.org/x/time v0.5.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/api v0.0.0-20240711142825-46eb208f015d // indirect
|
||||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 // 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/inf.v0 v0.9.1 // indirect
|
||||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
|
|
|
@ -4,43 +4,4 @@ import "github.com/zeromicro/go-zero/zrpc"
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
zrpc.RpcServerConf
|
zrpc.RpcServerConf
|
||||||
|
|
||||||
SMTPConfig struct {
|
|
||||||
Enable bool
|
|
||||||
Sort int
|
|
||||||
GoroutinePoolNum int
|
|
||||||
|
|
||||||
Host string
|
|
||||||
Port int
|
|
||||||
Username string
|
|
||||||
Password string
|
|
||||||
}
|
|
||||||
|
|
||||||
AmazonSesSettings struct {
|
|
||||||
Enable bool
|
|
||||||
Sort int
|
|
||||||
PoolSize int
|
|
||||||
|
|
||||||
Region string
|
|
||||||
Sender string
|
|
||||||
Charset string
|
|
||||||
AccessKey string
|
|
||||||
SecretKey string
|
|
||||||
Token string
|
|
||||||
}
|
|
||||||
MitakeSMSSender struct {
|
|
||||||
Enable bool
|
|
||||||
Sort int
|
|
||||||
PoolSize int
|
|
||||||
|
|
||||||
User string
|
|
||||||
Password string
|
|
||||||
}
|
|
||||||
|
|
||||||
// PyroScope 監控
|
|
||||||
PyroScope struct {
|
|
||||||
Enable bool
|
|
||||||
URL string
|
|
||||||
Token string
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,42 +0,0 @@
|
||||||
package senderservicelogic
|
|
||||||
|
|
||||||
import (
|
|
||||||
"code.30cm.net/digimon/app-cloudep-notification-service/pkg/domain/template"
|
|
||||||
"context"
|
|
||||||
|
|
||||||
"code.30cm.net/digimon/app-cloudep-notification-service/gen_result/pb/notification"
|
|
||||||
"code.30cm.net/digimon/app-cloudep-notification-service/internal/svc"
|
|
||||||
|
|
||||||
"github.com/zeromicro/go-zero/core/logx"
|
|
||||||
)
|
|
||||||
|
|
||||||
type GetStaticTemplateLogic struct {
|
|
||||||
ctx context.Context
|
|
||||||
svcCtx *svc.ServiceContext
|
|
||||||
logx.Logger
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewGetStaticTemplateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetStaticTemplateLogic {
|
|
||||||
return &GetStaticTemplateLogic{
|
|
||||||
ctx: ctx,
|
|
||||||
svcCtx: svcCtx,
|
|
||||||
Logger: logx.WithContext(ctx),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetStaticTemplate 取得 Template
|
|
||||||
func (l *GetStaticTemplateLogic) GetStaticTemplate(in *notification.TemplateReq) (*notification.TemplateResp, error) {
|
|
||||||
tmp, err := l.svcCtx.TemplateUseCase.GetEmailTemplateByStatic(
|
|
||||||
l.ctx,
|
|
||||||
template.Language(in.GetLanguage()),
|
|
||||||
template.Type(in.GetTemplateId()),
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return ¬ification.TemplateResp{
|
|
||||||
Title: tmp.Title,
|
|
||||||
Body: tmp.Body,
|
|
||||||
}, nil
|
|
||||||
}
|
|
|
@ -0,0 +1,33 @@
|
||||||
|
package senderservicelogic
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"app-cloudep-notification-service/gen_result/pb/notification"
|
||||||
|
"app-cloudep-notification-service/internal/svc"
|
||||||
|
|
||||||
|
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 ¬ification.OKResp{}, nil
|
||||||
|
}
|
|
@ -1,11 +1,10 @@
|
||||||
package senderservicelogic
|
package senderservicelogic
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"code.30cm.net/digimon/app-cloudep-notification-service/pkg/domain/usecase"
|
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"code.30cm.net/digimon/app-cloudep-notification-service/gen_result/pb/notification"
|
"app-cloudep-notification-service/gen_result/pb/notification"
|
||||||
"code.30cm.net/digimon/app-cloudep-notification-service/internal/svc"
|
"app-cloudep-notification-service/internal/svc"
|
||||||
|
|
||||||
"github.com/zeromicro/go-zero/core/logx"
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
)
|
)
|
||||||
|
@ -26,15 +25,7 @@ func NewSendMailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendMail
|
||||||
|
|
||||||
// SendMail 寄信
|
// SendMail 寄信
|
||||||
func (l *SendMailLogic) SendMail(in *notification.SendMailReq) (*notification.OKResp, error) {
|
func (l *SendMailLogic) SendMail(in *notification.SendMailReq) (*notification.OKResp, error) {
|
||||||
err := l.svcCtx.DeliveryUseCase.SendEmail(l.ctx, usecase.MailReq{
|
// todo: add your logic here and delete this line
|
||||||
To: []string{in.GetTo()},
|
|
||||||
Subject: in.GetSubject(),
|
|
||||||
Body: in.GetBody(),
|
|
||||||
From: in.GetFrom(),
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return ¬ification.OKResp{}, nil
|
return ¬ification.OKResp{}, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
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 SendSmsByTemplateIdLogic struct {
|
||||||
|
ctx context.Context
|
||||||
|
svcCtx *svc.ServiceContext
|
||||||
|
logx.Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSendSmsByTemplateIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendSmsByTemplateIdLogic {
|
||||||
|
return &SendSmsByTemplateIdLogic{
|
||||||
|
ctx: ctx,
|
||||||
|
svcCtx: svcCtx,
|
||||||
|
Logger: logx.WithContext(ctx),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SendSMSByTemplateID 寄送模板簡訊
|
||||||
|
func (l *SendSmsByTemplateIdLogic) SendSmsByTemplateId(in *notification.SendByTemplateIDReq) (*notification.OKResp, error) {
|
||||||
|
// todo: add your logic here and delete this line
|
||||||
|
|
||||||
|
return ¬ification.OKResp{}, nil
|
||||||
|
}
|
|
@ -1,11 +1,10 @@
|
||||||
package senderservicelogic
|
package senderservicelogic
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"code.30cm.net/digimon/app-cloudep-notification-service/pkg/domain/usecase"
|
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"code.30cm.net/digimon/app-cloudep-notification-service/gen_result/pb/notification"
|
"app-cloudep-notification-service/gen_result/pb/notification"
|
||||||
"code.30cm.net/digimon/app-cloudep-notification-service/internal/svc"
|
"app-cloudep-notification-service/internal/svc"
|
||||||
|
|
||||||
"github.com/zeromicro/go-zero/core/logx"
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
)
|
)
|
||||||
|
@ -24,16 +23,9 @@ func NewSendSmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendSmsLo
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendSms 寄簡訊
|
// SendSMS 寄簡訊
|
||||||
func (l *SendSmsLogic) SendSms(in *notification.SendSMSReq) (*notification.OKResp, error) {
|
func (l *SendSmsLogic) SendSms(in *notification.SendSMSReq) (*notification.OKResp, error) {
|
||||||
err := l.svcCtx.DeliveryUseCase.SendMessage(l.ctx, usecase.SMSMessageRequest{
|
// todo: add your logic here and delete this line
|
||||||
PhoneNumber: in.GetTo(),
|
|
||||||
RecipientName: in.GetRecipientName(),
|
|
||||||
MessageContent: in.GetBody(),
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return ¬ification.OKResp{}, nil
|
return ¬ification.OKResp{}, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,15 +1,14 @@
|
||||||
// Code generated by goctl. DO NOT EDIT.
|
// Code generated by goctl. DO NOT EDIT.
|
||||||
// goctl 1.7.3
|
// Source: service.proto
|
||||||
// Source: notification.proto
|
|
||||||
|
|
||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"code.30cm.net/digimon/app-cloudep-notification-service/gen_result/pb/notification"
|
"app-cloudep-notification-service/gen_result/pb/notification"
|
||||||
senderservicelogic "code.30cm.net/digimon/app-cloudep-notification-service/internal/logic/senderservice"
|
senderservicelogic "app-cloudep-notification-service/internal/logic/senderservice"
|
||||||
"code.30cm.net/digimon/app-cloudep-notification-service/internal/svc"
|
"app-cloudep-notification-service/internal/svc"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SenderServiceServer struct {
|
type SenderServiceServer struct {
|
||||||
|
@ -29,14 +28,20 @@ func (s *SenderServiceServer) SendMail(ctx context.Context, in *notification.Sen
|
||||||
return l.SendMail(in)
|
return l.SendMail(in)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendSms 寄簡訊
|
// SendSMS 寄簡訊
|
||||||
func (s *SenderServiceServer) SendSms(ctx context.Context, in *notification.SendSMSReq) (*notification.OKResp, error) {
|
func (s *SenderServiceServer) SendSms(ctx context.Context, in *notification.SendSMSReq) (*notification.OKResp, error) {
|
||||||
l := senderservicelogic.NewSendSmsLogic(ctx, s.svcCtx)
|
l := senderservicelogic.NewSendSmsLogic(ctx, s.svcCtx)
|
||||||
return l.SendSms(in)
|
return l.SendSms(in)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 取得 Template
|
// SendMailByTemplateID 寄送模板信件
|
||||||
func (s *SenderServiceServer) GetStaticTemplate(ctx context.Context, in *notification.TemplateReq) (*notification.TemplateResp, error) {
|
func (s *SenderServiceServer) SendMailByTemplateId(ctx context.Context, in *notification.SendByTemplateIDReq) (*notification.OKResp, error) {
|
||||||
l := senderservicelogic.NewGetStaticTemplateLogic(ctx, s.svcCtx)
|
l := senderservicelogic.NewSendMailByTemplateIdLogic(ctx, s.svcCtx)
|
||||||
return l.GetStaticTemplate(in)
|
return l.SendMailByTemplateId(in)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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,46 +0,0 @@
|
||||||
package svc
|
|
||||||
|
|
||||||
import (
|
|
||||||
"code.30cm.net/digimon/app-cloudep-notification-service/internal/config"
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/grafana/pyroscope-go"
|
|
||||||
"github.com/zeromicro/go-zero/core/logx"
|
|
||||||
)
|
|
||||||
|
|
||||||
func InitPyroScope(c config.Config) {
|
|
||||||
if c.PyroScope.Enable {
|
|
||||||
podName := os.Getenv("POD_NAME")
|
|
||||||
|
|
||||||
_, err := pyroscope.Start(pyroscope.Config{
|
|
||||||
ApplicationName: fmt.Sprintf("biz-%s-%s", c.Name, podName),
|
|
||||||
ServerAddress: c.PyroScope.URL,
|
|
||||||
Logger: logx.WithContext(context.Background()),
|
|
||||||
ProfileTypes: []pyroscope.ProfileType{
|
|
||||||
pyroscope.ProfileCPU,
|
|
||||||
|
|
||||||
pyroscope.ProfileInuseObjects,
|
|
||||||
pyroscope.ProfileAllocObjects,
|
|
||||||
pyroscope.ProfileInuseSpace,
|
|
||||||
pyroscope.ProfileAllocSpace,
|
|
||||||
|
|
||||||
pyroscope.ProfileGoroutines,
|
|
||||||
|
|
||||||
pyroscope.ProfileMutexCount,
|
|
||||||
pyroscope.ProfileMutexDuration,
|
|
||||||
|
|
||||||
pyroscope.ProfileBlockCount,
|
|
||||||
pyroscope.ProfileBlockDuration,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
logx.WithCallerSkip(1).WithFields(logx.LogField{
|
|
||||||
Key: "error", Value: err.Error(),
|
|
||||||
}).Error("failed to init pyroscope")
|
|
||||||
|
|
||||||
panic(fmt.Sprintf("Pyroscope start err: %s", err.Error()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,88 +1,13 @@
|
||||||
package svc
|
package svc
|
||||||
|
|
||||||
import (
|
import "app-cloudep-notification-service/internal/config"
|
||||||
"code.30cm.net/digimon/app-cloudep-notification-service/internal/config"
|
|
||||||
cfg "code.30cm.net/digimon/app-cloudep-notification-service/pkg/config"
|
|
||||||
useD "code.30cm.net/digimon/app-cloudep-notification-service/pkg/domain/usecase"
|
|
||||||
"code.30cm.net/digimon/app-cloudep-notification-service/pkg/repository"
|
|
||||||
"code.30cm.net/digimon/app-cloudep-notification-service/pkg/usecase"
|
|
||||||
|
|
||||||
"code.30cm.net/digimon/library-go/errs"
|
|
||||||
"code.30cm.net/digimon/library-go/errs/code"
|
|
||||||
)
|
|
||||||
|
|
||||||
type ServiceContext struct {
|
type ServiceContext struct {
|
||||||
Config config.Config
|
Config config.Config
|
||||||
DeliveryUseCase useD.DeliveryUseCase
|
|
||||||
TemplateUseCase useD.TemplateUseCase
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServiceContext(c config.Config) *ServiceContext {
|
func NewServiceContext(c config.Config) *ServiceContext {
|
||||||
errs.Scope = code.CloudEPNotification
|
|
||||||
|
|
||||||
InitPyroScope(c)
|
|
||||||
|
|
||||||
param := usecase.DeliveryUseCaseParam{}
|
|
||||||
if c.AmazonSesSettings.Enable {
|
|
||||||
sesRepo := repository.MustAwsSesMailRepository(repository.AwsEmailDeliveryParam{
|
|
||||||
Conf: &cfg.AmazonSesSettings{
|
|
||||||
Enable: c.AmazonSesSettings.Enable,
|
|
||||||
Sort: c.AmazonSesSettings.Sort,
|
|
||||||
PoolSize: c.AmazonSesSettings.PoolSize,
|
|
||||||
Region: c.AmazonSesSettings.Region,
|
|
||||||
Sender: c.AmazonSesSettings.Sender,
|
|
||||||
Charset: c.AmazonSesSettings.Charset,
|
|
||||||
AccessKey: c.AmazonSesSettings.AccessKey,
|
|
||||||
SecretKey: c.AmazonSesSettings.SecretKey,
|
|
||||||
Token: c.AmazonSesSettings.Token,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
param.EmailProviders = append(param.EmailProviders, useD.EmailProvider{
|
|
||||||
Sort: int64(c.AmazonSesSettings.Sort),
|
|
||||||
Repo: sesRepo,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
if c.SMTPConfig.Enable {
|
|
||||||
smtpRepo := repository.MustSMTPUseCase(repository.SMTPMailUseCaseParam{
|
|
||||||
Conf: cfg.SMTPConfig{
|
|
||||||
Enable: c.SMTPConfig.Enable,
|
|
||||||
Sort: c.SMTPConfig.Sort,
|
|
||||||
GoroutinePoolNum: c.SMTPConfig.GoroutinePoolNum,
|
|
||||||
Host: c.SMTPConfig.Host,
|
|
||||||
Port: c.SMTPConfig.Port,
|
|
||||||
Username: c.SMTPConfig.Username,
|
|
||||||
Password: c.SMTPConfig.Password,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
param.EmailProviders = append(param.EmailProviders, useD.EmailProvider{
|
|
||||||
Sort: int64(c.SMTPConfig.Sort),
|
|
||||||
Repo: smtpRepo,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
if c.MitakeSMSSender.Enable {
|
|
||||||
param.SMSProviders = append(param.SMSProviders, useD.SMSProvider{
|
|
||||||
Sort: int64(c.MitakeSMSSender.Sort),
|
|
||||||
Repo: repository.MustMitakeRepository(repository.MitakeSMSDeliveryParam{
|
|
||||||
Conf: &cfg.MitakeSMSSender{
|
|
||||||
Enable: c.MitakeSMSSender.Enable,
|
|
||||||
Sort: c.MitakeSMSSender.Sort,
|
|
||||||
User: c.MitakeSMSSender.User,
|
|
||||||
Password: c.MitakeSMSSender.Password,
|
|
||||||
PoolSize: c.MitakeSMSSender.PoolSize,
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
uc := usecase.MustDeliveryUseCase(param)
|
|
||||||
|
|
||||||
return &ServiceContext{
|
return &ServiceContext{
|
||||||
Config: c,
|
Config: c,
|
||||||
DeliveryUseCase: uc,
|
|
||||||
TemplateUseCase: usecase.MustTemplateUseCase(usecase.TemplateUseCaseParam{}),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,34 +0,0 @@
|
||||||
package config
|
|
||||||
|
|
||||||
type SMTPConfig struct {
|
|
||||||
Enable bool
|
|
||||||
Sort int
|
|
||||||
GoroutinePoolNum int
|
|
||||||
|
|
||||||
Host string
|
|
||||||
Port int
|
|
||||||
Username string
|
|
||||||
Password string
|
|
||||||
}
|
|
||||||
|
|
||||||
type AmazonSesSettings struct {
|
|
||||||
Enable bool
|
|
||||||
Sort int
|
|
||||||
PoolSize int
|
|
||||||
|
|
||||||
Region string
|
|
||||||
Sender string
|
|
||||||
Charset string
|
|
||||||
AccessKey string
|
|
||||||
SecretKey string
|
|
||||||
Token string
|
|
||||||
}
|
|
||||||
|
|
||||||
type MitakeSMSSender struct {
|
|
||||||
Enable bool
|
|
||||||
Sort int
|
|
||||||
PoolSize int
|
|
||||||
|
|
||||||
User string
|
|
||||||
Password string
|
|
||||||
}
|
|
|
@ -1,28 +0,0 @@
|
||||||
package domain
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
ers "code.30cm.net/digimon/library-go/errs"
|
|
||||||
"code.30cm.net/digimon/library-go/errs/code"
|
|
||||||
"github.com/zeromicro/go-zero/core/logx"
|
|
||||||
)
|
|
||||||
|
|
||||||
func ThirdPartyError(scope uint32, ec ers.ErrorCode, s ...string) *ers.LibError {
|
|
||||||
return ers.NewError(scope, code.ThirdParty, ec.ToUint32(), fmt.Sprintf("thirty error: %s", strings.Join(s, " ")))
|
|
||||||
}
|
|
||||||
|
|
||||||
func ThirdPartyErrorL(scope uint32, ec ers.ErrorCode,
|
|
||||||
l logx.Logger, filed []logx.LogField, s ...string) *ers.LibError {
|
|
||||||
e := ThirdPartyError(scope, ec, s...)
|
|
||||||
l.WithCallerSkip(1).WithFields(filed...).Error(e.Error())
|
|
||||||
|
|
||||||
return e
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
|
||||||
NotificationErrorCode = 1 + iota
|
|
||||||
FailedToSendEmailErrorCode
|
|
||||||
FailedToSendSMSErrorCode
|
|
||||||
)
|
|
|
@ -1,9 +0,0 @@
|
||||||
package notification
|
|
||||||
|
|
||||||
// Language 定義模板請求
|
|
||||||
type Language string
|
|
||||||
|
|
||||||
const (
|
|
||||||
LanguageZhTW Language = "zh-tw"
|
|
||||||
LanguageEnUS Language = "en-us"
|
|
||||||
)
|
|
|
@ -1,16 +0,0 @@
|
||||||
package notification
|
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
type TypeID int64
|
|
||||||
|
|
||||||
func (id TypeID) String() string {
|
|
||||||
return fmt.Sprintf("%4d", id)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 驗證碼通知類 0 ~ 100
|
|
||||||
const (
|
|
||||||
BindingEmail TypeID = 1 // 驗證碼:綁定 Email
|
|
||||||
BindingPhone TypeID = 2 // 驗證碼:綁定 手機
|
|
||||||
ForgetPasswordVerify TypeID = 3 // 驗證碼: 忘記密碼
|
|
||||||
)
|
|
|
@ -1,15 +0,0 @@
|
||||||
package repository
|
|
||||||
|
|
||||||
import "context"
|
|
||||||
|
|
||||||
type MailRepository interface {
|
|
||||||
// SendMail 送出 Email
|
|
||||||
SendMail(ctx context.Context, req MailReq) error
|
|
||||||
}
|
|
||||||
|
|
||||||
type MailReq struct {
|
|
||||||
To []string
|
|
||||||
From string
|
|
||||||
Subject string
|
|
||||||
Body string
|
|
||||||
}
|
|
|
@ -1,16 +0,0 @@
|
||||||
package repository
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
)
|
|
||||||
|
|
||||||
type SMSClientRepository interface {
|
|
||||||
SendSMS(ctx context.Context, req SMSMessageRequest) error
|
|
||||||
}
|
|
||||||
|
|
||||||
// SMSMessageRequest SMS 訊息請求結構
|
|
||||||
type SMSMessageRequest struct {
|
|
||||||
PhoneNumber string `json:"phone_number" validate:"required,e164"` // 接收者號碼 (e164 格式用於驗證國際號碼)
|
|
||||||
RecipientName string `json:"recipient_name" validate:"required"` // 接收者姓名
|
|
||||||
MessageContent string `json:"message_content" validate:"required"` // 要傳送的訊息
|
|
||||||
}
|
|
|
@ -1,12 +0,0 @@
|
||||||
package template
|
|
||||||
|
|
||||||
// ==============================
|
|
||||||
// 產品資訊常數
|
|
||||||
// ==============================
|
|
||||||
|
|
||||||
const (
|
|
||||||
ProductName = "TrueHeart 團隊"
|
|
||||||
ProductLink = "https://code.30cm.net"
|
|
||||||
ProductLogo = "https://true-heart-dev.s3.ap-northeast-3.amazonaws.com/f70904eb-1a29-40f7-8940-9a124f23793a.png"
|
|
||||||
ProductCopyright = "© 2025 TrueHeart Inc. 版權所有"
|
|
||||||
)
|
|
|
@ -1,128 +0,0 @@
|
||||||
package template
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/matcornic/hermes/v2"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ==============================
|
|
||||||
// Email 結構定義
|
|
||||||
// ==============================
|
|
||||||
|
|
||||||
// EmailTemplate 代表 Email 樣板
|
|
||||||
type EmailTemplate struct {
|
|
||||||
Title string
|
|
||||||
Body string
|
|
||||||
}
|
|
||||||
|
|
||||||
// ProductInfo 包含產品相關的資訊
|
|
||||||
type ProductInfo struct {
|
|
||||||
Name string
|
|
||||||
Link string
|
|
||||||
Logo string
|
|
||||||
Copyright string
|
|
||||||
}
|
|
||||||
|
|
||||||
// EmailBodyContent 包含郵件正文的資訊
|
|
||||||
type EmailBodyContent struct {
|
|
||||||
RecipientName string
|
|
||||||
Intros []string
|
|
||||||
Actions []hermes.Action
|
|
||||||
Outros []string
|
|
||||||
Signature string
|
|
||||||
}
|
|
||||||
|
|
||||||
// EmailContentParams 組成 Email 內容的參數
|
|
||||||
type EmailContentParams struct {
|
|
||||||
Product ProductInfo
|
|
||||||
Content EmailBodyContent
|
|
||||||
}
|
|
||||||
|
|
||||||
// ==============================
|
|
||||||
// Email 內容產生器
|
|
||||||
// ==============================
|
|
||||||
|
|
||||||
// GenerateEmailBody 根據參數產生 Email HTML 內容
|
|
||||||
func GenerateEmailBody(params EmailContentParams) (string, error) {
|
|
||||||
product := hermes.Product{
|
|
||||||
Name: params.Product.Name,
|
|
||||||
Link: params.Product.Link,
|
|
||||||
Logo: params.Product.Logo,
|
|
||||||
Copyright: params.Product.Copyright,
|
|
||||||
}
|
|
||||||
|
|
||||||
body := hermes.Body{
|
|
||||||
Name: params.Content.RecipientName,
|
|
||||||
Intros: params.Content.Intros,
|
|
||||||
Actions: params.Content.Actions,
|
|
||||||
Outros: params.Content.Outros,
|
|
||||||
Signature: params.Content.Signature,
|
|
||||||
}
|
|
||||||
|
|
||||||
h := hermes.Hermes{Product: product}
|
|
||||||
email := hermes.Email{Body: body}
|
|
||||||
|
|
||||||
return h.GenerateHTML(email)
|
|
||||||
}
|
|
||||||
|
|
||||||
// GenerateEmailContent 生成 Email 內容(可用於不同驗證類型)
|
|
||||||
func GenerateEmailContent(title string, intros []string) (EmailTemplate, error) {
|
|
||||||
req := EmailContentParams{
|
|
||||||
Product: ProductInfo{
|
|
||||||
Name: ProductName,
|
|
||||||
Link: ProductLink,
|
|
||||||
Logo: ProductLogo,
|
|
||||||
Copyright: ProductCopyright,
|
|
||||||
},
|
|
||||||
Content: EmailBodyContent{
|
|
||||||
RecipientName: "{{.Username}}",
|
|
||||||
Intros: intros,
|
|
||||||
Actions: []hermes.Action{
|
|
||||||
{
|
|
||||||
Instructions: "請複製您的驗證碼,到網頁重置",
|
|
||||||
InviteCode: "{{.VerifyCode}}",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Outros: []string{
|
|
||||||
"如果您沒有請求此操作,請忽略此郵件。",
|
|
||||||
},
|
|
||||||
Signature: "",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
emailBody, err := GenerateEmailBody(req)
|
|
||||||
if err != nil {
|
|
||||||
return EmailTemplate{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return EmailTemplate{
|
|
||||||
Title: title,
|
|
||||||
Body: emailBody,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ==============================
|
|
||||||
// Email 產生函數
|
|
||||||
// ==============================
|
|
||||||
|
|
||||||
// GenerateForgetPasswordEmailZHTW 生成繁體中文的忘記密碼驗證信
|
|
||||||
func GenerateForgetPasswordEmailZHTW() (EmailTemplate, error) {
|
|
||||||
return GenerateEmailContent("TrueHeart 重設密碼驗證信",
|
|
||||||
[]string{"您收到此電子郵件是因為我們收到了針對帳戶的密碼重置請求。"})
|
|
||||||
}
|
|
||||||
|
|
||||||
// GenerateBindingEmailZHTW 生成綁定帳號驗證信
|
|
||||||
func GenerateBindingEmailZHTW() (EmailTemplate, error) {
|
|
||||||
return GenerateEmailContent("TrueHeart 綁定信箱驗證信",
|
|
||||||
[]string{"您收到此電子郵件是因為我們收到了針對帳戶的 Email 認證請求。"})
|
|
||||||
}
|
|
||||||
|
|
||||||
// ==============================
|
|
||||||
// Email 模板對應表
|
|
||||||
// ==============================
|
|
||||||
|
|
||||||
var EmailTemplateMap = map[Language]map[Type]func() (EmailTemplate, error){
|
|
||||||
LanguageZhTW: {
|
|
||||||
ForgetPasswordVerify: GenerateForgetPasswordEmailZHTW,
|
|
||||||
BindingEmail: GenerateBindingEmailZHTW,
|
|
||||||
},
|
|
||||||
}
|
|
|
@ -1,11 +0,0 @@
|
||||||
package template
|
|
||||||
|
|
||||||
// ==============================
|
|
||||||
// 語言與驗證碼類型
|
|
||||||
// ==============================
|
|
||||||
|
|
||||||
type Language string
|
|
||||||
|
|
||||||
const (
|
|
||||||
LanguageZhTW Language = "zh-tw"
|
|
||||||
)
|
|
|
@ -1,9 +0,0 @@
|
||||||
package template
|
|
||||||
|
|
||||||
type Type string
|
|
||||||
|
|
||||||
const (
|
|
||||||
BindingEmail Type = "binding_email" // 驗證碼:綁定 Email
|
|
||||||
BindingPhone Type = "binding_phone" // 驗證碼:綁定 手機
|
|
||||||
ForgetPasswordVerify Type = "forget_password" // 驗證碼: 忘記密碼
|
|
||||||
)
|
|
|
@ -1,43 +0,0 @@
|
||||||
package usecase
|
|
||||||
|
|
||||||
import (
|
|
||||||
"code.30cm.net/digimon/app-cloudep-notification-service/pkg/domain/repository"
|
|
||||||
"context"
|
|
||||||
)
|
|
||||||
|
|
||||||
type DeliveryUseCase interface {
|
|
||||||
SendMessage(ctx context.Context, req SMSMessageRequest) error
|
|
||||||
SendEmail(ctx context.Context, req MailReq) error
|
|
||||||
}
|
|
||||||
|
|
||||||
type MailReq struct {
|
|
||||||
To []string
|
|
||||||
From string
|
|
||||||
Subject string
|
|
||||||
Body string
|
|
||||||
}
|
|
||||||
|
|
||||||
type SMSMessageRequest struct {
|
|
||||||
PhoneNumber string `json:"phone_number" validate:"required,e164"` // 接收者號碼 (e164 格式用於驗證國際號碼)
|
|
||||||
RecipientName string `json:"recipient_name" validate:"required"` // 接收者姓名
|
|
||||||
MessageContent string `json:"message_content" validate:"required"` // 要傳送的訊息
|
|
||||||
}
|
|
||||||
|
|
||||||
type EmailTemplateResp struct {
|
|
||||||
Subject string `json:"subject"` // 郵件主題
|
|
||||||
Body string `json:"body"` // 郵件內容
|
|
||||||
}
|
|
||||||
|
|
||||||
type SMSTemplateResp struct {
|
|
||||||
Body string `json:"body"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type SMSProvider struct {
|
|
||||||
Sort int64
|
|
||||||
Repo repository.SMSClientRepository
|
|
||||||
}
|
|
||||||
|
|
||||||
type EmailProvider struct {
|
|
||||||
Sort int64
|
|
||||||
Repo repository.MailRepository
|
|
||||||
}
|
|
|
@ -1,10 +0,0 @@
|
||||||
package usecase
|
|
||||||
|
|
||||||
import (
|
|
||||||
"code.30cm.net/digimon/app-cloudep-notification-service/pkg/domain/template"
|
|
||||||
"context"
|
|
||||||
)
|
|
||||||
|
|
||||||
type TemplateUseCase interface {
|
|
||||||
GetEmailTemplateByStatic(ctx context.Context, language template.Language, templateID template.Type) (template.EmailTemplate, error)
|
|
||||||
}
|
|
|
@ -1,109 +0,0 @@
|
||||||
package repository
|
|
||||||
|
|
||||||
import (
|
|
||||||
"code.30cm.net/digimon/app-cloudep-notification-service/pkg/config"
|
|
||||||
"code.30cm.net/digimon/app-cloudep-notification-service/pkg/domain"
|
|
||||||
"code.30cm.net/digimon/app-cloudep-notification-service/pkg/domain/repository"
|
|
||||||
"context"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"code.30cm.net/digimon/library-go/errs/code"
|
|
||||||
pool "code.30cm.net/digimon/library-go/worker_pool"
|
|
||||||
"github.com/aws/aws-sdk-go-v2/credentials"
|
|
||||||
"github.com/aws/aws-sdk-go-v2/service/ses/types"
|
|
||||||
"github.com/zeromicro/go-zero/core/logx"
|
|
||||||
|
|
||||||
"github.com/aws/aws-sdk-go-v2/aws"
|
|
||||||
"github.com/aws/aws-sdk-go-v2/service/ses"
|
|
||||||
)
|
|
||||||
|
|
||||||
// AwsEmailDeliveryParam 傳送參數配置
|
|
||||||
type AwsEmailDeliveryParam struct {
|
|
||||||
Conf *config.AmazonSesSettings
|
|
||||||
}
|
|
||||||
|
|
||||||
type AwsEmailDeliveryRepository struct {
|
|
||||||
Client *ses.Client
|
|
||||||
Pool pool.WorkerPool
|
|
||||||
}
|
|
||||||
|
|
||||||
func MustAwsSesMailRepository(param AwsEmailDeliveryParam) repository.MailRepository {
|
|
||||||
// 手動指定 AWS 配置,不使用默認配置
|
|
||||||
cfg := aws.Config{
|
|
||||||
Region: param.Conf.Region, // 自定義的 AWS 區域
|
|
||||||
Credentials: credentials.NewStaticCredentialsProvider(
|
|
||||||
param.Conf.AccessKey, // AWS Access Key
|
|
||||||
param.Conf.SecretKey, // AWS Secret Key
|
|
||||||
"",
|
|
||||||
),
|
|
||||||
}
|
|
||||||
// 創建 SES 客戶端
|
|
||||||
sesClient := ses.NewFromConfig(cfg)
|
|
||||||
|
|
||||||
return &AwsEmailDeliveryRepository{
|
|
||||||
Client: sesClient,
|
|
||||||
Pool: pool.NewWorkerPool(param.Conf.PoolSize),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (use *AwsEmailDeliveryRepository) SendMail(ctx context.Context, req repository.MailReq) error {
|
|
||||||
err := use.Pool.Submit(func() {
|
|
||||||
// 設置郵件參數
|
|
||||||
to := make([]string, 0, len(req.To))
|
|
||||||
to = append(to, req.To...)
|
|
||||||
|
|
||||||
input := &ses.SendEmailInput{
|
|
||||||
Destination: &types.Destination{
|
|
||||||
ToAddresses: to,
|
|
||||||
},
|
|
||||||
Message: &types.Message{
|
|
||||||
Body: &types.Body{
|
|
||||||
Html: &types.Content{
|
|
||||||
Charset: aws.String("UTF-8"),
|
|
||||||
Data: aws.String(req.Body),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Subject: &types.Content{
|
|
||||||
Charset: aws.String("UTF-8"),
|
|
||||||
Data: aws.String(req.Subject),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Source: aws.String(req.From),
|
|
||||||
}
|
|
||||||
|
|
||||||
// 發送郵件
|
|
||||||
// TODO 不明原因送不出去,會被 context cancel 這裡先把它手動加到100sec
|
|
||||||
newCtx, cancel := context.WithTimeout(context.Background(), 100*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
//nolint:contextcheck
|
|
||||||
if _, err := use.Client.SendEmail(newCtx, input); err != nil {
|
|
||||||
_ = domain.ThirdPartyErrorL(
|
|
||||||
code.CloudEPNotification,
|
|
||||||
domain.FailedToSendEmailErrorCode,
|
|
||||||
logx.WithContext(ctx),
|
|
||||||
[]logx.LogField{
|
|
||||||
{Key: "req", Value: req},
|
|
||||||
{Key: "func", Value: "AwsEmailDeliveryU.SendEmail"},
|
|
||||||
{Key: "err", Value: err.Error()},
|
|
||||||
},
|
|
||||||
"failed to send mail by aws ses")
|
|
||||||
}
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
e := domain.ThirdPartyErrorL(
|
|
||||||
code.CloudEPNotification,
|
|
||||||
domain.FailedToSendEmailErrorCode,
|
|
||||||
logx.WithContext(ctx),
|
|
||||||
[]logx.LogField{
|
|
||||||
{Key: "req", Value: req},
|
|
||||||
{Key: "func", Value: "AwsEmailDeliveryU.SendEmail"},
|
|
||||||
{Key: "err", Value: err.Error()},
|
|
||||||
},
|
|
||||||
"failed to send mail by aws ses")
|
|
||||||
|
|
||||||
return e
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
|
@ -1,63 +0,0 @@
|
||||||
package repository
|
|
||||||
|
|
||||||
import (
|
|
||||||
"code.30cm.net/digimon/app-cloudep-notification-service/pkg/config"
|
|
||||||
"code.30cm.net/digimon/app-cloudep-notification-service/pkg/domain"
|
|
||||||
"code.30cm.net/digimon/app-cloudep-notification-service/pkg/domain/repository"
|
|
||||||
"context"
|
|
||||||
|
|
||||||
"code.30cm.net/digimon/library-go/errs/code"
|
|
||||||
pool "code.30cm.net/digimon/library-go/worker_pool"
|
|
||||||
"github.com/minchao/go-mitake"
|
|
||||||
"github.com/zeromicro/go-zero/core/logx"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MitakeSMSDeliveryParam 三竹傳送參數配置
|
|
||||||
type MitakeSMSDeliveryParam struct {
|
|
||||||
Conf *config.MitakeSMSSender
|
|
||||||
}
|
|
||||||
|
|
||||||
type MitakeSMSDeliveryRepository struct {
|
|
||||||
Client *mitake.Client
|
|
||||||
Pool pool.WorkerPool
|
|
||||||
}
|
|
||||||
|
|
||||||
func (use *MitakeSMSDeliveryRepository) SendSMS(ctx context.Context, req repository.SMSMessageRequest) error {
|
|
||||||
// 用 goroutine pool 送,否則會超時
|
|
||||||
err := use.Pool.Submit(func() {
|
|
||||||
message := mitake.Message{
|
|
||||||
Dstaddr: req.PhoneNumber,
|
|
||||||
Destname: req.RecipientName,
|
|
||||||
Smbody: req.MessageContent,
|
|
||||||
}
|
|
||||||
_, err := use.Client.Send(message)
|
|
||||||
if err != nil {
|
|
||||||
logx.Error("failed to send sms via mitake")
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
// 錯誤代碼 20-201-04
|
|
||||||
e := domain.ThirdPartyErrorL(
|
|
||||||
code.CloudEPNotification,
|
|
||||||
domain.FailedToSendSMSErrorCode,
|
|
||||||
logx.WithContext(ctx),
|
|
||||||
[]logx.LogField{
|
|
||||||
{Key: "req", Value: req},
|
|
||||||
{Key: "func", Value: "MitakeSMSDeliveryRepository.Client.Send"},
|
|
||||||
{Key: "err", Value: err.Error()},
|
|
||||||
},
|
|
||||||
"failed to send sns by mitake").Wrap(err)
|
|
||||||
|
|
||||||
return e
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func MustMitakeRepository(param MitakeSMSDeliveryParam) repository.SMSClientRepository {
|
|
||||||
return &MitakeSMSDeliveryRepository{
|
|
||||||
Client: mitake.NewClient(param.Conf.User, param.Conf.Password, nil),
|
|
||||||
Pool: pool.NewWorkerPool(param.Conf.PoolSize),
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,52 +0,0 @@
|
||||||
package repository
|
|
||||||
|
|
||||||
import (
|
|
||||||
"code.30cm.net/digimon/app-cloudep-notification-service/pkg/config"
|
|
||||||
"code.30cm.net/digimon/app-cloudep-notification-service/pkg/domain/repository"
|
|
||||||
"context"
|
|
||||||
|
|
||||||
pool "code.30cm.net/digimon/library-go/worker_pool"
|
|
||||||
"github.com/zeromicro/go-zero/core/logx"
|
|
||||||
"gopkg.in/gomail.v2"
|
|
||||||
)
|
|
||||||
|
|
||||||
type SMTPMailUseCaseParam struct {
|
|
||||||
Conf config.SMTPConfig
|
|
||||||
}
|
|
||||||
|
|
||||||
type SMTPMailRepository struct {
|
|
||||||
Client *gomail.Dialer
|
|
||||||
Pool pool.WorkerPool
|
|
||||||
}
|
|
||||||
|
|
||||||
func MustSMTPUseCase(param SMTPMailUseCaseParam) repository.MailRepository {
|
|
||||||
return &SMTPMailRepository{
|
|
||||||
Client: gomail.NewDialer(
|
|
||||||
param.Conf.Host,
|
|
||||||
param.Conf.Port,
|
|
||||||
param.Conf.Username,
|
|
||||||
param.Conf.Password,
|
|
||||||
),
|
|
||||||
Pool: pool.NewWorkerPool(param.Conf.GoroutinePoolNum),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (repo *SMTPMailRepository) SendMail(_ context.Context, req repository.MailReq) error {
|
|
||||||
// 用 goroutine pool 送,否則會超時
|
|
||||||
err := repo.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)
|
|
||||||
if err := repo.Client.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
|
|
||||||
}
|
|
|
@ -1,75 +0,0 @@
|
||||||
package usecase
|
|
||||||
|
|
||||||
import (
|
|
||||||
"code.30cm.net/digimon/app-cloudep-notification-service/pkg/domain/repository"
|
|
||||||
"code.30cm.net/digimon/app-cloudep-notification-service/pkg/domain/usecase"
|
|
||||||
"context"
|
|
||||||
"sort"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// DeliveryUseCaseParam 傳送參數配置
|
|
||||||
type DeliveryUseCaseParam struct {
|
|
||||||
SMSProviders []usecase.SMSProvider
|
|
||||||
EmailProviders []usecase.EmailProvider
|
|
||||||
}
|
|
||||||
|
|
||||||
// DeliveryUseCase 通知
|
|
||||||
type DeliveryUseCase struct {
|
|
||||||
param DeliveryUseCaseParam
|
|
||||||
}
|
|
||||||
|
|
||||||
func MustDeliveryUseCase(param DeliveryUseCaseParam) usecase.DeliveryUseCase {
|
|
||||||
return &DeliveryUseCase{
|
|
||||||
param: param,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (use *DeliveryUseCase) SendMessage(ctx context.Context, req usecase.SMSMessageRequest) error {
|
|
||||||
var err error
|
|
||||||
// 根據 Sort 欄位對 SMSProviders 進行排序
|
|
||||||
sort.Slice(use.param.SMSProviders, func(i, j int) bool {
|
|
||||||
return use.param.SMSProviders[i].Sort < use.param.SMSProviders[j].Sort
|
|
||||||
})
|
|
||||||
|
|
||||||
newCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
// 依序嘗試發送
|
|
||||||
for _, provider := range use.param.SMSProviders {
|
|
||||||
if err = provider.Repo.SendSMS(newCtx, repository.SMSMessageRequest{
|
|
||||||
PhoneNumber: req.PhoneNumber,
|
|
||||||
RecipientName: req.RecipientName,
|
|
||||||
MessageContent: req.MessageContent,
|
|
||||||
}); err == nil {
|
|
||||||
return nil // 發送成功
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (use *DeliveryUseCase) SendEmail(ctx context.Context, req usecase.MailReq) error {
|
|
||||||
var err error
|
|
||||||
// 根據 Sort 欄位對 SMSProviders 進行排序
|
|
||||||
sort.Slice(use.param.EmailProviders, func(i, j int) bool {
|
|
||||||
return use.param.EmailProviders[i].Sort < use.param.EmailProviders[j].Sort
|
|
||||||
})
|
|
||||||
|
|
||||||
newCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
// 依序嘗試發送 dreq
|
|
||||||
for _, provider := range use.param.EmailProviders {
|
|
||||||
if err = provider.Repo.SendMail(newCtx, repository.MailReq{
|
|
||||||
From: req.From,
|
|
||||||
To: req.To,
|
|
||||||
Subject: req.Subject,
|
|
||||||
Body: req.Body,
|
|
||||||
}); err == nil {
|
|
||||||
return nil // 發送成功
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
|
@ -1,43 +0,0 @@
|
||||||
package usecase
|
|
||||||
|
|
||||||
import (
|
|
||||||
"code.30cm.net/digimon/app-cloudep-notification-service/pkg/domain/template"
|
|
||||||
"code.30cm.net/digimon/app-cloudep-notification-service/pkg/domain/usecase"
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
)
|
|
||||||
|
|
||||||
type TemplateUseCaseParam struct{}
|
|
||||||
|
|
||||||
type TemplateUseCase struct {
|
|
||||||
TemplateUseCaseParam
|
|
||||||
}
|
|
||||||
|
|
||||||
func MustTemplateUseCase(param TemplateUseCaseParam) usecase.TemplateUseCase {
|
|
||||||
return &TemplateUseCase{
|
|
||||||
param,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (use *TemplateUseCase) GetEmailTemplateByStatic(_ context.Context, language template.Language, templateID template.Type) (template.EmailTemplate, error) {
|
|
||||||
// 查找指定語言的模板映射
|
|
||||||
templateByLang, exists := template.EmailTemplateMap[language]
|
|
||||||
if !exists {
|
|
||||||
return template.EmailTemplate{}, fmt.Errorf("email template not found for language: %s", language)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 查找指定類型的模板生成函數
|
|
||||||
templateFunc, exists := templateByLang[templateID]
|
|
||||||
if !exists {
|
|
||||||
return template.EmailTemplate{}, fmt.Errorf("email template not found for type ID: %s", templateID)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 執行模板生成函數
|
|
||||||
tmp, err := templateFunc()
|
|
||||||
if err != nil {
|
|
||||||
return template.EmailTemplate{}, fmt.Errorf("error generating email template: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 返回構建好的響應
|
|
||||||
return tmp, nil
|
|
||||||
}
|
|
|
@ -2,13 +2,12 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/zeromicro/go-zero/core/logx"
|
"app-cloudep-notification-service/gen_result/pb/notification"
|
||||||
|
"app-cloudep-notification-service/internal/config"
|
||||||
"code.30cm.net/digimon/app-cloudep-notification-service/gen_result/pb/notification"
|
senderserviceServer "app-cloudep-notification-service/internal/server/senderservice"
|
||||||
"code.30cm.net/digimon/app-cloudep-notification-service/internal/config"
|
"app-cloudep-notification-service/internal/svc"
|
||||||
senderserviceServer "code.30cm.net/digimon/app-cloudep-notification-service/internal/server/senderservice"
|
|
||||||
"code.30cm.net/digimon/app-cloudep-notification-service/internal/svc"
|
|
||||||
|
|
||||||
"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"
|
||||||
|
@ -17,7 +16,7 @@ import (
|
||||||
"google.golang.org/grpc/reflection"
|
"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() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
@ -35,6 +34,6 @@ func main() {
|
||||||
})
|
})
|
||||||
defer s.Stop()
|
defer s.Stop()
|
||||||
|
|
||||||
logx.Infof("Starting rpc server at %s...\n", c.ListenOn)
|
fmt.Printf("Starting rpc server at %s...\n", c.ListenOn)
|
||||||
s.Start()
|
s.Start()
|
||||||
}
|
}
|
Loading…
Reference in New Issue