feat 测试
Some checks failed
Build image / build (push) Failing after 4m48s

This commit is contained in:
timerzz 2024-05-20 17:57:25 +08:00
parent 6d7314b9a3
commit 4ec0cd1564
39 changed files with 4056 additions and 388 deletions

View File

@ -0,0 +1,16 @@
name: Build image
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: build
run: docker build -t ${{ vars.DOCKER_REGISTRY }}/${{ vars.IMAGE_NAME }}:1.3 -f Dockerfile .
- name: tag
run: docker tag ${{ vars.DOCKER_REGISTRY }}/${{ vars.IMAGE_NAME }}:1.3 ${{ vars.DOCKER_REGISTRY }}/${{ vars.IMAGE_NAME }}:latest
- name: push 1.3
run: docker push ${{ vars.DOCKER_REGISTRY }}/${{ vars.IMAGE_NAME }}:1.3
- name: push latest
run: docker push ${{ vars.DOCKER_REGISTRY }}/${{ vars.IMAGE_NAME }}:latest

16
Dockerfile Normal file
View File

@ -0,0 +1,16 @@
FROM golang:1.22 as back
WORKDIR /build
COPY . /build
ARG CGO_ENABLED=0
ARG GOPROXY=https://goproxy.cn,direct
ARG GOPRIVATE=gitea.timerzz.com
ARG GONOSUMDB=gitea.timerzz.com
ARG GONOPROXY=gitea.timerzz.com
RUN go build -trimpath -ldflags '-w -s' -o pusher .
FROM alpine:latest
WORKDIR /work
COPY --from=back /build/run /work/
ENTRYPOINT ["/work/pusher"]

View File

@ -1,9 +1,9 @@
package dal
import (
"gitea.timerzz.com/kedaya_haitao/pusher/biz/dal/pgsql"
"gitea.timerzz.com/kedaya_haitao/pusher/biz/dal/postgres"
)
func Init() {
pgsql.Init()
postgres.Init()
}

View File

