feat(grpc): initial grpc server. (#253)

* feat(grpc): initial grpc server.

* refactor(vendor): add missing vendor.

* fix testing

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu
2017-07-24 15:06:23 +08:00
committed by GitHub
parent 478e39ec65
commit 9a52f8f2b5
102 changed files with 21796 additions and 441 deletions

35
rpc/example/client.go Normal file
View File

@@ -0,0 +1,35 @@
package main
import (
"log"
pb "github.com/appleboy/gorush/rpc/proto"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
const (
address = "localhost:50051"
defaultName = "world"
)
func main() {
// Set up a connection to the server.
conn, err := grpc.Dial(address, grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
c := pb.NewGorushClient(conn)
r, err := c.Send(context.Background(), &pb.NotificationRequest{
Platform: 2,
Tokens: []string{"1234567890"},
Message: "test message",
})
if err != nil {
log.Fatalf("could not greet: %v", err)
}
log.Printf("Success: %t\n", r.Success)
log.Printf("Count: %d\n", r.Counts)
}

212
rpc/proto/gorush.pb.go Normal file
View File

@@ -0,0 +1,212 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// source: gorush.proto
/*
Package proto is a generated protocol buffer package.
It is generated from these files:
gorush.proto
It has these top-level messages:
NotificationRequest
NotificationReply
*/
package proto
import proto1 "github.com/golang/protobuf/proto"
import fmt "fmt"
import math "math"
import (
context "golang.org/x/net/context"
grpc "google.golang.org/grpc"
)
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto1.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 _ = proto1.ProtoPackageIsVersion2 // please upgrade the proto package
type NotificationRequest struct {
Tokens []string `protobuf:"bytes,1,rep,name=tokens" json:"tokens,omitempty"`
Platform int32 `protobuf:"varint,2,opt,name=platform" json:"platform,omitempty"`
Message string `protobuf:"bytes,3,opt,name=message" json:"message,omitempty"`
Title string `protobuf:"bytes,4,opt,name=title" json:"title,omitempty"`
Topic string `protobuf:"bytes,5,opt,name=topic" json:"topic,omitempty"`
Key string `protobuf:"bytes,6,opt,name=key" json:"key,omitempty"`
}
func (m *NotificationRequest) Reset() { *m = NotificationRequest{} }
func (m *NotificationRequest) String() string { return proto1.CompactTextString(m) }
func (*NotificationRequest) ProtoMessage() {}
func (*NotificationRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
func (m *NotificationRequest) GetTokens() []string {
if m != nil {
return m.Tokens
}
return nil
}
func (m *NotificationRequest) GetPlatform() int32 {
if m != nil {
return m.Platform
}
return 0
}
func (m *NotificationRequest) GetMessage() string {
if m != nil {
return m.Message
}
return ""
}
func (m *NotificationRequest) GetTitle() string {
if m != nil {
return m.Title
}
return ""
}
func (m *NotificationRequest) GetTopic() string {
if m != nil {
return m.Topic
}
return ""
}
func (m *NotificationRequest) GetKey() string {
if m != nil {
return m.Key
}
return ""
}
type NotificationReply struct {
Success bool `protobuf:"varint,1,opt,name=success" json:"success,omitempty"`
Counts int32 `protobuf:"varint,2,opt,name=counts" json:"counts,omitempty"`
}
func (m *NotificationReply) Reset() { *m = NotificationReply{} }
func (m *NotificationReply) String() string { return proto1.CompactTextString(m) }
func (*NotificationReply) ProtoMessage() {}
func (*NotificationReply) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} }
func (m *NotificationReply) GetSuccess() bool {
if m != nil {
return m.Success
}
return false
}
func (m *NotificationReply) GetCounts() int32 {
if m != nil {
return m.Counts
}
return 0
}
func init() {
proto1.RegisterType((*NotificationRequest)(nil), "proto.NotificationRequest")
proto1.RegisterType((*NotificationReply)(nil), "proto.NotificationReply")
}
// 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
// Client API for Gorush service
type GorushClient interface {
Send(ctx context.Context, in *NotificationRequest, opts ...grpc.CallOption) (*NotificationReply, error)
}
type gorushClient struct {
cc *grpc.ClientConn
}
func NewGorushClient(cc *grpc.ClientConn) GorushClient {
return &gorushClient{cc}
}
func (c *gorushClient) Send(ctx context.Context, in *NotificationRequest, opts ...grpc.CallOption) (*NotificationReply, error) {
out := new(NotificationReply)
err := grpc.Invoke(ctx, "/proto.Gorush/Send", in, out, c.cc, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// Server API for Gorush service
type GorushServer interface {
Send(context.Context, *NotificationRequest) (*NotificationReply, error)
}
func RegisterGorushServer(s *grpc.Server, srv GorushServer) {
s.RegisterService(&_Gorush_serviceDesc, srv)
}
func _Gorush_Send_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(NotificationRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(GorushServer).Send(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/proto.Gorush/Send",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(GorushServer).Send(ctx, req.(*NotificationRequest))
}
return interceptor(ctx, in, info, handler)
}
var _Gorush_serviceDesc = grpc.ServiceDesc{
ServiceName: "proto.Gorush",
HandlerType: (*GorushServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "Send",
Handler: _Gorush_Send_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "gorush.proto",
}
func init() { proto1.RegisterFile("gorush.proto", fileDescriptor0) }
var fileDescriptor0 = []byte{
// 231 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x90, 0xb1, 0x4e, 0xc3, 0x30,
0x10, 0x86, 0x31, 0x69, 0x42, 0x7b, 0x62, 0x00, 0x83, 0x90, 0x95, 0x29, 0xca, 0x94, 0xa9, 0x03,
0xec, 0x6c, 0x08, 0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, 0x00, 0x00, 0xff, 0xff, 0x52,
0x8a, 0x53, 0x9a, 0x52, 0x01, 0x00, 0x00,
}

22
rpc/proto/gorush.proto Normal file
View File

@@ -0,0 +1,22 @@
syntax = "proto3";
package proto;
message NotificationRequest {
repeated string tokens = 1;
int32 platform = 2;
string message = 3;
string title = 4;
string topic = 5;
string key = 6;
}
message NotificationReply {
bool success = 1;
int32 counts = 2;
}
service Gorush {
rpc Send (NotificationRequest) returns (NotificationReply) {}
}

63
rpc/server.go Normal file
View File

@@ -0,0 +1,63 @@
package rpc
import (
"net"
"github.com/appleboy/gorush/gorush"
pb "github.com/appleboy/gorush/rpc/proto"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)
const (
port = ":50051"
)
// server is used to implement gorush grpc server.
type server struct{}
// Send implements helloworld.GreeterServer
func (s *server) Send(ctx context.Context, in *pb.NotificationRequest) (*pb.NotificationReply, error) {
notification := gorush.PushNotification{
Platform: int(in.Platform),
Tokens: in.Tokens,
Message: in.Message,
Title: in.Title,
Topic: in.Topic,
APIKey: in.Key,
}
success := false
switch notification.Platform {
case gorush.PlatFormIos:
success = gorush.PushToIOS(notification)
case gorush.PlatFormAndroid:
success = gorush.PushToAndroid(notification)
}
return &pb.NotificationReply{
Success: success,
Counts: int32(len(notification.Tokens)),
}, nil
}
// RunGRPCServer run gorush grpc server
func RunGRPCServer() error {
lis, err := net.Listen("tcp", port)
if err != nil {
gorush.LogError.Error("failed to listen: %v", err)
return err
}
s := grpc.NewServer()
pb.RegisterGorushServer(s, &server{})
// Register reflection service on gRPC server.
reflection.Register(s)
if err := s.Serve(lis); err != nil {
gorush.LogError.Error("failed to serve: %v", err)
return err
}
return nil
}