2017-07-24 07:06:23 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2017-07-24 13:17:11 +00:00
|
|
|
gorush.SendNotification(notification)
|
2017-07-24 07:06:23 +00:00
|
|
|
|
|
|
|
return &pb.NotificationReply{
|
2017-07-24 13:17:11 +00:00
|
|
|
Success: false,
|
2017-07-24 07:06:23 +00:00
|
|
|
Counts: int32(len(notification.Tokens)),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunGRPCServer run gorush grpc server
|
|
|
|
func RunGRPCServer() error {
|
2017-07-24 10:58:30 +00:00
|
|
|
if !gorush.PushConf.GRPC.Enabled {
|
|
|
|
gorush.LogAccess.Debug("gRPC server is disabled.")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
lis, err := net.Listen("tcp", ":"+gorush.PushConf.GRPC.Port)
|
2017-07-24 07:06:23 +00:00
|
|
|
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)
|
2017-07-24 10:58:30 +00:00
|
|
|
gorush.LogAccess.Debug("gRPC server is running on " + gorush.PushConf.GRPC.Port + " port.")
|
2017-07-24 07:06:23 +00:00
|
|
|
if err := s.Serve(lis); err != nil {
|
|
|
|
gorush.LogError.Error("failed to serve: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|