From c1188a8308a635decbb1ce954200c9c345c495f6 Mon Sep 17 00:00:00 2001 From: "daniel.w" Date: Tue, 20 Aug 2024 14:35:14 +0800 Subject: [PATCH] feat: make file --- Makefile | 37 ++++++++ client/senderservice/sender_service.go | 66 ++++++++++++++ etc/service.yaml | 6 ++ generate/database/readme.md | 2 +- generate/protobuf/notification.proto | 21 ----- generate/protobuf/service.proto | 44 ++++++++++ go.mod | 88 +++++++++++++++++++ internal/config/config.go | 7 ++ .../send_mail_by_template_id_logic.go | 31 +++++++ .../logic/senderservice/send_mail_logic.go | 31 +++++++ .../send_sms_by_template_id_logic.go | 31 +++++++ .../logic/senderservice/send_sms_logic.go | 31 +++++++ .../senderservice/sender_service_server.go | 47 ++++++++++ internal/svc/service_context.go | 13 +++ makefile | 0 service.go | 39 ++++++++ 16 files changed, 472 insertions(+), 22 deletions(-) create mode 100644 Makefile create mode 100644 client/senderservice/sender_service.go create mode 100644 etc/service.yaml delete mode 100644 generate/protobuf/notification.proto create mode 100644 generate/protobuf/service.proto create mode 100644 go.mod create mode 100755 internal/config/config.go create mode 100644 internal/logic/senderservice/send_mail_by_template_id_logic.go create mode 100644 internal/logic/senderservice/send_mail_logic.go create mode 100644 internal/logic/senderservice/send_sms_by_template_id_logic.go create mode 100644 internal/logic/senderservice/send_sms_logic.go create mode 100644 internal/server/senderservice/sender_service_server.go create mode 100644 internal/svc/service_context.go delete mode 100644 makefile create mode 100644 service.go diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..fe4b62c --- /dev/null +++ b/Makefile @@ -0,0 +1,37 @@ +GO_CTL_NAME=goctl + + +# go-zero 生成風格 +GO_ZERO_STYLE=go_zero + +GO ?= go +GOFMT ?= gofmt "-s" +GOFILES := $(shell find . -name "*.go") +LDFLAGS := -s -w + +.PHONY: test +test: # 進行測試 + go test -v --cover ./... + +.PHONY: fmt +fmt: # 格式優化 + $(GOFMT) -w $(GOFILES) + goimports -w ./ + +.PHONY: gen-rpc +gen-rpc: # 建立 rpc code + $(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" + +.PHONY: gen-clean +gen-clean: # 建立 rpc code + rm -rf ./client + rm -rf ./etc + rm -rf ./gen_result + rm -rf ./internal + rm -rf go.mod + rm -rf go.sum + rm -rf service.go + @echo "Generate core-api files successfully" \ No newline at end of file diff --git a/client/senderservice/sender_service.go b/client/senderservice/sender_service.go new file mode 100644 index 0000000..a647688 --- /dev/null +++ b/client/senderservice/sender_service.go @@ -0,0 +1,66 @@ +// Code generated by goctl. DO NOT EDIT. +// Source: service.proto + +package senderservice + +import ( + "context" + + "app-cloudep-notification-service/gen_result/pb/notification" + + "github.com/zeromicro/go-zero/zrpc" + "google.golang.org/grpc" +) + +type ( + NoneReq = notification.NoneReq + OKResp = notification.OKResp + SendByTemplateIDReq = notification.SendByTemplateIDReq + SendMailReq = notification.SendMailReq + SendSMSReq = notification.SendSMSReq + + SenderService interface { + // SendMail 寄信 + SendMail(ctx context.Context, in *SendMailReq, opts ...grpc.CallOption) (*OKResp, error) + // SendSMS 寄簡訊 + SendSms(ctx context.Context, in *SendSMSReq, opts ...grpc.CallOption) (*OKResp, error) + // SendMailByTemplateID 寄送模板信件 + 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 { + cli zrpc.Client + } +) + +func NewSenderService(cli zrpc.Client) SenderService { + return &defaultSenderService{ + cli: cli, + } +} + +// SendMail 寄信 +func (m *defaultSenderService) SendMail(ctx context.Context, in *SendMailReq, opts ...grpc.CallOption) (*OKResp, error) { + client := notification.NewSenderServiceClient(m.cli.Conn()) + return client.SendMail(ctx, in, opts...) +} + +// 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 寄送模板信件 +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 寄送模板簡訊 +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...) +} diff --git a/etc/service.yaml b/etc/service.yaml new file mode 100644 index 0000000..f9a189a --- /dev/null +++ b/etc/service.yaml @@ -0,0 +1,6 @@ +Name: service.rpc +ListenOn: 0.0.0.0:8080 +Etcd: + Hosts: + - 127.0.0.1:2379 + Key: service.rpc diff --git a/generate/database/readme.md b/generate/database/readme.md index 58e2e2e..4db6e02 100644 --- a/generate/database/readme.md +++ b/generate/database/readme.md @@ -5,7 +5,7 @@ ## 安裝 make ```shell -brew install makefile +brew install Makefile ``` ## 安裝 golang-migrate diff --git a/generate/protobuf/notification.proto b/generate/protobuf/notification.proto deleted file mode 100644 index c748ac4..0000000 --- a/generate/protobuf/notification.proto +++ /dev/null @@ -1,21 +0,0 @@ -syntax = "proto3"; - -package "app-cloudep-notification-service"; - -option go_package="./app-cloudep-notification-service"; - -// OKResp -message OKResp {} -// NoneReq -message NoneReq {} - - -// DemoService ... -service NotificationService { - // Ping ... - rpc Ping(OKResp)returns(NoneReq); - -} - - - diff --git a/generate/protobuf/service.proto b/generate/protobuf/service.proto new file mode 100644 index 0000000..2d84dce --- /dev/null +++ b/generate/protobuf/service.proto @@ -0,0 +1,44 @@ +syntax = "proto3"; + +package notification; + +option go_package="./notification"; + +// OKResp +message OKResp {} +// NoneReq +message NoneReq {} + +message SendMailReq { + string to = 1; + string subject = 2; + string body = 3; +} + +message SendSMSReq { + string to = 1; + string body = 2; +} + +message SendByTemplateIDReq { + string to = 1; + string template_id =2; + string lang=3; + map content_data = 4; +} + + +service SenderService { + // SendMail 寄信 + rpc SendMail(SendMailReq) returns(OKResp); + // SendSMS 寄簡訊 + rpc SendSms(SendSMSReq) returns(OKResp); + // SendMailByTemplateID 寄送模板信件 + rpc SendMailByTemplateId(SendByTemplateIDReq) returns(OKResp); + // SendSMSByTemplateID 寄送模板簡訊 + rpc SendSmsByTemplateId(SendByTemplateIDReq) returns(OKResp); +} + + + + diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..fb80d99 --- /dev/null +++ b/go.mod @@ -0,0 +1,88 @@ +module app-cloudep-notification-service + +go 1.22.3 + +require ( + github.com/zeromicro/go-zero v1.7.0 + google.golang.org/grpc v1.65.0 + google.golang.org/protobuf v1.34.2 +) + +require ( + github.com/beorn7/perks v1.0.1 // indirect + github.com/cenkalti/backoff/v4 v4.3.0 // indirect + github.com/cespare/xxhash/v2 v2.3.0 // indirect + github.com/coreos/go-semver v0.3.1 // indirect + github.com/coreos/go-systemd/v22 v22.5.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect + 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/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/gogo/protobuf v1.3.2 // indirect + github.com/golang/mock v1.6.0 // indirect + github.com/golang/protobuf v1.5.4 // indirect + github.com/google/gnostic-models v0.6.8 // indirect + github.com/google/go-cmp v0.6.0 // indirect + github.com/google/gofuzz v1.2.0 // indirect + github.com/google/uuid v1.6.0 // indirect + 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/mailru/easyjson v0.7.7 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + 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/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 + github.com/prometheus/common v0.48.0 // indirect + github.com/prometheus/procfs v0.12.0 // indirect + github.com/redis/go-redis/v9 v9.6.1 // indirect + github.com/spaolacci/murmur3 v1.1.0 // 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/v3 v3.5.15 // indirect + go.opentelemetry.io/otel v1.24.0 // indirect + go.opentelemetry.io/otel/exporters/jaeger v1.17.0 // indirect + go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.24.0 // indirect + go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.24.0 // indirect + go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.24.0 // indirect + go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.24.0 // indirect + go.opentelemetry.io/otel/exporters/zipkin v1.24.0 // indirect + go.opentelemetry.io/otel/metric v1.24.0 // indirect + go.opentelemetry.io/otel/sdk v1.24.0 // indirect + go.opentelemetry.io/otel/trace v1.24.0 // indirect + go.opentelemetry.io/proto/otlp v1.3.1 // indirect + go.uber.org/atomic v1.10.0 // indirect + 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/net v0.27.0 // indirect + golang.org/x/oauth2 v0.20.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/inf.v0 v0.9.1 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect + k8s.io/api v0.29.3 // indirect + k8s.io/apimachinery v0.29.4 // indirect + k8s.io/client-go v0.29.3 // indirect + k8s.io/klog/v2 v2.110.1 // indirect + k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect + k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect + sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect + sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect + sigs.k8s.io/yaml v1.3.0 // indirect +) diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100755 index 0000000..c1f85b9 --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,7 @@ +package config + +import "github.com/zeromicro/go-zero/zrpc" + +type Config struct { + zrpc.RpcServerConf +} diff --git a/internal/logic/senderservice/send_mail_by_template_id_logic.go b/internal/logic/senderservice/send_mail_by_template_id_logic.go new file mode 100644 index 0000000..d5790f1 --- /dev/null +++ b/internal/logic/senderservice/send_mail_by_template_id_logic.go @@ -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 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) { + // todo: add your logic here and delete this line + + return ¬ification.OKResp{}, nil +} diff --git a/internal/logic/senderservice/send_mail_logic.go b/internal/logic/senderservice/send_mail_logic.go new file mode 100644 index 0000000..6af7fc1 --- /dev/null +++ b/internal/logic/senderservice/send_mail_logic.go @@ -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 SendMailLogic struct { + ctx context.Context + svcCtx *svc.ServiceContext + logx.Logger +} + +func NewSendMailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendMailLogic { + return &SendMailLogic{ + ctx: ctx, + svcCtx: svcCtx, + Logger: logx.WithContext(ctx), + } +} + +// SendMail 寄信 +func (l *SendMailLogic) SendMail(in *notification.SendMailReq) (*notification.OKResp, error) { + // todo: add your logic here and delete this line + + return ¬ification.OKResp{}, nil +} diff --git a/internal/logic/senderservice/send_sms_by_template_id_logic.go b/internal/logic/senderservice/send_sms_by_template_id_logic.go new file mode 100644 index 0000000..5895774 --- /dev/null +++ b/internal/logic/senderservice/send_sms_by_template_id_logic.go @@ -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 +} diff --git a/internal/logic/senderservice/send_sms_logic.go b/internal/logic/senderservice/send_sms_logic.go new file mode 100644 index 0000000..7e5e4f9 --- /dev/null +++ b/internal/logic/senderservice/send_sms_logic.go @@ -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 SendSmsLogic struct { + ctx context.Context + svcCtx *svc.ServiceContext + logx.Logger +} + +func NewSendSmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendSmsLogic { + return &SendSmsLogic{ + ctx: ctx, + svcCtx: svcCtx, + Logger: logx.WithContext(ctx), + } +} + +// SendSMS 寄簡訊 +func (l *SendSmsLogic) SendSms(in *notification.SendSMSReq) (*notification.OKResp, error) { + // todo: add your logic here and delete this line + + return ¬ification.OKResp{}, nil +} diff --git a/internal/server/senderservice/sender_service_server.go b/internal/server/senderservice/sender_service_server.go new file mode 100644 index 0000000..cfd9b87 --- /dev/null +++ b/internal/server/senderservice/sender_service_server.go @@ -0,0 +1,47 @@ +// Code generated by goctl. DO NOT EDIT. +// Source: service.proto + +package server + +import ( + "context" + + "app-cloudep-notification-service/gen_result/pb/notification" + "app-cloudep-notification-service/internal/logic/senderservice" + "app-cloudep-notification-service/internal/svc" +) + +type SenderServiceServer struct { + svcCtx *svc.ServiceContext + notification.UnimplementedSenderServiceServer +} + +func NewSenderServiceServer(svcCtx *svc.ServiceContext) *SenderServiceServer { + return &SenderServiceServer{ + svcCtx: svcCtx, + } +} + +// SendMail 寄信 +func (s *SenderServiceServer) SendMail(ctx context.Context, in *notification.SendMailReq) (*notification.OKResp, error) { + l := senderservicelogic.NewSendMailLogic(ctx, s.svcCtx) + return l.SendMail(in) +} + +// 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 寄送模板信件 +func (s *SenderServiceServer) SendMailByTemplateId(ctx context.Context, in *notification.SendByTemplateIDReq) (*notification.OKResp, error) { + l := senderservicelogic.NewSendMailByTemplateIdLogic(ctx, s.svcCtx) + 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) +} diff --git a/internal/svc/service_context.go b/internal/svc/service_context.go new file mode 100644 index 0000000..d0f930f --- /dev/null +++ b/internal/svc/service_context.go @@ -0,0 +1,13 @@ +package svc + +import "app-cloudep-notification-service/internal/config" + +type ServiceContext struct { + Config config.Config +} + +func NewServiceContext(c config.Config) *ServiceContext { + return &ServiceContext{ + Config: c, + } +} diff --git a/makefile b/makefile deleted file mode 100644 index e69de29..0000000 diff --git a/service.go b/service.go new file mode 100644 index 0000000..e202e4e --- /dev/null +++ b/service.go @@ -0,0 +1,39 @@ +package main + +import ( + "flag" + "fmt" + + "app-cloudep-notification-service/gen_result/pb/notification" + "app-cloudep-notification-service/internal/config" + senderserviceServer "app-cloudep-notification-service/internal/server/senderservice" + "app-cloudep-notification-service/internal/svc" + + "github.com/zeromicro/go-zero/core/conf" + "github.com/zeromicro/go-zero/core/service" + "github.com/zeromicro/go-zero/zrpc" + "google.golang.org/grpc" + "google.golang.org/grpc/reflection" +) + +var configFile = flag.String("f", "etc/service.yaml", "the config file") + +func main() { + flag.Parse() + + var c config.Config + conf.MustLoad(*configFile, &c) + ctx := svc.NewServiceContext(c) + + s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) { + notification.RegisterSenderServiceServer(grpcServer, senderserviceServer.NewSenderServiceServer(ctx)) + + if c.Mode == service.DevMode || c.Mode == service.TestMode { + reflection.Register(grpcServer) + } + }) + defer s.Stop() + + fmt.Printf("Starting rpc server at %s...\n", c.ListenOn) + s.Start() +} -- 2.40.1