-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
275 lines (230 loc) · 7.11 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"sync"
"github.com/go-redis/redis/v8"
"github.com/gorilla/mux"
"github.com/streadway/amqp"
)
type Config struct {
RabbitMQ struct {
HostName string `json:"HostName"`
Port int `json:"Port"`
UserName string `json:"UserName"`
Password string `json:"Password"`
Exchange string `json:"Exchange"`
} `json:"RabbitMQ"`
Redis struct {
HostName string `json:"HostName"`
Port int `json:"Port"`
} `json:"Redis"`
}
type Listener struct {
EventName string `json:"EventName"`
URL string `json:"URL"`
}
type EventObject struct {
ControllerName string `json:"ControllerName"`
Name string `json:"Name"`
Payload string `json:"Payload"`
}
type ServiceEvent struct {
ServiceName string `json:"ServiceName"`
EventObj EventObject `json:"EventObj"`
TimeStamp string `json:"TimeStamp"`
}
var (
config Config
redisClient *redis.Client
listeners = make(map[string][]Listener)
lock sync.Mutex
)
func formatServiceName(serviceEvent ServiceEvent) string {
return fmt.Sprintf("%s.%s.%s", serviceEvent.ServiceName, serviceEvent.EventObj.ControllerName, serviceEvent.EventObj.Name)
}
func main() {
log.Println("Starting service...")
loadConfig()
setupRedis()
router := mux.NewRouter()
router.HandleFunc("/api/getListeners", getListenersHandler).Methods("GET")
router.HandleFunc("/api/register", registerListener).Methods("POST")
router.HandleFunc("/api/remove", removeListener).Methods("POST")
router.HandleFunc("/api/removeAll", removeAllListeners).Methods("POST")
log.Println("Setting up RabbitMQ listener...")
go listenToRabbitMQ()
log.Println("Service is now listening on port 8080...")
log.Fatal(http.ListenAndServe(":8080", router))
}
func loadConfig() {
file, err := os.Open("config.json")
if err != nil {
log.Fatalf("Error opening config file: %v", err)
}
defer file.Close()
if err := json.NewDecoder(file).Decode(&config); err != nil {
log.Fatalf("Error decoding config file: %v", err)
}
log.Println("Configuration loaded successfully.")
}
func setupRedis() {
redisClient = redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%d", config.Redis.HostName, config.Redis.Port),
})
if _, err := redisClient.Ping(context.Background()).Result(); err != nil {
log.Fatalf("Failed to connect to Redis: %v", err)
}
log.Println("Connected to Redis successfully.")
}
func getListenersHandler(w http.ResponseWriter, r *http.Request) {
eventName := r.URL.Query().Get("eventName")
if eventName == "" {
http.Error(w, "Event name is required", http.StatusBadRequest)
return
}
listeners, err := getEventListeners(eventName)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
response, err := json.Marshal(listeners)
if err != nil {
http.Error(w, "Failed to parse listeners response", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(response)
}
// getEventListeners returns all URLs registered for a given event name.
func getEventListeners(eventName string) ([]string, error) {
ctx := context.Background()
// Use LRange to get all elements stored in the list for this event name.
listeners, err := redisClient.LRange(ctx, "listeners_"+eventName, 0, -1).Result()
if err != nil {
return nil, fmt.Errorf("failed to retrieve listeners from Redis: %v", err)
}
// Convert the list of Redis values to a slice of strings.
urls := make([]string, len(listeners))
for i, listener := range listeners {
urls[i] = listener
}
return urls, nil
}
func registerListener(w http.ResponseWriter, r *http.Request) {
var listener Listener
if err := json.NewDecoder(r.Body).Decode(&listener); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
lock.Lock()
listeners[listener.EventName] = append(listeners[listener.EventName], listener)
lock.Unlock()
log.Printf("Registered new listener for event %s to %s\n", listener.EventName, listener.URL)
w.WriteHeader(http.StatusCreated)
}
func removeListener(w http.ResponseWriter, r *http.Request) {
var listener Listener
if err := json.NewDecoder(r.Body).Decode(&listener); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
lock.Lock()
defer lock.Unlock()
if lst, ok := listeners[listener.EventName]; ok {
for i, l := range lst {
if l.URL == listener.URL {
listeners[listener.EventName] = append(lst[:i], lst[i+1:]...)
break
}
}
}
log.Printf("Removed listener for event %s from %s\n", listener.EventName, listener.URL)
w.WriteHeader(http.StatusOK)
}
func removeAllListeners(w http.ResponseWriter, r *http.Request) {
lock.Lock()
listeners = make(map[string][]Listener)
lock.Unlock()
log.Println("Removed all listeners")
w.WriteHeader(http.StatusOK)
}
func listenToRabbitMQ() {
conn, err := amqp.Dial(fmt.Sprintf("amqp://%s:%s@%s:%d/", config.RabbitMQ.UserName, config.RabbitMQ.Password, config.RabbitMQ.HostName, config.RabbitMQ.Port))
if err != nil {
log.Fatalf("Failed to connect to RabbitMQ: %v", err)
}
defer conn.Close()
ch, err := conn.Channel()
if err != nil {
log.Fatalf("Failed to open a channel: %v", err)
}
defer ch.Close()
if err := ch.ExchangeDeclare(
config.RabbitMQ.Exchange, "fanout", false, false, false, false, nil,
); err != nil {
log.Fatalf("Failed to declare an exchange: %v", err)
}
q, err := ch.QueueDeclare("", false, true, true, false, nil)
if err != nil {
log.Fatalf("Failed to declare a queue: %v", err)
}
if err := ch.QueueBind(q.Name, "*", config.RabbitMQ.Exchange, false, nil); err != nil {
log.Fatalf("Failed to bind a queue: %v", err)
}
msgs, err := ch.Consume(q.Name, "", true, false, false, false, nil)
if err != nil {
log.Fatalf("Failed to register a consumer: %v", err)
}
forever := make(chan bool)
go func() {
for d := range msgs {
log.Printf("Received a message: %s", d.Body)
var event ServiceEvent
if err := json.Unmarshal(d.Body, &event); err != nil {
log.Printf("Error parsing message JSON: %v", err)
continue
}
dispatchEvent(event)
}
}()
log.Println(" [*] Waiting for messages. To exit press CTRL+C")
<-forever
}
func dispatchEvent(event ServiceEvent) {
formattedEventName := formatServiceName(event)
if urls, ok := listeners[formattedEventName]; ok {
log.Printf("Dispatching event to %d listeners for %s\n", len(urls), formattedEventName)
for _, listener := range urls {
go callListenerAPI(listener.URL, event)
}
} else {
log.Printf("No listeners registered for event %s\n", formattedEventName)
}
}
func callListenerAPI(url string, event ServiceEvent) {
message, err := json.Marshal(event)
if err != nil {
log.Printf("Error marshalling event to JSON: %v", err)
return
}
req, err := http.NewRequest("POST", url, bytes.NewReader(message))
if err != nil {
log.Printf("Error creating request: %v", err)
return
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Printf("Error calling listener API: %v", err)
return
}
defer resp.Body.Close()
log.Printf("Called %s, got status %d", url, resp.StatusCode)
}