2017-07-24 07:06:23 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
2020-02-04 14:42:06 +00:00
|
|
|
"context"
|
2020-03-10 13:09:03 +00:00
|
|
|
"net"
|
2020-07-31 09:26:58 +00:00
|
|
|
"strings"
|
2017-11-12 14:44:33 +00:00
|
|
|
"sync"
|
2017-07-24 07:06:23 +00:00
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
"github.com/appleboy/gorush/config"
|
2021-07-13 08:32:39 +00:00
|
|
|
"github.com/appleboy/gorush/core"
|
2017-07-24 07:06:23 +00:00
|
|
|
"github.com/appleboy/gorush/gorush"
|
2021-07-13 08:32:39 +00:00
|
|
|
"github.com/appleboy/gorush/logx"
|
2017-11-12 14:44:33 +00:00
|
|
|
"github.com/appleboy/gorush/rpc/proto"
|
2017-07-24 07:06:23 +00:00
|
|
|
|
|
|
|
"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 {
|
2021-07-16 04:10:34 +00:00
|
|
|
cfg config.ConfYaml
|
|
|
|
mu sync.Mutex
|
2017-11-12 14:44:33 +00:00
|
|
|
// statusMap stores the serving status of the services this Server monitors.
|
|
|
|
statusMap map[string]proto.HealthCheckResponse_ServingStatus
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewServer returns a new Server.
|
2021-07-16 04:10:34 +00:00
|
|
|
func NewServer(cfg config.ConfYaml) *Server {
|
2017-11-12 14:44:33 +00:00
|
|
|
return &Server{
|
2021-07-16 04:10:34 +00:00
|
|
|
cfg: cfg,
|
2017-11-12 14:44:33 +00:00
|
|
|
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) {
|
2021-01-23 01:39:06 +00:00
|
|
|
badge := int(in.Badge)
|
2017-07-24 07:06:23 +00:00
|
|
|
notification := gorush.PushNotification{
|
2021-07-16 08:30:01 +00:00
|
|
|
Cfg: s.cfg,
|
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-16 03:26:45 +00:00
|
|
|
MutableContent: in.MutableContent,
|
2020-03-13 16:00:03 +00:00
|
|
|
Image: in.Image,
|
2020-09-27 04:45:58 +00:00
|
|
|
Priority: strings.ToLower(in.GetPriority().String()),
|
2018-01-03 09:08:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if badge > 0 {
|
|
|
|
notification.Badge = &badge
|
|
|
|
}
|
|
|
|
|
2021-07-13 08:32:39 +00:00
|
|
|
if in.Topic != "" && in.Platform == core.PlatFormAndroid {
|
2020-07-09 07:46:52 +00:00
|
|
|
notification.To = in.Topic
|
|
|
|
}
|
|
|
|
|
2018-01-03 09:08:28 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-05-21 06:10:32 +00:00
|
|
|
if in.Data != nil {
|
|
|
|
notification.Data = map[string]interface{}{}
|
|
|
|
for k, v := range in.Data.Fields {
|
|
|
|
notification.Data[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-17 17:58:46 +00:00
|
|
|
go gorush.SendNotification(¬ification)
|
2017-07-24 07:06:23 +00:00
|
|
|
|
2017-11-12 14:44:33 +00:00
|
|
|
return &proto.NotificationReply{
|
2018-04-11 02:14:28 +00:00
|
|
|
Success: true,
|
2017-07-24 07:06:23 +00:00
|
|
|
Counts: int32(len(notification.Tokens)),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunGRPCServer run gorush grpc server
|
2021-07-16 04:10:34 +00:00
|
|
|
func RunGRPCServer(ctx context.Context, cfg config.ConfYaml) error {
|
|
|
|
if !cfg.GRPC.Enabled {
|
2021-07-13 08:32:39 +00:00
|
|
|
logx.LogAccess.Info("gRPC server is disabled.")
|
2017-07-24 10:58:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-07-24 07:06:23 +00:00
|
|
|
s := grpc.NewServer()
|
2021-07-16 04:10:34 +00:00
|
|
|
rpcSrv := NewServer(cfg)
|
2020-02-04 14:42:06 +00:00
|
|
|
proto.RegisterGorushServer(s, rpcSrv)
|
|
|
|
proto.RegisterHealthServer(s, rpcSrv)
|
2020-03-10 13:09:03 +00:00
|
|
|
|
2017-07-24 07:06:23 +00:00
|
|
|
// Register reflection service on gRPC server.
|
|
|
|
reflection.Register(s)
|
2020-02-04 14:42:06 +00:00
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
lis, err := net.Listen("tcp", ":"+cfg.GRPC.Port)
|
2020-03-10 13:09:03 +00:00
|
|
|
if err != nil {
|
2021-07-13 08:32:39 +00:00
|
|
|
logx.LogError.Fatalln(err)
|
2020-03-10 13:09:03 +00:00
|
|
|
return err
|
2017-07-24 07:06:23 +00:00
|
|
|
}
|
2021-07-16 04:10:34 +00:00
|
|
|
logx.LogAccess.Info("gRPC server is running on " + cfg.GRPC.Port + " port.")
|
2020-03-10 13:09:03 +00:00
|
|
|
go func() {
|
2020-02-04 14:42:06 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2020-03-10 13:09:03 +00:00
|
|
|
s.GracefulStop() // graceful shutdown
|
2021-07-13 08:32:39 +00:00
|
|
|
logx.LogAccess.Info("shutdown the gRPC server")
|
2020-02-04 14:42:06 +00:00
|
|
|
}
|
2020-03-10 13:09:03 +00:00
|
|
|
}()
|
|
|
|
if err = s.Serve(lis); err != nil {
|
2021-07-13 08:32:39 +00:00
|
|
|
logx.LogError.Fatalln(err)
|
2020-03-10 13:09:03 +00:00
|
|
|
}
|
|
|
|
return err
|
2017-07-24 07:06:23 +00:00
|
|
|
}
|