Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = ["rpc_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/interface/main/history/model:go_default_library",
"//app/interface/main/history/rpc/client:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = ["rpc.go"],
importpath = "go-common/app/interface/main/history/server/gorpc",
tags = ["automanaged"],
deps = [
"//app/interface/main/history/conf:go_default_library",
"//app/interface/main/history/model:go_default_library",
"//app/interface/main/history/service:go_default_library",
"//library/ecode:go_default_library",
"//library/net/rpc:go_default_library",
"//library/net/rpc/context:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,136 @@
package gorpc
import (
"go-common/app/interface/main/history/conf"
"go-common/app/interface/main/history/model"
"go-common/app/interface/main/history/service"
"go-common/library/ecode"
"go-common/library/net/rpc"
"go-common/library/net/rpc/context"
)
// RPC represent rpc server
type RPC struct {
svc *service.Service
}
// New init rpc.
func New(c *conf.Config, s *service.Service) (svr *rpc.Server) {
r := &RPC{svc: s}
svr = rpc.NewServer(c.RPCServer)
if err := svr.Register(r); err != nil {
panic(err)
}
return
}
// Ping check connection success.
func (r *RPC) Ping(c context.Context, arg *struct{}, res *struct{}) (err error) {
return
}
// Progress report a user hisotry.
func (r *RPC) Progress(c context.Context, arg *model.ArgPro, res *map[int64]*model.History) (err error) {
*res, err = r.svc.Progress(c, arg.Mid, arg.Aids)
return
}
// Position report a user hisotry.
func (r *RPC) Position(c context.Context, arg *model.ArgPos, res *model.History) (err error) {
var v *model.History
tp, err := model.CheckBusiness(arg.Business)
if err != nil {
return
}
if tp > 0 {
arg.TP = tp
}
v, err = r.svc.Position(c, arg.Mid, arg.Aid, arg.TP)
if err == nil {
*res = *v
}
return
}
// Add (c context.Context, mid, src, rtime int64, ip string, h *model.History) (err error) .
func (r *RPC) Add(c context.Context, arg *model.ArgHistory, res *struct{}) (err error) {
if arg.History != nil {
var tp int8
if tp, err = model.CheckBusiness(arg.History.Business); err != nil {
return
} else if tp > 0 {
arg.History.TP = tp
}
}
err = r.svc.AddHistory(c, arg.Mid, arg.Realtime, arg.History)
return
}
// Delete delete histories
func (r *RPC) Delete(c context.Context, arg *model.ArgDelete, res *struct{}) (err error) {
if len(arg.Resources) == 0 {
err = ecode.RequestErr
return
}
histories := make([]*model.History, 0, len(arg.Resources))
for _, history := range arg.Resources {
var tp int8
if tp, err = model.MustCheckBusiness(history.Business); err != nil {
return
}
histories = append(histories, &model.History{
Aid: history.Oid,
TP: tp,
})
}
err = r.svc.Delete(c, arg.Mid, histories)
return
}
// History return all history .
func (r *RPC) History(c context.Context, arg *model.ArgHistories, res *[]*model.Resource) (err error) {
tp, err := model.CheckBusiness(arg.Business)
if err != nil {
return
}
if tp > 0 {
arg.TP = tp
}
*res, err = r.svc.Histories(c, arg.Mid, arg.TP, arg.Pn, arg.Ps)
return
}
// HistoryCursor return all history .
func (r *RPC) HistoryCursor(c context.Context, arg *model.ArgCursor, res *[]*model.Resource) (err error) {
tp, err := model.CheckBusiness(arg.Business)
if err != nil {
return
}
if tp > 0 {
arg.TP = tp
}
var tps []int8
for _, b := range arg.Businesses {
tp, err = model.CheckBusiness(b)
if err != nil {
return
}
tps = append(tps, tp)
}
*res, err = r.svc.HistoryCursor(c, arg.Mid, arg.Max, arg.ViewAt, arg.Ps, arg.TP, tps, arg.RealIP)
return
}
// Clear clear history
func (r *RPC) Clear(c context.Context, arg *model.ArgClear, res *struct{}) (err error) {
var tps []int8
for _, b := range arg.Businesses {
var tp int8
if tp, err = model.MustCheckBusiness(b); err != nil {
return
}
tps = append(tps, tp)
}
err = r.svc.ClearHistory(c, arg.Mid, tps)
return
}

View File

@@ -0,0 +1,55 @@
package gorpc
import (
"context"
"testing"
"go-common/app/interface/main/history/model"
rpcClient "go-common/app/interface/main/history/rpc/client"
. "github.com/smartystreets/goconvey/convey"
)
var (
ctx = context.TODO()
client *rpcClient.Service
)
func WithRPC(f func(client *rpcClient.Service)) func() {
return func() {
client = rpcClient.New(nil)
f(client)
}
}
func Test_Histroy_rpc(t *testing.T) {
Convey("rpc client Add", t, WithRPC(func(client *rpcClient.Service) {
arg := &model.ArgHistory{
Mid: 14771787,
History: &model.History{
Aid: 17406762,
TP: 1,
Pro: 122,
},
}
client.Add(ctx, arg)
}))
Convey("rpc client preogress", t, WithRPC(func(client *rpcClient.Service) {
arg := &model.ArgPro{
Mid: 14771787,
Aids: []int64{17406762},
}
client.Progress(ctx, arg)
}))
Convey("rpc client delete", t, WithRPC(func(client *rpcClient.Service) {
r := &model.Resource{Oid: 100, Business: "archive"}
arg := &model.ArgDelete{
Mid: 14771787,
Resources: []*model.Resource{r},
}
client.Delete(ctx, arg)
}))
Convey("rpc client history", t, WithRPC(func(client *rpcClient.Service) {
arg := &model.ArgHistories{Mid: 14771787}
client.History(ctx, arg)
}))
}