-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
190 lines (163 loc) · 4.5 KB
/
main.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"sync"
"github.com/confluentinc/confluent-kafka-go/v2/kafka"
"github.com/gorilla/websocket"
"github.com/tanush-128/openzo_backend/websocket/config"
"github.com/tanush-128/openzo_backend/websocket/internal/pb"
"google.golang.org/grpc"
)
var (
UserClient pb.UserServiceClient
upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
clients = make(map[string]map[*websocket.Conn]bool)
clientsMutex = &sync.Mutex{}
)
func main() {
cfg, err := config.LoadConfig()
if err != nil {
log.Fatal(fmt.Errorf("failed to load config: %w", err))
}
// Initialize gRPC client
conn, err := grpc.Dial(cfg.UserGrpc, grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
UserClient = pb.NewUserServiceClient(conn)
go consumeKafka()
http.HandleFunc("/echo", echoHandler)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "websockets.html")
})
log.Printf("Server starting on port %s", cfg.HTTPPort)
http.ListenAndServe(fmt.Sprintf(":%s", cfg.HTTPPort), nil)
}
func echoHandler(w http.ResponseWriter, r *http.Request) {
//add Connection upgrade header
log.Printf("Header: %+v", r.Header)
r.Header.Add("Connection", "upgrade")
r.Header.Add("Upgrade", "websocket")
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Printf("Failed to upgrade to WebSocket: %v", err)
http.Error(w, "Failed to upgrade to WebSocket", http.StatusInternalServerError)
return
}
storeId := r.URL.Query().Get("storeId")
log.Printf("Store ID: %s", storeId)
if storeId == "" {
log.Printf("Invalid storeId")
conn.Close()
return
}
clientsMutex.Lock()
if clients[storeId] == nil {
clients[storeId] = make(map[*websocket.Conn]bool)
}
clients[storeId][conn] = true
clientsMutex.Unlock()
defer func() {
conn.Close()
clientsMutex.Lock()
delete(clients[storeId], conn)
clientsMutex.Unlock()
}()
conn.SetCloseHandler(func(code int, text string) error {
clientsMutex.Lock()
delete(clients[storeId], conn)
clientsMutex.Unlock()
return nil
})
for {
msgType, msg, err := conn.ReadMessage()
if err != nil {
log.Printf("ReadMessage error: %v", err)
return
}
log.Printf("%s sent: %s", conn.RemoteAddr(), string(msg))
clientsMutex.Lock()
for client := range clients[storeId] {
if err := client.WriteMessage(msgType, msg); err != nil {
log.Printf("WriteMessage error: %v", err)
client.Close()
delete(clients[storeId], client)
}
}
clientsMutex.Unlock()
}
}
// func broadcastMessageByStoreID(clients map[*websocket.Conn]bool, message []byte) {
// log.Printf("Broadcasting message to %d clients", len(clients))
// for client := range clients {
// if err := client.WriteMessage(websocket.TextMessage, message); err != nil {
// return
// }
// }
// }
// type Notification struct {
// Message string `json:"message"`
// FCMToken string `json:"fcm_token"`
// Data string `json:"data,omitempty"`
// Topic string `json:"topic,omitempty"`
// }
func consumeKafka() {
conf := ReadConfig()
topic := "sales"
conf["group.id"] = "websocket-group-1"
conf["auto.offset.reset"] = "latest"
consumer, err := kafka.NewConsumer(&conf)
if err != nil {
log.Fatalf("Failed to create Kafka consumer: %v", err)
}
defer consumer.Close()
err = consumer.SubscribeTopics([]string{topic}, nil)
if err != nil {
log.Fatalf("Failed to subscribe to topics: %v", err)
}
run := true
for run {
e := consumer.Poll(1000)
switch ev := e.(type) {
case *kafka.Message:
var message map[string]interface{}
err := json.Unmarshal(ev.Value, &message)
if err != nil {
log.Printf("Error unmarshalling JSON: %v", err)
continue
}
storeId, ok := message["store_id"].(string)
if !ok {
log.Printf("store_id is missing or not a string in message: %v", message)
continue
}
clientsMutex.Lock()
if clients[storeId] != nil {
broadcastMessageByStoreID(clients[storeId], ev.Value)
}
clientsMutex.Unlock()
case kafka.Error:
// Log Kafka errors but don't stop the consumer
log.Printf("Kafka error: %v", ev)
default:
// Handle other event types if necessary
}
}
}
func broadcastMessageByStoreID(storeClients map[*websocket.Conn]bool, message []byte) {
for client := range storeClients {
err := client.WriteMessage(websocket.TextMessage, message)
if err != nil {
log.Printf("Error writing message to WebSocket client: %v", err)
client.Close()
delete(storeClients, client)
}
}
}