-
Notifications
You must be signed in to change notification settings - Fork 71
/
websocket.go
147 lines (134 loc) · 3.87 KB
/
websocket.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
146
147
package routes
import (
"encoding/json"
"fmt"
"net/http"
"github.com/RedHatInsights/chrome-service-backend/rest/cloudevents"
"github.com/RedHatInsights/chrome-service-backend/rest/connectionhub"
"github.com/RedHatInsights/chrome-service-backend/rest/util"
"github.com/go-chi/chi/v5"
"github.com/gorilla/websocket"
"github.com/sirupsen/logrus"
)
type WSRequestPayload struct {
connectionhub.WsMessage
Type string `json:"type"`
Id string `json:"id"`
}
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
Subprotocols: []string{"cloudevents.json"},
CheckOrigin: func(r *http.Request) bool {
return true
},
}
func MakeWsRoute(sub chi.Router) {
sub.Get("/", HandleWsConnection)
}
func BroadcastMessage(sub chi.Router) {
sub.Post("/", EmitMessage)
}
func HandleWsConnection(w http.ResponseWriter, r *http.Request) {
jwtCookie, err := r.Cookie("cs_jwt")
if err != nil {
logrus.Errorln("Unable to find cs_jwt cookie", err)
return
}
identity, err := util.ParseJWTToken(jwtCookie.Value)
if err != nil {
logrus.Errorln("Unable to parse jwt token", err)
return
}
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
logrus.Errorln("Unable to upgrade WS connection", err)
return
}
client := connectionhub.Client{
User: identity.UserId,
Organization: identity.OrgId,
Username: identity.Username,
Roles: []string{},
Conn: &connectionhub.Connection{Send: make(chan []byte, 256), Ws: ws},
}
logrus.Infoln("New client added to the connection hub: ", client.User)
connectionhub.ConnectionHub.Register <- client
go client.WritePump()
client.ReadPump()
}
func EmitMessage(w http.ResponseWriter, r *http.Request) {
var p WSRequestPayload
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&p)
logrus.Infoln("Attempting to emit new broadcast message", p)
if err != nil {
logrus.Errorln(err)
payload := make(map[string]string)
payload["msg"] = "Unable to decode payload!"
response, _ := json.Marshal(payload)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
w.Write(response)
return
}
event := cloudevents.WrapPayload(p.Payload, cloudevents.URI(r.Host+r.URL.Path), p.Id, p.Type)
dctErr := event.DataContentType.IsValid()
if dctErr != nil {
logrus.Errorln(dctErr)
payload := make(map[string]string)
payload["msg"] = "The Data Content Type needs to be in application/json format!"
response, _ := json.Marshal(payload)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
w.Write(response)
return
}
svErr := event.SpecVersion.IsValid()
if svErr != nil {
logrus.Errorln(svErr)
payload := make(map[string]string)
payload["msg"] = "Spec version needs to be 1.0.2!"
response, _ := json.Marshal(payload)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
w.Write(response)
return
}
uriErr := event.Source.IsValid()
if uriErr != nil {
logrus.Errorln(svErr)
payload := make(map[string]string)
payload["msg"] = "Invalid URI!"
response, _ := json.Marshal(payload)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
w.Write(response)
return
}
data, err := json.Marshal(event)
if err != nil {
logrus.Errorln(err)
payload := make(map[string]string)
payload["msg"] = "Unable to decode payload!"
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
return
}
fmt.Println("p", p)
newMessage := connectionhub.Message{
Destinations: connectionhub.MessageDestinations{
Users: p.Users,
Roles: p.Roles,
Organizations: p.Organizations,
},
Broadcast: p.Broadcast,
Data: data,
}
if newMessage.Broadcast {
connectionhub.ConnectionHub.Broadcast <- newMessage
} else {
connectionhub.ConnectionHub.Emit <- newMessage
}
w.WriteHeader(http.StatusOK)
}