2017-07-24 07:06:23 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2017-11-12 14:44:33 +00:00
|
|
|
"sync"
|
2017-07-24 07:06:23 +00:00
|
|
|
|
|
|
|
"github.com/appleboy/gorush/gorush"
|
2017-11-12 14:44:33 +00:00
|
|
|
"github.com/appleboy/gorush/rpc/proto"
|
2017-07-24 07:06:23 +00:00
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
"google.golang.org/grpc"
|
2017-11-12 14:44:33 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
2017-07-24 07:06:23 +00:00
|
|
|
"google.golang.org/grpc/reflection"
|
2017-11-12 14:44:33 +00:00
|
|
|
"google.golang.org/grpc/status"
|
2017-07-24 07:06:23 +00:00
|
|
|
)
|
|
|
|
|
2017-11-12 14:44:33 +00:00
|
|
|
// Server is used to implement gorush grpc server.
|
|
|
|
type Server struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
// statusMap stores the serving status of the services this Server monitors.
|
|
|
|
statusMap map[string]proto.HealthCheckResponse_ServingStatus
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewServer returns a new Server.
|
|
|
|
func NewServer() *Server {
|
|
|
|
return &Server{
|
|
|
|
statusMap: make(map[string]proto.HealthCheckResponse_ServingStatus),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check implements `service Health`.
|
|
|
|
func (s *Server) Check(ctx context.Context, in *proto.HealthCheckRequest) (*proto.HealthCheckResponse, error) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
if in.Service == "" {
|
|
|
|
// check the server overall health status.
|
|
|
|
return &proto.HealthCheckResponse{
|
|
|
|
Status: proto.HealthCheckResponse_SERVING,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
if status, ok := s.statusMap[in.Service]; ok {
|
|
|
|
return &proto.HealthCheckResponse{
|
|
|
|
Status: status,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
return nil, status.Error(codes.NotFound, "unknown service")
|
|
|
|
}
|
2017-07-24 07:06:23 +00:00
|
|
|
|
|
|
|
// Send implements helloworld.GreeterServer
|
2017-11-12 14:44:33 +00:00
|
|
|
func (s *Server) Send(ctx context.Context, in *proto.NotificationRequest) (*proto.NotificationReply, error) {
|
2018-01-02 07:53:15 +00:00
|
|
|
var badge = int(in.Badge)
|
2017-07-24 07:06:23 +00:00
|
|
|
notification := gorush.PushNotification{
|
2018-01-10 01:36:27 +00:00
|
|
|
Platform: int(in.Platform),
|
|
|
|
Tokens: in.Tokens,
|
|
|
|
Message: in.Message,
|
|
|
|
Title: in.Title,
|
|
|
|
Topic: in.Topic,
|
|
|
|
APIKey: in.Key,
|
|
|
|
Category: in.Category,
|
|
|
|
Sound: in.Sound,
|
|
|
|
ContentAvailable: in.ContentAvailable,
|
|
|
|
ThreadID: in.ThreadID,
|
2018-01-03 09:08:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if badge > 0 {
|
|
|
|
notification.Badge = &badge
|
|
|
|
}
|
|
|
|
|
|
|
|
if in.Alert != nil {
|
|
|
|
notification.Alert = gorush.Alert{
|
2018-01-06 07:03:21 +00:00
|
|
|
Title: in.Alert.Title,
|
|
|
|
Body: in.Alert.Body,
|
|
|
|
Subtitle: in.Alert.Subtitle,
|
|
|
|
Action: in.Alert.Action,
|
|
|
|
ActionLocKey: in.Alert.Action,
|
|
|
|
LaunchImage: in.Alert.LaunchImage,
|
|
|
|
LocArgs: in.Alert.LocArgs,
|
|
|
|
LocKey: in.Alert.LocKey,
|
|
|
|
TitleLocArgs: in.Alert.TitleLocArgs,
|
|
|
|
TitleLocKey: in.Alert.TitleLocKey,
|
2018-01-03 09:08:28 +00:00
|
|
|
}
|
2017-07-24 07:06:23 +00:00
|
|
|
}
|
|
|
|
|
2017-07-24 13:46:42 +00:00
|
|
|
go gorush.SendNotification(notification)
|
2017-07-24 07:06:23 +00:00
|
|
|
|
2017-11-12 14:44:33 +00:00
|
|
|
return &proto.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 {
|
2017-10-19 01:24:47 +00:00
|
|
|
gorush.LogError.Errorf("failed to listen: %v", err)
|
2017-07-24 07:06:23 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
s := grpc.NewServer()
|
2017-11-12 14:44:33 +00:00
|
|
|
srv := NewServer()
|
|
|
|
proto.RegisterGorushServer(s, srv)
|
|
|
|
proto.RegisterHealthServer(s, srv)
|
2017-07-24 07:06:23 +00:00
|
|
|
// 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 {
|
2017-10-19 01:24:47 +00:00
|
|
|
gorush.LogError.Errorf("failed to serve: %v", err)
|
2017-07-24 07:06:23 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|