Create & Init Project...
This commit is contained in:
23
app/service/live/grpc-demo/BUILD
Normal file
23
app/service/live/grpc-demo/BUILD
Normal file
@ -0,0 +1,23 @@
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [
|
||||
":package-srcs",
|
||||
"//app/service/live/grpc-demo/api/grpc/v1:all-srcs",
|
||||
"//app/service/live/grpc-demo/cmd:all-srcs",
|
||||
"//app/service/live/grpc-demo/conf:all-srcs",
|
||||
"//app/service/live/grpc-demo/dao:all-srcs",
|
||||
"//app/service/live/grpc-demo/model:all-srcs",
|
||||
"//app/service/live/grpc-demo/server/grpc:all-srcs",
|
||||
"//app/service/live/grpc-demo/server/http:all-srcs",
|
||||
"//app/service/live/grpc-demo/service:all-srcs",
|
||||
],
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
2
app/service/live/grpc-demo/CHANGELOG.md
Normal file
2
app/service/live/grpc-demo/CHANGELOG.md
Normal file
@ -0,0 +1,2 @@
|
||||
# v1.0.0
|
||||
1. 上线功能xxx
|
6
app/service/live/grpc-demo/CONTRIBUTORS.md
Normal file
6
app/service/live/grpc-demo/CONTRIBUTORS.md
Normal file
@ -0,0 +1,6 @@
|
||||
# Owner
|
||||
liugang
|
||||
|
||||
# Author
|
||||
|
||||
# Reviewer
|
10
app/service/live/grpc-demo/OWNERS
Normal file
10
app/service/live/grpc-demo/OWNERS
Normal file
@ -0,0 +1,10 @@
|
||||
# See the OWNERS docs at https://go.k8s.io/owners
|
||||
|
||||
approvers:
|
||||
- liugang
|
||||
labels:
|
||||
- live
|
||||
- service
|
||||
- service/live/grpc-demo
|
||||
options:
|
||||
no_parent_owners: true
|
12
app/service/live/grpc-demo/README.md
Normal file
12
app/service/live/grpc-demo/README.md
Normal file
@ -0,0 +1,12 @@
|
||||
# gprc-demo-service
|
||||
|
||||
# 项目简介
|
||||
1.
|
||||
|
||||
# 编译环境
|
||||
|
||||
|
||||
# 依赖包
|
||||
|
||||
|
||||
# 编译执行
|
56
app/service/live/grpc-demo/api/grpc/v1/BUILD
Normal file
56
app/service/live/grpc-demo/api/grpc/v1/BUILD
Normal file
@ -0,0 +1,56 @@
|
||||
load(
|
||||
"@io_bazel_rules_go//proto:def.bzl",
|
||||
"go_proto_library",
|
||||
)
|
||||
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
)
|
||||
|
||||
proto_library(
|
||||
name = "v1_proto",
|
||||
srcs = ["api.proto"],
|
||||
tags = ["automanaged"],
|
||||
deps = ["@gogo_special_proto//github.com/gogo/protobuf/gogoproto"],
|
||||
)
|
||||
|
||||
go_proto_library(
|
||||
name = "v1_go_proto",
|
||||
compilers = ["@io_bazel_rules_go//proto:gogofast_grpc"],
|
||||
importpath = "go-common/app/service/live/grpc-demo/api/grpc/v1",
|
||||
proto = ":v1_proto",
|
||||
tags = ["automanaged"],
|
||||
deps = ["@com_github_gogo_protobuf//gogoproto:go_default_library"],
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = [],
|
||||
embed = [":v1_go_proto"],
|
||||
importpath = "go-common/app/service/live/grpc-demo/api/grpc/v1",
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"@com_github_gogo_protobuf//gogoproto:go_default_library",
|
||||
"@com_github_gogo_protobuf//proto:go_default_library",
|
||||
"@org_golang_google_grpc//:go_default_library",
|
||||
"@org_golang_x_net//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"],
|
||||
)
|
607
app/service/live/grpc-demo/api/grpc/v1/api.pb.go
Normal file
607
app/service/live/grpc-demo/api/grpc/v1/api.pb.go
Normal file
@ -0,0 +1,607 @@
|
||||
// Code generated by protoc-gen-gogo. DO NOT EDIT.
|
||||
// source: app/service/live/grpc-demo/api/grpc/v1/api.proto
|
||||
|
||||
package v1
|
||||
|
||||
import proto "github.com/gogo/protobuf/proto"
|
||||
import fmt "fmt"
|
||||
import math "math"
|
||||
import _ "github.com/gogo/protobuf/gogoproto"
|
||||
|
||||
import (
|
||||
context "golang.org/x/net/context"
|
||||
grpc "google.golang.org/grpc"
|
||||
)
|
||||
|
||||
import io "io"
|
||||
|
||||
// Reference imports to suppress errors if they are not otherwise used.
|
||||
var _ = proto.Marshal
|
||||
var _ = fmt.Errorf
|
||||
var _ = math.Inf
|
||||
|
||||
// This is a compile-time assertion to ensure that this generated file
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.GoGoProtoPackageIsVersion2 // please upgrade the proto package
|
||||
|
||||
type GeeterReq struct {
|
||||
Uid int32 `protobuf:"varint,1,opt,name=uid,proto3" json:"uid,omitempty" form:"uid" validate:"required"`
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
XXX_unrecognized []byte `json:"-"`
|
||||
XXX_sizecache int32 `json:"-"`
|
||||
}
|
||||
|
||||
func (m *GeeterReq) Reset() { *m = GeeterReq{} }
|
||||
func (m *GeeterReq) String() string { return proto.CompactTextString(m) }
|
||||
func (*GeeterReq) ProtoMessage() {}
|
||||
func (*GeeterReq) Descriptor() ([]byte, []int) {
|
||||
return fileDescriptor_api_021c7d441ded66d0, []int{0}
|
||||
}
|
||||
func (m *GeeterReq) XXX_Unmarshal(b []byte) error {
|
||||
return m.Unmarshal(b)
|
||||
}
|
||||
func (m *GeeterReq) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
||||
if deterministic {
|
||||
return xxx_messageInfo_GeeterReq.Marshal(b, m, deterministic)
|
||||
} else {
|
||||
b = b[:cap(b)]
|
||||
n, err := m.MarshalTo(b)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return b[:n], nil
|
||||
}
|
||||
}
|
||||
func (dst *GeeterReq) XXX_Merge(src proto.Message) {
|
||||
xxx_messageInfo_GeeterReq.Merge(dst, src)
|
||||
}
|
||||
func (m *GeeterReq) XXX_Size() int {
|
||||
return m.Size()
|
||||
}
|
||||
func (m *GeeterReq) XXX_DiscardUnknown() {
|
||||
xxx_messageInfo_GeeterReq.DiscardUnknown(m)
|
||||
}
|
||||
|
||||
var xxx_messageInfo_GeeterReq proto.InternalMessageInfo
|
||||
|
||||
func (m *GeeterReq) GetUid() int32 {
|
||||
if m != nil {
|
||||
return m.Uid
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
type GreeterResp struct {
|
||||
Uid int32 `protobuf:"varint,1,opt,name=uid,proto3" json:"uid"`
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
XXX_unrecognized []byte `json:"-"`
|
||||
XXX_sizecache int32 `json:"-"`
|
||||
}
|
||||
|
||||
func (m *GreeterResp) Reset() { *m = GreeterResp{} }
|
||||
func (m *GreeterResp) String() string { return proto.CompactTextString(m) }
|
||||
func (*GreeterResp) ProtoMessage() {}
|
||||
func (*GreeterResp) Descriptor() ([]byte, []int) {
|
||||
return fileDescriptor_api_021c7d441ded66d0, []int{1}
|
||||
}
|
||||
func (m *GreeterResp) XXX_Unmarshal(b []byte) error {
|
||||
return m.Unmarshal(b)
|
||||
}
|
||||
func (m *GreeterResp) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
||||
if deterministic {
|
||||
return xxx_messageInfo_GreeterResp.Marshal(b, m, deterministic)
|
||||
} else {
|
||||
b = b[:cap(b)]
|
||||
n, err := m.MarshalTo(b)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return b[:n], nil
|
||||
}
|
||||
}
|
||||
func (dst *GreeterResp) XXX_Merge(src proto.Message) {
|
||||
xxx_messageInfo_GreeterResp.Merge(dst, src)
|
||||
}
|
||||
func (m *GreeterResp) XXX_Size() int {
|
||||
return m.Size()
|
||||
}
|
||||
func (m *GreeterResp) XXX_DiscardUnknown() {
|
||||
xxx_messageInfo_GreeterResp.DiscardUnknown(m)
|
||||
}
|
||||
|
||||
var xxx_messageInfo_GreeterResp proto.InternalMessageInfo
|
||||
|
||||
func (m *GreeterResp) GetUid() int32 {
|
||||
if m != nil {
|
||||
return m.Uid
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func init() {
|
||||
proto.RegisterType((*GeeterReq)(nil), "live.grpcdemo.v1.GeeterReq")
|
||||
proto.RegisterType((*GreeterResp)(nil), "live.grpcdemo.v1.GreeterResp")
|
||||
}
|
||||
|
||||
// Reference imports to suppress errors if they are not otherwise used.
|
||||
var _ context.Context
|
||||
var _ grpc.ClientConn
|
||||
|
||||
// This is a compile-time assertion to ensure that this generated file
|
||||
// is compatible with the grpc package it is being compiled against.
|
||||
const _ = grpc.SupportPackageIsVersion4
|
||||
|
||||
// GreeterClient is the client API for Greeter service.
|
||||
//
|
||||
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.
|
||||
type GreeterClient interface {
|
||||
// `method:"POST" internal:"true"`
|
||||
SayHelloInternal(ctx context.Context, in *GeeterReq, opts ...grpc.CallOption) (*GreeterResp, error)
|
||||
SayHello(ctx context.Context, in *GeeterReq, opts ...grpc.CallOption) (*GreeterResp, error)
|
||||
}
|
||||
|
||||
type greeterClient struct {
|
||||
cc *grpc.ClientConn
|
||||
}
|
||||
|
||||
func NewGreeterClient(cc *grpc.ClientConn) GreeterClient {
|
||||
return &greeterClient{cc}
|
||||
}
|
||||
|
||||
func (c *greeterClient) SayHelloInternal(ctx context.Context, in *GeeterReq, opts ...grpc.CallOption) (*GreeterResp, error) {
|
||||
out := new(GreeterResp)
|
||||
err := c.cc.Invoke(ctx, "/live.grpcdemo.v1.Greeter/say_hello_internal", in, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *greeterClient) SayHello(ctx context.Context, in *GeeterReq, opts ...grpc.CallOption) (*GreeterResp, error) {
|
||||
out := new(GreeterResp)
|
||||
err := c.cc.Invoke(ctx, "/live.grpcdemo.v1.Greeter/say_hello", in, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// GreeterServer is the server API for Greeter service.
|
||||
type GreeterServer interface {
|
||||
// `method:"POST" internal:"true"`
|
||||
SayHelloInternal(context.Context, *GeeterReq) (*GreeterResp, error)
|
||||
SayHello(context.Context, *GeeterReq) (*GreeterResp, error)
|
||||
}
|
||||
|
||||
func RegisterGreeterServer(s *grpc.Server, srv GreeterServer) {
|
||||
s.RegisterService(&_Greeter_serviceDesc, srv)
|
||||
}
|
||||
|
||||
func _Greeter_SayHelloInternal_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(GeeterReq)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(GreeterServer).SayHelloInternal(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: "/live.grpcdemo.v1.Greeter/SayHelloInternal",
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(GreeterServer).SayHelloInternal(ctx, req.(*GeeterReq))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _Greeter_SayHello_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(GeeterReq)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(GreeterServer).SayHello(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: "/live.grpcdemo.v1.Greeter/SayHello",
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(GreeterServer).SayHello(ctx, req.(*GeeterReq))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
var _Greeter_serviceDesc = grpc.ServiceDesc{
|
||||
ServiceName: "live.grpcdemo.v1.Greeter",
|
||||
HandlerType: (*GreeterServer)(nil),
|
||||
Methods: []grpc.MethodDesc{
|
||||
{
|
||||
MethodName: "say_hello_internal",
|
||||
Handler: _Greeter_SayHelloInternal_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "say_hello",
|
||||
Handler: _Greeter_SayHello_Handler,
|
||||
},
|
||||
},
|
||||
Streams: []grpc.StreamDesc{},
|
||||
Metadata: "app/service/live/grpc-demo/api/grpc/v1/api.proto",
|
||||
}
|
||||
|
||||
func (m *GeeterReq) Marshal() (dAtA []byte, err error) {
|
||||
size := m.Size()
|
||||
dAtA = make([]byte, size)
|
||||
n, err := m.MarshalTo(dAtA)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return dAtA[:n], nil
|
||||
}
|
||||
|
||||
func (m *GeeterReq) MarshalTo(dAtA []byte) (int, error) {
|
||||
var i int
|
||||
_ = i
|
||||
var l int
|
||||
_ = l
|
||||
if m.Uid != 0 {
|
||||
dAtA[i] = 0x8
|
||||
i++
|
||||
i = encodeVarintApi(dAtA, i, uint64(m.Uid))
|
||||
}
|
||||
if m.XXX_unrecognized != nil {
|
||||
i += copy(dAtA[i:], m.XXX_unrecognized)
|
||||
}
|
||||
return i, nil
|
||||
}
|
||||
|
||||
func (m *GreeterResp) Marshal() (dAtA []byte, err error) {
|
||||
size := m.Size()
|
||||
dAtA = make([]byte, size)
|
||||
n, err := m.MarshalTo(dAtA)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return dAtA[:n], nil
|
||||
}
|
||||
|
||||
func (m *GreeterResp) MarshalTo(dAtA []byte) (int, error) {
|
||||
var i int
|
||||
_ = i
|
||||
var l int
|
||||
_ = l
|
||||
if m.Uid != 0 {
|
||||
dAtA[i] = 0x8
|
||||
i++
|
||||
i = encodeVarintApi(dAtA, i, uint64(m.Uid))
|
||||
}
|
||||
if m.XXX_unrecognized != nil {
|
||||
i += copy(dAtA[i:], m.XXX_unrecognized)
|
||||
}
|
||||
return i, nil
|
||||
}
|
||||
|
||||
func encodeVarintApi(dAtA []byte, offset int, v uint64) int {
|
||||
for v >= 1<<7 {
|
||||
dAtA[offset] = uint8(v&0x7f | 0x80)
|
||||
v >>= 7
|
||||
offset++
|
||||
}
|
||||
dAtA[offset] = uint8(v)
|
||||
return offset + 1
|
||||
}
|
||||
func (m *GeeterReq) Size() (n int) {
|
||||
if m == nil {
|
||||
return 0
|
||||
}
|
||||
var l int
|
||||
_ = l
|
||||
if m.Uid != 0 {
|
||||
n += 1 + sovApi(uint64(m.Uid))
|
||||
}
|
||||
if m.XXX_unrecognized != nil {
|
||||
n += len(m.XXX_unrecognized)
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func (m *GreeterResp) Size() (n int) {
|
||||
if m == nil {
|
||||
return 0
|
||||
}
|
||||
var l int
|
||||
_ = l
|
||||
if m.Uid != 0 {
|
||||
n += 1 + sovApi(uint64(m.Uid))
|
||||
}
|
||||
if m.XXX_unrecognized != nil {
|
||||
n += len(m.XXX_unrecognized)
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func sovApi(x uint64) (n int) {
|
||||
for {
|
||||
n++
|
||||
x >>= 7
|
||||
if x == 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
func sozApi(x uint64) (n int) {
|
||||
return sovApi(uint64((x << 1) ^ uint64((int64(x) >> 63))))
|
||||
}
|
||||
func (m *GeeterReq) Unmarshal(dAtA []byte) error {
|
||||
l := len(dAtA)
|
||||
iNdEx := 0
|
||||
for iNdEx < l {
|
||||
preIndex := iNdEx
|
||||
var wire uint64
|
||||
for shift := uint(0); ; shift += 7 {
|
||||
if shift >= 64 {
|
||||
return ErrIntOverflowApi
|
||||
}
|
||||
if iNdEx >= l {
|
||||
return io.ErrUnexpectedEOF
|
||||
}
|
||||
b := dAtA[iNdEx]
|
||||
iNdEx++
|
||||
wire |= (uint64(b) & 0x7F) << shift
|
||||
if b < 0x80 {
|
||||
break
|
||||
}
|
||||
}
|
||||
fieldNum := int32(wire >> 3)
|
||||
wireType := int(wire & 0x7)
|
||||
if wireType == 4 {
|
||||
return fmt.Errorf("proto: GeeterReq: wiretype end group for non-group")
|
||||
}
|
||||
if fieldNum <= 0 {
|
||||
return fmt.Errorf("proto: GeeterReq: illegal tag %d (wire type %d)", fieldNum, wire)
|
||||
}
|
||||
switch fieldNum {
|
||||
case 1:
|
||||
if wireType != 0 {
|
||||
return fmt.Errorf("proto: wrong wireType = %d for field Uid", wireType)
|
||||
}
|
||||
m.Uid = 0
|
||||
for shift := uint(0); ; shift += 7 {
|
||||
if shift >= 64 {
|
||||
return ErrIntOverflowApi
|
||||
}
|
||||
if iNdEx >= l {
|
||||
return io.ErrUnexpectedEOF
|
||||
}
|
||||
b := dAtA[iNdEx]
|
||||
iNdEx++
|
||||
m.Uid |= (int32(b) & 0x7F) << shift
|
||||
if b < 0x80 {
|
||||
break
|
||||
}
|
||||
}
|
||||
default:
|
||||
iNdEx = preIndex
|
||||
skippy, err := skipApi(dAtA[iNdEx:])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if skippy < 0 {
|
||||
return ErrInvalidLengthApi
|
||||
}
|
||||
if (iNdEx + skippy) > l {
|
||||
return io.ErrUnexpectedEOF
|
||||
}
|
||||
m.XXX_unrecognized = append(m.XXX_unrecognized, dAtA[iNdEx:iNdEx+skippy]...)
|
||||
iNdEx += skippy
|
||||
}
|
||||
}
|
||||
|
||||
if iNdEx > l {
|
||||
return io.ErrUnexpectedEOF
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (m *GreeterResp) Unmarshal(dAtA []byte) error {
|
||||
l := len(dAtA)
|
||||
iNdEx := 0
|
||||
for iNdEx < l {
|
||||
preIndex := iNdEx
|
||||
var wire uint64
|
||||
for shift := uint(0); ; shift += 7 {
|
||||
if shift >= 64 {
|
||||
return ErrIntOverflowApi
|
||||
}
|
||||
if iNdEx >= l {
|
||||
return io.ErrUnexpectedEOF
|
||||
}
|
||||
b := dAtA[iNdEx]
|
||||
iNdEx++
|
||||
wire |= (uint64(b) & 0x7F) << shift
|
||||
if b < 0x80 {
|
||||
break
|
||||
}
|
||||
}
|
||||
fieldNum := int32(wire >> 3)
|
||||
wireType := int(wire & 0x7)
|
||||
if wireType == 4 {
|
||||
return fmt.Errorf("proto: GreeterResp: wiretype end group for non-group")
|
||||
}
|
||||
if fieldNum <= 0 {
|
||||
return fmt.Errorf("proto: GreeterResp: illegal tag %d (wire type %d)", fieldNum, wire)
|
||||
}
|
||||
switch fieldNum {
|
||||
case 1:
|
||||
if wireType != 0 {
|
||||
return fmt.Errorf("proto: wrong wireType = %d for field Uid", wireType)
|
||||
}
|
||||
m.Uid = 0
|
||||
for shift := uint(0); ; shift += 7 {
|
||||
if shift >= 64 {
|
||||
return ErrIntOverflowApi
|
||||
}
|
||||
if iNdEx >= l {
|
||||
return io.ErrUnexpectedEOF
|
||||
}
|
||||
b := dAtA[iNdEx]
|
||||
iNdEx++
|
||||
m.Uid |= (int32(b) & 0x7F) << shift
|
||||
if b < 0x80 {
|
||||
break
|
||||
}
|
||||
}
|
||||
default:
|
||||
iNdEx = preIndex
|
||||
skippy, err := skipApi(dAtA[iNdEx:])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if skippy < 0 {
|
||||
return ErrInvalidLengthApi
|
||||
}
|
||||
if (iNdEx + skippy) > l {
|
||||
return io.ErrUnexpectedEOF
|
||||
}
|
||||
m.XXX_unrecognized = append(m.XXX_unrecognized, dAtA[iNdEx:iNdEx+skippy]...)
|
||||
iNdEx += skippy
|
||||
}
|
||||
}
|
||||
|
||||
if iNdEx > l {
|
||||
return io.ErrUnexpectedEOF
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func skipApi(dAtA []byte) (n int, err error) {
|
||||
l := len(dAtA)
|
||||
iNdEx := 0
|
||||
for iNdEx < l {
|
||||
var wire uint64
|
||||
for shift := uint(0); ; shift += 7 {
|
||||
if shift >= 64 {
|
||||
return 0, ErrIntOverflowApi
|
||||
}
|
||||
if iNdEx >= l {
|
||||
return 0, io.ErrUnexpectedEOF
|
||||
}
|
||||
b := dAtA[iNdEx]
|
||||
iNdEx++
|
||||
wire |= (uint64(b) & 0x7F) << shift
|
||||
if b < 0x80 {
|
||||
break
|
||||
}
|
||||
}
|
||||
wireType := int(wire & 0x7)
|
||||
switch wireType {
|
||||
case 0:
|
||||
for shift := uint(0); ; shift += 7 {
|
||||
if shift >= 64 {
|
||||
return 0, ErrIntOverflowApi
|
||||
}
|
||||
if iNdEx >= l {
|
||||
return 0, io.ErrUnexpectedEOF
|
||||
}
|
||||
iNdEx++
|
||||
if dAtA[iNdEx-1] < 0x80 {
|
||||
break
|
||||
}
|
||||
}
|
||||
return iNdEx, nil
|
||||
case 1:
|
||||
iNdEx += 8
|
||||
return iNdEx, nil
|
||||
case 2:
|
||||
var length int
|
||||
for shift := uint(0); ; shift += 7 {
|
||||
if shift >= 64 {
|
||||
return 0, ErrIntOverflowApi
|
||||
}
|
||||
if iNdEx >= l {
|
||||
return 0, io.ErrUnexpectedEOF
|
||||
}
|
||||
b := dAtA[iNdEx]
|
||||
iNdEx++
|
||||
length |= (int(b) & 0x7F) << shift
|
||||
if b < 0x80 {
|
||||
break
|
||||
}
|
||||
}
|
||||
iNdEx += length
|
||||
if length < 0 {
|
||||
return 0, ErrInvalidLengthApi
|
||||
}
|
||||
return iNdEx, nil
|
||||
case 3:
|
||||
for {
|
||||
var innerWire uint64
|
||||
var start int = iNdEx
|
||||
for shift := uint(0); ; shift += 7 {
|
||||
if shift >= 64 {
|
||||
return 0, ErrIntOverflowApi
|
||||
}
|
||||
if iNdEx >= l {
|
||||
return 0, io.ErrUnexpectedEOF
|
||||
}
|
||||
b := dAtA[iNdEx]
|
||||
iNdEx++
|
||||
innerWire |= (uint64(b) & 0x7F) << shift
|
||||
if b < 0x80 {
|
||||
break
|
||||
}
|
||||
}
|
||||
innerWireType := int(innerWire & 0x7)
|
||||
if innerWireType == 4 {
|
||||
break
|
||||
}
|
||||
next, err := skipApi(dAtA[start:])
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
iNdEx = start + next
|
||||
}
|
||||
return iNdEx, nil
|
||||
case 4:
|
||||
return iNdEx, nil
|
||||
case 5:
|
||||
iNdEx += 4
|
||||
return iNdEx, nil
|
||||
default:
|
||||
return 0, fmt.Errorf("proto: illegal wireType %d", wireType)
|
||||
}
|
||||
}
|
||||
panic("unreachable")
|
||||
}
|
||||
|
||||
var (
|
||||
ErrInvalidLengthApi = fmt.Errorf("proto: negative length found during unmarshaling")
|
||||
ErrIntOverflowApi = fmt.Errorf("proto: integer overflow")
|
||||
)
|
||||
|
||||
func init() {
|
||||
proto.RegisterFile("app/service/live/grpc-demo/api/grpc/v1/api.proto", fileDescriptor_api_021c7d441ded66d0)
|
||||
}
|
||||
|
||||
var fileDescriptor_api_021c7d441ded66d0 = []byte{
|
||||
// 271 bytes of a gzipped FileDescriptorProto
|
||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xa4, 0x90, 0x3d, 0x4e, 0xc3, 0x30,
|
||||
0x18, 0x40, 0x65, 0x10, 0x54, 0x35, 0x4b, 0xe5, 0x09, 0x8a, 0x48, 0x2b, 0x4f, 0x5d, 0x6a, 0x13,
|
||||
0x60, 0xea, 0x46, 0x97, 0xac, 0xa8, 0x23, 0x4b, 0xe5, 0x24, 0x5f, 0x53, 0x4b, 0x49, 0xed, 0x3a,
|
||||
0xb6, 0x25, 0xee, 0xc3, 0x61, 0x18, 0x39, 0x41, 0x85, 0x32, 0x32, 0x72, 0x02, 0x64, 0x53, 0x55,
|
||||
0x15, 0x62, 0x63, 0xfb, 0x9e, 0xfd, 0xf4, 0xfc, 0x83, 0x6f, 0x85, 0xd6, 0xbc, 0x05, 0xe3, 0x65,
|
||||
0x01, 0xbc, 0x96, 0x1e, 0x78, 0x65, 0x74, 0x31, 0x2d, 0xa1, 0x51, 0x5c, 0x68, 0x19, 0x89, 0xfb,
|
||||
0x34, 0xcc, 0x4c, 0x1b, 0x65, 0x15, 0x19, 0x04, 0x8b, 0x85, 0xf5, 0x20, 0x31, 0x9f, 0x0e, 0xa7,
|
||||
0x95, 0xb4, 0x6b, 0x97, 0xb3, 0x42, 0x35, 0xbc, 0x52, 0x95, 0xe2, 0x51, 0xcc, 0xdd, 0x2a, 0x52,
|
||||
0x84, 0x38, 0xfd, 0x04, 0xe8, 0x23, 0xee, 0x67, 0x00, 0x16, 0xcc, 0x02, 0xb6, 0xe4, 0x01, 0x9f,
|
||||
0x3a, 0x59, 0x5e, 0xa2, 0x31, 0x9a, 0x9c, 0xcd, 0xe9, 0xd7, 0x6e, 0x94, 0xac, 0x94, 0x69, 0x66,
|
||||
0xd4, 0xc9, 0x92, 0x8e, 0xbd, 0xa8, 0x65, 0x29, 0x2c, 0xcc, 0xa8, 0x81, 0xad, 0x93, 0x06, 0x4a,
|
||||
0xba, 0x08, 0x3a, 0x9d, 0xe0, 0x8b, 0xcc, 0xec, 0x1b, 0xad, 0x26, 0x57, 0xc7, 0x91, 0xde, 0xe7,
|
||||
0x6e, 0x14, 0x30, 0x9a, 0x77, 0xaf, 0x08, 0xf7, 0xf6, 0x2a, 0x79, 0xc2, 0xa4, 0x15, 0x2f, 0xcb,
|
||||
0x35, 0xd4, 0xb5, 0x5a, 0xca, 0x8d, 0x05, 0xb3, 0x11, 0x35, 0xb9, 0x66, 0xbf, 0x1f, 0xc4, 0x0e,
|
||||
0xd7, 0x1b, 0xde, 0xfc, 0xb1, 0x79, 0x74, 0x70, 0x86, 0xfb, 0x87, 0xe2, 0x7f, 0x42, 0xf3, 0xc1,
|
||||
0x5b, 0x97, 0xa0, 0xf7, 0x2e, 0x41, 0x1f, 0x5d, 0x82, 0x9e, 0x4f, 0x7c, 0x9a, 0x9f, 0xc7, 0xcf,
|
||||
0xba, 0xff, 0x0e, 0x00, 0x00, 0xff, 0xff, 0xe5, 0xee, 0x9e, 0xe9, 0xa1, 0x01, 0x00, 0x00,
|
||||
}
|
22
app/service/live/grpc-demo/api/grpc/v1/api.proto
Normal file
22
app/service/live/grpc-demo/api/grpc/v1/api.proto
Normal file
@ -0,0 +1,22 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package live.grpcdemo.v1;
|
||||
|
||||
option go_package = "v1";
|
||||
|
||||
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
|
||||
|
||||
|
||||
service Greeter {
|
||||
// `method:"POST" internal:"true"`
|
||||
rpc say_hello_internal(GeeterReq) returns (GreeterResp);
|
||||
rpc say_hello(GeeterReq) returns (GreeterResp);
|
||||
}
|
||||
|
||||
message GeeterReq {
|
||||
int32 uid = 1 [(gogoproto.moretags) = "form:\"uid\" validate:\"required\""];
|
||||
}
|
||||
|
||||
message GreeterResp {
|
||||
int32 uid = 1 [(gogoproto.jsontag) = "uid"];
|
||||
}
|
1
app/service/live/grpc-demo/api/http/api.md
Normal file
1
app/service/live/grpc-demo/api/http/api.md
Normal file
@ -0,0 +1 @@
|
||||
# HTTP API文档
|
47
app/service/live/grpc-demo/cmd/BUILD
Normal file
47
app/service/live/grpc-demo/cmd/BUILD
Normal file
@ -0,0 +1,47 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_binary",
|
||||
"go_library",
|
||||
)
|
||||
|
||||
go_binary(
|
||||
name = "cmd",
|
||||
embed = [":go_default_library"],
|
||||
tags = ["automanaged"],
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["main.go"],
|
||||
data = ["test.toml"],
|
||||
importpath = "go-common/app/service/live/grpc-demo/cmd",
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//app/service/live/grpc-demo/conf:go_default_library",
|
||||
"//app/service/live/grpc-demo/server/grpc:go_default_library",
|
||||
"//app/service/live/grpc-demo/server/http:go_default_library",
|
||||
"//library/ecode/tip:go_default_library",
|
||||
"//library/log:go_default_library",
|
||||
"//library/net/trace:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [
|
||||
":package-srcs",
|
||||
"//app/service/live/grpc-demo/cmd/client:all-srcs",
|
||||
],
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
40
app/service/live/grpc-demo/cmd/client/BUILD
Normal file
40
app/service/live/grpc-demo/cmd/client/BUILD
Normal file
@ -0,0 +1,40 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_binary",
|
||||
"go_library",
|
||||
)
|
||||
|
||||
go_binary(
|
||||
name = "client",
|
||||
embed = [":go_default_library"],
|
||||
tags = ["automanaged"],
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["main.go"],
|
||||
importpath = "go-common/app/service/live/grpc-demo/cmd/client",
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//app/service/live/grpc-demo/api/grpc/v1:go_default_library",
|
||||
"//library/net/rpc/warden:go_default_library",
|
||||
"//library/time: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"],
|
||||
)
|
41
app/service/live/grpc-demo/cmd/client/main.go
Normal file
41
app/service/live/grpc-demo/cmd/client/main.go
Normal file
@ -0,0 +1,41 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"go-common/app/service/live/grpc-demo/api/grpc/v1"
|
||||
"go-common/library/net/rpc/warden"
|
||||
xtime "go-common/library/time"
|
||||
)
|
||||
|
||||
var name, addr string
|
||||
|
||||
func init() {
|
||||
flag.StringVar(&name, "name", "lily", "name")
|
||||
flag.StringVar(&addr, "addr", "127.0.0.1:9000", "server addr")
|
||||
}
|
||||
func main() {
|
||||
flag.Parse()
|
||||
cfg := &warden.ClientConfig{
|
||||
Dial: xtime.Duration(time.Second * 3),
|
||||
Timeout: xtime.Duration(time.Second * 3),
|
||||
}
|
||||
cc, err := warden.NewClient(cfg).Dial(context.Background(), addr)
|
||||
if err != nil {
|
||||
log.Fatalf("new client failed!err:=%v", err)
|
||||
return
|
||||
}
|
||||
client := v1.NewGreeterClient(cc)
|
||||
resp, err := client.SayHello(context.Background(), &v1.GeeterReq{
|
||||
Uid: 123,
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatalf("say hello failed!err:=%v", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("got HelloReply:%+v", resp)
|
||||
}
|
44
app/service/live/grpc-demo/cmd/main.go
Normal file
44
app/service/live/grpc-demo/cmd/main.go
Normal file
@ -0,0 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"go-common/app/service/live/grpc-demo/conf"
|
||||
"go-common/app/service/live/grpc-demo/server/grpc"
|
||||
"go-common/app/service/live/grpc-demo/server/http"
|
||||
ecode "go-common/library/ecode/tip"
|
||||
"go-common/library/log"
|
||||
"go-common/library/net/trace"
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
if err := conf.Init(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log.Init(conf.Conf.Log)
|
||||
defer log.Close()
|
||||
log.Info("start")
|
||||
trace.Init(conf.Conf.Tracer)
|
||||
defer trace.Close()
|
||||
ecode.Init(conf.Conf.Ecode)
|
||||
http.Init(conf.Conf)
|
||||
grpc.Init(conf.Conf)
|
||||
c := make(chan os.Signal, 1)
|
||||
signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
|
||||
for {
|
||||
s := <-c
|
||||
log.Info("get a signal %s", s.String())
|
||||
switch s {
|
||||
case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
|
||||
log.Info("exit")
|
||||
return
|
||||
case syscall.SIGHUP:
|
||||
default:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
35
app/service/live/grpc-demo/cmd/test.toml
Normal file
35
app/service/live/grpc-demo/cmd/test.toml
Normal file
@ -0,0 +1,35 @@
|
||||
|
||||
[mysql]
|
||||
addr = "127.0.0.1:3306"
|
||||
dsn = "test:test@tcp(127.0.0.1:3306)/test?timeout=200ms&readTimeout=200ms&writeTimeout=200ms&parseTime=true&loc=Local&charset=utf8,utf8mb4"
|
||||
readDSN = ["test:test@tcp(127.0.0.2:3306)/test? timeout=200ms&readTimeout=200ms&writeTimeout=200ms&parseTime=true&loc=Local&charset=utf8,utf8mb4","test:test@tcp(127.0.0.3:3306)/test?timeout=200ms&readTimeout=200ms&writeTimeout=200ms&parseTime=true&loc=Local&charset=utf8,utf8mb4"]
|
||||
active = 20
|
||||
idle = 10
|
||||
idleTimeout ="4h"
|
||||
queryTimeout = "100ms"
|
||||
execTimeout = "100ms"
|
||||
tranTimeout = "200ms"
|
||||
|
||||
[redis]
|
||||
name = "gprc-demo-service"
|
||||
proto = "tcp"
|
||||
addr = ""
|
||||
idle = 10
|
||||
active = 10
|
||||
dialTimeout = "1s"
|
||||
readTimeout = "1s"
|
||||
writeTimeout = "1s"
|
||||
idleTimeout = "10s"
|
||||
expire = "1m"
|
||||
|
||||
[memcache]
|
||||
name = "gprc-demo-service"
|
||||
proto = "tcp"
|
||||
addr = ""
|
||||
active = 50
|
||||
idle = 10
|
||||
dialTimeout = "1s"
|
||||
readTimeout = "1s"
|
||||
writeTimeout = "1s"
|
||||
idleTimeout = "10s"
|
||||
expire = "24h"
|
40
app/service/live/grpc-demo/conf/BUILD
Normal file
40
app/service/live/grpc-demo/conf/BUILD
Normal file
@ -0,0 +1,40 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["conf.go"],
|
||||
importpath = "go-common/app/service/live/grpc-demo/conf",
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//library/cache/memcache:go_default_library",
|
||||
"//library/cache/redis:go_default_library",
|
||||
"//library/conf:go_default_library",
|
||||
"//library/database/sql:go_default_library",
|
||||
"//library/ecode/tip:go_default_library",
|
||||
"//library/log:go_default_library",
|
||||
"//library/net/http/blademaster:go_default_library",
|
||||
"//library/net/http/blademaster/middleware/verify:go_default_library",
|
||||
"//library/net/trace:go_default_library",
|
||||
"//vendor/github.com/BurntSushi/toml: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"],
|
||||
)
|
88
app/service/live/grpc-demo/conf/conf.go
Normal file
88
app/service/live/grpc-demo/conf/conf.go
Normal file
@ -0,0 +1,88 @@
|
||||
package conf
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"flag"
|
||||
|
||||
"go-common/library/cache/memcache"
|
||||
"go-common/library/cache/redis"
|
||||
"go-common/library/conf"
|
||||
"go-common/library/database/sql"
|
||||
ecode "go-common/library/ecode/tip"
|
||||
"go-common/library/log"
|
||||
bm "go-common/library/net/http/blademaster"
|
||||
"go-common/library/net/http/blademaster/middleware/verify"
|
||||
"go-common/library/net/trace"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
|
||||
var (
|
||||
confPath string
|
||||
client *conf.Client
|
||||
// Conf config
|
||||
Conf = &Config{}
|
||||
)
|
||||
|
||||
// Config .
|
||||
type Config struct {
|
||||
Log *log.Config
|
||||
BM *bm.ServerConfig
|
||||
Verify *verify.Config
|
||||
Tracer *trace.Config
|
||||
Redis *redis.Config
|
||||
Memcache *memcache.Config
|
||||
MySQL *sql.Config
|
||||
Ecode *ecode.Config
|
||||
}
|
||||
|
||||
func init() {
|
||||
flag.StringVar(&confPath, "conf", "", "default config path")
|
||||
}
|
||||
|
||||
// Init init conf
|
||||
func Init() error {
|
||||
if confPath != "" {
|
||||
return local()
|
||||
}
|
||||
return remote()
|
||||
}
|
||||
|
||||
func local() (err error) {
|
||||
_, err = toml.DecodeFile(confPath, &Conf)
|
||||
return
|
||||
}
|
||||
|
||||
func remote() (err error) {
|
||||
if client, err = conf.New(); err != nil {
|
||||
return
|
||||
}
|
||||
if err = load(); err != nil {
|
||||
return
|
||||
}
|
||||
go func() {
|
||||
for range client.Event() {
|
||||
log.Info("config reload")
|
||||
if load() != nil {
|
||||
log.Error("config reload error (%v)", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
return
|
||||
}
|
||||
|
||||
func load() (err error) {
|
||||
var (
|
||||
s string
|
||||
ok bool
|
||||
tmpConf *Config
|
||||
)
|
||||
if s, ok = client.Toml2(); !ok {
|
||||
return errors.New("load config center error")
|
||||
}
|
||||
if _, err = toml.Decode(s, &tmpConf); err != nil {
|
||||
return errors.New("could not decode config")
|
||||
}
|
||||
*Conf = *tmpConf
|
||||
return
|
||||
}
|
34
app/service/live/grpc-demo/dao/BUILD
Normal file
34
app/service/live/grpc-demo/dao/BUILD
Normal file
@ -0,0 +1,34 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["dao.go"],
|
||||
importpath = "go-common/app/service/live/grpc-demo/dao",
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//app/service/live/grpc-demo/conf:go_default_library",
|
||||
"//library/cache/memcache:go_default_library",
|
||||
"//library/cache/redis:go_default_library",
|
||||
"//library/database/sql: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"],
|
||||
)
|
43
app/service/live/grpc-demo/dao/dao.go
Normal file
43
app/service/live/grpc-demo/dao/dao.go
Normal file
@ -0,0 +1,43 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go-common/app/service/live/grpc-demo/conf"
|
||||
"go-common/library/cache/memcache"
|
||||
"go-common/library/cache/redis"
|
||||
xsql "go-common/library/database/sql"
|
||||
)
|
||||
|
||||
// Dao dao
|
||||
type Dao struct {
|
||||
c *conf.Config
|
||||
mc *memcache.Pool
|
||||
redis *redis.Pool
|
||||
db *xsql.DB
|
||||
}
|
||||
|
||||
// New init mysql db
|
||||
func New(c *conf.Config) (dao *Dao) {
|
||||
dao = &Dao{
|
||||
c: c,
|
||||
mc: memcache.NewPool(c.Memcache),
|
||||
redis: redis.NewPool(c.Redis),
|
||||
db: xsql.NewMySQL(c.MySQL),
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Close close the resource.
|
||||
func (d *Dao) Close() {
|
||||
d.mc.Close()
|
||||
d.redis.Close()
|
||||
d.db.Close()
|
||||
return
|
||||
}
|
||||
|
||||
// Ping dao ping
|
||||
func (d *Dao) Ping(c context.Context) error {
|
||||
// TODO: if you need use mc,redis, please add
|
||||
return d.db.Ping(c)
|
||||
}
|
28
app/service/live/grpc-demo/model/BUILD
Normal file
28
app/service/live/grpc-demo/model/BUILD
Normal file
@ -0,0 +1,28 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["model.go"],
|
||||
importpath = "go-common/app/service/live/grpc-demo/model",
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [":package-srcs"],
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
1
app/service/live/grpc-demo/model/model.go
Normal file
1
app/service/live/grpc-demo/model/model.go
Normal file
@ -0,0 +1 @@
|
||||
package model
|
35
app/service/live/grpc-demo/server/grpc/BUILD
Normal file
35
app/service/live/grpc-demo/server/grpc/BUILD
Normal file
@ -0,0 +1,35 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["server.go"],
|
||||
importpath = "go-common/app/service/live/grpc-demo/server/grpc",
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//app/service/live/grpc-demo/api/grpc/v1:go_default_library",
|
||||
"//app/service/live/grpc-demo/conf:go_default_library",
|
||||
"//app/service/live/grpc-demo/service/v1:go_default_library",
|
||||
"//library/log:go_default_library",
|
||||
"//library/net/rpc/warden: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"],
|
||||
)
|
21
app/service/live/grpc-demo/server/grpc/server.go
Normal file
21
app/service/live/grpc-demo/server/grpc/server.go
Normal file
@ -0,0 +1,21 @@
|
||||
package grpc
|
||||
|
||||
import (
|
||||
pb "go-common/app/service/live/grpc-demo/api/grpc/v1"
|
||||
"go-common/app/service/live/grpc-demo/conf"
|
||||
svc "go-common/app/service/live/grpc-demo/service/v1"
|
||||
"go-common/library/log"
|
||||
"go-common/library/net/rpc/warden"
|
||||
)
|
||||
|
||||
// TODO
|
||||
|
||||
func Init(c *conf.Config) {
|
||||
s := warden.NewServer(nil)
|
||||
pb.RegisterGreeterServer(s.Server(), svc.NewGreeterService(c))
|
||||
_, err := s.Start()
|
||||
if err != nil {
|
||||
log.Error("grpc Start error(%v)", err)
|
||||
panic(err)
|
||||
}
|
||||
}
|
35
app/service/live/grpc-demo/server/http/BUILD
Normal file
35
app/service/live/grpc-demo/server/http/BUILD
Normal file
@ -0,0 +1,35 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["http.go"],
|
||||
importpath = "go-common/app/service/live/grpc-demo/server/http",
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//app/service/live/grpc-demo/conf:go_default_library",
|
||||
"//app/service/live/grpc-demo/service:go_default_library",
|
||||
"//library/log:go_default_library",
|
||||
"//library/net/http/blademaster:go_default_library",
|
||||
"//library/net/http/blademaster/middleware/verify: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"],
|
||||
)
|
53
app/service/live/grpc-demo/server/http/http.go
Normal file
53
app/service/live/grpc-demo/server/http/http.go
Normal file
@ -0,0 +1,53 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"go-common/app/service/live/grpc-demo/conf"
|
||||
"go-common/app/service/live/grpc-demo/service"
|
||||
"go-common/library/log"
|
||||
bm "go-common/library/net/http/blademaster"
|
||||
"go-common/library/net/http/blademaster/middleware/verify"
|
||||
)
|
||||
|
||||
var (
|
||||
srv *service.Service
|
||||
vfy *verify.Verify
|
||||
)
|
||||
|
||||
// Init init
|
||||
func Init(c *conf.Config) {
|
||||
srv = service.New(c)
|
||||
vfy = verify.New(c.Verify)
|
||||
engine := bm.DefaultServer(c.BM)
|
||||
route(engine)
|
||||
if err := engine.Start(); err != nil {
|
||||
log.Error("bm Start error(%v)", err)
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func route(e *bm.Engine) {
|
||||
e.Ping(ping)
|
||||
e.Register(register)
|
||||
g := e.Group("/x/grpc-demo")
|
||||
{
|
||||
g.GET("/start", vfy.Verify, howToStart)
|
||||
}
|
||||
}
|
||||
|
||||
func ping(c *bm.Context) {
|
||||
if err := srv.Ping(c); err != nil {
|
||||
log.Error("ping error(%v)", err)
|
||||
c.AbortWithStatus(http.StatusServiceUnavailable)
|
||||
}
|
||||
}
|
||||
|
||||
func register(c *bm.Context) {
|
||||
c.JSON(map[string]interface{}{}, nil)
|
||||
}
|
||||
|
||||
// example for http request handler
|
||||
func howToStart(c *bm.Context) {
|
||||
c.String(0, "Golang 大法好 !!!")
|
||||
}
|
35
app/service/live/grpc-demo/service/BUILD
Normal file
35
app/service/live/grpc-demo/service/BUILD
Normal file
@ -0,0 +1,35 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["service.go"],
|
||||
importpath = "go-common/app/service/live/grpc-demo/service",
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//app/service/live/grpc-demo/conf:go_default_library",
|
||||
"//app/service/live/grpc-demo/dao:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [
|
||||
":package-srcs",
|
||||
"//app/service/live/grpc-demo/service/v1:all-srcs",
|
||||
],
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
33
app/service/live/grpc-demo/service/service.go
Normal file
33
app/service/live/grpc-demo/service/service.go
Normal file
@ -0,0 +1,33 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go-common/app/service/live/grpc-demo/conf"
|
||||
"go-common/app/service/live/grpc-demo/dao"
|
||||
)
|
||||
|
||||
// Service struct
|
||||
type Service struct {
|
||||
c *conf.Config
|
||||
dao *dao.Dao
|
||||
}
|
||||
|
||||
// New init
|
||||
func New(c *conf.Config) (s *Service) {
|
||||
s = &Service{
|
||||
c: c,
|
||||
dao: dao.New(c),
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// Ping Service
|
||||
func (s *Service) Ping(c context.Context) (err error) {
|
||||
return s.dao.Ping(c)
|
||||
}
|
||||
|
||||
// Close Service
|
||||
func (s *Service) Close() {
|
||||
s.dao.Close()
|
||||
}
|
33
app/service/live/grpc-demo/service/v1/BUILD
Normal file
33
app/service/live/grpc-demo/service/v1/BUILD
Normal file
@ -0,0 +1,33 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["greeter.go"],
|
||||
importpath = "go-common/app/service/live/grpc-demo/service/v1",
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//app/service/live/grpc-demo/api/grpc/v1:go_default_library",
|
||||
"//app/service/live/grpc-demo/conf:go_default_library",
|
||||
"@org_golang_google_grpc//metadata: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"],
|
||||
)
|
40
app/service/live/grpc-demo/service/v1/greeter.go
Normal file
40
app/service/live/grpc-demo/service/v1/greeter.go
Normal file
@ -0,0 +1,40 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
v1pb "go-common/app/service/live/grpc-demo/api/grpc/v1"
|
||||
"go-common/app/service/live/grpc-demo/conf"
|
||||
|
||||
"google.golang.org/grpc/metadata"
|
||||
)
|
||||
|
||||
// GreeterService struct
|
||||
type GreeterService struct {
|
||||
conf *conf.Config
|
||||
// optionally add other properties here, such as dao
|
||||
// dao *dao.Dao
|
||||
}
|
||||
|
||||
//NewGreeterService init
|
||||
func NewGreeterService(c *conf.Config) (s *GreeterService) {
|
||||
s = &GreeterService{
|
||||
conf: c,
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// SayHello implementation
|
||||
func (s *GreeterService) SayHello(ctx context.Context, req *v1pb.GeeterReq) (resp *v1pb.GreeterResp, err error) {
|
||||
resp = &v1pb.GreeterResp{}
|
||||
metadata.FromIncomingContext(ctx)
|
||||
resp.Uid = req.Uid
|
||||
return
|
||||
}
|
||||
|
||||
// SayHelloInternal implementation
|
||||
// `method:"POST" internal:"true"`
|
||||
func (s *GreeterService) SayHelloInternal(ctx context.Context, req *v1pb.GeeterReq) (resp *v1pb.GreeterResp, err error) {
|
||||
resp = &v1pb.GreeterResp{}
|
||||
return
|
||||
}
|
Reference in New Issue
Block a user