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,41 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = ["client_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = ["//app/service/main/location/model:go_default_library"],
)
go_library(
name = "go_default_library",
srcs = ["client.go"],
importpath = "go-common/app/service/main/location/rpc/client",
tags = ["automanaged"],
deps = [
"//app/service/main/location/model:go_default_library",
"//library/net/rpc: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,87 @@
package client
import (
"context"
"go-common/app/service/main/location/model"
"go-common/library/net/rpc"
)
const (
_archive = "RPC.Archive"
_archive2 = "RPC.Archive2"
_group = "RPC.Group"
_authPIDs = "RPC.AuthPIDs"
// new
_info = "RPC.Info"
_infos = "RPC.Infos"
_infoComplete = "RPC.InfoComplete"
_infosComplete = "RPC.InfosComplete"
// app id
_appid = "location.service"
)
// Service is resource rpc client.
type Service struct {
client *rpc.Client2
}
// New new a resource rpc client.
func New(c *rpc.ClientConfig) (s *Service) {
s = &Service{}
s.client = rpc.NewDiscoveryCli(_appid, c)
return
}
// Archive get the aid auth.
func (s *Service) Archive(c context.Context, arg *model.Archive) (res *int64, err error) {
res = new(int64)
err = s.client.Call(c, _archive, arg, res)
return
}
// Archive2 get the aid auth.
func (s *Service) Archive2(c context.Context, arg *model.Archive) (res *model.Auth, err error) {
res = new(model.Auth)
err = s.client.Call(c, _archive2, arg, res)
return
}
// Group get the gip auth.
func (s *Service) Group(c context.Context, arg *model.Group) (res *model.Auth, err error) {
res = new(model.Auth)
err = s.client.Call(c, _group, arg, res)
return
}
// AuthPIDs check if ip in pids.
func (s *Service) AuthPIDs(c context.Context, arg *model.ArgPids) (res map[int64]*model.Auth, err error) {
err = s.client.Call(c, _authPIDs, arg, &res)
return
}
// Info get the ip info.
func (s *Service) Info(c context.Context, arg *model.ArgIP) (res *model.Info, err error) {
res = new(model.Info)
err = s.client.Call(c, _info, arg, res)
return
}
// Infos get the ips info.
func (s *Service) Infos(c context.Context, arg []string) (res map[string]*model.Info, err error) {
err = s.client.Call(c, _infos, arg, &res)
return
}
// InfoComplete get the whold ip info.
func (s *Service) InfoComplete(c context.Context, arg *model.ArgIP) (res *model.InfoComplete, err error) {
res = new(model.InfoComplete)
err = s.client.Call(c, _infoComplete, arg, res)
return
}
// InfosComplete get the whold ips infos.
func (s *Service) InfosComplete(c context.Context, arg []string) (res map[string]*model.InfoComplete, err error) {
err = s.client.Call(c, _infosComplete, arg, &res)
return
}

View File

@ -0,0 +1,94 @@
package client
import (
"context"
"testing"
"time"
"go-common/app/service/main/location/model"
)
const (
_aid = 16428
_gid = 317
_mid = 0
_ip = "139.214.144.59"
_cip = "127.0.0.1"
)
func TestLocation(t *testing.T) {
s := New(nil)
time.Sleep(1 * time.Second)
testArchive(t, s)
testArchive2(t, s)
testGroup(t, s)
testAuthPIDs(t, s)
testInfo(t, s)
testInfos(t, s)
testInfoComplete(t, s)
testInfosComplete(t, s)
}
func testArchive(t *testing.T, s *Service) {
if res, err := s.Archive(context.TODO(), &model.Archive{Aid: _aid, Mid: _mid, IP: _ip, CIP: _cip}); err != nil {
t.Errorf("Service: archive err: %v", err)
} else {
t.Logf("Service: archive res: %v", res)
}
}
func testArchive2(t *testing.T, s *Service) {
if res, err := s.Archive2(context.TODO(), &model.Archive{Aid: _aid, Mid: _mid, IP: _ip, CIP: _cip}); err != nil {
t.Errorf("Service: archive2 err: %v", err)
} else {
t.Logf("Service: archive2 res: %v", res)
}
}
func testGroup(t *testing.T, s *Service) {
if res, err := s.Group(context.TODO(), &model.Group{Gid: _gid, Mid: _mid, IP: _ip, CIP: _cip}); err != nil {
t.Errorf("Service: group err: %v", err)
} else {
t.Logf("Service: group res: %v", res)
}
}
func testAuthPIDs(t *testing.T, s *Service) {
if res, err := s.AuthPIDs(context.TODO(), &model.ArgPids{IP: _ip, Pids: "2,1163,86,87", CIP: _cip}); err != nil {
t.Errorf("Service: AuthPIDs err: %v", err)
} else {
t.Logf("Service: AuthPIDs res: %v", res)
}
}
func testInfo(t *testing.T, s *Service) {
if res, err := s.Info(context.TODO(), &model.ArgIP{IP: _ip}); err != nil {
t.Errorf("Service: info err: %v", err)
} else {
t.Logf("Service: info res: %v", res)
}
}
func testInfos(t *testing.T, s *Service) {
if res, err := s.Infos(context.TODO(), []string{"61.216.166.156", "211.139.80.6"}); err != nil {
t.Errorf("Service: infos err: %v", err)
} else {
t.Logf("Service: infos res: %v", res)
}
}
func testInfoComplete(t *testing.T, s *Service) {
if res, err := s.InfoComplete(context.TODO(), &model.ArgIP{IP: _ip}); err != nil {
t.Errorf("Service: infoComplete err: %v", err)
} else {
t.Logf("Service: infoComplete res: %v", res)
}
}
func testInfosComplete(t *testing.T, s *Service) {
if res, err := s.InfosComplete(context.TODO(), []string{"61.216.166.156", "211.139.80.6"}); err != nil {
t.Errorf("Service: infosComplete err: %v", err)
} else {
t.Logf("Service: infosComplete res: %v", res)
}
}

View File

@ -0,0 +1,48 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = ["server_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/service/main/location/conf:go_default_library",
"//app/service/main/location/model:go_default_library",
"//app/service/main/location/service:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = ["server.go"],
importpath = "go-common/app/service/main/location/rpc/server",
tags = ["automanaged"],
deps = [
"//app/service/main/location/conf:go_default_library",
"//app/service/main/location/model:go_default_library",
"//app/service/main/location/service: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,92 @@
package server
import (
"go-common/app/service/main/location/conf"
"go-common/app/service/main/location/model"
"go-common/app/service/main/location/service"
"go-common/library/net/rpc"
"go-common/library/net/rpc/context"
)
// RPC struct
type RPC struct {
s *service.Service
}
// New init rpc server.
func New(c *conf.Config, s *service.Service) (svr *rpc.Server) {
r := &RPC{s: 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
}
// Archive get aid auth.
func (r *RPC) Archive(c context.Context, a *model.Archive, res *int64) (err error) {
var play int64
if play, err = r.s.Auth(c, a.Aid, a.Mid, a.IP, a.CIP); err == nil {
*res = play
}
return
}
// Archive2 get aid auth.
func (r *RPC) Archive2(c context.Context, a *model.Archive, res *model.Auth) (err error) {
var auth *model.Auth
if auth, err = r.s.Archive2(c, a.Aid, a.Mid, a.IP, a.CIP); err == nil {
*res = *auth
}
return
}
// Group get gid auth.
func (r *RPC) Group(c context.Context, a *model.Group, res *model.Auth) (err error) {
auth := r.s.AuthGID(c, a.Gid, a.Mid, a.IP, a.CIP)
if auth != nil {
*res = *auth
}
return
}
// AuthPIDs check if ip in pids.
func (r *RPC) AuthPIDs(c context.Context, a *model.ArgPids, res *map[int64]*model.Auth) (err error) {
*res, err = r.s.AuthPIDs(c, a.Pids, a.IP, a.CIP)
return
}
// Info get IP info
func (r *RPC) Info(c context.Context, a *model.ArgIP, res *model.Info) (err error) {
var ti *model.Info
if ti, err = r.s.Info(c, a.IP); err == nil {
*res = *ti
}
return
}
// Infos get IPs infos.
func (r *RPC) Infos(c context.Context, a []string, res *map[string]*model.Info) (err error) {
*res, err = r.s.Infos(c, a)
return
}
// InfoComplete get IP whole info.
func (r *RPC) InfoComplete(c context.Context, a *model.ArgIP, res *model.InfoComplete) (err error) {
var info *model.InfoComplete
if info, err = r.s.InfoComplete(c, a.IP); err == nil {
*res = *info
}
return
}
// InfosComplete get ips whole infos.
func (r *RPC) InfosComplete(c context.Context, a []string, res *map[string]*model.InfoComplete) (err error) {
*res, err = r.s.InfosComplete(c, a)
return
}

View File

@ -0,0 +1,173 @@
package server
import (
"fmt"
"net/rpc"
"testing"
"time"
"go-common/app/service/main/location/conf"
"go-common/app/service/main/location/model"
"go-common/app/service/main/location/service"
)
// rpc server const
const (
addr = "127.0.0.1:6293"
_archive = "RPC.Archive"
_archive2 = "RPC.Archive2"
_group = "RPC.Group"
_authPIDs = "RPC.AuthPIDs"
_info = "RPC.Info"
_infos = "RPC.Infos"
_infoComplete = "RPC.InfoComplete"
_infosComplete = "RPC.InfosComplete"
)
// TestLocation test rpc server
func TestLocation(t *testing.T) {
if err := conf.Init(); err != nil {
t.Errorf("conf.Init() error(%v)", err)
t.FailNow()
}
svr := service.New(conf.Conf)
New(conf.Conf, svr)
time.Sleep(time.Second * 3)
client, err := rpc.Dial("tcp", addr)
defer client.Close()
if err != nil {
t.Errorf("rpc.Dial(tcp, \"%s\") error(%v)", addr, err)
t.FailNow()
}
archiveRPC(client, t)
archive2RPC(client, t)
groupRPC(client, t)
authPIDsRPC(client, t)
infoRPC(client, t)
infosRPC(client, t)
infoCompleteRPC(client, t)
infosCompleteRPC(client, t)
}
func archiveRPC(client *rpc.Client, t *testing.T) {
var res int64
arg := &model.Archive{
Aid: 740955,
Mid: 0,
IP: "2.20.32.123",
CIP: "127.0.0.1",
}
err := client.Call(_archive, arg, &res)
if err != nil {
t.Errorf("err:%v.", err)
} else {
result("archive", t, res)
}
t.Logf("%+v", res)
}
func archive2RPC(client *rpc.Client, t *testing.T) {
res := &model.Auth{}
arg := &model.Archive{
Aid: 740955,
Mid: 0,
IP: "2.20.32.123",
CIP: "127.0.0.1",
}
err := client.Call(_archive2, arg, &res)
if err != nil {
t.Errorf("err:%v.", err)
} else {
result("archive2", t, res)
}
t.Logf("%+v", res)
}
func groupRPC(client *rpc.Client, t *testing.T) {
res := &model.Auth{}
arg := &model.Group{
Gid: 317,
Mid: 0,
IP: "2.20.32.123",
CIP: "127.0.0.1",
}
err := client.Call(_group, arg, &res)
if err != nil {
t.Errorf("err:%v.", err)
} else {
result("group", t, res)
}
t.Logf("%+v", res)
}
func authPIDsRPC(client *rpc.Client, t *testing.T) {
arg := &model.ArgPids{IP: "61.216.166.156", Pids: "150,92"}
res := map[int64]*model.Auth{}
err := client.Call(_authPIDs, arg, &res)
if err != nil {
t.Errorf("err:%v.", err)
} else {
result("authPIDs", t, res)
}
t.Logf("%+v,err:%v.", res, err)
}
func infoRPC(client *rpc.Client, t *testing.T) {
var res = new(model.Info)
arg := &model.ArgIP{
IP: "139.214.144.59",
}
err := client.Call(_info, arg, res)
if err != nil {
t.Errorf("err:%v.", err)
} else {
result("info", t, res)
}
t.Logf("%+v,err:%v.", res, err)
}
func infosRPC(client *rpc.Client, t *testing.T) {
arg := []string{"61.216.166.156", "211.139.80.6"}
res := map[string]*model.Info{}
err := client.Call(_infos, arg, &res)
if err != nil {
t.Errorf("err:%v.", err)
} else {
result("infos", t, res)
}
t.Logf("%+v,err:%v.", res, err)
}
func infoCompleteRPC(client *rpc.Client, t *testing.T) {
var res = new(model.InfoComplete)
arg := &model.ArgIP{
IP: "139.214.144.59",
}
err := client.Call(_infoComplete, arg, res)
if err != nil {
t.Errorf("err:%v.", err)
} else {
result("infoComplete", t, res)
}
t.Logf("%+v,err:%v.", res, err)
}
func infosCompleteRPC(client *rpc.Client, t *testing.T) {
arg := []string{"61.216.166.156", "211.139.80.6"}
res := map[string]*model.InfoComplete{}
err := client.Call(_infosComplete, arg, &res)
if err != nil {
t.Errorf("err:%v.", err)
} else {
result("infosComplete", t, res)
}
t.Logf("%+v,err:%v.", res, err)
}
func result(name string, t *testing.T, res interface{}) {
fmt.Printf("res : %+v \n", res)
t.Log("[==========" + name + "单元测试结果==========]")
t.Log(res)
t.Log("[↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑]\r\n")
}