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

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
}