/
notifications.go
77 lines (63 loc) 路 1.81 KB
/
notifications.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
package mock
import (
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"net/http"
)
type NotificationsRequest struct {
AppId string `form:"appId" binding:"required"`
Cluster string `form:"cluster" binding:"required"`
Notifications string `form:"notifications" binding:"required"`
}
type Notification struct {
NamespaceName string `json:"namespaceName"`
NotificationId int64 `json:"notificationId"`
}
type NotificationRes struct {
NamespaceName string `json:"namespaceName"`
NotificationId int64 `json:"notificationId"`
Messages Message `json:"messages"`
}
type Message struct {
Details map[string]interface{} `json:"details"`
}
func (s *Server) handleNotifications(c *gin.Context) {
req := NotificationsRequest{}
if err := c.ShouldBindQuery(&req); err != nil {
c.JSON(http.StatusForbidden, err.Error())
fmt.Printf("ShouldBindQuery fail:%s", err.Error())
return
}
fmt.Printf("req:%+v\n", req)
var resp []NotificationRes
var notifications []Notification
err := json.Unmarshal([]byte(req.Notifications), ¬ifications)
if err != nil {
c.JSON(http.StatusForbidden, err.Error())
return
}
for _, notification := range notifications {
// if notificationId is lower than 50, then nothing change
if notification.NotificationId < 50 {
continue
}
key := fmt.Sprintf("%s+%s+%s", req.AppId, req.Cluster, notification.NamespaceName)
message := Message{}
message.Details = make(map[string]interface{})
notificationId := notification.NotificationId + 1
message.Details[key] = notificationId
resItem := NotificationRes{
NamespaceName: notification.NamespaceName,
NotificationId: notificationId,
Messages: message,
}
resp = append(resp, resItem)
}
if len(resp) == 0 {
c.JSON(http.StatusNotModified, nil)
return
}
c.JSON(http.StatusOK, resp)
return
}