forked from appleboy/gorush
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
145 lines (128 loc) · 3.64 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package rpc
import (
"context"
"net"
"strings"
"sync"
"github.com/miczone/gorush/config"
"github.com/miczone/gorush/core"
"github.com/miczone/gorush/logx"
"github.com/miczone/gorush/notify"
"github.com/miczone/gorush/rpc/proto"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/reflection"
"google.golang.org/grpc/status"
)
// Server is used to implement gorush grpc server.
type Server struct {
cfg config.ConfYaml
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(cfg config.ConfYaml) *Server {
return &Server{
cfg: cfg,
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")
}
// Send implements helloworld.GreeterServer
func (s *Server) Send(ctx context.Context, in *proto.NotificationRequest) (*proto.NotificationReply, error) {
badge := int(in.Badge)
notification := notify.PushNotification{
Cfg: s.cfg,
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,
MutableContent: in.MutableContent,
Image: in.Image,
Priority: strings.ToLower(in.GetPriority().String()),
}
if badge > 0 {
notification.Badge = &badge
}
if in.Topic != "" && in.Platform == core.PlatFormAndroid {
notification.To = in.Topic
}
if in.Alert != nil {
notification.Alert = notify.Alert{
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,
}
}
if in.Data != nil {
notification.Data = map[string]interface{}{}
for k, v := range in.Data.Fields {
notification.Data[k] = v
}
}
go notify.SendNotification(¬ification)
return &proto.NotificationReply{
Success: true,
Counts: int32(len(notification.Tokens)),
}, nil
}
// RunGRPCServer run gorush grpc server
func RunGRPCServer(ctx context.Context, cfg config.ConfYaml) error {
if !cfg.GRPC.Enabled {
logx.LogAccess.Info("gRPC server is disabled.")
return nil
}
s := grpc.NewServer()
rpcSrv := NewServer(cfg)
proto.RegisterGorushServer(s, rpcSrv)
proto.RegisterHealthServer(s, rpcSrv)
// Register reflection service on gRPC server.
reflection.Register(s)
lis, err := net.Listen("tcp", ":"+cfg.GRPC.Port)
if err != nil {
logx.LogError.Fatalln(err)
return err
}
logx.LogAccess.Info("gRPC server is running on " + cfg.GRPC.Port + " port.")
go func() {
select {
case <-ctx.Done():
s.GracefulStop() // graceful shutdown
logx.LogAccess.Info("shutdown the gRPC server")
}
}()
if err = s.Serve(lis); err != nil {
logx.LogError.Fatalln(err)
}
return err
}