@ -1,7 +1,8 @@
package pgsql
package postgres
import (
"gitea.timerzz.com/kedaya_haitao/pusher/conf"
"gitea.timerzz.com/kedaya_haitao/pusher/kitex_gen/config"
"gorm.io/driver/postgres"
"gorm.io/gorm"
@ -22,4 +23,5 @@ func Init() {
if err != nil {
panic(err)
}
_ = DB.AutoMigrate(&config.PusherConfig{})
}

View File

@ -4,7 +4,85 @@ package push
import (
"context"
"gitea.timerzz.com/kedaya_haitao/pusher/biz/service"
config "gitea.timerzz.com/kedaya_haitao/pusher/kitex_gen/config"
push "gitea.timerzz.com/kedaya_haitao/pusher/kitex_gen/push"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/protocol/consts"
)
// Push .
// @router /api/v1/push [POST]
func Push(ctx context.Context, c *app.RequestContext) {
var err error
var req push.PushReq
err = c.BindAndValidate(&req)
if err != nil {
c.String(consts.StatusBadRequest, err.Error())
return
}
resp, err := service.NewPushService(ctx).Run(&req)
if err != nil {
c.String(consts.StatusBadRequest, err.Error())
return
}
c.JSON(consts.StatusOK, resp)
}
// Add .
// @router /api/v1/pushers [POST]
func Add(ctx context.Context, c *app.RequestContext) {
var err error
var req config.PusherConfig
err = c.BindAndValidate(&req)
if err != nil {
c.String(consts.StatusBadRequest, err.Error())
return
}
resp := new(push.Resp)
resp, err = service.NewAddService(ctx).Run(&req)
if err != nil {
c.String(consts.StatusBadRequest, err.Error())
return
}
c.JSON(consts.StatusOK, resp)
}
// List .
// @router /api/v1/pushers [GET]
func List(ctx context.Context, c *app.RequestContext) {
var err error
var req push.ListPusherRequest
err = c.BindAndValidate(&req)
if err != nil {
c.String(consts.StatusBadRequest, err.Error())
return
}
var resp *push.ListPusherResponse
resp, err = service.NewListService(ctx).Run(&req)
if err != nil {
c.String(consts.StatusBadRequest, err.Error())
return
}
c.JSON(consts.StatusOK, resp)
}
// GetPusherOptions .
// @router /api/v1/pushers/options [GET]
func GetPusherOptions(ctx context.Context, c *app.RequestContext) {
resp, err := service.NewGetPusherOptionsService(ctx).Run()
if err != nil {
c.String(consts.StatusBadRequest, err.Error())
return
}
c.JSON(consts.StatusOK, resp)
}

View File

@ -5,3 +5,43 @@ package push
import (
"github.com/cloudwego/hertz/pkg/app"
)
func rootMw() []app.HandlerFunc {
// your code...
return nil
}
func _apiMw() []app.HandlerFunc {
// your code...
return nil
}
func _v1Mw() []app.HandlerFunc {
// your code...
return nil
}
func _pushMw() []app.HandlerFunc {
// your code...
return nil
}
func _pushersMw() []app.HandlerFunc {
// your code...
return nil
}
func _addMw() []app.HandlerFunc {
// your code...
return nil
}
func _getpusheroptionsMw() []app.HandlerFunc {
// your code...
return nil
}
func _listMw() []app.HandlerFunc {
// your code...
return nil
}

View File

@ -16,4 +16,16 @@ import (
// Register register routes based on the IDL 'api.${HTTP Method}' annotation.
func Register(r *server.Hertz) {
root := r.Group("/", rootMw()...)
{
_api := root.Group("/api", _apiMw()...)
{
_v1 := _api.Group("/v1", _v1Mw()...)
_v1.POST("/push", append(_pushMw(), push.Push)...)
_v1.POST("/pushers", append(_addMw(), push.Add)...)
_pushers := _v1.Group("/pushers", _pushersMw()...)
_pushers.GET("/options", append(_getpusheroptionsMw(), push.GetPusherOptions)...)
_v1.GET("/pushers", append(_listMw(), push.List)...)
}
}
}

View File

@ -2,21 +2,26 @@ package service
import (
"context"
"gitea.timerzz.com/kedaya_haitao/pusher/biz/dal/postgres"
config "gitea.timerzz.com/kedaya_haitao/pusher/kitex_gen/config"
push "gitea.timerzz.com/kedaya_haitao/pusher/kitex_gen/push"
)
type AddService struct {
ctx context.Context
}
// NewAddService new AddService
} // NewAddService new AddService
func NewAddService(ctx context.Context) *AddService {
return &AddService{ctx: ctx}
}
// Run create note info
func (s *AddService) Run(req *config.PushConfig) error {
func (s *AddService) Run(req *config.PusherConfig) (resp *push.Resp, err error) {
// Finish your business logic.
return nil
resp = push.NewResp()
err = postgres.DB.Create(req).Error
if err != nil {
resp.Code = 100
resp.Msg = err.Error()
}
return
}

View File

@ -3,6 +3,7 @@ package service
import (
"context"
config "gitea.timerzz.com/kedaya_haitao/pusher/kitex_gen/config"
push "gitea.timerzz.com/kedaya_haitao/pusher/kitex_gen/push"
"testing"
)
@ -11,12 +12,11 @@ func TestAdd_Run(t *testing.T) {
s := NewAddService(ctx)
// init req and assert value
req := &config.PushConfig{}
req := &config.PusherConfig{}
resp, err := s.Run(req)
t.Logf("err: %v", err)
t.Logf("resp: %v", resp)
err := s.Run(req)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
// todo: edit your unit test
}

View File

@ -0,0 +1,40 @@
package service
import (
"context"
"gitea.timerzz.com/kedaya_haitao/pusher/kitex_gen/config"
push "gitea.timerzz.com/kedaya_haitao/pusher/kitex_gen/push"
"reflect"
)
type GetPusherOptionsService struct {
ctx context.Context
} // NewGetPusherOptionsService new GetPusherOptionsService
func NewGetPusherOptionsService(ctx context.Context) *GetPusherOptionsService {
return &GetPusherOptionsService{ctx: ctx}
}
// Run create note info
func (s *GetPusherOptionsService) Run() (resp *push.GetPusherOptionsResponse, err error) {
// Finish your business logic.
resp = new(push.GetPusherOptionsResponse)
resp.Options = map[config.PusherConfigType][]*config.FormItem{
config.PusherConfigType_AnPush: toFormItems(config.AnPush{}),
config.PusherConfigType_Email: toFormItems(config.EmailPush{}),
}
return
}
func toFormItems(in any) []*config.FormItem {
valueOfIn := reflect.ValueOf(in)
numOfFields := valueOfIn.NumField()
items := make([]*config.FormItem, 0, numOfFields)
for i := 0; i < numOfFields; i++ {
items = append(items, &config.FormItem{
Param: valueOfIn.Field(i).String(),
Type: valueOfIn.Field(i).Type().String(),
Require: true,
})
}
return items
}

View File

@ -0,0 +1,19 @@
package service
import (
"context"
push "gitea.timerzz.com/kedaya_haitao/pusher/kitex_gen/push"
"testing"
)
func TestGetPusherOptions_Run(t *testing.T) {
ctx := context.Background()
s := NewGetPusherOptionsService(ctx)
// init req and assert value
resp, err := s.Run()
t.Logf("err: %v", err)
t.Logf("resp: %v", resp)
// todo: edit your unit test
}

View File

@ -2,6 +2,9 @@ package service
import (
"context"
"fmt"
"gitea.timerzz.com/kedaya_haitao/pusher/biz/dal/postgres"
"gitea.timerzz.com/kedaya_haitao/pusher/kitex_gen/config"
push "gitea.timerzz.com/kedaya_haitao/pusher/kitex_gen/push"
)
@ -15,6 +18,32 @@ func NewListService(ctx context.Context) *ListService {
// Run create note info
func (s *ListService) Run(req *push.ListPusherRequest) (resp *push.ListPusherResponse, err error) {
// Finish your business logic.
tx := postgres.DB
if req.Keyword != "" {
tx = tx.Where("name LIKE ? or remark LIKE ?", fmt.Sprintf("%%%s%%", req.Keyword), fmt.Sprintf("%%%s%%", req.Keyword))
}
resp = new(push.ListPusherResponse)
if err = tx.Model(&config.PusherConfig{}).Find(&resp.List).Error; err != nil {
return nil, fmt.Errorf("查询总数失败:%v", err)
}
resp.Total = int64(len(resp.List))
if req.All || resp.Total == 0 {
return
}
// 查询列表
if req.Page < 1 {
req.Page = 1
}
if req.Size < 1 {
req.Size = 10
}
offset := (req.Page - 1) * int64(req.Size)
if err = tx.Order("created_at desc").Limit(int(req.Size)).Offset(int(offset)).
Find(&resp.List).Error; err != nil {
return nil, fmt.Errorf("查询列表失败:%v", err)
}
return
}

View File

@ -2,7 +2,9 @@ package service
import (
"context"
"fmt"
push "gitea.timerzz.com/kedaya_haitao/pusher/kitex_gen/push"
"gitea.timerzz.com/kedaya_haitao/pusher/pushers"
)
type PushService struct {
@ -13,8 +15,16 @@ func NewPushService(ctx context.Context) *PushService {
}
// Run create note info
func (s *PushService) Run(req *push.PushReq) (resp *push.PushResp, err error) {
func (s *PushService) Run(req *push.PushReq) (resp *push.Resp, err error) {
// Finish your business logic.
if len(req.Ids) == 0 {
return nil, fmt.Errorf("缺少id")
}
resp = push.NewResp()
err = pushers.Push(s.ctx, req.Ids, req.Title, req.Content)
if err != nil {
resp.Code = 100
resp.Msg = err.Error()
}
return
}

View File

@ -1,7 +0,0 @@
#!/usr/bin/env bash
RUN_NAME="pusher"
mkdir -p output/bin output/conf
cp script/* output/
cp -r conf/* output/conf
chmod +x output/bootstrap.sh
go build -o output/bin/${RUN_NAME}

View File

@ -54,7 +54,7 @@ func GetConf() *Config {
}
func initConf() {
prefix := "conf"
prefix := "/data"
confFileRelPath := filepath.Join(prefix, filepath.Join(GetEnv(), "conf.yaml"))
content, err := ioutil.ReadFile(confFileRelPath)
if err != nil {

View File

@ -14,4 +14,4 @@ registry:
password: ""
db:
dsn: "gorm:gorm@tcp(127.0.0.1:3306)/gorm?charset=utf8mb4&parseTime=True&loc=Local"
dsn: "host=192.168.31.55 user=timerzz password=zhhg1997 dbname=kedaya_dev port=5432 sslmode=disable TimeZone=Asia/Shanghai"

View File

@ -14,4 +14,4 @@ registry:
password: ""
db:
dsn: "gorm:gorm@tcp(127.0.0.1:3306)/gorm?charset=utf8mb4&parseTime=True&loc=Local"
dsn: "host=192.168.31.55 user=timerzz password=zhhg1997 dbname=kedaya port=5432 sslmode=disable TimeZone=Asia/Shanghai"

2
gen.sh Normal file
View File

@ -0,0 +1,2 @@
cwgo server --type RPC --idl .\idl\push.thrift --service pusher --module gitea.timerzz.com/kedaya_haitao/pusher --hex
cwgo client --type RPC --idl .\idl\push.thrift --service pusher --module gitea.timerzz.com/kedaya_haitao/pusher

10
go.mod
View File

@ -8,6 +8,7 @@ require (
github.com/apache/thrift v0.13.0
github.com/cloudwego/hertz v0.9.0
github.com/cloudwego/kitex v0.9.1
github.com/go-resty/resty/v2 v2.13.1
github.com/kitex-contrib/obs-opentelemetry/logging/logrus v0.0.0-20240515092919-1f776656cb66
github.com/kr/pretty v0.3.0
go.uber.org/zap v1.27.0
@ -62,15 +63,16 @@ require (
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/wneessen/go-mail v0.4.1 // indirect
go.opentelemetry.io/otel v1.19.0 // indirect
go.opentelemetry.io/otel/trace v1.19.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/arch v0.2.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
google.golang.org/genproto v0.0.0-20210513213006-bf773b8c8384 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect

24
go.sum
View File

@ -116,6 +116,8 @@ github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/go-pdf/fpdf v0.5.0/go.mod h1:HzcnA+A23uwogo0tp9yU+l3V+KXhiESpt1PMayhOh5M=
github.com/go-pdf/fpdf v0.6.0/go.mod h1:HzcnA+A23uwogo0tp9yU+l3V+KXhiESpt1PMayhOh5M=
github.com/go-resty/resty/v2 v2.13.1 h1:x+LHXBI2nMB1vqndymf26quycC4aggYJ7DECYbiz03g=
github.com/go-resty/resty/v2 v2.13.1/go.mod h1:GznXlLxkq6Nh4sU59rPmUw3VtgpO3aS96ORAI6Q7d+0=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
@ -255,6 +257,8 @@ github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2
github.com/v2pro/plz v0.0.0-20221028024117-e5f9aec5b631/go.mod h1:3gacX+hQo+xvl0vtLqCMufzxuNCwt4geAVOMt2LQYfE=
github.com/v2pro/quokka v0.0.0-20171201153428-382cb39c6ee6/go.mod h1:0VP5W9AFNVWU8C1QLNeVg8TvzoEkIHWZ4vxtxEVFWUY=
github.com/v2pro/wombat v0.0.0-20180402055224-a56dbdcddef2/go.mod h1:wen8nMxrRrUmXnRwH+3wGAW+hyYTHcOrTNhMpxyp/i0=
github.com/wneessen/go-mail v0.4.1 h1:m2rSg/sc8FZQCdtrV5M8ymHYOFrC6KJAQAIcgrXvqoo=
github.com/wneessen/go-mail v0.4.1/go.mod h1:zxOlafWCP/r6FEhAaRgH4IC1vg2YXxO0Nar9u0IScZ8=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
@ -287,8 +291,10 @@ golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20180807140117-3d87b88a115f/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
@ -340,8 +346,10 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug
golang.org/x/net v0.0.0-20221014081412-f15817d10f9b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@ -382,13 +390,17 @@ golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220817070843-5a390386f1f2/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U=
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
@ -397,8 +409,12 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk=
golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/tools v0.0.0-20180525024113-a5b4c53f6e8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

View File

@ -2,7 +2,6 @@ package main
import (
"context"
"gitea.timerzz.com/kedaya_haitao/pusher/biz/service"
config "gitea.timerzz.com/kedaya_haitao/pusher/kitex_gen/config"
push "gitea.timerzz.com/kedaya_haitao/pusher/kitex_gen/push"
@ -12,17 +11,17 @@ import (
type PushServiceImpl struct{}
// Push implements the PushServiceImpl interface.
func (s *PushServiceImpl) Push(ctx context.Context, req *push.PushReq) (resp *push.PushResp, err error) {
func (s *PushServiceImpl) Push(ctx context.Context, req *push.PushReq) (resp *push.Resp, err error) {
resp, err = service.NewPushService(ctx).Run(req)
return resp, err
}
// Add implements the PushServiceImpl interface.
func (s *PushServiceImpl) Add(ctx context.Context, req *config.PushConfig) (err error) {
err = service.NewAddService(ctx).Run(req)
func (s *PushServiceImpl) Add(ctx context.Context, req *config.PusherConfig) (resp *push.Resp, err error) {
resp, err = service.NewAddService(ctx).Run(req)
return err
return resp, err
}
// List implements the PushServiceImpl interface.
@ -31,3 +30,10 @@ func (s *PushServiceImpl) List(ctx context.Context, req *push.ListPusherRequest)
return resp, err
}
// GetPusherOptions implements the PushServiceImpl interface.
func (s *PushServiceImpl) GetPusherOptions(ctx context.Context) (resp *push.GetPusherOptionsResponse, err error) {
resp, err = service.NewGetPusherOptionsService(ctx).Run()
return resp, err
}

View File

@ -1,8 +1,33 @@
struct PushConfig {
enum PusherConfigType {
AnPush = 1,
Email
}
struct PusherConfig {
1: i64 id
2: i64 createdAt
3: i8 type
4: string name
3: PusherConfigType type
4: required string name
5: string remark
6: string option
6: required string option
}
struct FormItem {
1: string param
2: string type
3: bool require
}
struct AnPush {
1: string token
2: string channel
}
struct EmailPush {
1: string from
2: string to
3: string username
4: string password
5: string host
6: i32 port
}

View File

@ -1,31 +1,36 @@
include "config.thrift"
struct PushReq {
1: required i64 id
2: required string title
3: required string content
1: required list<i64> ids (api.body="ids");
2: required string title (api.body="title");
3: required string content (api.body="content");
}
struct PushResp {
1: required i64 errCode
2: string errMsg
3: string msgId
struct Resp {
1: required i64 code (api.body="code");
2: string msg (api.body="msg");
3: string msgId (api.body="msgId");
}
struct ListPusherRequest {
1: string keyword
2: optional i64 page = 10
3: optional i16 size = 1
4: bool all
1: string keyword (api.query="keyword");
2: optional i64 page = 10 (api.query="page");
3: optional i16 size = 1 (api.query="size");
4: bool all (api.query="all");
}
struct ListPusherResponse {
1: i64 total
2: list<config.PushConfig> list
1: i64 total (api.body="total");
2: list<config.PusherConfig> list (api.body="list");
}
struct GetPusherOptionsResponse {
1: map<config.PusherConfigType,list<config.FormItem>> options
}
service PushService{
PushResp Push(1: PushReq req)
void Add(1: config.PushConfig req)
ListPusherResponse List(1: ListPusherRequest req)
Resp Push(1: PushReq req) (api.post="/api/v1/push");
Resp Add(1: config.PusherConfig req) (api.post="/api/v1/pushers");
ListPusherResponse List(1: ListPusherRequest req) (api.get="/api/v1/pushers");
GetPusherOptionsResponse GetPusherOptions() (api.get="/api/v1/pushers/options")
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -32,7 +32,7 @@ func (p *PushReq) FastRead(buf []byte) (int, error) {
var l int
var fieldTypeId thrift.TType
var fieldId int16
var issetId bool = false
var issetIds bool = false
var issetTitle bool = false
var issetContent bool = false
_, l, err = bthrift.Binary.ReadStructBegin(buf)
@ -52,13 +52,13 @@ func (p *PushReq) FastRead(buf []byte) (int, error) {
}
switch fieldId {
case 1:
if fieldTypeId == thrift.I64 {
if fieldTypeId == thrift.LIST {
l, err = p.FastReadField1(buf[offset:])
offset += l
if err != nil {
goto ReadFieldError
}
issetId = true
issetIds = true
} else {
l, err = bthrift.Binary.Skip(buf[offset:], fieldTypeId)
offset += l
@ -116,7 +116,7 @@ func (p *PushReq) FastRead(buf []byte) (int, error) {
goto ReadStructEndError
}
if !issetId {
if !issetIds {
fieldId = 1
goto RequiredFieldNotSetError
}
@ -150,13 +150,29 @@ RequiredFieldNotSetError:
func (p *PushReq) FastReadField1(buf []byte) (int, error) {
offset := 0
if v, l, err := bthrift.Binary.ReadI64(buf[offset:]); err != nil {
_, size, l, err := bthrift.Binary.ReadListBegin(buf[offset:])
offset += l
if err != nil {
return offset, err
}
p.Ids = make([]int64, 0, size)
for i := 0; i < size; i++ {
var _elem int64
if v, l, err := bthrift.Binary.ReadI64(buf[offset:]); err != nil {
return offset, err
} else {
offset += l
_elem = v
}
p.Ids = append(p.Ids, _elem)
}
if l, err := bthrift.Binary.ReadListEnd(buf[offset:]); err != nil {
return offset, err
} else {
offset += l
p.Id = v
}
return offset, nil
}
@ -222,9 +238,17 @@ func (p *PushReq) BLength() int {
func (p *PushReq) fastWriteField1(buf []byte, binaryWriter bthrift.BinaryWriter) int {
offset := 0
offset += bthrift.Binary.WriteFieldBegin(buf[offset:], "id", thrift.I64, 1)
offset += bthrift.Binary.WriteI64(buf[offset:], p.Id)
offset += bthrift.Binary.WriteFieldBegin(buf[offset:], "ids", thrift.LIST, 1)
listBeginOffset := offset
offset += bthrift.Binary.ListBeginLength(thrift.I64, 0)
var length int
for _, v := range p.Ids {
length++
offset += bthrift.Binary.WriteI64(buf[offset:], v)
}
bthrift.Binary.WriteListBegin(buf[listBeginOffset:], thrift.I64, length)
offset += bthrift.Binary.WriteListEnd(buf[offset:])
offset += bthrift.Binary.WriteFieldEnd(buf[offset:])
return offset
}
@ -249,9 +273,11 @@ func (p *PushReq) fastWriteField3(buf []byte, binaryWriter bthrift.BinaryWriter)
func (p *PushReq) field1Length() int {
l := 0
l += bthrift.Binary.FieldBeginLength("id", thrift.I64, 1)
l += bthrift.Binary.I64Length(p.Id)
l += bthrift.Binary.FieldBeginLength("ids", thrift.LIST, 1)
l += bthrift.Binary.ListBeginLength(thrift.I64, len(p.Ids))
var tmpV int64
l += bthrift.Binary.I64Length(int64(tmpV)) * len(p.Ids)
l += bthrift.Binary.ListEndLength()
l += bthrift.Binary.FieldEndLength()
return l
}
@ -274,13 +300,13 @@ func (p *PushReq) field3Length() int {
return l
}
func (p *PushResp) FastRead(buf []byte) (int, error) {
func (p *Resp) FastRead(buf []byte) (int, error) {
var err error
var offset int
var l int
var fieldTypeId thrift.TType
var fieldId int16
var issetErrCode bool = false
var issetCode bool = false
_, l, err = bthrift.Binary.ReadStructBegin(buf)
offset += l
if err != nil {
@ -304,7 +330,7 @@ func (p *PushResp) FastRead(buf []byte) (int, error) {
if err != nil {
goto ReadFieldError
}
issetErrCode = true
issetCode = true
} else {
l, err = bthrift.Binary.Skip(buf[offset:], fieldTypeId)
offset += l
@ -360,7 +386,7 @@ func (p *PushResp) FastRead(buf []byte) (int, error) {
goto ReadStructEndError
}
if !issetErrCode {
if !issetCode {
fieldId = 1
goto RequiredFieldNotSetError
}
@ -370,7 +396,7 @@ ReadStructBeginError:
ReadFieldBeginError:
return offset, thrift.PrependError(fmt.Sprintf("%T read field %d begin error: ", p, fieldId), err)
ReadFieldError:
return offset, thrift.PrependError(fmt.Sprintf("%T read field %d '%s' error: ", p, fieldId, fieldIDToName_PushResp[fieldId]), err)
return offset, thrift.PrependError(fmt.Sprintf("%T read field %d '%s' error: ", p, fieldId, fieldIDToName_Resp[fieldId]), err)
SkipFieldError:
return offset, thrift.PrependError(fmt.Sprintf("%T field %d skip type %d error: ", p, fieldId, fieldTypeId), err)
ReadFieldEndError:
@ -378,10 +404,10 @@ ReadFieldEndError:
ReadStructEndError:
return offset, thrift.PrependError(fmt.Sprintf("%T read struct end error: ", p), err)
RequiredFieldNotSetError:
return offset, thrift.NewTProtocolExceptionWithType(thrift.INVALID_DATA, fmt.Errorf("required field %s is not set", fieldIDToName_PushResp[fieldId]))
return offset, thrift.NewTProtocolExceptionWithType(thrift.INVALID_DATA, fmt.Errorf("required field %s is not set", fieldIDToName_Resp[fieldId]))
}
func (p *PushResp) FastReadField1(buf []byte) (int, error) {
func (p *Resp) FastReadField1(buf []byte) (int, error) {
offset := 0
if v, l, err := bthrift.Binary.ReadI64(buf[offset:]); err != nil {
@ -389,13 +415,13 @@ func (p *PushResp) FastReadField1(buf []byte) (int, error) {
} else {
offset += l
p.ErrCode = v
p.Code = v
}
return offset, nil
}
func (p *PushResp) FastReadField2(buf []byte) (int, error) {
func (p *Resp) FastReadField2(buf []byte) (int, error) {
offset := 0
if v, l, err := bthrift.Binary.ReadString(buf[offset:]); err != nil {
@ -403,13 +429,13 @@ func (p *PushResp) FastReadField2(buf []byte) (int, error) {
} else {
offset += l
p.ErrMsg = v
p.Msg = v
}
return offset, nil
}
func (p *PushResp) FastReadField3(buf []byte) (int, error) {
func (p *Resp) FastReadField3(buf []byte) (int, error) {
offset := 0
if v, l, err := bthrift.Binary.ReadString(buf[offset:]); err != nil {
@ -424,13 +450,13 @@ func (p *PushResp) FastReadField3(buf []byte) (int, error) {
}
// for compatibility
func (p *PushResp) FastWrite(buf []byte) int {
func (p *Resp) FastWrite(buf []byte) int {
return 0
}
func (p *PushResp) FastWriteNocopy(buf []byte, binaryWriter bthrift.BinaryWriter) int {
func (p *Resp) FastWriteNocopy(buf []byte, binaryWriter bthrift.BinaryWriter) int {
offset := 0
offset += bthrift.Binary.WriteStructBegin(buf[offset:], "PushResp")
offset += bthrift.Binary.WriteStructBegin(buf[offset:], "Resp")
if p != nil {
offset += p.fastWriteField1(buf[offset:], binaryWriter)
offset += p.fastWriteField2(buf[offset:], binaryWriter)
@ -441,9 +467,9 @@ func (p *PushResp) FastWriteNocopy(buf []byte, binaryWriter bthrift.BinaryWriter
return offset
}
func (p *PushResp) BLength() int {
func (p *Resp) BLength() int {
l := 0
l += bthrift.Binary.StructBeginLength("PushResp")
l += bthrift.Binary.StructBeginLength("Resp")
if p != nil {
l += p.field1Length()
l += p.field2Length()
@ -454,25 +480,25 @@ func (p *PushResp) BLength() int {
return l
}
func (p *PushResp) fastWriteField1(buf []byte, binaryWriter bthrift.BinaryWriter) int {
func (p *Resp) fastWriteField1(buf []byte, binaryWriter bthrift.BinaryWriter) int {
offset := 0
offset += bthrift.Binary.WriteFieldBegin(buf[offset:], "errCode", thrift.I64, 1)
offset += bthrift.Binary.WriteI64(buf[offset:], p.ErrCode)
offset += bthrift.Binary.WriteFieldBegin(buf[offset:], "code", thrift.I64, 1)
offset += bthrift.Binary.WriteI64(buf[offset:], p.Code)
offset += bthrift.Binary.WriteFieldEnd(buf[offset:])
return offset
}
func (p *PushResp) fastWriteField2(buf []byte, binaryWriter bthrift.BinaryWriter) int {
func (p *Resp) fastWriteField2(buf []byte, binaryWriter bthrift.BinaryWriter) int {
offset := 0
offset += bthrift.Binary.WriteFieldBegin(buf[offset:], "errMsg", thrift.STRING, 2)
offset += bthrift.Binary.WriteStringNocopy(buf[offset:], binaryWriter, p.ErrMsg)
offset += bthrift.Binary.WriteFieldBegin(buf[offset:], "msg", thrift.STRING, 2)
offset += bthrift.Binary.WriteStringNocopy(buf[offset:], binaryWriter, p.Msg)
offset += bthrift.Binary.WriteFieldEnd(buf[offset:])
return offset
}
func (p *PushResp) fastWriteField3(buf []byte, binaryWriter bthrift.BinaryWriter) int {
func (p *Resp) fastWriteField3(buf []byte, binaryWriter bthrift.BinaryWriter) int {
offset := 0
offset += bthrift.Binary.WriteFieldBegin(buf[offset:], "msgId", thrift.STRING, 3)
offset += bthrift.Binary.WriteStringNocopy(buf[offset:], binaryWriter, p.MsgId)
@ -481,25 +507,25 @@ func (p *PushResp) fastWriteField3(buf []byte, binaryWriter bthrift.BinaryWriter
return offset
}
func (p *PushResp) field1Length() int {
func (p *Resp) field1Length() int {
l := 0
l += bthrift.Binary.FieldBeginLength("errCode", thrift.I64, 1)
l += bthrift.Binary.I64Length(p.ErrCode)
l += bthrift.Binary.FieldBeginLength("code", thrift.I64, 1)
l += bthrift.Binary.I64Length(p.Code)
l += bthrift.Binary.FieldEndLength()
return l
}
func (p *PushResp) field2Length() int {
func (p *Resp) field2Length() int {
l := 0
l += bthrift.Binary.FieldBeginLength("errMsg", thrift.STRING, 2)
l += bthrift.Binary.StringLengthNocopy(p.ErrMsg)
l += bthrift.Binary.FieldBeginLength("msg", thrift.STRING, 2)
l += bthrift.Binary.StringLengthNocopy(p.Msg)
l += bthrift.Binary.FieldEndLength()
return l
}
func (p *PushResp) field3Length() int {
func (p *Resp) field3Length() int {
l := 0
l += bthrift.Binary.FieldBeginLength("msgId", thrift.STRING, 3)
l += bthrift.Binary.StringLengthNocopy(p.MsgId)
@ -897,9 +923,9 @@ func (p *ListPusherResponse) FastReadField2(buf []byte) (int, error) {
if err != nil {
return offset, err
}
p.List = make([]*config.PushConfig, 0, size)
p.List = make([]*config.PusherConfig, 0, size)
for i := 0; i < size; i++ {
_elem := config.NewPushConfig()
_elem := config.NewPusherConfig()
if l, err := _elem.FastRead(buf[offset:]); err != nil {
return offset, err
} else {
@ -991,6 +1017,202 @@ func (p *ListPusherResponse) field2Length() int {
return l
}
func (p *GetPusherOptionsResponse) FastRead(buf []byte) (int, error) {
var err error
var offset int
var l int
var fieldTypeId thrift.TType
var fieldId int16
_, l, err = bthrift.Binary.ReadStructBegin(buf)
offset += l
if err != nil {
goto ReadStructBeginError
}
for {
_, fieldTypeId, fieldId, l, err = bthrift.Binary.ReadFieldBegin(buf[offset:])
offset += l
if err != nil {
goto ReadFieldBeginError
}
if fieldTypeId == thrift.STOP {
break
}
switch fieldId {
case 1:
if fieldTypeId == thrift.MAP {
l, err = p.FastReadField1(buf[offset:])
offset += l
if err != nil {
goto ReadFieldError
}
} else {
l, err = bthrift.Binary.Skip(buf[offset:], fieldTypeId)
offset += l
if err != nil {
goto SkipFieldError
}
}
default:
l, err = bthrift.Binary.Skip(buf[offset:], fieldTypeId)
offset += l
if err != nil {
goto SkipFieldError
}
}
l, err = bthrift.Binary.ReadFieldEnd(buf[offset:])
offset += l
if err != nil {
goto ReadFieldEndError
}
}
l, err = bthrift.Binary.ReadStructEnd(buf[offset:])
offset += l
if err != nil {
goto ReadStructEndError
}
return offset, nil
ReadStructBeginError:
return offset, thrift.PrependError(fmt.Sprintf("%T read struct begin error: ", p), err)
ReadFieldBeginError:
return offset, thrift.PrependError(fmt.Sprintf("%T read field %d begin error: ", p, fieldId), err)
ReadFieldError:
return offset, thrift.PrependError(fmt.Sprintf("%T read field %d '%s' error: ", p, fieldId, fieldIDToName_GetPusherOptionsResponse[fieldId]), err)
SkipFieldError:
return offset, thrift.PrependError(fmt.Sprintf("%T field %d skip type %d error: ", p, fieldId, fieldTypeId), err)
ReadFieldEndError:
return offset, thrift.PrependError(fmt.Sprintf("%T read field end error", p), err)
ReadStructEndError:
return offset, thrift.PrependError(fmt.Sprintf("%T read struct end error: ", p), err)
}
func (p *GetPusherOptionsResponse) FastReadField1(buf []byte) (int, error) {
offset := 0
_, _, size, l, err := bthrift.Binary.ReadMapBegin(buf[offset:])
offset += l
if err != nil {
return offset, err
}
p.Options = make(map[config.PusherConfigType][]*config.FormItem, size)
for i := 0; i < size; i++ {
var _key config.PusherConfigType
if v, l, err := bthrift.Binary.ReadI32(buf[offset:]); err != nil {
return offset, err
} else {
offset += l
_key = config.PusherConfigType(v)
}
_, size, l, err := bthrift.Binary.ReadListBegin(buf[offset:])
offset += l
if err != nil {
return offset, err
}
_val := make([]*config.FormItem, 0, size)
for i := 0; i < size; i++ {
_elem := config.NewFormItem()
if l, err := _elem.FastRead(buf[offset:]); err != nil {
return offset, err
} else {
offset += l
}
_val = append(_val, _elem)
}
if l, err := bthrift.Binary.ReadListEnd(buf[offset:]); err != nil {
return offset, err
} else {
offset += l
}
p.Options[_key] = _val
}
if l, err := bthrift.Binary.ReadMapEnd(buf[offset:]); err != nil {
return offset, err
} else {
offset += l
}
return offset, nil
}
// for compatibility
func (p *GetPusherOptionsResponse) FastWrite(buf []byte) int {
return 0
}
func (p *GetPusherOptionsResponse) FastWriteNocopy(buf []byte, binaryWriter bthrift.BinaryWriter) int {
offset := 0
offset += bthrift.Binary.WriteStructBegin(buf[offset:], "GetPusherOptionsResponse")
if p != nil {
offset += p.fastWriteField1(buf[offset:], binaryWriter)
}
offset += bthrift.Binary.WriteFieldStop(buf[offset:])
offset += bthrift.Binary.WriteStructEnd(buf[offset:])
return offset
}
func (p *GetPusherOptionsResponse) BLength() int {
l := 0
l += bthrift.Binary.StructBeginLength("GetPusherOptionsResponse")
if p != nil {
l += p.field1Length()
}
l += bthrift.Binary.FieldStopLength()
l += bthrift.Binary.StructEndLength()
return l
}
func (p *GetPusherOptionsResponse) fastWriteField1(buf []byte, binaryWriter bthrift.BinaryWriter) int {
offset := 0
offset += bthrift.Binary.WriteFieldBegin(buf[offset:], "options", thrift.MAP, 1)
mapBeginOffset := offset
offset += bthrift.Binary.MapBeginLength(thrift.I32, thrift.LIST, 0)
var length int
for k, v := range p.Options {
length++
offset += bthrift.Binary.WriteI32(buf[offset:], int32(k))
listBeginOffset := offset
offset += bthrift.Binary.ListBeginLength(thrift.STRUCT, 0)
var length int
for _, v := range v {
length++
offset += v.FastWriteNocopy(buf[offset:], binaryWriter)
}
bthrift.Binary.WriteListBegin(buf[listBeginOffset:], thrift.STRUCT, length)
offset += bthrift.Binary.WriteListEnd(buf[offset:])
}
bthrift.Binary.WriteMapBegin(buf[mapBeginOffset:], thrift.I32, thrift.LIST, length)
offset += bthrift.Binary.WriteMapEnd(buf[offset:])
offset += bthrift.Binary.WriteFieldEnd(buf[offset:])
return offset
}
func (p *GetPusherOptionsResponse) field1Length() int {
l := 0
l += bthrift.Binary.FieldBeginLength("options", thrift.MAP, 1)
l += bthrift.Binary.MapBeginLength(thrift.I32, thrift.LIST, len(p.Options))
for k, v := range p.Options {
l += bthrift.Binary.I32Length(int32(k))
l += bthrift.Binary.ListBeginLength(thrift.STRUCT, len(v))
for _, v := range v {
l += v.BLength()
}
l += bthrift.Binary.ListEndLength()
}
l += bthrift.Binary.MapEndLength()
l += bthrift.Binary.FieldEndLength()
return l
}
func (p *PushServicePushArgs) FastRead(buf []byte) (int, error) {
var err error
var offset int
@ -1192,7 +1414,7 @@ ReadStructEndError:
func (p *PushServicePushResult) FastReadField0(buf []byte) (int, error) {
offset := 0
tmp := NewPushResp()
tmp := NewResp()
if l, err := tmp.FastRead(buf[offset:]); err != nil {
return offset, err
} else {
@ -1323,7 +1545,7 @@ ReadStructEndError:
func (p *PushServiceAddArgs) FastReadField1(buf []byte) (int, error) {
offset := 0
tmp := config.NewPushConfig()
tmp := config.NewPusherConfig()
if l, err := tmp.FastRead(buf[offset:]); err != nil {
return offset, err
} else {
@ -1397,10 +1619,27 @@ func (p *PushServiceAddResult) FastRead(buf []byte) (int, error) {
if fieldTypeId == thrift.STOP {
break
}
l, err = bthrift.Binary.Skip(buf[offset:], fieldTypeId)
offset += l
if err != nil {
goto SkipFieldError
switch fieldId {
case 0:
if fieldTypeId == thrift.STRUCT {
l, err = p.FastReadField0(buf[offset:])
offset += l
if err != nil {
goto ReadFieldError
}
} else {
l, err = bthrift.Binary.Skip(buf[offset:], fieldTypeId)
offset += l
if err != nil {
goto SkipFieldError
}
}
default:
l, err = bthrift.Binary.Skip(buf[offset:], fieldTypeId)
offset += l
if err != nil {
goto SkipFieldError
}
}
l, err = bthrift.Binary.ReadFieldEnd(buf[offset:])
@ -1420,6 +1659,8 @@ ReadStructBeginError:
return offset, thrift.PrependError(fmt.Sprintf("%T read struct begin error: ", p), err)
ReadFieldBeginError:
return offset, thrift.PrependError(fmt.Sprintf("%T read field %d begin error: ", p, fieldId), err)
ReadFieldError:
return offset, thrift.PrependError(fmt.Sprintf("%T read field %d '%s' error: ", p, fieldId, fieldIDToName_PushServiceAddResult[fieldId]), err)
SkipFieldError:
return offset, thrift.PrependError(fmt.Sprintf("%T field %d skip type %d error: ", p, fieldId, fieldTypeId), err)
ReadFieldEndError:
@ -1428,6 +1669,19 @@ ReadStructEndError:
return offset, thrift.PrependError(fmt.Sprintf("%T read struct end error: ", p), err)
}
func (p *PushServiceAddResult) FastReadField0(buf []byte) (int, error) {
offset := 0
tmp := NewResp()
if l, err := tmp.FastRead(buf[offset:]); err != nil {
return offset, err
} else {
offset += l
}
p.Success = tmp
return offset, nil
}
// for compatibility
func (p *PushServiceAddResult) FastWrite(buf []byte) int {
return 0
@ -1437,6 +1691,7 @@ func (p *PushServiceAddResult) FastWriteNocopy(buf []byte, binaryWriter bthrift.
offset := 0
offset += bthrift.Binary.WriteStructBegin(buf[offset:], "Add_result")
if p != nil {
offset += p.fastWriteField0(buf[offset:], binaryWriter)
}
offset += bthrift.Binary.WriteFieldStop(buf[offset:])
offset += bthrift.Binary.WriteStructEnd(buf[offset:])
@ -1447,12 +1702,33 @@ func (p *PushServiceAddResult) BLength() int {
l := 0
l += bthrift.Binary.StructBeginLength("Add_result")
if p != nil {
l += p.field0Length()
}
l += bthrift.Binary.FieldStopLength()
l += bthrift.Binary.StructEndLength()
return l
}
func (p *PushServiceAddResult) fastWriteField0(buf []byte, binaryWriter bthrift.BinaryWriter) int {
offset := 0
if p.IsSetSuccess() {
offset += bthrift.Binary.WriteFieldBegin(buf[offset:], "success", thrift.STRUCT, 0)
offset += p.Success.FastWriteNocopy(buf[offset:], binaryWriter)
offset += bthrift.Binary.WriteFieldEnd(buf[offset:])
}
return offset
}
func (p *PushServiceAddResult) field0Length() int {
l := 0
if p.IsSetSuccess() {
l += bthrift.Binary.FieldBeginLength("success", thrift.STRUCT, 0)
l += p.Success.BLength()
l += bthrift.Binary.FieldEndLength()
}
return l
}
func (p *PushServiceListArgs) FastRead(buf []byte) (int, error) {
var err error
var offset int
@ -1711,6 +1987,214 @@ func (p *PushServiceListResult) field0Length() int {
return l
}
func (p *PushServiceGetPusherOptionsArgs) FastRead(buf []byte) (int, error) {
var err error
var offset int
var l int
var fieldTypeId thrift.TType
var fieldId int16
_, l, err = bthrift.Binary.ReadStructBegin(buf)
offset += l
if err != nil {
goto ReadStructBeginError
}
for {
_, fieldTypeId, fieldId, l, err = bthrift.Binary.ReadFieldBegin(buf[offset:])
offset += l
if err != nil {
goto ReadFieldBeginError
}
if fieldTypeId == thrift.STOP {
break
}
l, err = bthrift.Binary.Skip(buf[offset:], fieldTypeId)
offset += l
if err != nil {
goto SkipFieldError
}
l, err = bthrift.Binary.ReadFieldEnd(buf[offset:])
offset += l
if err != nil {
goto ReadFieldEndError
}
}
l, err = bthrift.Binary.ReadStructEnd(buf[offset:])
offset += l
if err != nil {
goto ReadStructEndError
}
return offset, nil
ReadStructBeginError:
return offset, thrift.PrependError(fmt.Sprintf("%T read struct begin error: ", p), err)
ReadFieldBeginError:
return offset, thrift.PrependError(fmt.Sprintf("%T read field %d begin error: ", p, fieldId), err)
SkipFieldError:
return offset, thrift.PrependError(fmt.Sprintf("%T field %d skip type %d error: ", p, fieldId, fieldTypeId), err)
ReadFieldEndError:
return offset, thrift.PrependError(fmt.Sprintf("%T read field end error", p), err)
ReadStructEndError:
return offset, thrift.PrependError(fmt.Sprintf("%T read struct end error: ", p), err)
}
// for compatibility
func (p *PushServiceGetPusherOptionsArgs) FastWrite(buf []byte) int {
return 0
}
func (p *PushServiceGetPusherOptionsArgs) FastWriteNocopy(buf []byte, binaryWriter bthrift.BinaryWriter) int {
offset := 0
offset += bthrift.Binary.WriteStructBegin(buf[offset:], "GetPusherOptions_args")
if p != nil {
}
offset += bthrift.Binary.WriteFieldStop(buf[offset:])
offset += bthrift.Binary.WriteStructEnd(buf[offset:])
return offset
}
func (p *PushServiceGetPusherOptionsArgs) BLength() int {
l := 0
l += bthrift.Binary.StructBeginLength("GetPusherOptions_args")
if p != nil {
}
l += bthrift.Binary.FieldStopLength()
l += bthrift.Binary.StructEndLength()
return l
}
func (p *PushServiceGetPusherOptionsResult) FastRead(buf []byte) (int, error) {
var err error
var offset int
var l int
var fieldTypeId thrift.TType
var fieldId int16
_, l, err = bthrift.Binary.ReadStructBegin(buf)
offset += l
if err != nil {
goto ReadStructBeginError
}
for {
_, fieldTypeId, fieldId, l, err = bthrift.Binary.ReadFieldBegin(buf[offset:])
offset += l
if err != nil {
goto ReadFieldBeginError
}
if fieldTypeId == thrift.STOP {
break
}
switch fieldId {
case 0:
if fieldTypeId == thrift.STRUCT {
l, err = p.FastReadField0(buf[offset:])
offset += l
if err != nil {
goto ReadFieldError
}
} else {
l, err = bthrift.Binary.Skip(buf[offset:], fieldTypeId)
offset += l
if err != nil {
goto SkipFieldError
}
}
default:
l, err = bthrift.Binary.Skip(buf[offset:], fieldTypeId)
offset += l
if err != nil {
goto SkipFieldError
}
}
l, err = bthrift.Binary.ReadFieldEnd(buf[offset:])
offset += l
if err != nil {
goto ReadFieldEndError
}
}
l, err = bthrift.Binary.ReadStructEnd(buf[offset:])
offset += l
if err != nil {
goto ReadStructEndError
}
return offset, nil
ReadStructBeginError:
return offset, thrift.PrependError(fmt.Sprintf("%T read struct begin error: ", p), err)
ReadFieldBeginError:
return offset, thrift.PrependError(fmt.Sprintf("%T read field %d begin error: ", p, fieldId), err)
ReadFieldError:
return offset, thrift.PrependError(fmt.Sprintf("%T read field %d '%s' error: ", p, fieldId, fieldIDToName_PushServiceGetPusherOptionsResult[fieldId]), err)
SkipFieldError:
return offset, thrift.PrependError(fmt.Sprintf("%T field %d skip type %d error: ", p, fieldId, fieldTypeId), err)
ReadFieldEndError:
return offset, thrift.PrependError(fmt.Sprintf("%T read field end error", p), err)
ReadStructEndError:
return offset, thrift.PrependError(fmt.Sprintf("%T read struct end error: ", p), err)
}
func (p *PushServiceGetPusherOptionsResult) FastReadField0(buf []byte) (int, error) {
offset := 0
tmp := NewGetPusherOptionsResponse()
if l, err := tmp.FastRead(buf[offset:]); err != nil {
return offset, err
} else {
offset += l
}
p.Success = tmp
return offset, nil
}
// for compatibility
func (p *PushServiceGetPusherOptionsResult) FastWrite(buf []byte) int {
return 0
}
func (p *PushServiceGetPusherOptionsResult) FastWriteNocopy(buf []byte, binaryWriter bthrift.BinaryWriter) int {
offset := 0
offset += bthrift.Binary.WriteStructBegin(buf[offset:], "GetPusherOptions_result")
if p != nil {
offset += p.fastWriteField0(buf[offset:], binaryWriter)
}
offset += bthrift.Binary.WriteFieldStop(buf[offset:])
offset += bthrift.Binary.WriteStructEnd(buf[offset:])
return offset
}
func (p *PushServiceGetPusherOptionsResult) BLength() int {
l := 0
l += bthrift.Binary.StructBeginLength("GetPusherOptions_result")
if p != nil {
l += p.field0Length()
}
l += bthrift.Binary.FieldStopLength()
l += bthrift.Binary.StructEndLength()
return l
}
func (p *PushServiceGetPusherOptionsResult) fastWriteField0(buf []byte, binaryWriter bthrift.BinaryWriter) int {
offset := 0
if p.IsSetSuccess() {
offset += bthrift.Binary.WriteFieldBegin(buf[offset:], "success", thrift.STRUCT, 0)
offset += p.Success.FastWriteNocopy(buf[offset:], binaryWriter)
offset += bthrift.Binary.WriteFieldEnd(buf[offset:])
}
return offset
}
func (p *PushServiceGetPusherOptionsResult) field0Length() int {
l := 0
if p.IsSetSuccess() {
l += bthrift.Binary.FieldBeginLength("success", thrift.STRUCT, 0)
l += p.Success.BLength()
l += bthrift.Binary.FieldEndLength()
}
return l
}
func (p *PushServicePushArgs) GetFirstArgument() interface{} {
return p.Req
}
@ -1724,7 +2208,7 @@ func (p *PushServiceAddArgs) GetFirstArgument() interface{} {
}
func (p *PushServiceAddResult) GetResult() interface{} {
return nil
return p.Success
}
func (p *PushServiceListArgs) GetFirstArgument() interface{} {
@ -1734,3 +2218,11 @@ func (p *PushServiceListArgs) GetFirstArgument() interface{} {
func (p *PushServiceListResult) GetResult() interface{} {
return p.Success
}
func (p *PushServiceGetPusherOptionsArgs) GetFirstArgument() interface{} {
return nil
}
func (p *PushServiceGetPusherOptionsResult) GetResult() interface{} {
return p.Success
}

File diff suppressed because it is too large Load Diff

View File

@ -12,9 +12,10 @@ import (
// Client is designed to provide IDL-compatible methods with call-option parameter for kitex framework.
type Client interface {
Push(ctx context.Context, req *push.PushReq, callOptions ...callopt.Option) (r *push.PushResp, err error)
Add(ctx context.Context, req *config.PushConfig, callOptions ...callopt.Option) (err error)
Push(ctx context.Context, req *push.PushReq, callOptions ...callopt.Option) (r *push.Resp, err error)
Add(ctx context.Context, req *config.PusherConfig, callOptions ...callopt.Option) (r *push.Resp, err error)
List(ctx context.Context, req *push.ListPusherRequest, callOptions ...callopt.Option) (r *push.ListPusherResponse, err error)
GetPusherOptions(ctx context.Context, callOptions ...callopt.Option) (r *push.GetPusherOptionsResponse, err error)
}
// NewClient creates a client for the service defined in IDL.
@ -46,12 +47,12 @@ type kPushServiceClient struct {
*kClient
}
func (p *kPushServiceClient) Push(ctx context.Context, req *push.PushReq, callOptions ...callopt.Option) (r *push.PushResp, err error) {
func (p *kPushServiceClient) Push(ctx context.Context, req *push.PushReq, callOptions ...callopt.Option) (r *push.Resp, err error) {
ctx = client.NewCtxWithCallOptions(ctx, callOptions)
return p.kClient.Push(ctx, req)
}
func (p *kPushServiceClient) Add(ctx context.Context, req *config.PushConfig, callOptions ...callopt.Option) (err error) {
func (p *kPushServiceClient) Add(ctx context.Context, req *config.PusherConfig, callOptions ...callopt.Option) (r *push.Resp, err error) {
ctx = client.NewCtxWithCallOptions(ctx, callOptions)
return p.kClient.Add(ctx, req)
}
@ -60,3 +61,8 @@ func (p *kPushServiceClient) List(ctx context.Context, req *push.ListPusherReque
ctx = client.NewCtxWithCallOptions(ctx, callOptions)
return p.kClient.List(ctx, req)
}
func (p *kPushServiceClient) GetPusherOptions(ctx context.Context, callOptions ...callopt.Option) (r *push.GetPusherOptionsResponse, err error) {
ctx = client.NewCtxWithCallOptions(ctx, callOptions)
return p.kClient.GetPusherOptions(ctx)
}

View File

@ -35,6 +35,13 @@ var serviceMethods = map[string]kitex.MethodInfo{
false,
kitex.WithStreamingMode(kitex.StreamingNone),
),
"GetPusherOptions": kitex.NewMethodInfo(
getPusherOptionsHandler,
newPushServiceGetPusherOptionsArgs,
newPushServiceGetPusherOptionsResult,
false,
kitex.WithStreamingMode(kitex.StreamingNone),
),
}
var (
@ -121,12 +128,12 @@ func newPushServicePushResult() interface{} {
func addHandler(ctx context.Context, handler interface{}, arg, result interface{}) error {
realArg := arg.(*push.PushServiceAddArgs)
err := handler.(push.PushService).Add(ctx, realArg.Req)
realResult := result.(*push.PushServiceAddResult)
success, err := handler.(push.PushService).Add(ctx, realArg.Req)
if err != nil {
return err
}
realResult.Success = success
return nil
}
func newPushServiceAddArgs() interface{} {
@ -155,6 +162,24 @@ func newPushServiceListResult() interface{} {
return push.NewPushServiceListResult()
}
func getPusherOptionsHandler(ctx context.Context, handler interface{}, arg, result interface{}) error {
_ = arg.(*push.PushServiceGetPusherOptionsArgs)
realResult := result.(*push.PushServiceGetPusherOptionsResult)
success, err := handler.(push.PushService).GetPusherOptions(ctx)
if err != nil {
return err
}
realResult.Success = success
return nil
}
func newPushServiceGetPusherOptionsArgs() interface{} {
return push.NewPushServiceGetPusherOptionsArgs()
}
func newPushServiceGetPusherOptionsResult() interface{} {
return push.NewPushServiceGetPusherOptionsResult()
}
type kClient struct {
c client.Client
}
@ -165,7 +190,7 @@ func newServiceClient(c client.Client) *kClient {
}
}
func (p *kClient) Push(ctx context.Context, req *push.PushReq) (r *push.PushResp, err error) {
func (p *kClient) Push(ctx context.Context, req *push.PushReq) (r *push.Resp, err error) {
var _args push.PushServicePushArgs
_args.Req = req
var _result push.PushServicePushResult
@ -175,14 +200,14 @@ func (p *kClient) Push(ctx context.Context, req *push.PushReq) (r *push.PushResp
return _result.GetSuccess(), nil
}
func (p *kClient) Add(ctx context.Context, req *config.PushConfig) (err error) {
func (p *kClient) Add(ctx context.Context, req *config.PusherConfig) (r *push.Resp, err error) {
var _args push.PushServiceAddArgs
_args.Req = req
var _result push.PushServiceAddResult
if err = p.c.Call(ctx, "Add", &_args, &_result); err != nil {
return
}
return nil
return _result.GetSuccess(), nil
}
func (p *kClient) List(ctx context.Context, req *push.ListPusherRequest) (r *push.ListPusherResponse, err error) {
@ -194,3 +219,12 @@ func (p *kClient) List(ctx context.Context, req *push.ListPusherRequest) (r *pus
}
return _result.GetSuccess(), nil
}
func (p *kClient) GetPusherOptions(ctx context.Context) (r *push.GetPusherOptionsResponse, err error) {
var _args push.PushServiceGetPusherOptionsArgs
var _result push.PushServiceGetPusherOptionsResult
if err = p.c.Call(ctx, "GetPusherOptions", &_args, &_result); err != nil {
return
}
return _result.GetSuccess(), nil
}

28
main.go
View File

@ -1,6 +1,8 @@
package main
import (
"gitea.timerzz.com/kedaya_haitao/pusher/biz/dal"
"gitea.timerzz.com/kedaya_haitao/pusher/pushers"
"net"
"time"
@ -16,6 +18,8 @@ import (
)
func main() {
dal.Init()
pushers.Init()
opts := kitexInit()
svr := pushservice.NewServer(new(PushServiceImpl), opts...)
@ -30,6 +34,30 @@ func kitexInit() (opts []server.Option) {
opts = append(opts, server.
WithTransHandlerFactory(&mixTransHandlerFactory{nil}))
opts = append(opts, server.
WithTransHandlerFactory(&mixTransHandlerFactory{nil}))
opts = append(opts, server.
WithTransHandlerFactory(&mixTransHandlerFactory{nil}))
opts = append(opts, server.
WithTransHandlerFactory(&mixTransHandlerFactory{nil}))
opts = append(opts, server.
WithTransHandlerFactory(&mixTransHandlerFactory{nil}))
opts = append(opts, server.
WithTransHandlerFactory(&mixTransHandlerFactory{nil}))
opts = append(opts, server.
WithTransHandlerFactory(&mixTransHandlerFactory{nil}))
opts = append(opts, server.
WithTransHandlerFactory(&mixTransHandlerFactory{nil}))
opts = append(opts, server.
WithTransHandlerFactory(&mixTransHandlerFactory{nil}))
// address
addr, err := net.ResolveTCPAddr("tcp", conf.GetConf().Kitex.Address)
if err != nil {

33
model/pusher.go Normal file
View File

@ -0,0 +1,33 @@
package model
import (
"gorm.io/gorm"
"time"
)
type PusherType int
const (
PusherType_AnPusher = iota + 1
)
type Pusher struct {
ID uint `gorm:"primaryKey" json:"id"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt gorm.DeletedAt `gorm:"index"`
Type PusherType `json:"type"`
Name string `json:"name"`
Remark string `json:"remark"`
RawOption string `json:"option" gorm:"type:json"`
}
func (p *Pusher) TableName() string {
return "pusher"
}
type AnPushOption struct {
Token string `json:"token"`
Channel string `json:"channel"`
}

7
model/pusher_test.go Normal file
View File

@ -0,0 +1,7 @@
package model
import "testing"
func TestModel(t *testing.T) {
var a AnPushOption
}

35
pushers/anPush.go Normal file
View File

@ -0,0 +1,35 @@
package pushers
import (
"context"
"fmt"
"gitea.timerzz.com/kedaya_haitao/pusher/kitex_gen/config"
"github.com/go-resty/resty/v2"
"strings"
)
type AnPush struct {
opt *config.AnPush
client *resty.Client
}
func NewAnPush(opt *config.AnPush) *AnPush {
return &AnPush{
opt: opt,
client: resty.New().SetHeader("Content-Type", "application/x-www-form-urlencoded"),
}
}
type msgResponse struct {
Errcode int `json:"errcode"`
Errmsg string `json:"errmsg"`
Msgid uint64 `json:"msgid"`
}
func (a *AnPush) Push(ctx context.Context, title, content string) error {
r := a.client.R().SetBody(strings.NewReader(fmt.Sprintf("title=%s&content=%s&channel=%s", title, content, a.opt.Channel))).SetContext(ctx)
var result msgResponse
r.SetResult(&result)
_, err := r.Post(fmt.Sprintf("https://api.anpush.com/push/%s", a.opt.Token))
return err
}

67
pushers/controller.go Normal file
View File

@ -0,0 +1,67 @@
package pushers
import (
"context"
"gitea.timerzz.com/kedaya_haitao/pusher/biz/dal/postgres"
"gitea.timerzz.com/kedaya_haitao/pusher/kitex_gen/config"
"github.com/bytedance/sonic"
"golang.org/x/sync/errgroup"
"sync"
)
func Init() {
c = &Controller{
pushers: make(map[int64]Pusher),
}
}
func Push(ctx context.Context, ids []int64, title, content string) error {
return c.Push(ctx, ids, title, content)
}
type Pusher interface {
Push(ctx context.Context, title, content string) error
}
type Controller struct {
pushers map[int64]Pusher
l sync.Mutex
}
var c *Controller
func (c *Controller) Push(ctx context.Context, ids []int64, title, content string) error {
wg, sub := errgroup.WithContext(ctx)
for _, id := range ids {
var i = id
wg.Go(func() error {
if pusher, ok := c.pushers[i]; ok {
return pusher.Push(sub, title, content)
} else {
var cfg config.PusherConfig
if err := postgres.DB.Where("id = ?", i).Find(&cfg).Error; err != nil {
return err
}
c.l.Lock()
c.pushers[cfg.Id] = NewPusher(cfg)
c.l.Unlock()
return c.pushers[cfg.Id].Push(sub, title, content)
}
})
}
return wg.Wait()
}
func NewPusher(cfg config.PusherConfig) Pusher {
switch cfg.Type {
case config.PusherConfigType_AnPush:
var opt config.AnPush
_ = sonic.UnmarshalString(cfg.Option, &opt)
return NewAnPush(&opt)
case config.PusherConfigType_Email:
var opt config.EmailPush
_ = sonic.UnmarshalString(cfg.Option, &opt)
return NewEmailPusher(&opt)
}
return nil
}

35
pushers/email.go Normal file
View File

@ -0,0 +1,35 @@
package pushers
import (
"context"
"fmt"
"gitea.timerzz.com/kedaya_haitao/pusher/kitex_gen/config"
"github.com/wneessen/go-mail"
)
type Email struct {
opt *config.EmailPush
}
func NewEmailPusher(opt *config.EmailPush) *Email {
return &Email{opt: opt}
}
func (e *Email) Push(ctx context.Context, title, content string) error {
em := mail.NewMsg()
if err := em.From(e.opt.From); err != nil {
return fmt.Errorf("设置from失败:%v", err)
}
if err := em.To(e.opt.To); err != nil {
return fmt.Errorf("设置to失败:%v", err)
}
em.Subject(title)
em.SetBodyString(mail.TypeTextPlain, content)
c, err := mail.NewClient(e.opt.Host, mail.WithPort(int(e.opt.Port)), mail.WithSMTPAuth(mail.SMTPAuthPlain),
mail.WithUsername(e.opt.Username), mail.WithPassword(e.opt.Password))
if err != nil {
return fmt.Errorf("创建email客户端失败%v", err)
}
return c.DialAndSendWithContext(ctx, em)
}

View File

@ -0,0 +1,62 @@
package pusher
import (
"context"
config "gitea.timerzz.com/kedaya_haitao/pusher/kitex_gen/config"
push "gitea.timerzz.com/kedaya_haitao/pusher/kitex_gen/push"
"gitea.timerzz.com/kedaya_haitao/pusher/kitex_gen/push/pushservice"
"github.com/cloudwego/kitex/client"
"github.com/cloudwego/kitex/client/callopt"
)
type RPCClient interface {
KitexClient() pushservice.Client
Service() string
Push(ctx context.Context, req *push.PushReq, callOptions ...callopt.Option) (r *push.Resp, err error)
Add(ctx context.Context, req *config.PusherConfig, callOptions ...callopt.Option) (r *push.Resp, err error)
List(ctx context.Context, req *push.ListPusherRequest, callOptions ...callopt.Option) (r *push.ListPusherResponse, err error)
GetPusherOptions(ctx context.Context, callOptions ...callopt.Option) (r *push.GetPusherOptionsResponse, err error)
}
func NewRPCClient(dstService string, opts ...client.Option) (RPCClient, error) {
kitexClient, err := pushservice.NewClient(dstService, opts...)
if err != nil {
return nil, err
}
cli := &clientImpl{
service: dstService,
kitexClient: kitexClient,
}
return cli, nil
}
type clientImpl struct {
service string
kitexClient pushservice.Client
}
func (c *clientImpl) Service() string {
return c.service
}
func (c *clientImpl) KitexClient() pushservice.Client {
return c.kitexClient
}
func (c *clientImpl) Push(ctx context.Context, req *push.PushReq, callOptions ...callopt.Option) (r *push.Resp, err error) {
return c.kitexClient.Push(ctx, req, callOptions...)
}
func (c *clientImpl) Add(ctx context.Context, req *config.PusherConfig, callOptions ...callopt.Option) (r *push.Resp, err error) {
return c.kitexClient.Add(ctx, req, callOptions...)
}
func (c *clientImpl) List(ctx context.Context, req *push.ListPusherRequest, callOptions ...callopt.Option) (r *push.ListPusherResponse, err error) {
return c.kitexClient.List(ctx, req, callOptions...)
}
func (c *clientImpl) GetPusherOptions(ctx context.Context, callOptions ...callopt.Option) (r *push.GetPusherOptionsResponse, err error) {
return c.kitexClient.GetPusherOptions(ctx, callOptions...)
}

View File

@ -0,0 +1,45 @@
package pusher
import (
"context"
config "gitea.timerzz.com/kedaya_haitao/pusher/kitex_gen/config"
push "gitea.timerzz.com/kedaya_haitao/pusher/kitex_gen/push"
"github.com/cloudwego/kitex/client/callopt"
"github.com/cloudwego/kitex/pkg/klog"
)
func Push(ctx context.Context, req *push.PushReq, callOptions ...callopt.Option) (resp *push.Resp, err error) {
resp, err = defaultClient.Push(ctx, req, callOptions...)
if err != nil {
klog.CtxErrorf(ctx, "Push call failed,err =%+v", err)
return nil, err
}
return resp, nil
}
func Add(ctx context.Context, req *config.PusherConfig, callOptions ...callopt.Option) (resp *push.Resp, err error) {
resp, err = defaultClient.Add(ctx, req, callOptions...)
if err != nil {
klog.CtxErrorf(ctx, "Add call failed,err =%+v", err)
return nil, err
}
return resp, nil
}
func List(ctx context.Context, req *push.ListPusherRequest, callOptions ...callopt.Option) (resp *push.ListPusherResponse, err error) {
resp, err = defaultClient.List(ctx, req, callOptions...)
if err != nil {
klog.CtxErrorf(ctx, "List call failed,err =%+v", err)
return nil, err
}
return resp, nil
}
func GetPusherOptions(ctx context.Context, callOptions ...callopt.Option) (resp *push.GetPusherOptionsResponse, err error) {
resp, err = defaultClient.GetPusherOptions(ctx, callOptions...)
if err != nil {
klog.CtxErrorf(ctx, "GetPusherOptions call failed,err =%+v", err)
return nil, err
}
return resp, nil
}

44
rpc/pusher/pusher_init.go Normal file
View File

@ -0,0 +1,44 @@
package pusher
import (
"sync"
"github.com/cloudwego/kitex/client"
"github.com/cloudwego/kitex/pkg/transmeta"
"github.com/cloudwego/kitex/transport"
)
var (
// todo edit custom config
defaultClient RPCClient
defaultDstService = "pusher"
defaultClientOpts = []client.Option{
client.WithHostPorts("127.0.0.1:8888"),
client.WithMetaHandler(transmeta.ClientTTHeaderHandler),
client.WithTransportProtocol(transport.TTHeader),
}
once sync.Once
)
func init() {
DefaultClient()
}
func DefaultClient() RPCClient {
once.Do(func() {
defaultClient = newClient(defaultDstService, defaultClientOpts...)
})
return defaultClient
}
func newClient(dstService string, opts ...client.Option) RPCClient {
c, err := NewRPCClient(dstService, opts...)
if err != nil {
panic("failed to init client: " + err.Error())
}
return c
}
func InitClient(dstService string, opts ...client.Option) {
defaultClient = newClient(dstService, opts...)
}

View File

@ -1,4 +0,0 @@
#! /usr/bin/env bash
CURDIR=$(cd $(dirname $0); pwd)
echo "$CURDIR/bin/pusher"
exec "$CURDIR/bin/pusher"