Compare commits

...

2 Commits

Author SHA1 Message Date
daniel.w dd872b7abf feat: init make file and docker file 2024-08-20 18:05:39 +08:00
daniel.w c1188a8308 feat: make file 2024-08-20 14:35:14 +08:00
17 changed files with 512 additions and 48 deletions

48
Makefile Normal file
View File

@ -0,0 +1,48 @@
GO_CTL_NAME=goctl
# go-zero 生成風格
GO_ZERO_STYLE=go_zero
GO ?= go
GOFMT ?= gofmt "-s"
GOFILES := $(shell find . -name "*.go")
LDFLAGS := -s -w
VERSION="v1.0.3"
DOCKER_REPO="igs170911/notification"
.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"
.PHONY: run-docker
run-docker: # 建立 rpc code
docker run --platform=linux/arm64/v8 -p 8080:8080 $(DOCKER_REPO):$(VERSION)
.PHONY: build-docker
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

View File

@ -2,46 +2,46 @@
# BUILDER #
###########
FROM golang:1.20 as builder
FROM golang:1.22.3 as builder
ARG VERSION
ARG BUILT
ARG GIT_COMMIT
ARG SSH_PRV_KEY
# private go packages
ENV GOPRIVATE=code.30cm.net
ENV FLAG="-s -w -X main.Version=${VERSION} -X main.Built=${BUILT} -X main.GitCommit=${GIT_COMMIT}"
WORKDIR /app
COPY . .
RUN apt-get update && \
apt-get install git
WORKDIR /app
# Make the root foler for our ssh
RUN mkdir -p /root/.ssh && \
chmod 0700 /root/.ssh && \
ssh-keyscan git.30cm.net > /root/.ssh/known_hosts && \
echo "$SSH_PRV_KEY" > /root/.ssh/id_rsa && \
chmod 600 /root/.ssh/id_rsa
COPY . .
# Download public key for yt.com
RUN mkdir -p -m 0700 ~/.ssh && ssh-keyscan yt.com >> ~/.ssh/known_hosts
# Forces the usage of git and ssh key fwded by ssh-agent for yt.com git repos
RUN git config --global url."git@yt.com:".insteadOf "https://yt.com"
# private go packages
ENV GOPRIVATE=yt.com
RUN --mount=type=ssh go mod download
ENV FLAG="-s -w -X main.Version=${VERSION} -X main.Built=${BUILT} -X main.GitCommit=${GIT_COMMIT}"
RUN CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build \
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
-ldflags "$FLAG" \
-o /permission-server ./cmd/permission
#########
# FINAL #
#########
FROM oraclelinux:9-slim AS final
-o service
##########
## FINAL #
##########
#
FROM gcr.io/distroless/static-debian11
WORKDIR /app
COPY --from=builder /permission-server /app/permission-server
COPY --from=builder /app/configs/config-example.yaml /app/config.yaml
COPY --from=builder /app/configs/rbac_model.conf /app/rbac_model.conf
CMD ["/app/permission-server"]
COPY --from=builder /app/service /app/service
COPY --from=builder /app/etc/service.yaml /app/etc/service.yaml
EXPOSE 8080
CMD ["/app/service"]

View File

@ -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...)
}

6
etc/service.yaml Normal file
View File

@ -0,0 +1,6 @@
Name: service.rpc
ListenOn: 0.0.0.0:8080
Etcd:
Hosts:
- docker-etcd.digimon.orb.local:2379
Key: service.rpc

View File

@ -5,7 +5,7 @@
## 安裝 make
```shell
brew install makefile
brew install Makefile
```
## 安裝 golang-migrate

View File

@ -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);
}

View File

@ -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<string, string> 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);
}

89
go.mod Normal file
View File

@ -0,0 +1,89 @@
module app-cloudep-notification-service
go 1.22.3
require (
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
)
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
)

7
internal/config/config.go Executable file
View File

@ -0,0 +1,7 @@
package config
import "github.com/zeromicro/go-zero/zrpc"
type Config struct {
zrpc.RpcServerConf
}

View File

@ -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 &notification.OKResp{}, nil
}

View File

@ -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 &notification.OKResp{}, nil
}

View File

@ -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 &notification.OKResp{}, nil
}

View File

@ -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 &notification.OKResp{}, nil
}

View File

@ -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"
senderservicelogic "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)
}

View File

@ -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,
}
}

View File

39
service.go Normal file
View File

@ -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()
}